##// END OF EJS Templates
Improved worker pool
cin -
r17:7cd4a843b4e4 promises
parent child
Show More
@@ -99,7 +99,7 namespace Implab.Test {
99 99
100 100 [TestMethod]
101 101 public void WorkerPoolSizeTest() {
102 var pool = new WorkerPool(5, 10);
102 var pool = new WorkerPool(5, 10, 0);
103 103
104 104 Assert.AreEqual(5, pool.ThreadCount);
105 105
@@ -119,7 +119,7 namespace Implab.Test {
119 119
120 120 [TestMethod]
121 121 public void WorkerPoolCorrectTest() {
122 var pool = new WorkerPool();
122 var pool = new WorkerPool(0,1000,100);
123 123
124 124 int iterations = 1000;
125 125 int pending = iterations;
@@ -244,7 +244,7 namespace Implab.Test {
244 244 [TestMethod]
245 245 public void ChainedMapTest() {
246 246
247 using (var pool = new WorkerPool(1,10)) {
247 using (var pool = new WorkerPool(8,100,0)) {
248 248 int count = 10000;
249 249
250 250 double[] args = new double[count];
1 NO CONTENT: modified file, binary diff hidden
@@ -62,7 +62,36 namespace Implab.Parallels {
62 62 }
63 63 }
64 64
65 bool StartWorker() {
65 protected abstract bool TryDequeue(out TUnit unit);
66
67 protected virtual bool ExtendPool() {
68 if (m_suspended > 0) {
69 m_hasTasks.Set();
70 return true;
71 } else
72 return StartWorker();
73 }
74
75 /// <summary>
76 /// Запускает либо новый поток, если раньше не было ни одного потока, либо устанавливает событие пробуждение одного спящего потока
77 /// </summary>
78 protected void WakePool() {
79 m_hasTasks.Set(); // wake sleeping thread;
80
81 if (AllocateThreadSlot(1)) {
82 var worker = new Thread(this.Worker);
83 worker.IsBackground = true;
84 worker.Start();
85 }
86 }
87
88 protected virtual void Suspend() {
89 m_hasTasks.WaitOne();
90 }
91
92 #region thread slots traits
93
94 bool AllocateThreadSlot() {
66 95 int current;
67 96 // use spins to allocate slot for the new thread
68 97 do {
@@ -72,38 +101,67 namespace Implab.Parallels {
72 101 return false;
73 102 } while (current != Interlocked.CompareExchange(ref m_runningThreads, current + 1, current));
74 103
75 m_maxRunningThreads = Math.Max(m_maxRunningThreads, current + 1);
104 UpdateMaxThreads(current + 1);
105
106 return true;
107 }
108
109 bool AllocateThreadSlot(int desired) {
110 if (desired - 1 != Interlocked.CompareExchange(ref m_runningThreads, desired, desired - 1))
111 return false;
112
113 UpdateMaxThreads(desired);
114
115 return true;
116 }
117
118 bool ReleaseThreadSlot(out bool last) {
119 last = false;
120 int current;
121 // use spins to release slot for the new thread
122 do {
123 current = m_runningThreads;
124 if (current <= m_minThreads && m_exitRequired == 0)
125 // the thread is reserved
126 return false;
127 } while (current != Interlocked.CompareExchange(ref m_runningThreads, current - 1, current));
128
129 last = (current == 1);
76 130
131 return true;
132 }
133
134 /// <summary>
135 /// releases thread slot unconditionally, used during cleanup
136 /// </summary>
137 /// <returns>true - no more threads left</returns>
138 bool ReleaseThreadSlotAnyway() {
139 var left = Interlocked.Decrement(ref m_runningThreads);
140 return left == 0;
141 }
142
143 void UpdateMaxThreads(int count) {
144 int max;
145 do {
146 max = m_maxRunningThreads;
147 if (max >= count)
148 break;
149 } while(max != Interlocked.CompareExchange(ref m_maxRunningThreads, count, max));
150 }
151
152 #endregion
153
154 bool StartWorker() {
155 if (AllocateThreadSlot()) {
77 156 // slot successfully allocated
78
79 157 var worker = new Thread(this.Worker);
80 158 worker.IsBackground = true;
81 159 worker.Start();
82 160
83 161 return true;
84 }
85
86 protected abstract bool TryDequeue(out TUnit unit);
87
88 protected virtual void WakeNewWorker(bool extend) {
89 if (m_suspended > 0)
90 m_hasTasks.Set();
91 else
92 StartWorker();
162 } else {
163 return false;
93 164 }
94
95 /// <summary>
96 /// Запускает либо новый поток, если раньше не было ни одного потока, либо устанавливает событие пробуждение одного спящего потока
97 /// </summary>
98 protected void StartIfIdle() {
99 int threads;
100 do {
101
102 }
103 }
104
105 protected virtual void Suspend() {
106 m_hasTasks.WaitOne();
107 165 }
108 166
109 167 bool FetchTask(out TUnit unit) {
@@ -111,35 +169,32 namespace Implab.Parallels {
111 169 // exit if requested
112 170 if (m_exitRequired != 0) {
113 171 // release the thread slot
114 var running = Interlocked.Decrement(ref m_runningThreads);
115 if (running == 0) // it was the last worker
172 if (ReleaseThreadSlotAnyway()) // it was the last worker
116 173 m_hasTasks.Dispose();
117 174 else
118 m_hasTasks.Set(); // release next worker
175 m_hasTasks.Set(); // wake next worker
119 176 unit = default(TUnit);
120 177 return false;
121 178 }
122 179
123 180 // fetch task
124 181 if (TryDequeue(out unit)) {
125 WakeNewWorker(true);
182 ExtendPool();
126 183 return true;
127 184 }
128 185
129 186 //no tasks left, exit if the thread is no longer needed
130 int runningThreads;
131 bool exit = true;
132 do {
133 runningThreads = m_runningThreads;
134 if (runningThreads <= m_minThreads) {
135 // check wheather this is the last thread and we have tasks
187 bool last;
188 if (ReleaseThreadSlot(out last)) {
189 if (last && m_hasTasks.WaitOne(0)) {
190 if (AllocateThreadSlot(1))
191 continue; // spin again...
192 else
193 // we failed to reallocate slot for this thread
194 // therefore we need to release the event
195 m_hasTasks.Set();
196 }
136 197
137 exit = false;
138 break;
139 }
140 } while (runningThreads != Interlocked.CompareExchange(ref m_runningThreads, runningThreads - 1, runningThreads));
141
142 if (exit) {
143 198 return false;
144 199 }
145 200
@@ -57,17 +57,16 namespace Implab.Parallels {
57 57 var len = Interlocked.Increment(ref m_queueLength);
58 58 m_queue.Enqueue(unit);
59 59
60 if (ThreadCount == 0)
61 // force to start
62 WakeNewWorker(false);
60 if(!ExtendPool())
61 WakePool();
63 62 }
64 63
65 protected override void WakeNewWorker(bool extend) {
66 if (extend && m_queueLength <= m_threshold)
64 protected override bool ExtendPool() {
65 if (m_queueLength <= m_threshold*ThreadCount)
67 66 // in this case we are in active thread and it request for additional workers
68 67 // satisfy it only when queue is longer than threshold
69 return;
70 base.WakeNewWorker(extend);
68 return false;
69 return base.ExtendPool();
71 70 }
72 71
73 72 protected override bool TryDequeue(out Action unit) {
General Comments 0
You need to be logged in to leave comments. Login now