00001 #ifndef _sys_apr_Thread_h
00002 #define _sys_apr_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 "APRPool.h"
00026 #include "APRBase.h"
00027
00028 #include <apr_thread_proc.h>
00029 #include <apr_portable.h>
00030
00031 namespace qpid {
00032 namespace sys {
00033
00034 class Runnable;
00035
00036 class Thread
00037 {
00038 public:
00039 inline static Thread current();
00040
00044 inline static long logId();
00045
00046 inline static void yield();
00047
00048 inline Thread();
00049 inline explicit Thread(qpid::sys::Runnable*);
00050 inline explicit Thread(qpid::sys::Runnable&);
00051
00052 inline void join();
00053
00054 inline long id();
00055
00056 private:
00057 static void* APR_THREAD_FUNC runRunnable(apr_thread_t* thread, void *data);
00058 inline Thread(apr_thread_t* t);
00059 apr_thread_t* thread;
00060 };
00061
00062 Thread::Thread() : thread(0) {}
00063
00064 Thread::Thread(Runnable* runnable) {
00065 CHECK_APR_SUCCESS(
00066 apr_thread_create(&thread, 0, runRunnable, runnable, APRPool::get()));
00067 }
00068
00069 Thread::Thread(Runnable& runnable) {
00070 CHECK_APR_SUCCESS(
00071 apr_thread_create(&thread, 0, runRunnable, &runnable, APRPool::get()));
00072 }
00073
00074 void Thread::join(){
00075 apr_status_t status;
00076 if (thread != 0)
00077 CHECK_APR_SUCCESS(apr_thread_join(&status, thread));
00078 }
00079
00080 long Thread::id() {
00081 return long(thread);
00082 }
00083
00087 long Thread::logId() {
00088 return static_cast<long>(apr_os_thread_current());
00089 }
00090
00091 Thread::Thread(apr_thread_t* t) : thread(t) {}
00092
00093 Thread Thread::current(){
00094 apr_thread_t* thr;
00095 apr_os_thread_t osthr = apr_os_thread_current();
00096 CHECK_APR_SUCCESS(apr_os_thread_put(&thr, &osthr, APRPool::get()));
00097 return Thread(thr);
00098 }
00099
00100 void Thread::yield()
00101 {
00102 apr_thread_yield();
00103 }
00104
00105 }}
00106 #endif