diff Implab/Parallels/WorkerPool.cs @ 80:4f20870d0816 v2

added memory barriers
author cin
date Fri, 26 Sep 2014 03:32:34 +0400
parents fe33f4e02ad5
children 2c5631b43c7d
line wrap: on
line diff
--- a/Implab/Parallels/WorkerPool.cs	Mon Sep 22 18:20:49 2014 +0400
+++ b/Implab/Parallels/WorkerPool.cs	Fri Sep 26 03:32:34 2014 +0400
@@ -12,20 +12,24 @@
         MTQueue<Action> m_queue = new MTQueue<Action>();
         int m_queueLength = 0;
         readonly int m_threshold = 1;
+        int m_workers = 0;
 
         public WorkerPool(int minThreads, int maxThreads, int threshold)
             : base(minThreads, maxThreads) {
             m_threshold = threshold;
+            m_workers = minThreads;
             InitPool();
         }
 
         public WorkerPool(int minThreads, int maxThreads) :
             base(minThreads, maxThreads) {
+            m_workers = minThreads;
             InitPool();
         }
 
         public WorkerPool(int threads)
             : base(threads) {
+            m_workers = threads;
             InitPool();
         }
 
@@ -62,8 +66,10 @@
             var len = Interlocked.Increment(ref m_queueLength);
             m_queue.Enqueue(unit);
 
-            if (len > m_threshold*ActiveThreads)
+            if (len > m_threshold * m_workers) {
+                Interlocked.Increment(ref m_workers);
                 GrowPool();
+            }
         }
 
         protected override bool TryDequeue(out Action unit) {
@@ -85,8 +91,10 @@
             // Suspend
             //    queueLength > 0
             // continue
+            Thread.MemoryBarrier();
             if (m_queueLength > 0)
                 return true;
+            Interlocked.Decrement(ref m_workers);
             return base.Suspend();
         }