Main Page | Class Hierarchy | Class List | File List | Class Members

RIPCThread.h

00001 #ifndef __RIPC_THREAD_H__
00002 #define __RIPC_THREAD_H__
00003 
00004 #include "RIPCdef.h"
00005 
00006 #ifdef _WIN32
00007 
00008 #include <windows.h>
00009 
00010 #define thread_proc WINAPI
00011 
00012 class RIPC_DLL_ENTRY RIPCThread {
00013     HANDLE h;
00014   public:
00015     enum ThreadPriority { 
00016         THR_PRI_LOW, 
00017         THR_PRI_HIGH
00018     };
00019 
00020     void setPriority(ThreadPriority pri) { 
00021         SetThreadPriority(h, pri == THR_PRI_LOW ? THREAD_PRIORITY_IDLE : THREAD_PRIORITY_HIGHEST);
00022     }
00023 
00024     typedef void (thread_proc* thread_proc_t)(void*);
00025 
00026     void start(thread_proc_t f, void* arg) {
00027         DWORD threadid;
00028         h = CreateThread(NULL, NULL, LPTHREAD_START_ROUTINE(f), arg,
00029                          0, &threadid);
00030     }
00031     void join() {
00032         WaitForSingleObject(h, INFINITE);
00033         CloseHandle(h);
00034         h = NULL;
00035     }
00036     void detach() {
00037         if (h != NULL) {
00038             CloseHandle(h);
00039             h = NULL;
00040         }
00041     }
00042 
00043     RIPCThread() {
00044         h = NULL;
00045     }
00046 
00047     ~RIPCThread() {
00048         if (h != NULL) {
00049             CloseHandle(h);
00050         }
00051     }
00052 };
00053 
00054 class RIPCCurrentThread { 
00055   public:
00056     static void  setThreadData(void* obj) {
00057         TlsSetValue(tlsIndex, obj);
00058     }
00059     static void* getThreadData() { 
00060         return TlsGetValue(tlsIndex);
00061     }
00062     RIPCCurrentThread() { 
00063         tlsIndex = TlsAlloc();
00064     }
00065 
00066     ~RIPCCurrentThread() { 
00067         TlsFree(tlsIndex);
00068     }    
00069   private:
00070     static int tlsIndex;    
00071 };
00072 
00073 #else
00074 
00075 #define thread_proc
00076 
00077 #include <unistd.h>
00078 #include <sys/time.h>
00079 #include <pthread.h>
00080 
00081 const size_t RIPCThreadStackSize = 1024*1024;
00082 
00083 class RIPCThread { 
00084     pthread_t thread;
00085   public:
00086     typedef void (thread_proc* thread_proc_t)(void*);
00087 
00088     void start(thread_proc_t f, void* arg) {
00089         pthread_attr_t attr;
00090         pthread_attr_init(&attr);
00091 #if !defined(__linux__)
00092         pthread_attr_setstacksize(&attr, RIPCThreadStackSize);
00093 #endif
00094 #if defined(_AIX41)
00095         // At AIX 4.1, 4.2 threads are by default created detached
00096         pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_UNDETACHED);
00097 #endif
00098         pthread_create(&thread, &attr, (void*(*)(void*))f, arg);
00099         pthread_attr_destroy(&attr);
00100     }
00101 
00102     enum ThreadPriority { 
00103         THR_PRI_LOW, 
00104         THR_PRI_HIGH
00105     };
00106     void setPriority(ThreadPriority pri) { 
00107 #if defined(PRI_OTHER_MIN) && defined(PRI_OTHER_MAX)
00108         struct sched_param sp;
00109         sp.sched_priority = pri == THR_PRI_LOW ? IPRI_OTHER_MIN : PRI_OTHER_MAX;
00110         pthread_setschedparam(thread, SCHED_OTHER, &sp); 
00111 #endif
00112     }
00113 
00114     void join() {
00115         void* result;
00116         pthread_join(thread, &result);
00117     }
00118 
00119     void detach() {
00120         pthread_detach(thread);
00121     }
00122 };
00123 
00124 
00125 class RIPCCurrentThread { 
00126   public:
00127     static void  setThreadData(void* obj) {
00128         pthread_setspecific(key, obj);
00129     }
00130     static void* getThreadData() { 
00131         return pthread_getspecific(key);    
00132     }
00133     RIPCCurrentThread() { 
00134         pthread_key_create(&key, NULL);
00135     }
00136 
00137     ~RIPCCurrentThread() { 
00138         pthread_key_delete(key);
00139     }    
00140   private:
00141     static pthread_key_t key;
00142 };
00143 
00144 #endif
00145 
00146 #endif
00147 
00148 
00149 
00150 
00151 
00152 
00153 
00154 
00155 
00156 
00157 
00158 
00159 
00160 
00161 

Generated on Wed Nov 26 14:58:18 2003 for RIPC by doxygen 1.3.5