X-Git-Url: http://xvm.mit.edu/gitweb/invirt/third/libt4.git/blobdiff_plain/16e7c282c6fcec8189425bd15ec9e8a4a0ee857d..ab6c1548ac2b1907bca92c8ce43e919c1a649a6f:/rpc/fifo.h diff --git a/rpc/fifo.h b/rpc/fifo.h index e3f0c38..dfb4d05 100644 --- a/rpc/fifo.h +++ b/rpc/fifo.h @@ -8,45 +8,40 @@ template class fifo { public: fifo(size_t limit=0) : max_(limit) {} - bool enq(T, bool blocking=true); - void deq(T *); + + bool enq(T e, bool blocking=true) { + lock ml(m_); + 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; + } + + void deq(T * e) { + lock ml(m_); + while(q_.empty()) + non_empty_c_.wait(ml); + *e = q_.front(); + q_.pop_front(); + if (max_ && q_.size() < max_) + has_space_c_.notify_one(); + } + bool size() { lock ml(m_); return q_.size(); } private: - list q_; - mutex m_; + std::list q_; + std::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 }; -template bool -fifo::enq(T e, bool blocking) -{ - lock ml(m_); - 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; -} - -template void -fifo::deq(T *e) -{ - lock ml(m_); - 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