5 #include "os_workqueue.hpp"
11 * return 0 on batch complete, -1 on thread exit request.
13 int WorkQueue::run_tasks(void)
15 pthread_mutex_lock(&lock);
17 while (work_queue.empty() && !exit_workqueue)
18 pthread_cond_wait(&wake_cond, &lock);
21 pthread_mutex_unlock(&lock);
25 std::queue<WorkQueueWork *> batch;
26 std::swap(work_queue, batch);
29 pthread_mutex_unlock(&lock);
31 assert(!batch.empty());
32 while (!batch.empty()) {
41 pthread_mutex_lock(&lock);
44 pthread_cond_signal(&complete_cond);
46 pthread_mutex_unlock(&lock);
51 /* Must be called with WorkQueue::lock held */
52 void WorkQueue::wake_up_thread(void)
54 pthread_cond_signal(&wake_cond);
57 void WorkQueue::queue_work(WorkQueueWork *task)
59 pthread_mutex_lock(&lock);
60 work_queue.push(task);
62 pthread_mutex_unlock(&lock);
65 void WorkQueue::flush(void)
67 pthread_mutex_lock(&lock);
68 while (!work_queue.empty() || busy)
69 pthread_cond_wait(&complete_cond, &lock);
70 pthread_mutex_unlock(&lock);
73 void WorkQueue::thread_entry(void)
82 void WorkQueue::destroy(void)
84 pthread_mutex_lock(&lock);
85 exit_workqueue = true;
87 pthread_mutex_unlock(&lock);
91 void *WorkQueue__entry_thunk(void *data)
93 WorkQueue *thread = static_cast<WorkQueue *>(data);
95 thread->thread_entry();
100 WorkQueue::WorkQueue(void) :
101 busy(false), exit_workqueue(false)
105 pthread_cond_init(&wake_cond, NULL);
106 pthread_cond_init(&complete_cond, NULL);
107 pthread_mutex_init(&lock, NULL);
108 err = pthread_create(&handle, NULL, WorkQueue__entry_thunk, this);
112 WorkQueue::~WorkQueue(void)
114 pthread_join(handle, NULL);