X-Git-Url: http://xvm.mit.edu/gitweb/invirt/third/libt4.git/blobdiff_plain/5a5c578e2e358a121cdb9234a6cb11c4ecfbf323..3abd3952c1f4441f0dd6eae9883b2d01ed9cd56b:/rpc/fifo.h diff --git a/rpc/fifo.h b/rpc/fifo.h index 215ec5b..e3f0c38 100644 --- a/rpc/fifo.h +++ b/rpc/fifo.h @@ -6,42 +6,42 @@ // blocks enq() and deq() when queue is FULL or EMPTY template class fifo { - public: - fifo(size_t limit=0) : max_(limit) {} - bool enq(T, bool blocking=true); - void deq(T *); - bool size() { + public: + fifo(size_t limit=0) : max_(limit) {} + bool enq(T, bool blocking=true); + void deq(T *); + bool size() { lock ml(m_); return q_.size(); } - private: - list q_; + private: + list q_; mutex m_; cond non_empty_c_; // q went non-empty - cond has_space_c_; // q is not longer overfull - size_t max_; // maximum capacity of the queue, block enq threads if exceeds this limit + cond has_space_c_; // q is not longer overfull + size_t max_; // maximum capacity of the queue, block enq threads if exceeds this limit }; template bool fifo::enq(T e, bool blocking) { lock ml(m_); - while (max_ && q_.size() >= max_) { - if (!blocking) - return false; + 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; + return true; } template void fifo::deq(T *e) { - lock ml(m_); - while(q_.empty()) + lock ml(m_); + while(q_.empty()) non_empty_c_.wait(ml); *e = q_.front(); q_.pop_front();