forked from solidoss/solidframe
-
Notifications
You must be signed in to change notification settings - Fork 0
/
workpool.hpp
348 lines (304 loc) · 7.02 KB
/
workpool.hpp
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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
// solid/utility/workpool.hpp
//
// Copyright (c) 2007, 2008 Valentin Palade (vipalade @ gmail . com)
//
// This file is part of SolidFrame framework.
//
// Distributed under the Boost Software License, Version 1.0.
// See accompanying file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt.
//
#ifndef UTILITY_WORKPOOL_HPP
#define UTILITY_WORKPOOL_HPP
#include <thread>
#include <mutex>
#include <vector>
#include <condition_variable>
#include "solid/utility/common.hpp"
#include "solid/utility/queue.hpp"
namespace solid{
//! Base class for every workpool workers
struct WorkerBase{
uint32_t wkrid;
};
//! Base class for workpool
struct WorkPoolBase{
enum State{
Stopped = 0,
Stopping,
Running
};
State state()const{
return st;
}
void state(State _s){
st = _s;
}
bool isRunning()const{
return st == Running;
}
bool isStopping()const{
return st == Stopping;
}
protected:
WorkPoolBase():st(Stopped), wkrcnt(0) {}
State st;
int wkrcnt;
std::condition_variable thrcnd;
std::condition_variable sigcnd;
std::mutex mtx;
};
//! A controller structure for WorkPool
/*!
* The WorkPool will call methods of this structure.
* Inherit and implement the needed methods.
* No need for virtualization as the Controller is
* a template parameter for WorkPool.
*/
struct WorkPoolControllerBase{
bool prepareWorker(WorkerBase &_rw){
return true;
}
void unprepareWorker(WorkerBase &_rw){
}
void onPush(WorkPoolBase &){
}
void onMultiPush(WorkPoolBase &, size_t _cnd){
}
size_t onPopStart(WorkPoolBase &, WorkerBase &, size_t _maxcnt){
return _maxcnt;
}
void onPopDone(WorkPoolBase &, WorkerBase &){
}
void onStop(){
}
};
//! A generic workpool
/*!
* The template parameters are:<br>
* J - the Job type to be processed by the workpool. I
* will hold a Queue\<J\>.<br>
* C - WorkPool controller, the workpool will call controller
* methods on different ocasions / events<br>
* W - Base class for workers. Specify this if you want certain data
* kept per worker. The workpool's actual workers will publicly
* inherit W.<br>
*/
template <class J, class C, class W = WorkerBase>
class WorkPool: public WorkPoolBase{
typedef std::vector<J> JobVectorT;
typedef WorkPool<J, C, W> ThisT;
typedef std::vector<std::thread> ThreadVectorT;
struct SingleWorker: W{
SingleWorker(ThisT &_rw):rw(_rw){}
void run(){
if(!rw.enterWorker(*this)){
return;
}
J job;
while(rw.pop(*this, job)){
rw.execute(*this, job);
}
rw.exitWorker(*this);
}
ThisT &rw;
};
struct MultiWorker: W{
MultiWorker(ThisT &_rw, size_t _maxcnt):rw(_rw), maxcnt(_maxcnt){}
void run(){
if(!rw.enterWorker(*this)){
return;
}
JobVectorT jobvec;
if(maxcnt == 0) maxcnt = 1;
while(rw.pop(*this, jobvec, maxcnt)){
rw.execute(*this, jobvec);
jobvec.clear();
}
rw.exitWorker(*this);
}
ThisT &rw;
size_t maxcnt;
};
static void single_worker_run(ThisT *_pthis){
SingleWorker wkr(*_pthis);
wkr.run();
}
static void multi_worker_run(ThisT *_pthis, const size_t _cnt){
MultiWorker wkr(*_pthis, _cnt);
wkr.run();
}
public:
typedef ThisT WorkPoolT;
typedef C ControllerT;
typedef W WorkerT;
typedef J JobT;
WorkPool(){
}
template <class T>
WorkPool(T &_rt):ctrl(_rt){
}
~WorkPool(){
stop(true);
}
ControllerT& controller(){
return ctrl;
}
//! Push a new job
void push(const JobT& _jb){
std::unique_lock<std::mutex> lock(mtx);
jobq.push(_jb);
sigcnd.notify_one();
ctrl.onPush(*this);
}
//! Push a table of jobs of size _cnt
template <class I>
void push(I _i, const I &_end){
std::unique_lock<std::mutex> lock(mtx);
size_t cnt(_end - _i);
for(; _i != _end; ++_i){
jobq.push(*_i);
}
sigcnd.notify_all();
ctrl.onMultiPush(*this, cnt);
}
//! Starts the workpool, creating _minwkrcnt
void start(ushort _minwkrcnt = 0){
std::unique_lock<std::mutex> lock(mtx);
if(state() == Running){
return;
}
if(state() != Stopped){
doStop(lock, true);
}
wkrcnt = 0;
state(Running);
for(ushort i(0); i < _minwkrcnt; ++i){
createWorker();
}
}
//! Initiate workpool stop
/*!
It can block waiting for all workers to stop or just initiate stopping.
The ideea is that when one have M workpools, it is faster to
first initiate stop for all pools (wp[i].stop(false)) and then wait
for actual stoping (wp[i].stop(true))
*/
void stop(bool _wait = true){
std::unique_lock<std::mutex> lock(mtx);
doStop(lock, _wait);
}
size_t size()const{
return jobq.size();
}
bool empty()const{
return jobq.empty();
}
void createWorker(){
++wkrcnt;
static const std::thread empty_thread;
thread_vec.push_back(std::move(std::thread()));
ctrl.createWorker(*this, wkrcnt, thread_vec.back());
if(thread_vec.back().get_id() == empty_thread.get_id()){
--wkrcnt;
thread_vec.pop_back();
thrcnd.notify_all();
}
}
void createSingleWorker(std::thread &_rthr){
try{
_rthr = std::thread(single_worker_run, this);
}catch(...){
_rthr.join();
_rthr = std::thread();
}
}
void createMultiWorker(std::thread &_rthr, size_t _maxcnt){
try{
_rthr = std::thread(multi_worker_run, this, _maxcnt);
}catch(...){
_rthr.join();
_rthr = std::thread();
}
}
private:
friend struct SingleWorker;
friend struct MultiWorker;
void doStop(std::unique_lock<std::mutex> &_lock, bool _wait){
if(state() == Stopped) return;
state(Stopping);
sigcnd.notify_all();
ctrl.onStop();
if(!_wait) return;
while(wkrcnt){
thrcnd.wait(_lock);
}
for(std::thread &thr: thread_vec){
thr.join();
}
state(Stopped);
}
bool pop(WorkerT &_rw, JobVectorT &_rjobvec, size_t _maxcnt){
std::unique_lock<std::mutex> lock(mtx);
uint32_t insertcount(ctrl.onPopStart(*this, _rw, _maxcnt));
if(!insertcount){
return true;
}
if(doWaitJob(lock)){
do{
_rjobvec.push_back(jobq.front());
jobq.pop();
}while(jobq.size() && --insertcount);
ctrl.onPopDone(*this, _rw);
return true;
}
return false;
}
bool pop(WorkerT &_rw, JobT &_rjob){
std::unique_lock<std::mutex> lock(mtx);
if(ctrl.onPopStart(*this, _rw, 1) == 0){
sigcnd.notify_one();
return false;
}
if(doWaitJob(lock)){
_rjob = jobq.front();
jobq.pop();
ctrl.onPopDone(*this, _rw);
return true;
}
return false;
}
size_t doWaitJob(std::unique_lock<std::mutex> &_lock){
while(jobq.empty() && isRunning()){
sigcnd.wait(_lock);
}
return jobq.size();
}
bool enterWorker(WorkerT &_rw){
std::unique_lock<std::mutex> lock(mtx);
if(!ctrl.prepareWorker(_rw)){
return false;
}
//++wkrcnt;
thrcnd.notify_all();
return true;
}
void exitWorker(WorkerT &_rw){
std::unique_lock<std::mutex> lock(mtx);
ctrl.unprepareWorker(_rw);
--wkrcnt;
SOLID_ASSERT(wkrcnt >= 0);
thrcnd.notify_all();
}
void execute(WorkerT &_rw, JobT &_rjob){
ctrl.execute(*this, _rw, _rjob);
}
void execute(WorkerT &_rw, JobVectorT &_rjobvec){
ctrl.execute(*this, _rw, _rjobvec);
}
private:
Queue<JobT> jobq;
ControllerT ctrl;
ThreadVectorT thread_vec;
};
}//namespace solid
#endif