annotate Implab/Parallels/DispatchPool.cs @ 80:4f20870d0816 v2

added memory barriers
author cin
date Fri, 26 Sep 2014 03:32:34 +0400
parents 2fc0fbe7d58b
children 2c5631b43c7d
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
15
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
1 using System;
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
2 using System.Collections.Generic;
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
3 using System.Linq;
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
4 using System.Text;
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
5 using System.Threading;
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
6 using System.Diagnostics;
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
7
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
8 namespace Implab.Parallels {
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
9 public abstract class DispatchPool<TUnit> : IDisposable {
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
10 readonly int m_minThreads;
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
11 readonly int m_maxThreads;
80
4f20870d0816 added memory barriers
cin
parents: 41
diff changeset
12 readonly int m_releaseTimeout = 100; // the timeout while the working thread will wait for the new tasks before exit
21
6a56df4ec59e DispatchPool works again, but performance is poor in some cases
cin
parents: 20
diff changeset
13
6a56df4ec59e DispatchPool works again, but performance is poor in some cases
cin
parents: 20
diff changeset
14 int m_createdThreads = 0; // the current size of the pool
6a56df4ec59e DispatchPool works again, but performance is poor in some cases
cin
parents: 20
diff changeset
15 int m_activeThreads = 0; // the count of threads which are active
6a56df4ec59e DispatchPool works again, but performance is poor in some cases
cin
parents: 20
diff changeset
16 int m_sleepingThreads = 0; // the count of currently inactive threads
6a56df4ec59e DispatchPool works again, but performance is poor in some cases
cin
parents: 20
diff changeset
17 int m_maxRunningThreads = 0; // the meximum reached size of the pool
6a56df4ec59e DispatchPool works again, but performance is poor in some cases
cin
parents: 20
diff changeset
18 int m_exitRequired = 0; // the pool is going to shutdown, all unused workers are released
80
4f20870d0816 added memory barriers
cin
parents: 41
diff changeset
19
21
6a56df4ec59e DispatchPool works again, but performance is poor in some cases
cin
parents: 20
diff changeset
20 int m_wakeEvents = 0; // the count of wake events
6a56df4ec59e DispatchPool works again, but performance is poor in some cases
cin
parents: 20
diff changeset
21
80
4f20870d0816 added memory barriers
cin
parents: 41
diff changeset
22 readonly object m_signalLocker = new object();
15
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
23
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
24 protected DispatchPool(int min, int max) {
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
25 if (min < 0)
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
26 throw new ArgumentOutOfRangeException("min");
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
27 if (max <= 0)
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
28 throw new ArgumentOutOfRangeException("max");
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
29
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
30 if (min > max)
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
31 min = max;
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
32 m_minThreads = min;
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
33 m_maxThreads = max;
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
34 }
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
35
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
36 protected DispatchPool(int threads)
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
37 : this(threads, threads) {
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
38 }
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
39
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
40 protected DispatchPool() {
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
41 int maxThreads, maxCP;
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
42 ThreadPool.GetMaxThreads(out maxThreads, out maxCP);
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
43
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
44 m_minThreads = 0;
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
45 m_maxThreads = maxThreads;
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
46 }
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
47
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
48 protected void InitPool() {
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
49 for (int i = 0; i < m_minThreads; i++)
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
50 StartWorker();
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
51 }
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
52
20
1c3b3d518480 refactoring, sync
cin
parents: 17
diff changeset
53 public int PoolSize {
15
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
54 get {
80
4f20870d0816 added memory barriers
cin
parents: 41
diff changeset
55 Thread.MemoryBarrier();
20
1c3b3d518480 refactoring, sync
cin
parents: 17
diff changeset
56 return m_createdThreads;
1c3b3d518480 refactoring, sync
cin
parents: 17
diff changeset
57 }
1c3b3d518480 refactoring, sync
cin
parents: 17
diff changeset
58 }
1c3b3d518480 refactoring, sync
cin
parents: 17
diff changeset
59
1c3b3d518480 refactoring, sync
cin
parents: 17
diff changeset
60 public int ActiveThreads {
1c3b3d518480 refactoring, sync
cin
parents: 17
diff changeset
61 get {
80
4f20870d0816 added memory barriers
cin
parents: 41
diff changeset
62 Thread.MemoryBarrier();
20
1c3b3d518480 refactoring, sync
cin
parents: 17
diff changeset
63 return m_activeThreads;
15
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
64 }
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
65 }
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
66
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
67 public int MaxRunningThreads {
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
68 get {
80
4f20870d0816 added memory barriers
cin
parents: 41
diff changeset
69 Thread.MemoryBarrier();
15
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
70 return m_maxRunningThreads;
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
71 }
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
72 }
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
73
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
74 protected bool IsDisposed {
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
75 get {
80
4f20870d0816 added memory barriers
cin
parents: 41
diff changeset
76 Thread.MemoryBarrier();
4f20870d0816 added memory barriers
cin
parents: 41
diff changeset
77 return m_exitRequired == 1;
15
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
78 }
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
79 }
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
80
17
7cd4a843b4e4 Improved worker pool
cin
parents: 16
diff changeset
81 protected abstract bool TryDequeue(out TUnit unit);
7cd4a843b4e4 Improved worker pool
cin
parents: 16
diff changeset
82
80
4f20870d0816 added memory barriers
cin
parents: 41
diff changeset
83 #region thread signaling traits
21
6a56df4ec59e DispatchPool works again, but performance is poor in some cases
cin
parents: 20
diff changeset
84 int SignalThread() {
6a56df4ec59e DispatchPool works again, but performance is poor in some cases
cin
parents: 20
diff changeset
85 var signals = Interlocked.Increment(ref m_wakeEvents);
6a56df4ec59e DispatchPool works again, but performance is poor in some cases
cin
parents: 20
diff changeset
86 if(signals == 1)
80
4f20870d0816 added memory barriers
cin
parents: 41
diff changeset
87 lock(m_signalLocker)
4f20870d0816 added memory barriers
cin
parents: 41
diff changeset
88 Monitor.Pulse(m_signalLocker);
21
6a56df4ec59e DispatchPool works again, but performance is poor in some cases
cin
parents: 20
diff changeset
89 return signals;
6a56df4ec59e DispatchPool works again, but performance is poor in some cases
cin
parents: 20
diff changeset
90 }
6a56df4ec59e DispatchPool works again, but performance is poor in some cases
cin
parents: 20
diff changeset
91
22
5a35900264f5 implemented nonblocking wake singnals processing
cin
parents: 21
diff changeset
92 bool FetchSignalOrWait(int timeout) {
5a35900264f5 implemented nonblocking wake singnals processing
cin
parents: 21
diff changeset
93 var start = Environment.TickCount;
80
4f20870d0816 added memory barriers
cin
parents: 41
diff changeset
94 int signals;
4f20870d0816 added memory barriers
cin
parents: 41
diff changeset
95 Thread.MemoryBarrier(); // m_wakeEvents volatile first read
22
5a35900264f5 implemented nonblocking wake singnals processing
cin
parents: 21
diff changeset
96 do {
80
4f20870d0816 added memory barriers
cin
parents: 41
diff changeset
97 signals = m_wakeEvents;
4f20870d0816 added memory barriers
cin
parents: 41
diff changeset
98 if (signals == 0)
4f20870d0816 added memory barriers
cin
parents: 41
diff changeset
99 break;
4f20870d0816 added memory barriers
cin
parents: 41
diff changeset
100 } while (Interlocked.CompareExchange(ref m_wakeEvents, signals - 1, signals) != signals);
22
5a35900264f5 implemented nonblocking wake singnals processing
cin
parents: 21
diff changeset
101
80
4f20870d0816 added memory barriers
cin
parents: 41
diff changeset
102 if (signals == 0) {
4f20870d0816 added memory barriers
cin
parents: 41
diff changeset
103 // no signal is fetched
4f20870d0816 added memory barriers
cin
parents: 41
diff changeset
104 lock(m_signalLocker) {
4f20870d0816 added memory barriers
cin
parents: 41
diff changeset
105 while(m_wakeEvents == 0) {
4f20870d0816 added memory barriers
cin
parents: 41
diff changeset
106 if (timeout != -1)
4f20870d0816 added memory barriers
cin
parents: 41
diff changeset
107 timeout = Math.Max(0, timeout - (Environment.TickCount - start));
4f20870d0816 added memory barriers
cin
parents: 41
diff changeset
108 if(!Monitor.Wait(m_signalLocker,timeout))
4f20870d0816 added memory barriers
cin
parents: 41
diff changeset
109 return false; // timeout
4f20870d0816 added memory barriers
cin
parents: 41
diff changeset
110 }
4f20870d0816 added memory barriers
cin
parents: 41
diff changeset
111 // m_wakeEvents > 0
4f20870d0816 added memory barriers
cin
parents: 41
diff changeset
112 if (Interlocked.Decrement(ref m_wakeEvents) > 0) //syncronized
4f20870d0816 added memory barriers
cin
parents: 41
diff changeset
113 Monitor.Pulse(m_signalLocker);
4f20870d0816 added memory barriers
cin
parents: 41
diff changeset
114
4f20870d0816 added memory barriers
cin
parents: 41
diff changeset
115 // signal fetched
22
5a35900264f5 implemented nonblocking wake singnals processing
cin
parents: 21
diff changeset
116 return true;
5a35900264f5 implemented nonblocking wake singnals processing
cin
parents: 21
diff changeset
117 }
5a35900264f5 implemented nonblocking wake singnals processing
cin
parents: 21
diff changeset
118
80
4f20870d0816 added memory barriers
cin
parents: 41
diff changeset
119 } else {
4f20870d0816 added memory barriers
cin
parents: 41
diff changeset
120 // signal fetched
4f20870d0816 added memory barriers
cin
parents: 41
diff changeset
121 return true;
4f20870d0816 added memory barriers
cin
parents: 41
diff changeset
122 }
22
5a35900264f5 implemented nonblocking wake singnals processing
cin
parents: 21
diff changeset
123
5a35900264f5 implemented nonblocking wake singnals processing
cin
parents: 21
diff changeset
124
5a35900264f5 implemented nonblocking wake singnals processing
cin
parents: 21
diff changeset
125 }
5a35900264f5 implemented nonblocking wake singnals processing
cin
parents: 21
diff changeset
126
21
6a56df4ec59e DispatchPool works again, but performance is poor in some cases
cin
parents: 20
diff changeset
127 bool Sleep(int timeout) {
6a56df4ec59e DispatchPool works again, but performance is poor in some cases
cin
parents: 20
diff changeset
128 Interlocked.Increment(ref m_sleepingThreads);
22
5a35900264f5 implemented nonblocking wake singnals processing
cin
parents: 21
diff changeset
129 if (FetchSignalOrWait(timeout)) {
5a35900264f5 implemented nonblocking wake singnals processing
cin
parents: 21
diff changeset
130 Interlocked.Decrement(ref m_sleepingThreads);
17
7cd4a843b4e4 Improved worker pool
cin
parents: 16
diff changeset
131 return true;
21
6a56df4ec59e DispatchPool works again, but performance is poor in some cases
cin
parents: 20
diff changeset
132 } else {
6a56df4ec59e DispatchPool works again, but performance is poor in some cases
cin
parents: 20
diff changeset
133 Interlocked.Decrement(ref m_sleepingThreads);
6a56df4ec59e DispatchPool works again, but performance is poor in some cases
cin
parents: 20
diff changeset
134 return false;
20
1c3b3d518480 refactoring, sync
cin
parents: 17
diff changeset
135 }
17
7cd4a843b4e4 Improved worker pool
cin
parents: 16
diff changeset
136 }
21
6a56df4ec59e DispatchPool works again, but performance is poor in some cases
cin
parents: 20
diff changeset
137 #endregion
17
7cd4a843b4e4 Improved worker pool
cin
parents: 16
diff changeset
138
7cd4a843b4e4 Improved worker pool
cin
parents: 16
diff changeset
139 /// <summary>
7cd4a843b4e4 Improved worker pool
cin
parents: 16
diff changeset
140 /// Запускает либо новый поток, если раньше не было ни одного потока, либо устанавливает событие пробуждение одного спящего потока
7cd4a843b4e4 Improved worker pool
cin
parents: 16
diff changeset
141 /// </summary>
21
6a56df4ec59e DispatchPool works again, but performance is poor in some cases
cin
parents: 20
diff changeset
142 protected void GrowPool() {
80
4f20870d0816 added memory barriers
cin
parents: 41
diff changeset
143 Thread.MemoryBarrier();
4f20870d0816 added memory barriers
cin
parents: 41
diff changeset
144 if (m_exitRequired == 1)
21
6a56df4ec59e DispatchPool works again, but performance is poor in some cases
cin
parents: 20
diff changeset
145 return;
6a56df4ec59e DispatchPool works again, but performance is poor in some cases
cin
parents: 20
diff changeset
146 if (m_sleepingThreads > m_wakeEvents) {
22
5a35900264f5 implemented nonblocking wake singnals processing
cin
parents: 21
diff changeset
147 //Console.WriteLine("Waking threads (sleeps {0}, pending {1})", m_sleepingThreads, m_wakeEvents);
5a35900264f5 implemented nonblocking wake singnals processing
cin
parents: 21
diff changeset
148
21
6a56df4ec59e DispatchPool works again, but performance is poor in some cases
cin
parents: 20
diff changeset
149 // all sleeping threads may gone
6a56df4ec59e DispatchPool works again, but performance is poor in some cases
cin
parents: 20
diff changeset
150 SignalThread(); // wake a sleeping thread;
17
7cd4a843b4e4 Improved worker pool
cin
parents: 16
diff changeset
151
21
6a56df4ec59e DispatchPool works again, but performance is poor in some cases
cin
parents: 20
diff changeset
152 // we can't check whether signal has been processed
6a56df4ec59e DispatchPool works again, but performance is poor in some cases
cin
parents: 20
diff changeset
153 // anyway it may take some time for the thread to start
6a56df4ec59e DispatchPool works again, but performance is poor in some cases
cin
parents: 20
diff changeset
154 // we will ensure that at least one thread is running
6a56df4ec59e DispatchPool works again, but performance is poor in some cases
cin
parents: 20
diff changeset
155
30
2fad2d1f4b03 small refactoring, cleanup.
cin
parents: 24
diff changeset
156 EnsurePoolIsAlive();
21
6a56df4ec59e DispatchPool works again, but performance is poor in some cases
cin
parents: 20
diff changeset
157 } else {
6a56df4ec59e DispatchPool works again, but performance is poor in some cases
cin
parents: 20
diff changeset
158 // if there is no sleeping threads in the pool
30
2fad2d1f4b03 small refactoring, cleanup.
cin
parents: 24
diff changeset
159 if (!StartWorker()) {
2fad2d1f4b03 small refactoring, cleanup.
cin
parents: 24
diff changeset
160 // we haven't started a new thread, but the current can be on the way to terminate and it can't process the queue
24
ee04e1fa78da fixed dispatch pool race condition
cin
parents: 22
diff changeset
161 // send it a signal to spin again
30
2fad2d1f4b03 small refactoring, cleanup.
cin
parents: 24
diff changeset
162 SignalThread();
2fad2d1f4b03 small refactoring, cleanup.
cin
parents: 24
diff changeset
163 EnsurePoolIsAlive();
2fad2d1f4b03 small refactoring, cleanup.
cin
parents: 24
diff changeset
164 }
2fad2d1f4b03 small refactoring, cleanup.
cin
parents: 24
diff changeset
165 }
2fad2d1f4b03 small refactoring, cleanup.
cin
parents: 24
diff changeset
166 }
2fad2d1f4b03 small refactoring, cleanup.
cin
parents: 24
diff changeset
167
34
dabf79fde388 fixed race condition in DispatchPool
cin
parents: 30
diff changeset
168 protected void EnsurePoolIsAlive() {
30
2fad2d1f4b03 small refactoring, cleanup.
cin
parents: 24
diff changeset
169 if (AllocateThreadSlot(1)) {
2fad2d1f4b03 small refactoring, cleanup.
cin
parents: 24
diff changeset
170 // if there were no threads in the pool
2fad2d1f4b03 small refactoring, cleanup.
cin
parents: 24
diff changeset
171 var worker = new Thread(this.Worker);
2fad2d1f4b03 small refactoring, cleanup.
cin
parents: 24
diff changeset
172 worker.IsBackground = true;
2fad2d1f4b03 small refactoring, cleanup.
cin
parents: 24
diff changeset
173 worker.Start();
17
7cd4a843b4e4 Improved worker pool
cin
parents: 16
diff changeset
174 }
7cd4a843b4e4 Improved worker pool
cin
parents: 16
diff changeset
175 }
7cd4a843b4e4 Improved worker pool
cin
parents: 16
diff changeset
176
34
dabf79fde388 fixed race condition in DispatchPool
cin
parents: 30
diff changeset
177 protected virtual bool Suspend() {
20
1c3b3d518480 refactoring, sync
cin
parents: 17
diff changeset
178 //no tasks left, exit if the thread is no longer needed
1c3b3d518480 refactoring, sync
cin
parents: 17
diff changeset
179 bool last;
1c3b3d518480 refactoring, sync
cin
parents: 17
diff changeset
180 bool requestExit;
1c3b3d518480 refactoring, sync
cin
parents: 17
diff changeset
181
21
6a56df4ec59e DispatchPool works again, but performance is poor in some cases
cin
parents: 20
diff changeset
182 // if threads have a timeout before releasing
20
1c3b3d518480 refactoring, sync
cin
parents: 17
diff changeset
183 if (m_releaseTimeout > 0)
1c3b3d518480 refactoring, sync
cin
parents: 17
diff changeset
184 requestExit = !Sleep(m_releaseTimeout);
1c3b3d518480 refactoring, sync
cin
parents: 17
diff changeset
185 else
1c3b3d518480 refactoring, sync
cin
parents: 17
diff changeset
186 requestExit = true;
1c3b3d518480 refactoring, sync
cin
parents: 17
diff changeset
187
21
6a56df4ec59e DispatchPool works again, but performance is poor in some cases
cin
parents: 20
diff changeset
188 if (!requestExit)
6a56df4ec59e DispatchPool works again, but performance is poor in some cases
cin
parents: 20
diff changeset
189 return true;
6a56df4ec59e DispatchPool works again, but performance is poor in some cases
cin
parents: 20
diff changeset
190
6a56df4ec59e DispatchPool works again, but performance is poor in some cases
cin
parents: 20
diff changeset
191 // release unsused thread
20
1c3b3d518480 refactoring, sync
cin
parents: 17
diff changeset
192 if (requestExit && ReleaseThreadSlot(out last)) {
1c3b3d518480 refactoring, sync
cin
parents: 17
diff changeset
193 // in case at the moment the last thread was being released
1c3b3d518480 refactoring, sync
cin
parents: 17
diff changeset
194 // a new task was added to the queue, we need to try
1c3b3d518480 refactoring, sync
cin
parents: 17
diff changeset
195 // to revoke the thread to avoid the situation when the task is left unprocessed
30
2fad2d1f4b03 small refactoring, cleanup.
cin
parents: 24
diff changeset
196 if (last && FetchSignalOrWait(0)) { // FetchSignalOrWait(0) will fetch pending task or will return false
2fad2d1f4b03 small refactoring, cleanup.
cin
parents: 24
diff changeset
197 SignalThread(); // since FetchSignalOrWait(0) has fetched the signal we need to reschedule it
2fad2d1f4b03 small refactoring, cleanup.
cin
parents: 24
diff changeset
198 return AllocateThreadSlot(1); // ensure that at least one thread is alive
20
1c3b3d518480 refactoring, sync
cin
parents: 17
diff changeset
199 }
1c3b3d518480 refactoring, sync
cin
parents: 17
diff changeset
200
1c3b3d518480 refactoring, sync
cin
parents: 17
diff changeset
201 return false;
1c3b3d518480 refactoring, sync
cin
parents: 17
diff changeset
202 }
1c3b3d518480 refactoring, sync
cin
parents: 17
diff changeset
203
21
6a56df4ec59e DispatchPool works again, but performance is poor in some cases
cin
parents: 20
diff changeset
204 // wait till infinity
20
1c3b3d518480 refactoring, sync
cin
parents: 17
diff changeset
205 Sleep(-1);
1c3b3d518480 refactoring, sync
cin
parents: 17
diff changeset
206
1c3b3d518480 refactoring, sync
cin
parents: 17
diff changeset
207 return true;
17
7cd4a843b4e4 Improved worker pool
cin
parents: 16
diff changeset
208 }
7cd4a843b4e4 Improved worker pool
cin
parents: 16
diff changeset
209
7cd4a843b4e4 Improved worker pool
cin
parents: 16
diff changeset
210 #region thread slots traits
7cd4a843b4e4 Improved worker pool
cin
parents: 16
diff changeset
211
7cd4a843b4e4 Improved worker pool
cin
parents: 16
diff changeset
212 bool AllocateThreadSlot() {
16
cin
parents: 15
diff changeset
213 int current;
15
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
214 // use spins to allocate slot for the new thread
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
215 do {
20
1c3b3d518480 refactoring, sync
cin
parents: 17
diff changeset
216 current = m_createdThreads;
80
4f20870d0816 added memory barriers
cin
parents: 41
diff changeset
217 if (current >= m_maxThreads || m_exitRequired == 1)
15
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
218 // no more slots left or the pool has been disposed
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
219 return false;
20
1c3b3d518480 refactoring, sync
cin
parents: 17
diff changeset
220 } while (current != Interlocked.CompareExchange(ref m_createdThreads, current + 1, current));
15
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
221
17
7cd4a843b4e4 Improved worker pool
cin
parents: 16
diff changeset
222 UpdateMaxThreads(current + 1);
7cd4a843b4e4 Improved worker pool
cin
parents: 16
diff changeset
223
7cd4a843b4e4 Improved worker pool
cin
parents: 16
diff changeset
224 return true;
7cd4a843b4e4 Improved worker pool
cin
parents: 16
diff changeset
225 }
15
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
226
17
7cd4a843b4e4 Improved worker pool
cin
parents: 16
diff changeset
227 bool AllocateThreadSlot(int desired) {
20
1c3b3d518480 refactoring, sync
cin
parents: 17
diff changeset
228 if (desired - 1 != Interlocked.CompareExchange(ref m_createdThreads, desired, desired - 1))
17
7cd4a843b4e4 Improved worker pool
cin
parents: 16
diff changeset
229 return false;
7cd4a843b4e4 Improved worker pool
cin
parents: 16
diff changeset
230
7cd4a843b4e4 Improved worker pool
cin
parents: 16
diff changeset
231 UpdateMaxThreads(desired);
15
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
232
17
7cd4a843b4e4 Improved worker pool
cin
parents: 16
diff changeset
233 return true;
7cd4a843b4e4 Improved worker pool
cin
parents: 16
diff changeset
234 }
7cd4a843b4e4 Improved worker pool
cin
parents: 16
diff changeset
235
7cd4a843b4e4 Improved worker pool
cin
parents: 16
diff changeset
236 bool ReleaseThreadSlot(out bool last) {
7cd4a843b4e4 Improved worker pool
cin
parents: 16
diff changeset
237 last = false;
7cd4a843b4e4 Improved worker pool
cin
parents: 16
diff changeset
238 int current;
7cd4a843b4e4 Improved worker pool
cin
parents: 16
diff changeset
239 // use spins to release slot for the new thread
80
4f20870d0816 added memory barriers
cin
parents: 41
diff changeset
240 Thread.MemoryBarrier();
17
7cd4a843b4e4 Improved worker pool
cin
parents: 16
diff changeset
241 do {
20
1c3b3d518480 refactoring, sync
cin
parents: 17
diff changeset
242 current = m_createdThreads;
17
7cd4a843b4e4 Improved worker pool
cin
parents: 16
diff changeset
243 if (current <= m_minThreads && m_exitRequired == 0)
7cd4a843b4e4 Improved worker pool
cin
parents: 16
diff changeset
244 // the thread is reserved
7cd4a843b4e4 Improved worker pool
cin
parents: 16
diff changeset
245 return false;
20
1c3b3d518480 refactoring, sync
cin
parents: 17
diff changeset
246 } while (current != Interlocked.CompareExchange(ref m_createdThreads, current - 1, current));
17
7cd4a843b4e4 Improved worker pool
cin
parents: 16
diff changeset
247
7cd4a843b4e4 Improved worker pool
cin
parents: 16
diff changeset
248 last = (current == 1);
15
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
249
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
250 return true;
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
251 }
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
252
17
7cd4a843b4e4 Improved worker pool
cin
parents: 16
diff changeset
253 /// <summary>
7cd4a843b4e4 Improved worker pool
cin
parents: 16
diff changeset
254 /// releases thread slot unconditionally, used during cleanup
7cd4a843b4e4 Improved worker pool
cin
parents: 16
diff changeset
255 /// </summary>
7cd4a843b4e4 Improved worker pool
cin
parents: 16
diff changeset
256 /// <returns>true - no more threads left</returns>
7cd4a843b4e4 Improved worker pool
cin
parents: 16
diff changeset
257 bool ReleaseThreadSlotAnyway() {
20
1c3b3d518480 refactoring, sync
cin
parents: 17
diff changeset
258 var left = Interlocked.Decrement(ref m_createdThreads);
17
7cd4a843b4e4 Improved worker pool
cin
parents: 16
diff changeset
259 return left == 0;
15
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
260 }
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
261
17
7cd4a843b4e4 Improved worker pool
cin
parents: 16
diff changeset
262 void UpdateMaxThreads(int count) {
7cd4a843b4e4 Improved worker pool
cin
parents: 16
diff changeset
263 int max;
16
cin
parents: 15
diff changeset
264 do {
17
7cd4a843b4e4 Improved worker pool
cin
parents: 16
diff changeset
265 max = m_maxRunningThreads;
7cd4a843b4e4 Improved worker pool
cin
parents: 16
diff changeset
266 if (max >= count)
7cd4a843b4e4 Improved worker pool
cin
parents: 16
diff changeset
267 break;
7cd4a843b4e4 Improved worker pool
cin
parents: 16
diff changeset
268 } while(max != Interlocked.CompareExchange(ref m_maxRunningThreads, count, max));
16
cin
parents: 15
diff changeset
269 }
cin
parents: 15
diff changeset
270
17
7cd4a843b4e4 Improved worker pool
cin
parents: 16
diff changeset
271 #endregion
7cd4a843b4e4 Improved worker pool
cin
parents: 16
diff changeset
272
7cd4a843b4e4 Improved worker pool
cin
parents: 16
diff changeset
273 bool StartWorker() {
7cd4a843b4e4 Improved worker pool
cin
parents: 16
diff changeset
274 if (AllocateThreadSlot()) {
7cd4a843b4e4 Improved worker pool
cin
parents: 16
diff changeset
275 // slot successfully allocated
7cd4a843b4e4 Improved worker pool
cin
parents: 16
diff changeset
276 var worker = new Thread(this.Worker);
7cd4a843b4e4 Improved worker pool
cin
parents: 16
diff changeset
277 worker.IsBackground = true;
80
4f20870d0816 added memory barriers
cin
parents: 41
diff changeset
278 Interlocked.Increment(ref m_activeThreads);
17
7cd4a843b4e4 Improved worker pool
cin
parents: 16
diff changeset
279 worker.Start();
7cd4a843b4e4 Improved worker pool
cin
parents: 16
diff changeset
280
7cd4a843b4e4 Improved worker pool
cin
parents: 16
diff changeset
281 return true;
7cd4a843b4e4 Improved worker pool
cin
parents: 16
diff changeset
282 } else {
7cd4a843b4e4 Improved worker pool
cin
parents: 16
diff changeset
283 return false;
7cd4a843b4e4 Improved worker pool
cin
parents: 16
diff changeset
284 }
16
cin
parents: 15
diff changeset
285 }
cin
parents: 15
diff changeset
286
21
6a56df4ec59e DispatchPool works again, but performance is poor in some cases
cin
parents: 20
diff changeset
287 protected abstract void InvokeUnit(TUnit unit);
6a56df4ec59e DispatchPool works again, but performance is poor in some cases
cin
parents: 20
diff changeset
288
41
2fc0fbe7d58b Added TraceContext support to array traits
cin
parents: 34
diff changeset
289 protected virtual void Worker() {
21
6a56df4ec59e DispatchPool works again, but performance is poor in some cases
cin
parents: 20
diff changeset
290 TUnit unit;
22
5a35900264f5 implemented nonblocking wake singnals processing
cin
parents: 21
diff changeset
291 //Console.WriteLine("{0}: Active", Thread.CurrentThread.ManagedThreadId);
80
4f20870d0816 added memory barriers
cin
parents: 41
diff changeset
292 int count = 0;;
4f20870d0816 added memory barriers
cin
parents: 41
diff changeset
293 Thread.MemoryBarrier();
15
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
294 do {
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
295 // exit if requested
80
4f20870d0816 added memory barriers
cin
parents: 41
diff changeset
296 if (m_exitRequired == 1) {
15
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
297 // release the thread slot
20
1c3b3d518480 refactoring, sync
cin
parents: 17
diff changeset
298 Interlocked.Decrement(ref m_activeThreads);
80
4f20870d0816 added memory barriers
cin
parents: 41
diff changeset
299 if (!ReleaseThreadSlotAnyway()) // it was the last worker
21
6a56df4ec59e DispatchPool works again, but performance is poor in some cases
cin
parents: 20
diff changeset
300 SignalThread(); // wake next worker
6a56df4ec59e DispatchPool works again, but performance is poor in some cases
cin
parents: 20
diff changeset
301 break;
15
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
302 }
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
303
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
304 // fetch task
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
305 if (TryDequeue(out unit)) {
21
6a56df4ec59e DispatchPool works again, but performance is poor in some cases
cin
parents: 20
diff changeset
306 InvokeUnit(unit);
80
4f20870d0816 added memory barriers
cin
parents: 41
diff changeset
307 count ++;
21
6a56df4ec59e DispatchPool works again, but performance is poor in some cases
cin
parents: 20
diff changeset
308 continue;
15
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
309 }
20
1c3b3d518480 refactoring, sync
cin
parents: 17
diff changeset
310 Interlocked.Decrement(ref m_activeThreads);
15
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
311
80
4f20870d0816 added memory barriers
cin
parents: 41
diff changeset
312 Console.WriteLine("{0}: Suspend processed({1})", Thread.CurrentThread.ManagedThreadId,count);
16
cin
parents: 15
diff changeset
313 // entering suspend state
cin
parents: 15
diff changeset
314 // keep this thread and wait
20
1c3b3d518480 refactoring, sync
cin
parents: 17
diff changeset
315 if (!Suspend())
21
6a56df4ec59e DispatchPool works again, but performance is poor in some cases
cin
parents: 20
diff changeset
316 break;
80
4f20870d0816 added memory barriers
cin
parents: 41
diff changeset
317 count = 0;
22
5a35900264f5 implemented nonblocking wake singnals processing
cin
parents: 21
diff changeset
318 //Console.WriteLine("{0}: Awake", Thread.CurrentThread.ManagedThreadId);
20
1c3b3d518480 refactoring, sync
cin
parents: 17
diff changeset
319 Interlocked.Increment(ref m_activeThreads);
15
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
320 } while (true);
22
5a35900264f5 implemented nonblocking wake singnals processing
cin
parents: 21
diff changeset
321 //Console.WriteLine("{0}: Exited", Thread.CurrentThread.ManagedThreadId);
15
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
322 }
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
323
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
324 protected virtual void Dispose(bool disposing) {
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
325 if (disposing) {
80
4f20870d0816 added memory barriers
cin
parents: 41
diff changeset
326 if (0 == Interlocked.CompareExchange(ref m_exitRequired, 1, 0)) { // implies memory barrier
15
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
327 // wake sleeping threads
21
6a56df4ec59e DispatchPool works again, but performance is poor in some cases
cin
parents: 20
diff changeset
328 if (m_createdThreads > 0)
6a56df4ec59e DispatchPool works again, but performance is poor in some cases
cin
parents: 20
diff changeset
329 SignalThread();
15
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
330 GC.SuppressFinalize(this);
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
331 }
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
332 }
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
333 }
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
334
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
335 public void Dispose() {
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
336 Dispose(true);
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
337 }
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
338
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
339 ~DispatchPool() {
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
340 Dispose(false);
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
341 }
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
342 }
0f982f9b7d4d implemented parallel map and foreach for arrays
cin
parents:
diff changeset
343 }