1 |
/*************************************************************************** |
/*************************************************************************** |
2 |
* * |
* * |
3 |
* Copyright (C) 2007 Christian Schoenebeck, Grigor Iliev * |
* Copyright (C) 2007 - 2013 Christian Schoenebeck, Grigor Iliev * |
4 |
* * |
* * |
5 |
* This program is free software; you can redistribute it and/or modify * |
* This program is free software; you can redistribute it and/or modify * |
6 |
* it under the terms of the GNU General Public License as published by * |
* it under the terms of the GNU General Public License as published by * |
27 |
WorkerThread::WorkerThread() : Thread(true, false, 0, -4) { } |
WorkerThread::WorkerThread() : Thread(true, false, 0, -4) { } |
28 |
|
|
29 |
void WorkerThread::Execute(Runnable* pJob) { |
void WorkerThread::Execute(Runnable* pJob) { |
30 |
mutex.Lock(); |
{ |
31 |
queue.push_back(pJob); |
LockGuard lock(mutex); |
32 |
mutex.Unlock(); |
queue.push_back(pJob); |
33 |
|
} |
34 |
StartThread(); // ensure thread is running |
StartThread(); // ensure thread is running |
35 |
conditionJobsLeft.Set(true); // wake up thread |
conditionJobsLeft.Set(true); // wake up thread |
36 |
} |
} |
39 |
int WorkerThread::Main() { |
int WorkerThread::Main() { |
40 |
while (true) { |
while (true) { |
41 |
|
|
42 |
#if CONFIG_PTHREAD_TESTCANCEL |
#if CONFIG_PTHREAD_TESTCANCEL |
43 |
TestCancel(); |
TestCancel(); |
44 |
#endif |
#endif |
45 |
while (!queue.empty()) { |
while (true) { |
46 |
Runnable* pJob; |
Runnable* pJob; |
47 |
|
|
48 |
// grab a new job from the queue |
// grab a new job from the queue |
49 |
mutex.Lock(); |
{ |
50 |
pJob = queue.front(); |
LockGuard lock(mutex); |
51 |
mutex.Unlock(); |
if (queue.empty()) break; |
52 |
|
|
53 |
|
pJob = queue.front(); |
54 |
|
queue.pop_front(); |
55 |
|
} |
56 |
|
|
57 |
try { |
try { |
58 |
pJob->Run(); |
pJob->Run(); |
63 |
std::cerr << std::endl << std::flush; |
std::cerr << std::endl << std::flush; |
64 |
} |
} |
65 |
|
|
|
// remove processed job from the queue |
|
|
mutex.Lock(); |
|
|
queue.pop_front(); |
|
|
mutex.Unlock(); |
|
|
|
|
66 |
delete pJob; |
delete pJob; |
67 |
} |
} |
68 |
|
|