]> git.cworth.org Git - apitrace/blobdiff - common/workqueue_posix.cpp
Implement and use os::thread.
[apitrace] / common / workqueue_posix.cpp
index dbcb82e5bf02d43e143daf489b08c4ee3ac898e8..7df7e54e1d4e3bbc845c555b89cc82c60cbc0fe2 100644 (file)
@@ -1,4 +1,3 @@
-#include <pthread.h>
 #include <queue>
 #include <assert.h>
 
@@ -12,13 +11,13 @@ namespace os
  */
 int WorkQueue::run_tasks(void)
 {
-    pthread_mutex_lock(&lock);
+    os::unique_lock<os::mutex> lock(mutex);
 
-    while (work_queue.empty() && !exit_workqueue)
-        pthread_cond_wait(&wake_cond, &lock);
+    while (work_queue.empty() && !exit_workqueue) {
+        wake_cond.wait(lock);
+    }
 
     if (exit_workqueue) {
-        pthread_mutex_unlock(&lock);
         return -1;
     }
 
@@ -26,7 +25,7 @@ int WorkQueue::run_tasks(void)
     std::swap(work_queue, batch);
     busy = true;
 
-    pthread_mutex_unlock(&lock);
+    lock.unlock();
 
     assert(!batch.empty());
     while (!batch.empty()) {
@@ -38,12 +37,10 @@ int WorkQueue::run_tasks(void)
         delete task;
     }
 
-    pthread_mutex_lock(&lock);
+    lock.lock();
 
     busy = false;
-    pthread_cond_signal(&complete_cond);
-
-    pthread_mutex_unlock(&lock);
+    complete_cond.signal();
 
     return 0;
 }
@@ -51,23 +48,23 @@ int WorkQueue::run_tasks(void)
 /* 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)
 {
-    pthread_mutex_lock(&lock);
+    mutex.lock();
     work_queue.push(task);
     wake_up_thread();
-    pthread_mutex_unlock(&lock);
+    mutex.unlock();
 }
 
 void WorkQueue::flush(void)
 {
-    pthread_mutex_lock(&lock);
-    while (!work_queue.empty() || busy)
-        pthread_cond_wait(&complete_cond, &lock);
-    pthread_mutex_unlock(&lock);
+    os::unique_lock<os::mutex> lock(mutex);
+    while (!work_queue.empty() || busy) {
+        complete_cond.wait(lock);
+    }
 }
 
 void WorkQueue::thread_entry(void)
@@ -81,13 +78,12 @@ void WorkQueue::thread_entry(void)
 
 void WorkQueue::destroy(void)
 {
-    pthread_mutex_lock(&lock);
+    mutex.lock();
     exit_workqueue = true;
     wake_up_thread();
-    pthread_mutex_unlock(&lock);
+    mutex.unlock();
 }
 
-extern "C"
 void *WorkQueue__entry_thunk(void *data)
 {
     WorkQueue *thread = static_cast<WorkQueue *>(data);
@@ -98,20 +94,15 @@ void *WorkQueue__entry_thunk(void *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);
-    pthread_mutex_init(&lock, NULL);
-    err = pthread_create(&handle, NULL, WorkQueue__entry_thunk, this);
-    assert(!err);
 }
 
 WorkQueue::~WorkQueue(void)
 {
-    pthread_join(handle, NULL);
+    thread.join();
 }
 
 }