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