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

00001 #ifndef _sys_posix_Thread_h
00002 #define _sys_posix_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 "check.h"
00026 #include <pthread.h>
00027 
00028 namespace qpid {
00029 namespace sys {
00030 
00031 class Runnable;
00032 
00033 class Thread
00034 {
00035   public:
00036     inline static Thread current();
00037 
00041     static unsigned long logId() { return current().id(); }
00042 
00043     inline static void yield();
00044 
00045     inline Thread();
00046     inline explicit Thread(qpid::sys::Runnable*);
00047     inline explicit Thread(qpid::sys::Runnable&);
00048     
00049     inline void join();
00050 
00051     inline unsigned long id();
00052         
00053   private:
00054     static void* runRunnable(void* runnable);
00055     inline Thread(pthread_t);
00056     pthread_t thread;
00057 };
00058 
00059 
00060 Thread::Thread() : thread(0) {}
00061 
00062 Thread::Thread(Runnable* runnable) {
00063     QPID_POSIX_ASSERT_THROW_IF(pthread_create(&thread, NULL, runRunnable, runnable));
00064 }
00065 
00066 Thread::Thread(Runnable& runnable) {
00067     QPID_POSIX_ASSERT_THROW_IF(pthread_create(&thread, NULL, runRunnable, &runnable));
00068 }
00069 
00070 void Thread::join(){
00071     if (thread != 0)
00072         QPID_POSIX_ASSERT_THROW_IF(pthread_join(thread, 0));
00073 }
00074 
00075 unsigned long Thread::id() {
00076     return thread;
00077 }
00078 
00079 Thread::Thread(pthread_t thr) : thread(thr) {}
00080 
00081 Thread Thread::current() {
00082     return Thread(pthread_self());
00083 }
00084 
00085 void Thread::yield() 
00086 {
00087     QPID_POSIX_ASSERT_THROW_IF(pthread_yield());
00088 }
00089 
00090 
00091 }}
00092 #endif  

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