diff dreactor/util/ThreadSafeQueue.d @ 3:e3dbc9208822

basic tests working
author rick@minifunk
date Tue, 08 Jul 2008 11:21:09 -0400
parents 7a315154bf5e
children
line wrap: on
line diff
--- a/dreactor/util/ThreadSafeQueue.d	Thu Jun 12 23:12:17 2008 -0400
+++ b/dreactor/util/ThreadSafeQueue.d	Tue Jul 08 11:21:09 2008 -0400
@@ -1,7 +1,10 @@
 module dreactor.util.ThreadSafeQueue;
 
 import tango.util.collection.CircularSeq;
+import tango.core.Atomic;
 
+import tango.util.log.Log;
+import tango.util.log.Config;
 /******************************************************************************
 
     ThreadSafeQueue
@@ -15,18 +18,19 @@
 public
     this(int maxsz = 1000)
     {
-        list = new CircularSeq!(TYPE);
-        maxsize = maxsz;
-        size = 0;
+        list_ = new CircularSeq!(TYPE);
+        maxsize_ = maxsz;
+        size_ = 0;
+        log = Log.lookup("dreactor.util.ThreadSafeQueue");
     }
 
     synchronized bool pop(ref TYPE t)
     {
-        if (list.size())
+        if (size_ > 0)
         {
-            TYPE t = list.head();
-            list.removeHead();
-            --size;
+            t = list_.head();
+            list_.removeHead();
+            size_--;
             return true;
         }
         else
@@ -35,10 +39,10 @@
 
     synchronized bool push(TYPE t)
     {
-        if (list.size < maxsize)
+        if (size_ < maxsize_)
         {
-            list.append(t);
-            ++size;
+            list_.append(t);
+            size_++;
             return true;
         }
         else
@@ -47,22 +51,31 @@
 
     synchronized int size()
     {
-        return size();
+        return size_;
     }
 
-    synchronized int processAll(int delegate(ref T value) dg)
+    synchronized int processAll(int delegate(ref TYPE value) dg)
     {
+        if (0 >= size_)
+            return 0;
+
         int count = 0;
-        foreach(T t; list)
+        foreach(TYPE t; list_)
         {
             if (dg(t) < 0)
                 break;
             ++count;
         }
-        if (count == size)
+        if (count == size_)
+        {
             clear_();
+            size_ = 0;
+        }
         else
-            list.removeRange(0, count);
+        {
+            list_.removeRange(0, count);
+            size_ -= count;
+        }
         return count;
     }
 
@@ -75,10 +88,12 @@
 
     void clear_()
     {
-        list.removeAll();
+        list_.clear();
+        size_ = 0 ;
     }
 
-    CircularSeq!(TYPE) list;
-    int maxsize;
-    int size;
+    int maxsize_;
+    int size_;
+    Logger log;
+    CircularSeq!(TYPE) list_;
 }