-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Expand file tree
/
Copy pathTThreadPool.h
More file actions
357 lines (305 loc) · 11.7 KB
/
TThreadPool.h
File metadata and controls
357 lines (305 loc) · 11.7 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
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
346
347
348
349
350
351
352
353
354
355
356
357
// @(#)root/thread:$Id$
// Author: Anar Manafov 20/09/2011
/*************************************************************************
* Copyright (C) 1995-2011, Rene Brun and Fons Rademakers. *
* All rights reserved. *
* *
* For the licensing terms see $ROOTSYS/LICENSE. *
* For the list of contributors see $ROOTSYS/README/CREDITS. *
*************************************************************************/
#ifndef ROOT_TThreadPool
#define ROOT_TThreadPool
//////////////////////////////////////////////////////////////////////////
// //
// TThreadPool //
// //
// //
//////////////////////////////////////////////////////////////////////////
// ROOT
#include "RtypesCore.h"
#include "TMutex.h"
#include "TCondition.h"
#include "TThread.h"
// STD
#include <queue>
#include <vector>
#include <iostream>
#include <sstream>
#include <utility>
#ifdef _MSC_VER
#define sleep(s) _sleep(s)
#else
#include <unistd.h>
#endif
//////////////////////////////////////////////////////////////////////////
// //
// TNonCopyable //
// Class which makes child to be non-copyable object. //
// //
//////////////////////////////////////////////////////////////////////////
class TNonCopyable {
protected:
TNonCopyable() { }
~TNonCopyable() { }
private:
TNonCopyable(const TNonCopyable&);
const TNonCopyable& operator=(const TNonCopyable&);
};
//////////////////////////////////////////////////////////////////////////
// //
// TThreadPoolTaskImp //
// A base class for thread pool tasks. Users must inherit their //
// tasks classes from it. //
// Example: //
// class TTestTask: public TThreadPoolTaskImp<TTestTask, int> //
// //
// in this example, //
// TTestTask - is a user class, which implements //
// thread pool task object. //
// int - is a type of argument to TTestTask::run method. //
// //
// Please see the tutorial "tutorials/thread/threadPool.C" for //
// more details on how to use TThreadPool. //
// //
//////////////////////////////////////////////////////////////////////////
template <class aTask, class aParam>
class TThreadPoolTaskImp {
public:
bool run(aParam ¶m) {
aTask *pThis = reinterpret_cast<aTask *>(this);
return pThis->runTask(param);
}
};
//////////////////////////////////////////////////////////////////////////
// //
// TThreadPoolTask //
// This is a supporting class for TThreadPool. //
// It wraps users task objects in order to pass tasks arguments in //
// type-safe way. //
// //
//////////////////////////////////////////////////////////////////////////
template <class aTask, class aParam>
class TThreadPoolTask {
public:
typedef TThreadPoolTaskImp<aTask, aParam> task_t;
public:
TThreadPoolTask(task_t &task, aParam ¶m):
fTask(task),
fTaskParam(param) {
}
bool run() {
return fTask.run(fTaskParam);
}
private:
task_t &fTask;
aParam fTaskParam;
};
//////////////////////////////////////////////////////////////////////////
// //
// TThreadPool //
// This class implement a simple Thread Pool pattern. //
// So far it supports only one type of queue - FIFO //
// //
// Please see the tutorial "tutorials/thread/threadPool.C" for //
// more details on how to use TThreadPool. //
// //
//////////////////////////////////////////////////////////////////////////
template <class aTask, class aParam>
class TThreadPool : public TNonCopyable {
typedef TThreadPoolTask<aTask, aParam> task_t;
typedef std::queue<task_t*> taskqueue_t;
typedef std::vector<TThread*> threads_array_t;
public:
TThreadPool(size_t threadsCount, bool needDbg = false):
fStopped(false),
fSuccessfulTasks(0),
fTasksCount(0),
fIdleThreads(threadsCount),
fSilent(!needDbg) {
fThreadNeeded = new TCondition(&fMutex);
fThreadAvailable = new TCondition(&fMutex);
fAllTasksDone = new TCondition(&fMutexAllTasksDone);
for (size_t i = 0; i < threadsCount; ++i) {
TThread *pThread = new TThread(&TThreadPool::Executor, this);
fThreads.push_back(pThread);
pThread->Run();
}
fThreadJoinHelper = new TThread(&TThreadPool::JoinHelper, this);
if (needDbg) {
fThreadMonitor = new TThread(&TThreadPool::Monitor, this);
fThreadMonitor->Run();
}
}
~TThreadPool() {
Stop();
// deleting threads
threads_array_t::const_iterator iter = fThreads.begin();
threads_array_t::const_iterator iter_end = fThreads.end();
for (; iter != iter_end; ++iter)
delete(*iter);
delete fThreadJoinHelper;
delete fThreadNeeded;
delete fThreadAvailable;
delete fAllTasksDone;
}
void AddThread() {
TLockGuard lock(&fMutex);
TThread *pThread = new TThread(&TThreadPool::Executor, this);
fThreads.push_back(pThread);
pThread->Run();
++fIdleThreads;
}
void PushTask(typename TThreadPoolTask<aTask, aParam>::task_t &task, aParam param) {
{
DbgLog("Main thread. Try to push a task");
TLockGuard lock(&fMutex);
task_t *t = new task_t(task, param);
fTasks.push(t);
++fTasksCount;
DbgLog("Main thread. the task is pushed");
}
TLockGuard lock(&fMutex);
fThreadNeeded->Broadcast();
}
void Stop(bool processRemainingJobs = false) {
// prevent more jobs from being added to the queue
if (fStopped)
return;
if (processRemainingJobs) {
TLockGuard lock(&fMutex);
// wait for queue to drain
while (!fTasks.empty() && !fStopped) {
DbgLog("Main thread is waiting");
fThreadAvailable->Wait();
DbgLog("Main thread is DONE waiting");
}
}
// tell all threads to stop
{
TLockGuard lock(&fMutex);
fStopped = true;
fThreadNeeded->Broadcast();
DbgLog("Main threads requests to STOP");
}
// Waiting for all threads to complete
fThreadJoinHelper->Run();
fThreadJoinHelper->Join();
}
void Drain() {
// This method stops the calling thread until the task queue is empty
TLockGuard lock(&fMutexAllTasksDone);
fAllTasksDone->Wait();
}
size_t TasksCount() const {
return fTasksCount;
}
size_t SuccessfulTasks() const {
return fSuccessfulTasks;
}
size_t IdleThreads() const {
return fIdleThreads;
}
private:
static void* Monitor(void *arg) {
if (NULL == arg)
return NULL;
TThreadPool *pThis = reinterpret_cast<TThreadPool*>(arg);
while (true && !pThis->fStopped) {
std::stringstream ss;
ss
<< ">>>> Check for tasks."
<< " Number of Tasks: " << pThis->fTasks.size()
<< "; Idle threads: " << pThis->IdleThreads();
pThis->DbgLog(ss.str());
sleep(1);
}
return NULL;
}
static void* Executor(void *arg) {
TThreadPool *pThis = reinterpret_cast<TThreadPool*>(arg);
while (!pThis->fStopped) {
task_t *task(NULL);
// There is a task, let's take it
{
// Find a task to perform
TLockGuard lock(&pThis->fMutex);
if (pThis->fTasks.empty() && !pThis->fStopped) {
pThis->DbgLog("waiting for a task");
if (pThis->fThreads.size() == pThis->fIdleThreads) {
TLockGuard l(&pThis->fMutexAllTasksDone);
pThis->fAllTasksDone->Broadcast();
}
// No tasks, we wait for a task to come
pThis->fThreadNeeded->Wait();
pThis->DbgLog("done waiting for tasks");
}
}
{
TLockGuard lock(&pThis->fMutex);
if (!pThis->fTasks.empty()) {
--pThis->fIdleThreads;
task = pThis->fTasks.front();
pThis->fTasks.pop();
pThis->DbgLog("get the task");
} else if (pThis->fThreads.size() == pThis->fIdleThreads) {
TLockGuard l(&pThis->fMutexAllTasksDone);
pThis->fAllTasksDone->Broadcast();
}
pThis->DbgLog("done Check <<<<");
}
// Execute the task
if (task) {
pThis->DbgLog("Run the task");
if (task->run()) {
TLockGuard lock(&pThis->fMutex);
++pThis->fSuccessfulTasks;
}
delete task;
task = NULL;
TLockGuard lock(&pThis->fMutex);
++pThis->fIdleThreads;
pThis->DbgLog("Done Running the task");
}
// Task is done, report that the thread is free
TLockGuard lock(&pThis->fMutex);
pThis->fThreadAvailable->Broadcast();
}
pThis->DbgLog("**** DONE ***");
return NULL;
}
static void *JoinHelper(void *arg) {
TThreadPool *pThis = reinterpret_cast<TThreadPool*>(arg);
threads_array_t::const_iterator iter = pThis->fThreads.begin();
threads_array_t::const_iterator iter_end = pThis->fThreads.end();
for (; iter != iter_end; ++iter)
(*iter)->Join();
return NULL;
}
static bool IsThreadActive(TThread *pThread) {
// so far we consider only kRunningState as activity
return (pThread->GetState() == TThread::kRunningState);
}
void DbgLog(const std::string &msg) {
if (fSilent)
return;
TLockGuard lock(&fDbgOutputMutex);
std::cout << "[" << TThread::SelfId() << "] " << msg << std::endl;
}
private:
taskqueue_t fTasks;
TMutex fMutex;
TCondition *fThreadNeeded;
TCondition *fThreadAvailable;
TMutex fMutexAllTasksDone;
TCondition *fAllTasksDone;
threads_array_t fThreads;
TThread *fThreadJoinHelper;
TThread *fThreadMonitor;
volatile bool fStopped;
size_t fSuccessfulTasks;
size_t fTasksCount;
size_t fIdleThreads;
TMutex fDbgOutputMutex;
bool fSilent; // No DBG messages
};
#endif