]> git.cworth.org Git - apitrace/commitdiff
Implement and use os::thread.
authorJosé Fonseca <jose.r.fonseca@gmail.com>
Tue, 23 Oct 2012 18:28:31 +0000 (19:28 +0100)
committerJosé Fonseca <jose.r.fonseca@gmail.com>
Tue, 23 Oct 2012 18:28:31 +0000 (19:28 +0100)
common/os_thread.hpp
common/os_workqueue.hpp
common/workqueue_posix.cpp

index e5eb19d3604c8dd61c47e5ccd22c4b57f1f72e28..9d6a9891524564803d844bfe923de13dfd8442c0 100644 (file)
@@ -1,6 +1,6 @@
 /**************************************************************************
  *
- * Copyright 2011 Jose Fonseca
+ * Copyright 2011-2012 Jose Fonseca
  * All Rights Reserved.
  *
  * Permission is hereby granted, free of charge, to any person obtaining a copy
@@ -299,6 +299,39 @@ namespace os {
     };
 
 
+    /**
+     * Same interface as std::thread
+     */
+    class thread {
+    public:
+#ifdef _WIN32
+        /* FIXME */
+#else
+        typedef pthread_t native_handle_type;
+#endif
+
+        template< class Function, class Arg >
+        explicit thread( Function& f, Arg & arg ) {
+#ifdef _WIN32
+            /* FIXME */
+#else
+            pthread_create(&_native_handle, NULL, f, arg);
+#endif
+        }
+
+        inline void
+        join() {
+#ifdef _WIN32
+            /* FIXME */
+#else
+            pthread_join(_native_handle, NULL);
+#endif
+        }
+
+    private:
+        native_handle_type _native_handle;
+    };
+
 } /* namespace os */
 
 #endif /* _OS_THREAD_HPP_ */
index 1c6798c1ac3f08ce2ee7ca11a5517e30f427230b..f8ab84c66cb3437a78e32883d435b62e31401298 100644 (file)
@@ -12,38 +12,39 @@ class WorkQueue;
 
 class WorkQueueWork {
 protected:
-       friend class WorkQueue;
+    friend class WorkQueue;
 
 public:
-       virtual void run(void) = 0;
-       virtual ~WorkQueueWork(void) { }
+    virtual void run(void) = 0;
+    virtual ~WorkQueueWork(void) { }
 };
 
 extern "C"
 void *WorkQueue__entry_thunk(void *data);
 
 class WorkQueue {
-       pthread_t handle;
-       std::queue<WorkQueueWork *> work_queue;
+    std::queue<WorkQueueWork *> work_queue;
 
-       bool busy;
-       bool exit_workqueue;
-       os::condition_variable wake_cond;
-       os::condition_variable complete_cond;
+    bool busy;
+    bool exit_workqueue;
+    os::condition_variable wake_cond;
+    os::condition_variable complete_cond;
 
-       os::mutex mutex;
+    os::mutex mutex;
 
-       void wake_up_thread(void);
-       void thread_entry(void);
-       int run_tasks(void);
-       friend void *WorkQueue__entry_thunk(void *data);
+    os::thread thread;
+
+    void wake_up_thread(void);
+    void thread_entry(void);
+    int run_tasks(void);
+    friend void *WorkQueue__entry_thunk(void *data);
 public:
-       void queue_work(WorkQueueWork *work);
-       void flush(void);
-       void destroy(void);
+    void queue_work(WorkQueueWork *work);
+    void flush(void);
+    void destroy(void);
 
-       WorkQueue(void);
-       ~WorkQueue();
+    WorkQueue(void);
+    ~WorkQueue();
 };
 
 }
index f46bbd977bd20ee87e5e273539df8512a38ac41d..7df7e54e1d4e3bbc845c555b89cc82c60cbc0fe2 100644 (file)
@@ -1,4 +1,3 @@
-#include <pthread.h>
 #include <queue>
 #include <assert.h>
 
@@ -85,7 +84,6 @@ void WorkQueue::destroy(void)
     mutex.unlock();
 }
 
-extern "C"
 void *WorkQueue__entry_thunk(void *data)
 {
     WorkQueue *thread = static_cast<WorkQueue *>(data);
@@ -96,17 +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;
-
-    err = pthread_create(&handle, NULL, WorkQueue__entry_thunk, this);
-    assert(!err);
 }
 
 WorkQueue::~WorkQueue(void)
 {
-    pthread_join(handle, NULL);
+    thread.join();
 }
 
 }