]> git.cworth.org Git - apitrace/commitdiff
Add and use os::mutex.
authorJosé Fonseca <jose.r.fonseca@gmail.com>
Mon, 22 Oct 2012 18:14:27 +0000 (19:14 +0100)
committerJosé Fonseca <jose.r.fonseca@gmail.com>
Mon, 22 Oct 2012 18:14:27 +0000 (19:14 +0100)
common/os_thread.hpp
common/os_workqueue.hpp
common/workqueue_posix.cpp
common/workqueue_win32.cpp

index fe7faaa004ae98fc318a07da9ea3aa174ee9fbec..c4bc130d4693b7d9bb201da8675543acdcd4659d 100644 (file)
 namespace os {
 
 
-    class recursive_mutex
+    /**
+     * Base class for mutex and recursive_mutex.
+     */
+    class _base_mutex
     {
     public:
 #ifdef _WIN32
@@ -51,7 +54,7 @@ namespace os {
         typedef pthread_mutex_t native_handle_type;
 #endif
 
-        recursive_mutex(void) {
+        _base_mutex(void) {
 #ifdef _WIN32
             InitializeCriticalSection(&_native_handle);
 #else
@@ -63,7 +66,7 @@ namespace os {
 #endif
         }
 
-        ~recursive_mutex() {
+        ~_base_mutex() {
 #ifdef _WIN32
             DeleteCriticalSection(&_native_handle);
 #else
@@ -89,11 +92,56 @@ namespace os {
 #endif
         }
 
-    private:
+        native_handle_type & native_handle() {
+            return _native_handle;
+        }
+
+    protected:
         native_handle_type _native_handle;
     };
 
 
+    /**
+     * Same interface as std::mutex.
+     */
+    class mutex : public _base_mutex
+    {
+    public:
+        inline
+        mutex(void) {
+#ifdef _WIN32
+            InitializeCriticalSection(&_native_handle);
+#else
+            pthread_mutex_init(&_native_handle, NULL);
+#endif
+        }
+    };
+
+
+    /**
+     * Same interface as std::recursive_mutex.
+     */
+    class recursive_mutex : public _base_mutex
+    {
+    public:
+        inline
+        recursive_mutex(void) {
+#ifdef _WIN32
+            InitializeCriticalSection(&_native_handle);
+#else
+            pthread_mutexattr_t attr;
+            pthread_mutexattr_init(&attr);
+            pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
+            pthread_mutex_init(&_native_handle, &attr);
+            pthread_mutexattr_destroy(&attr);
+#endif
+        }
+    };
+
+
+    /**
+     * Same interface as boost::thread_specific_ptr.
+     */
     template <typename T>
     class thread_specific_ptr
     {
@@ -158,6 +206,7 @@ namespace os {
         }
     };
 
+
 } /* namespace os */
 
 #endif /* _OS_THREAD_HPP_ */
index e6b77d4ca408c0c262589d5c9f025084e2cc6af8..e41d8b27db3ef547ceb14502ddd9ecb0c4dac33d 100644 (file)
@@ -3,8 +3,10 @@
 
 #include <queue>
 
-namespace os
-{
+#include "os_thread.hpp"
+
+namespace os {
+
 
 class WorkQueue;
 
@@ -29,7 +31,7 @@ class WorkQueue {
        pthread_cond_t wake_cond;
        pthread_cond_t complete_cond;
 
-       pthread_mutex_t lock;
+       os::mutex lock;
 
        void wake_up_thread(void);
        void thread_entry(void);
index dbcb82e5bf02d43e143daf489b08c4ee3ac898e8..9b02d6c0c2fc5683638280c99ac43182ae57ea73 100644 (file)
@@ -12,13 +12,13 @@ namespace os
  */
 int WorkQueue::run_tasks(void)
 {
-    pthread_mutex_lock(&lock);
+    lock.lock();
 
     while (work_queue.empty() && !exit_workqueue)
-        pthread_cond_wait(&wake_cond, &lock);
+        pthread_cond_wait(&wake_cond, &lock.native_handle());
 
     if (exit_workqueue) {
-        pthread_mutex_unlock(&lock);
+        lock.unlock();
         return -1;
     }
 
@@ -26,7 +26,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 +38,12 @@ int WorkQueue::run_tasks(void)
         delete task;
     }
 
-    pthread_mutex_lock(&lock);
+    lock.lock();
 
     busy = false;
     pthread_cond_signal(&complete_cond);
 
-    pthread_mutex_unlock(&lock);
+    lock.unlock();
 
     return 0;
 }
@@ -56,18 +56,18 @@ void WorkQueue::wake_up_thread(void)
 
 void WorkQueue::queue_work(WorkQueueWork *task)
 {
-    pthread_mutex_lock(&lock);
+    lock.lock();
     work_queue.push(task);
     wake_up_thread();
-    pthread_mutex_unlock(&lock);
+    lock.unlock();
 }
 
 void WorkQueue::flush(void)
 {
-    pthread_mutex_lock(&lock);
+    lock.lock();
     while (!work_queue.empty() || busy)
-        pthread_cond_wait(&complete_cond, &lock);
-    pthread_mutex_unlock(&lock);
+        pthread_cond_wait(&complete_cond, &lock.native_handle());
+    lock.unlock();
 }
 
 void WorkQueue::thread_entry(void)
@@ -81,10 +81,10 @@ void WorkQueue::thread_entry(void)
 
 void WorkQueue::destroy(void)
 {
-    pthread_mutex_lock(&lock);
+    lock.lock();
     exit_workqueue = true;
     wake_up_thread();
-    pthread_mutex_unlock(&lock);
+    lock.unlock();
 }
 
 extern "C"
@@ -104,7 +104,6 @@ WorkQueue::WorkQueue(void) :
 
     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);
 }
index cec66936005f2f685c811fd47f9607ddb3f965fd..3c1b9cd4fa37c8a5b8e9ce9a800785f05010fdeb 100644 (file)
@@ -1,4 +1,3 @@
-#include <pthread.h>
 #include <queue>
 #include <assert.h>