ffead.server.doc
PoolThread.cpp
1 /*
2  Copyright 2009-2012, Sumeet Chhetri
3 
4  Licensed under the Apache License, Version 2.0 (the "License");
5  you may not use this file except in compliance with the License.
6  You may obtain a copy of the License at
7 
8  http://www.apache.org/licenses/LICENSE-2.0
9 
10  Unless required by applicable law or agreed to in writing, software
11  distributed under the License is distributed on an "AS IS" BASIS,
12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  See the License for the specific language governing permissions and
14  limitations under the License.
15 */
16 /*
17  * PoolThread.cpp
18  *
19  * Created on: Mar 23, 2010
20  * Author: sumeet
21  */
22 
23 #include "PoolThread.h"
24 
25 void* PoolThread::run(void *arg)
26 {
27  PoolThread* ths = (PoolThread*)arg;
28  ths->m_mutex->lock();
29  bool console = ths->console;
30  bool fl = ths->runFlag;
31  ths->m_mutex->unlock();
32  while (fl)
33  {
34  ths->mthread->wait();
35  Task* task = ths->getTask();
36  if (task)
37  {
38  try
39  {
40  task->run();
41  if(task->cleanUp)
42  {
43  delete task;
44  }
45  }
46  catch(exception& e)
47  {
48  if(console)
49  {
50  ths->logger << e.what() << flush;
51  }
52  }
53  ths->release();
54  }
55  ths->m_mutex->lock();
56  fl = ths->runFlag;
57  ths->m_mutex->unlock();
58  }
59  ths->m_mutex->lock();
60  ths->complete = true;
61  ths->m_mutex->unlock();
62  return NULL;
63 }
64 
65 PoolThread::PoolThread(bool console) {
66  logger = Logger::getLogger("PoolThread");
67  this->task = NULL;
68  this->idle = true;
69  this->console = console;
70  this->complete = false;
71  this->runFlag = true;
72  this->thrdStarted = false;
73  m_mutex = new Mutex;
74  mthread = new Thread(&run, this);
75 }
76 
77 PoolThread::PoolThread() {
78  logger = Logger::getLogger("PoolThread");
79  this->task = NULL;
80  this->idle = true;
81  this->console = false;
82  this->thrdStarted = false;
83  m_mutex = new Mutex;
84  mthread = new Thread(&run, this);
85 }
86 
87 PoolThread::~PoolThread() {
88  this->runFlag = false;
89  m_mutex->lock();
90  bool fl = this->complete;
91  m_mutex->unlock();
92  while(!fl)
93  {
94  m_mutex->lock();
95  fl = this->complete;
96  m_mutex->unlock();
97  mthread->interrupt();
98  Thread::mSleep(1);
99  }
100  delete mthread;
101  delete m_mutex;
102  if(console)
103  {
104  logger << "Destroyed PoolThread\n" << flush;
105  }
106 }
107 
108 void PoolThread::execute() {
109  if(thrdStarted)return;
110  m_mutex->lock();
111  mthread->execute();
112  thrdStarted = true;
113  m_mutex->unlock();
114 }
115 
116 void PoolThread::checkout(Task *task)
117 {
118  m_mutex->lock();
119  this->idle = false;
120  this->task = task;
121  if(thrdStarted)
122  this->mthread->interrupt();
123  m_mutex->unlock();
124 }
125 
126 void PoolThread::release()
127 {
128  m_mutex->lock();
129  this->task = NULL;
130  this->idle = true;
131  m_mutex->unlock();
132 }
133 
134 bool PoolThread::isIdle()
135 {
136  m_mutex->lock();
137  bool isIdle = idle;
138  m_mutex->unlock();
139  return isIdle;
140 }
141 
142 Task* PoolThread::getTask()
143 {
144  this->m_mutex->lock();
145  Task* task = this->task;
146  this->m_mutex->unlock();
147  return task;
148 }