All random numbers generated via one PRNG seeded in one place.
[invirt/third/libt4.git] / rpc / fifo.h
index d190c26..dfb4d05 100644 (file)
@@ -1,82 +1,47 @@
 #ifndef fifo_h
 #define fifo_h
 
-// fifo template
-// blocks enq() and deq() when queue is FULL or EMPTY
-
-#include <errno.h>
-#include <list>
-#include <sys/time.h>
-#include <time.h>
-#include <errno.h>
-#include "lang/verify.h"
-#include "lock.h"
+#include "types.h"
 
+// blocks enq() and deq() when queue is FULL or EMPTY
 template<class T>
 class fifo {
-       public:
-               fifo(int m=0);
-               bool enq(T, bool blocking=true);
-               void deq(T *);
-               bool size();
-
-       private:
-               std::list<T> 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
-};
-
-template<class T>
-fifo<T>::fifo(int limit) : max_(limit)
-{
-}
-
-template<class T> bool
-fifo<T>::size()
-{
-    lock ml(m_);
-       return q_.size();
-}
+    public:
+        fifo(size_t limit=0) : max_(limit) {}
+
+        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;
+        }
 
-template<class T> bool
-fifo<T>::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);
+        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();
         }
-               else
-                       return false;
-       }
-    non_empty_c_.notify_one();
-       return true;
-}
 
-template<class T> void
-fifo<T>::deq(T *e)
-{
-       lock ml(m_);
+        bool size() {
+            lock ml(m_);
+            return q_.size();
+        }
 
-       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;
-}
+    private:
+        std::list<T> 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
+};
 
 #endif