mirror of https://github.com/PentHertz/srsLTE.git
Commented class tti_semaphore
This commit is contained in:
parent
4553ce7ff1
commit
96ffe1c3ad
|
@ -29,39 +29,73 @@
|
|||
#define SRSLTE_TTI_SEMPAHORE_H_
|
||||
|
||||
namespace srslte {
|
||||
|
||||
/**
|
||||
* Implements priority semaphore based on a FIFO queue wait . This class enqueues T type element identifiers (method
|
||||
* push) and waits until the enqueued object is the first (method wait). The first element is released by method
|
||||
* release. The method release_all waits for all the elements to be released.
|
||||
*
|
||||
* @tparam T Object identifier type
|
||||
*/
|
||||
template <class T>
|
||||
class tti_semaphore
|
||||
{
|
||||
private:
|
||||
std::mutex mutex;
|
||||
std::condition_variable cvar;
|
||||
std::deque<T> fifo;
|
||||
std::mutex mutex; ///< Used for scope mutexes
|
||||
std::condition_variable cvar; ///< Used for notifying element identifier releases
|
||||
std::deque<T> fifo; ///< Queue to keep order
|
||||
|
||||
public:
|
||||
/**
|
||||
* Waits for the first element of the queue match the element identifier provided.
|
||||
*
|
||||
* @param id the element identifier
|
||||
*/
|
||||
void wait(T id)
|
||||
{
|
||||
std::unique_lock<std::mutex> lock(mutex);
|
||||
while (!fifo.empty() && fifo.front() != id) {
|
||||
|
||||
// While the FIFO is not empty and the front ID does not match the provided element identifier, keep waiting
|
||||
while (not fifo.empty() and fifo.front() != id) {
|
||||
// Wait for a release
|
||||
cvar.wait(lock);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Enqueue (push) an element identifier to the queue
|
||||
*
|
||||
* @param id the element identifier
|
||||
*/
|
||||
void push(T id)
|
||||
{
|
||||
std::unique_lock<std::mutex> lock(mutex);
|
||||
|
||||
// Append the element identifier
|
||||
fifo.push_back(id);
|
||||
}
|
||||
|
||||
/**
|
||||
* Releases (pops) the first element
|
||||
*/
|
||||
void release()
|
||||
{
|
||||
std::unique_lock<std::mutex> lock(mutex);
|
||||
|
||||
// Pop first element
|
||||
fifo.pop_front();
|
||||
|
||||
// Notify release
|
||||
cvar.notify_all();
|
||||
}
|
||||
|
||||
/**
|
||||
* Waits for all the elements to be released
|
||||
*/
|
||||
void wait_all()
|
||||
{
|
||||
while (!fifo.empty()) {
|
||||
// Wait until the FIFO is empty
|
||||
while (not fifo.empty()) {
|
||||
wait(fifo.front());
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue