5 // blocks enq() and deq() when queue is FULL or EMPTY
13 #include "lang/verify.h"
20 bool enq(T, bool blocking=true);
27 pthread_cond_t non_empty_c_; // q went non-empty
28 pthread_cond_t has_space_c_; // q is not longer overfull
29 unsigned int max_; //maximum capacity of the queue, block enq threads if exceeds this limit
33 fifo<T>::fifo(int limit) : max_(limit)
35 VERIFY(pthread_mutex_init(&m_, 0) == 0);
36 VERIFY(pthread_cond_init(&non_empty_c_, 0) == 0);
37 VERIFY(pthread_cond_init(&has_space_c_, 0) == 0);
43 //fifo is to be deleted only when no threads are using it!
44 VERIFY(pthread_mutex_destroy(&m_)==0);
45 VERIFY(pthread_cond_destroy(&non_empty_c_) == 0);
46 VERIFY(pthread_cond_destroy(&has_space_c_) == 0);
49 template<class T> bool
56 template<class T> bool
57 fifo<T>::enq(T e, bool blocking)
61 if (!max_ || q_.size() < max_) {
66 VERIFY(pthread_cond_wait(&has_space_c_, &m_) == 0);
70 VERIFY(pthread_cond_signal(&non_empty_c_) == 0);
74 template<class T> void
81 VERIFY (pthread_cond_wait(&non_empty_c_, &m_) == 0);
85 if (max_ && q_.size() < max_) {
86 VERIFY(pthread_cond_signal(&has_space_c_)==0);