forked from mlkazar/lwt
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paththreadpool.cc
More file actions
247 lines (216 loc) · 6.28 KB
/
threadpool.cc
File metadata and controls
247 lines (216 loc) · 6.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
#include "threadpool.h"
int32_t
ThreadPool::get(Worker **workerpp, int wait)
{
int32_t code = 0;
Worker *workerp;
_lock.take();
while(1) {
workerp = _idleThreads.pop();
if (!workerp) {
if (_createdThreads < _nthreads) {
/* create a worker if we're not at our limit */
_createdThreads++;
_lock.release();
workerp = _factoryp->newWorker();
workerp->init(this);
*workerpp = workerp;
_lock.take();
/* we want to wait until workerp gets to its idle
* state; we can't use workerp immediately, since
* workerp is still initializing itself.
*/
if (_idleThreads.head() == NULL && wait) {
_idleCv.wait();
}
}
else if (wait) {
/* at limit, wait until someone becomes idle */
_idleCv.wait();
}
else {
/* can't wait and no workers are ready */
code = TP_ERR_ALL_RUNNING;
workerp = NULL;
break;
}
}
else {
/* we popped off an idle thread */
workerp->_state = TP_NONE;
*workerpp = workerp;
break;
}
}
if (code == 0) {
osp_assert(workerp->_state == TP_NONE);
workerp->_state = TP_ACTIVE;
_activeThreads.append(workerp);
}
_lock.release();
return code;
}
int32_t
ThreadPool::joinAny(Worker **workerpp, void **returnValuepp, int wait)
{
Worker *workerp = NULL;
int32_t code = 0;
_lock.take();
while(1) {
workerp = _joinThreads.pop();
if (!workerp) {
if (!wait) {
code = TP_ERR_ALL_DONE;
break;
}
_joinReadyCv.wait();
if (_shutdown) {
_lock.release();
return TP_ERR_SHUTDOWN;
}
}
else {
break;
}
}
_lock.release();
*workerpp = workerp;
if (workerp) {
*returnValuepp = workerp->_joinReturnValuep;
workerp->_state = TP_JOINED;
}
else {
*returnValuepp = NULL;
}
return code;
}
/* once you call start, the thread is available */
void
ThreadPool::Worker::init(ThreadPool *poolp)
{
_poolp = poolp;
_state = TP_NONE;
_didInit = 1;
_joinOneReadyCv.setMutex( &poolp->_lock);
_resumeCv.setMutex( &poolp->_lock);
_finishedCv.setMutex(&poolp->_lock);
queue(); /* resume at our start function */
}
/* worker starts here after init is called */
void *
ThreadPool::Worker::start()
{
/* init function sets first values of things that stay the same between activations,
* such as associated mutex for cond variables.
*/
printf("Worker thread=%p created\n", this);
_poolp->_lock.take();
while(1) {
/* at top, worker is not in any queue, and has no more work to do */
if (_state == TP_NONE) {
_state = TP_IDLE;
_poolp->_idleThreads.append(this);
_poolp->_idleCv.broadcast();
}
/* set initial state up for this activation; note that all this code will run
* before any other thread can allocate this thread from the idleThreads queue,
* since we don't drop the lock until we wait for resumed below.
*/
_waitForJoin = 1;
_resumeDone = 0;
_finishedDone = 0;
_joinReturnValuep = NULL;
/* wait for a new request */
while(!_resumeDone) {
_resumeCv.wait();
if (_poolp->_shutdown){
_poolp->_lock.release();
return NULL;
}
}
assert(_state == TP_ACTIVE);
/* make the callout, and save the response; call without holding
* any locks.
*/
_poolp->_lock.release();
_joinReturnValuep = tpStart();
_poolp->_lock.take();
/* move to inval state */
_state = TP_NONE;
_poolp->_activeThreads.remove(this);
if (_waitForJoin) {
/* go into the join queue */
_state = TP_JOIN;
_poolp->_joinThreads.append(this);
/* don't know if someone's going to wait for any thread,
* or this one, so signal both. Signalling a condition
* variable with no waiters is pretty inexpensive.
*/
_poolp->_joinReadyCv.broadcast();
_joinOneReadyCv.broadcast();
/* wait for the guy who joined with us to release us */
while(!_finishedDone) {
_finishedCv.wait();
if (_poolp->_shutdown) {
_poolp->_lock.release();
return NULL;
}
}
/* join and finished done, and we're not in any queue now.
* Go back to TP_NONE state and look for more work.
*/
_state = TP_NONE;
}
} /* loop forever waiting for reuse */
}
void *
ThreadPool::Worker::tpJoin()
{
osp_assert(_waitForJoin);
_poolp->_lock.take();
while(_state != TP_JOIN) {
/* sleep until the right state */
_joinOneReadyCv.wait();
if (_poolp->_shutdown) {
break;
}
}
_poolp->_joinThreads.remove(this);
_state = TP_JOINED;
_poolp->_lock.release();
return (_poolp->_shutdown? NULL : _joinReturnValuep);
}
void
ThreadPool::Worker::tpResume()
{
_poolp->_lock.take();
_resumeDone = 1;
_poolp->_lock.release();
_resumeCv.broadcast();
}
void
ThreadPool::Worker::tpFinished()
{
_poolp->_lock.take();
_finishedDone = 1;
_poolp->_lock.release();
_finishedCv.broadcast();
}
void
ThreadPool::shutdown()
{
Worker *workerp;
_lock.take();
_shutdown = 1;
_idleCv.broadcast();
_joinReadyCv.broadcast();
for(workerp = _activeThreads.head(); workerp; workerp=workerp->_dqNextp) {
workerp->_joinOneReadyCv.broadcast();
workerp->_resumeCv.broadcast();
}
for(workerp = _joinThreads.head(); workerp; workerp=workerp->_dqNextp) {
workerp->_joinOneReadyCv.broadcast();
workerp->_resumeCv.broadcast();
}
_lock.release();
}