-template<class T>
-fifo<T>::fifo(int limit) : max_(limit)
-{
- VERIFY(pthread_mutex_init(&m_, 0) == 0);
- VERIFY(pthread_cond_init(&non_empty_c_, 0) == 0);
- VERIFY(pthread_cond_init(&has_space_c_, 0) == 0);
-}
-
-template<class T>
-fifo<T>::~fifo()
-{
- //fifo is to be deleted only when no threads are using it!
- VERIFY(pthread_mutex_destroy(&m_)==0);
- VERIFY(pthread_cond_destroy(&non_empty_c_) == 0);
- VERIFY(pthread_cond_destroy(&has_space_c_) == 0);
-}
-
-template<class T> bool
-fifo<T>::size()
-{
- ScopedLock ml(&m_);
- return q_.size();
-}
-
-template<class T> bool
-fifo<T>::enq(T e, bool blocking)
-{
- ScopedLock ml(&m_);
- while (1) {
- if (!max_ || q_.size() < max_) {
- q_.push_back(e);
- break;
- }
- if (blocking)
- VERIFY(pthread_cond_wait(&has_space_c_, &m_) == 0);
- else
- return false;
- }
- VERIFY(pthread_cond_signal(&non_empty_c_) == 0);
- return true;
-}
-
-template<class T> void
-fifo<T>::deq(T *e)
-{
- ScopedLock ml(&m_);
-
- while(1) {
- if(q_.empty()){
- VERIFY (pthread_cond_wait(&non_empty_c_, &m_) == 0);
- } else {
- *e = q_.front();
- q_.pop_front();
- if (max_ && q_.size() < max_) {
- VERIFY(pthread_cond_signal(&has_space_c_)==0);
- }
- break;
- }
- }
- return;
-}
-