Commented class tti_semaphore

master
Xavier Arteaga 5 years ago committed by Xavier Arteaga
parent 4553ce7ff1
commit 96ffe1c3ad

@ -29,39 +29,73 @@
#define SRSLTE_TTI_SEMPAHORE_H_ #define SRSLTE_TTI_SEMPAHORE_H_
namespace srslte { 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> template <class T>
class tti_semaphore class tti_semaphore
{ {
private: private:
std::mutex mutex; std::mutex mutex; ///< Used for scope mutexes
std::condition_variable cvar; std::condition_variable cvar; ///< Used for notifying element identifier releases
std::deque<T> fifo; std::deque<T> fifo; ///< Queue to keep order
public: public:
/**
* Waits for the first element of the queue match the element identifier provided.
*
* @param id the element identifier
*/
void wait(T id) void wait(T id)
{ {
std::unique_lock<std::mutex> lock(mutex); 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); cvar.wait(lock);
} }
} }
/**
* Enqueue (push) an element identifier to the queue
*
* @param id the element identifier
*/
void push(T id) void push(T id)
{ {
std::unique_lock<std::mutex> lock(mutex); std::unique_lock<std::mutex> lock(mutex);
// Append the element identifier
fifo.push_back(id); fifo.push_back(id);
} }
/**
* Releases (pops) the first element
*/
void release() void release()
{ {
std::unique_lock<std::mutex> lock(mutex); std::unique_lock<std::mutex> lock(mutex);
// Pop first element
fifo.pop_front(); fifo.pop_front();
// Notify release
cvar.notify_all(); cvar.notify_all();
} }
/**
* Waits for all the elements to be released
*/
void wait_all() void wait_all()
{ {
while (!fifo.empty()) { // Wait until the FIFO is empty
while (not fifo.empty()) {
wait(fifo.front()); wait(fifo.front());
} }
} }

Loading…
Cancel
Save