/usr/share/cruisecontrol-bin-2.6.1/projects/qpid-trunk/cpp/src/qpid/sys/apr/Thread.h

00001 #ifndef _sys_apr_Thread_h
00002 #define _sys_apr_Thread_h
00003 
00004 /*
00005  *
00006  * Licensed to the Apache Software Foundation (ASF) under one
00007  * or more contributor license agreements.  See the NOTICE file
00008  * distributed with this work for additional information
00009  * regarding copyright ownership.  The ASF licenses this file
00010  * to you under the Apache License, Version 2.0 (the
00011  * "License"); you may not use this file except in compliance
00012  * with the License.  You may obtain a copy of the License at
00013  * 
00014  *   http://www.apache.org/licenses/LICENSE-2.0
00015  * 
00016  * Unless required by applicable law or agreed to in writing,
00017  * software distributed under the License is distributed on an
00018  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
00019  * KIND, either express or implied.  See the License for the
00020  * specific language governing permissions and limitations
00021  * under the License.
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  

Generated on Thu Apr 10 11:08:18 2008 for Qpid by  doxygen 1.4.7