From: José Fonseca Date: Tue, 23 Oct 2012 18:28:31 +0000 (+0100) Subject: Implement and use os::thread. X-Git-Url: https://git.cworth.org/git?p=apitrace;a=commitdiff_plain;h=23354536051650b7a8a713e824946d48a7734534 Implement and use os::thread. --- diff --git a/common/os_thread.hpp b/common/os_thread.hpp index e5eb19d..9d6a989 100644 --- a/common/os_thread.hpp +++ b/common/os_thread.hpp @@ -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_ */ diff --git a/common/os_workqueue.hpp b/common/os_workqueue.hpp index 1c6798c..f8ab84c 100644 --- a/common/os_workqueue.hpp +++ b/common/os_workqueue.hpp @@ -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 work_queue; + std::queue 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(); }; } diff --git a/common/workqueue_posix.cpp b/common/workqueue_posix.cpp index f46bbd9..7df7e54 100644 --- a/common/workqueue_posix.cpp +++ b/common/workqueue_posix.cpp @@ -1,4 +1,3 @@ -#include #include #include @@ -85,7 +84,6 @@ void WorkQueue::destroy(void) mutex.unlock(); } -extern "C" void *WorkQueue__entry_thunk(void *data) { WorkQueue *thread = static_cast(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(); } }