/usr/include/root/TThreadPool.h is in libroot-core-dev 5.34.14-1build1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 | // @(#)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
#ifndef ROOT_TObject
#include "TObject.h"
#endif
#ifndef ROOT_TMutex
#include "TMutex.h"
#endif
#ifndef ROOT_TCondition
#include "TCondition.h"
#endif
// STD
#include <queue>
#include <vector>
#include <iostream>
#include <sstream>
//////////////////////////////////////////////////////////////////////////
// //
// 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),
fSilent(!needDbg) {
fThreadNeeded = new TCondition(&fMutex);
fThreadAvailable = new TCondition(&fMutex);
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);
}
~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;
}
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();
}
size_t TasksCount() const {
return fTasksCount;
}
size_t SuccessfulTasks() const {
return fSuccessfulTasks;
}
private:
static void* Executor(void *arg) {
TThreadPool *pThis = reinterpret_cast<TThreadPool*>(arg);
while (!pThis->fStopped) {
task_t *task(NULL);
std::stringstream ss;
ss
<< ">>>> Check for tasks."
<< " Number of Tasks: " << pThis->fTasks.size();
pThis->DbgLog(ss.str());
// There is a task, let's take it
{
TLockGuard lock(&pThis->fMutex);
// Find a task to perform
if (pThis->fTasks.empty() && !pThis->fStopped) {
pThis->DbgLog("waiting for a task");
// No tasks, we wait for a task to come
pThis->fThreadNeeded->Wait();
pThis->DbgLog("done waiting for tasks");
}
{
if (!pThis->fTasks.empty()) {
task = pThis->fTasks.front();
pThis->fTasks.pop();
pThis->DbgLog("get the task");
}
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;
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;
threads_array_t fThreads;
TThread *fThreadJoinHelper;
volatile bool fStopped;
size_t fSuccessfulTasks;
size_t fTasksCount;
TMutex fDbgOutputMutex;
bool fSilent; // No DBG messages
};
#endif
|