-#include <pthread.h>
#include <queue>
#include <assert.h>
*/
int WorkQueue::run_tasks(void)
{
- lock.lock();
+ os::unique_lock<os::mutex> lock(mutex);
- while (work_queue.empty() && !exit_workqueue)
- pthread_cond_wait(&wake_cond, &lock.native_handle());
+ while (work_queue.empty() && !exit_workqueue) {
+ wake_cond.wait(lock);
+ }
if (exit_workqueue) {
- lock.unlock();
return -1;
}
lock.lock();
busy = false;
- pthread_cond_signal(&complete_cond);
-
- lock.unlock();
+ complete_cond.signal();
return 0;
}
/* Must be called with WorkQueue::lock held */
void WorkQueue::wake_up_thread(void)
{
- pthread_cond_signal(&wake_cond);
+ wake_cond.signal();
}
void WorkQueue::queue_work(WorkQueueWork *task)
{
- lock.lock();
+ mutex.lock();
work_queue.push(task);
wake_up_thread();
- lock.unlock();
+ mutex.unlock();
}
void WorkQueue::flush(void)
{
- lock.lock();
- while (!work_queue.empty() || busy)
- pthread_cond_wait(&complete_cond, &lock.native_handle());
- lock.unlock();
+ os::unique_lock<os::mutex> lock(mutex);
+ while (!work_queue.empty() || busy) {
+ complete_cond.wait(lock);
+ }
}
void WorkQueue::thread_entry(void)
void WorkQueue::destroy(void)
{
- lock.lock();
+ mutex.lock();
exit_workqueue = true;
wake_up_thread();
- lock.unlock();
+ mutex.unlock();
}
-extern "C"
void *WorkQueue__entry_thunk(void *data)
{
WorkQueue *thread = static_cast<WorkQueue *>(data);
}
WorkQueue::WorkQueue(void) :
- busy(false), exit_workqueue(false)
+ busy(false),
+ exit_workqueue(false),
+ thread(WorkQueue__entry_thunk, this)
{
- int err;
-
- pthread_cond_init(&wake_cond, NULL);
- pthread_cond_init(&complete_cond, NULL);
- err = pthread_create(&handle, NULL, WorkQueue__entry_thunk, this);
- assert(!err);
}
WorkQueue::~WorkQueue(void)
{
- pthread_join(handle, NULL);
+ thread.join();
}
}