00001 #ifndef _sys_Thread_h
00002 #define _sys_Thread_h
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025 #include <sys/Runnable.h>
00026
00027 #ifdef USE_APR
00028 # include <apr_thread_proc.h>
00029 # include <apr_portable.h>
00030 # include <apr/APRPool.h>
00031 # include <apr/APRBase.h>
00032 #else
00033 # include <posix/check.h>
00034 # include <pthread.h>
00035 #endif
00036
00037 namespace qpid {
00038 namespace sys {
00039
00040 class Thread
00041 {
00042 public:
00043 inline static Thread current();
00044 inline static void yield();
00045
00046 inline Thread();
00047 inline Thread(qpid::sys::Runnable*);
00048 inline Thread(qpid::sys::Runnable&);
00049 ~Thread();
00050 inline void join();
00051 inline long id();
00052
00053 private:
00054 #ifdef USE_APR
00055 static void* APR_THREAD_FUNC runRunnable(apr_thread_t* thread, void *data);
00056 inline Thread(apr_thread_t* t);
00057 apr_thread_t* thread;
00058 #else
00059 static void* runRunnable(void* runnable);
00060 inline Thread(pthread_t);
00061 pthread_t thread;
00062 #endif
00063 };
00064
00065
00066 Thread::Thread() : thread(0) {}
00067
00068
00069 #ifdef USE_APR
00070
00071 Thread::Thread(Runnable* runnable) {
00072 apr_pool_t* tmp_pool = APRPool::get();
00073 CHECK_APR_SUCCESS(
00074 apr_thread_create(&thread, 0, runRunnable, runnable, tmp_pool));
00075 APRPool::free(tmp_pool);
00076 }
00077
00078 Thread::Thread(Runnable& runnable) {
00079 apr_pool_t* tmp_pool = APRPool::get();
00080 CHECK_APR_SUCCESS(
00081 apr_thread_create(&thread, 0, runRunnable, &runnable, tmp_pool));
00082 APRPool::free(tmp_pool);
00083 }
00084
00085 void Thread::join(){
00086 apr_status_t status;
00087 if (thread != 0)
00088 CHECK_APR_SUCCESS(apr_thread_join(&status, thread));
00089 }
00090
00091 long Thread::id() {
00092 return long(thread);
00093 }
00094
00095 Thread::Thread(apr_thread_t* t) : thread(t) {}
00096
00097 Thread Thread::current(){
00098 apr_pool_t* tmp_pool = APRPool::get();
00099 apr_thread_t* thr;
00100 apr_os_thread_t osthr = apr_os_thread_current();
00101 CHECK_APR_SUCCESS(apr_os_thread_put(&thr, &osthr, tmp_pool));
00102 APRPool::free(tmp_pool);
00103 return Thread(thr);
00104 }
00105
00106 void Thread::yield()
00107 {
00108 apr_thread_yield();
00109 }
00110
00111
00112
00113 #else
00114
00115 Thread::Thread(Runnable* runnable) {
00116 QPID_POSIX_THROW_IF(pthread_create(&thread, NULL, runRunnable, runnable));
00117 }
00118
00119 Thread::Thread(Runnable& runnable) {
00120 QPID_POSIX_THROW_IF(pthread_create(&thread, NULL, runRunnable, &runnable));
00121 }
00122
00123 void Thread::join(){
00124 QPID_POSIX_THROW_IF(pthread_join(thread, 0));
00125 }
00126
00127 long Thread::id() {
00128 return long(thread);
00129 }
00130
00131 Thread::~Thread() {
00132 }
00133
00134 Thread::Thread(pthread_t thr) : thread(thr) {}
00135
00136 Thread Thread::current() {
00137 return Thread(pthread_self());
00138 }
00139
00140 void Thread::yield()
00141 {
00142 QPID_POSIX_THROW_IF(pthread_yield());
00143 }
00144
00145
00146 #endif
00147
00148 }}
00149
00150 #endif