25 void* TaskPool::run(
void *arg)
28 vector<Task*>::iterator iter;
29 pool->m_mutex->lock();
30 bool fl = pool->runFlag;
31 pool->m_mutex->unlock();
34 pool->m_mutex->lock();
35 int total = pool->scheduledtasks->size();
36 pool->m_mutex->unlock();
37 std::queue<int> tobeRemoved;
38 for (
int i=0; i<total;i++) {
39 pool->m_mutex->lock();
40 Task* task = pool->scheduledtasks->at(i);
41 Timer* timer = pool->scheduledTimers->at(i);
42 pool->m_mutex->unlock();
45 if(task->isWaitOver(timer))
48 pool->m_mutex->lock();
49 pool->tasks->push(task);
50 pool->m_mutex->unlock();
55 while (!tobeRemoved.empty()) {
56 int index = tobeRemoved.front() - counter;
59 pool->m_mutex->lock();
60 Timer* timer = pool->scheduledTimers->at(index);
61 pool->scheduledtasks->erase(pool->scheduledtasks->begin()+index);
62 pool->scheduledTimers->erase(pool->scheduledTimers->begin()+index);
64 pool->m_mutex->unlock();
67 pool->m_mutex->lock();
69 pool->m_mutex->unlock();
71 pool->m_mutex->lock();
72 pool->complete =
true;
73 pool->m_mutex->unlock();
77 TaskPool::TaskPool() {
78 m_mutex =
new Mutex();
79 tasks =
new std::queue<Task*>;
80 ptasks =
new list<Task*>;
81 scheduledtasks =
new vector<Task*>;
82 scheduledTimers =
new vector<Timer*>;
85 mthread =
new Thread(&run,
this);
89 void TaskPool::start() {
90 if(thrdStarted)
return;
95 void TaskPool::addTask(
Task &task) {
97 if (task.type >= 0 && task.type <= 6 && task.tunit > 0)
101 scheduledTimers->push_back(t);
102 scheduledtasks->push_back(&task);
111 void TaskPool::addTask(
Task *task) {
113 if (task->type >= 0 && task->type <= 6 && task->tunit > 0)
117 scheduledTimers->push_back(t);
118 scheduledtasks->push_back(task);
127 void TaskPool::addPTask(
Task &task) {
129 ptasks->push_back(&task);
132 void TaskPool::addPTask(
Task *task) {
134 ptasks->push_back(task);
137 Task* TaskPool::getTask() {
142 task = tasks->front();
148 Task* TaskPool::getPTask() {
152 list<Task*>::iterator iter, iter1;
153 for (iter = ptasks->begin(); iter != ptasks->end(); ++iter) {
154 if ((*iter)->priority > currpri) {
157 currpri = task->priority;
160 if(task!=NULL)ptasks->remove(task);
164 bool TaskPool::tasksPending() {
166 bool tp = !tasks->empty();
167 tp |= !scheduledtasks->empty();
171 bool TaskPool::tasksPPending() {
173 bool tp = !ptasks->empty();
174 tp |= !scheduledtasks->empty();
178 TaskPool::~TaskPool() {
183 bool fl = this->complete;
195 delete scheduledtasks;
196 delete scheduledTimers;