X-Git-Url: http://xvm.mit.edu/gitweb/invirt/third/libt4.git/blobdiff_plain/a4175b2e216a20b86cc872dea8a08005c60617a5..dfe8486473094c0769fd1922329c3f0dfd8f43c0:/rpc/fifo.h?ds=sidebyside diff --git a/rpc/fifo.h b/rpc/fifo.h index d190c26..93a79cf 100644 --- a/rpc/fifo.h +++ b/rpc/fifo.h @@ -1,60 +1,39 @@ #ifndef fifo_h #define fifo_h -// fifo template -// blocks enq() and deq() when queue is FULL or EMPTY - -#include #include -#include -#include -#include -#include "lang/verify.h" #include "lock.h" +// blocks enq() and deq() when queue is FULL or EMPTY template class fifo { public: - fifo(int m=0); + fifo(int limit=0) : max_(limit) {}; bool enq(T, bool blocking=true); void deq(T *); - bool size(); + bool size() { + lock ml(m_); + return q_.size(); + }; private: std::list q_; mutex m_; - std::condition_variable non_empty_c_; // q went non-empty - std::condition_variable has_space_c_; // q is not longer overfull - unsigned int max_; //maximum capacity of the queue, block enq threads if exceeds this limit + cond non_empty_c_; // q went non-empty + cond has_space_c_; // q is not longer overfull + unsigned int max_; // maximum capacity of the queue, block enq threads if exceeds this limit }; -template -fifo::fifo(int limit) : max_(limit) -{ -} - -template bool -fifo::size() -{ - lock ml(m_); - return q_.size(); -} - template bool fifo::enq(T e, bool blocking) { lock ml(m_); - while (1) { - if (!max_ || q_.size() < max_) { - q_.push_back(e); - break; - } - if (blocking) { - has_space_c_.wait(ml); - } - else + while (max_ && q_.size() >= max_) { + if (!blocking) return false; + has_space_c_.wait(ml); } + q_.push_back(e); non_empty_c_.notify_one(); return true; } @@ -63,20 +42,12 @@ template void fifo::deq(T *e) { lock ml(m_); - - while(1) { - if(q_.empty()){ - non_empty_c_.wait(ml); - } else { - *e = q_.front(); - q_.pop_front(); - if (max_ && q_.size() < max_) { - has_space_c_.notify_one(); - } - break; - } - } - return; + while(q_.empty()) + non_empty_c_.wait(ml); + *e = q_.front(); + q_.pop_front(); + if (max_ && q_.size() < max_) + has_space_c_.notify_one(); } #endif