15
|
1 using System;
|
|
2 using System.Collections.Generic;
|
|
3 using System.Linq;
|
|
4 using System.Text;
|
|
5 using System.Threading;
|
|
6 using System.Diagnostics;
|
|
7
|
|
8 namespace Implab.Parallels {
|
|
9 public abstract class DispatchPool<TUnit> : IDisposable {
|
|
10 readonly int m_minThreads;
|
|
11 readonly int m_maxThreads;
|
|
12 int m_runningThreads = 0;
|
|
13 int m_maxRunningThreads = 0;
|
|
14 int m_suspended = 0;
|
|
15 int m_exitRequired = 0;
|
|
16 AutoResetEvent m_hasTasks = new AutoResetEvent(false);
|
|
17
|
|
18 protected DispatchPool(int min, int max) {
|
|
19 if (min < 0)
|
|
20 throw new ArgumentOutOfRangeException("min");
|
|
21 if (max <= 0)
|
|
22 throw new ArgumentOutOfRangeException("max");
|
|
23
|
|
24 if (min > max)
|
|
25 min = max;
|
|
26 m_minThreads = min;
|
|
27 m_maxThreads = max;
|
|
28 }
|
|
29
|
|
30 protected DispatchPool(int threads)
|
|
31 : this(threads, threads) {
|
|
32 }
|
|
33
|
|
34 protected DispatchPool() {
|
|
35 int maxThreads, maxCP;
|
|
36 ThreadPool.GetMaxThreads(out maxThreads, out maxCP);
|
|
37
|
|
38 m_minThreads = 0;
|
|
39 m_maxThreads = maxThreads;
|
|
40 }
|
|
41
|
|
42 protected void InitPool() {
|
|
43 for (int i = 0; i < m_minThreads; i++)
|
|
44 StartWorker();
|
|
45 }
|
|
46
|
|
47 public int ThreadCount {
|
|
48 get {
|
|
49 return m_runningThreads;
|
|
50 }
|
|
51 }
|
|
52
|
|
53 public int MaxRunningThreads {
|
|
54 get {
|
|
55 return m_maxRunningThreads;
|
|
56 }
|
|
57 }
|
|
58
|
|
59 protected bool IsDisposed {
|
|
60 get {
|
|
61 return m_exitRequired != 0;
|
|
62 }
|
|
63 }
|
|
64
|
|
65 bool StartWorker() {
|
16
|
66 int current;
|
15
|
67 // use spins to allocate slot for the new thread
|
|
68 do {
|
16
|
69 current = m_runningThreads;
|
15
|
70 if (current >= m_maxThreads || m_exitRequired != 0)
|
|
71 // no more slots left or the pool has been disposed
|
|
72 return false;
|
|
73 } while (current != Interlocked.CompareExchange(ref m_runningThreads, current + 1, current));
|
|
74
|
|
75 m_maxRunningThreads = Math.Max(m_maxRunningThreads, current + 1);
|
|
76
|
|
77 // slot successfully allocated
|
|
78
|
|
79 var worker = new Thread(this.Worker);
|
|
80 worker.IsBackground = true;
|
|
81 worker.Start();
|
|
82
|
|
83 return true;
|
|
84 }
|
|
85
|
|
86 protected abstract bool TryDequeue(out TUnit unit);
|
|
87
|
16
|
88 protected virtual void WakeNewWorker(bool extend) {
|
15
|
89 if (m_suspended > 0)
|
|
90 m_hasTasks.Set();
|
|
91 else
|
|
92 StartWorker();
|
|
93 }
|
|
94
|
16
|
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 }
|
|
108
|
15
|
109 bool FetchTask(out TUnit unit) {
|
|
110 do {
|
|
111 // exit if requested
|
|
112 if (m_exitRequired != 0) {
|
|
113 // release the thread slot
|
16
|
114 var running = Interlocked.Decrement(ref m_runningThreads);
|
15
|
115 if (running == 0) // it was the last worker
|
|
116 m_hasTasks.Dispose();
|
|
117 else
|
|
118 m_hasTasks.Set(); // release next worker
|
|
119 unit = default(TUnit);
|
|
120 return false;
|
|
121 }
|
|
122
|
|
123 // fetch task
|
|
124 if (TryDequeue(out unit)) {
|
16
|
125 WakeNewWorker(true);
|
15
|
126 return true;
|
|
127 }
|
|
128
|
|
129 //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) {
|
16
|
135 // check wheather this is the last thread and we have tasks
|
|
136
|
15
|
137 exit = false;
|
|
138 break;
|
|
139 }
|
|
140 } while (runningThreads != Interlocked.CompareExchange(ref m_runningThreads, runningThreads - 1, runningThreads));
|
|
141
|
|
142 if (exit) {
|
|
143 return false;
|
|
144 }
|
|
145
|
16
|
146 // entering suspend state
|
15
|
147 Interlocked.Increment(ref m_suspended);
|
16
|
148 // keep this thread and wait
|
|
149 Suspend();
|
15
|
150 Interlocked.Decrement(ref m_suspended);
|
|
151 } while (true);
|
|
152 }
|
|
153
|
|
154 protected abstract void InvokeUnit(TUnit unit);
|
|
155
|
|
156 void Worker() {
|
|
157 TUnit unit;
|
|
158 while (FetchTask(out unit))
|
|
159 InvokeUnit(unit);
|
|
160 }
|
|
161
|
|
162 protected virtual void Dispose(bool disposing) {
|
|
163 if (disposing) {
|
|
164 if (m_exitRequired == 0) {
|
|
165 if (Interlocked.CompareExchange(ref m_exitRequired, 1, 0) != 0)
|
|
166 return;
|
|
167
|
|
168 // wake sleeping threads
|
|
169 m_hasTasks.Set();
|
|
170 GC.SuppressFinalize(this);
|
|
171 }
|
|
172 }
|
|
173 }
|
|
174
|
|
175 public void Dispose() {
|
|
176 Dispose(true);
|
|
177 }
|
|
178
|
|
179 ~DispatchPool() {
|
|
180 Dispose(false);
|
|
181 }
|
|
182 }
|
|
183 }
|