22 #include "ThreadPool.h"
24 ThreadPool::ThreadPool()
26 this->runFlag =
false;
28 logger = Logger::getLogger(
"ThreadPool");
31 void ThreadPool::init(
int initThreads,
int maxThreads,
bool console)
33 if(wpool!=NULL)
return;
34 this->console = console;
37 this->initThreads = initThreads;
38 this->maxThreads = maxThreads;
40 prioritypooling =
false;
45 ThreadPool::ThreadPool(
int initThreads,
int maxThreads,
int lowp,
int highp) {
47 throw "Low Priority should be less than Highest Priority";
48 logger = Logger::getLogger(
"ThreadPool");
49 this->console =
false;
50 this->initThreads = initThreads;
51 this->maxThreads = maxThreads;
54 this->runFlag =
false;
56 prioritypooling =
true;
59 ThreadPool::ThreadPool(
int initThreads,
int maxThreads,
int lowp,
int highp,
bool console) {
60 this->console = console;
62 throw "Low Priority should be less than Highest Priority";
63 logger = Logger::getLogger(
"ThreadPool");
64 this->initThreads = initThreads;
65 this->maxThreads = maxThreads;
68 this->runFlag =
false;
70 prioritypooling =
true;
74 ThreadPool::ThreadPool(
int initThreads,
int maxThreads) {
75 logger = Logger::getLogger(
"ThreadPool");
78 this->console =
false;
79 this->initThreads = initThreads;
80 this->maxThreads = maxThreads;
81 this->runFlag =
false;
83 prioritypooling =
false;
87 ThreadPool::ThreadPool(
int initThreads,
int maxThreads,
bool console) {
88 logger = Logger::getLogger(
"ThreadPool");
89 this->console = console;
92 this->initThreads = initThreads;
93 this->maxThreads = maxThreads;
94 this->runFlag =
false;
96 prioritypooling =
false;
100 void ThreadPool::initializeThreads()
104 wpool->console = console;
105 tpool =
new vector<PoolThread*>;
106 for (
int i = 0; i < initThreads; i++) {
109 tpool->push_back(thread);
112 poller =
new Thread(&ThreadPool::poll,
this);
114 pollerStarted =
false;
119 void ThreadPool::start()
121 if(pollerStarted)
return;
123 pollerStarted =
true;
126 void* ThreadPool::poll(
void *arg) {
128 ths->m_mutex->lock();
129 bool fl = ths->runFlag;
130 ths->m_mutex->unlock();
132 if (!ths->prioritypooling && ths->wpool->tasksPending()) {
133 Task *task = ths->wpool->getTask();
138 }
else if (ths->prioritypooling && ths->wpool->tasksPPending()) {
139 Task *task = ths->wpool->getPTask();
146 ths->m_mutex->lock();
148 ths->m_mutex->unlock();
150 ths->m_mutex->lock();
151 ths->complete =
true;
152 ths->m_mutex->unlock();
156 void ThreadPool::submit(
Task *task) {
159 for (
unsigned int var = 0; var < tpool->size(); var++) {
160 if (tpool->at(var)->isIdle()) {
161 tpool->at(var)->checkout(task);
170 void ThreadPool::joinAll() {
171 while (!joinComplete) {
172 if (!prioritypooling) {
173 while (wpool->tasksPending()) {
177 while (wpool->tasksPPending()) {
182 for (
unsigned int var = 0; var < tpool->size(); var++) {
183 if (tpool->at(var)->isIdle()) {
187 if (i == initThreads) {
195 void ThreadPool::execute(
Task &task,
int priority) {
199 logger <<
"Adding task to wpool\n" << flush;
203 task.priority = priority;
204 task.console = console;
205 if (!prioritypooling) {
206 wpool->addTask(task);
208 wpool->addPTask(task);
211 void ThreadPool::execute(
Task &task) {
215 logger <<
"Adding task to wpool\n" << flush;
220 task.console = console;
221 if (!prioritypooling) {
222 wpool->addTask(task);
224 wpool->addPTask(task);
227 void ThreadPool::schedule(
Task &task,
long long tunit,
int type) {
231 logger <<
"Added task to wpool\n" << flush;
236 task.console = console;
237 if (!prioritypooling) {
238 wpool->addTask(task);
240 wpool->addPTask(task);
244 ThreadPool::~ThreadPool() {
245 while(!joinComplete) {
249 this->m_mutex->lock();
250 this->runFlag =
false;
251 this->m_mutex->unlock();
254 bool fl = this->complete;
265 for (
int i = 0; i <(int)tpool->size(); i++) {
271 logger <<
"Destroyed PoolThread Pool\n" << flush;