thread.hpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041 #include <cstddef>
00042
00043 #ifdef GECODE_THREADS_WINDOWS
00044
00045 #ifndef NOMINMAX
00046 # define NOMINMAX
00047 #endif
00048
00049 #ifndef _WIN32_WINNT
00050 # define _WIN32_WINNT 0x400
00051 #endif
00052
00053 #ifndef WIN32_LEAN_AND_MEAN
00054 # define WIN32_LEAN_AND_MEAN
00055 #endif
00056
00057 #include <windows.h>
00058
00059 #endif
00060
00061 #ifdef GECODE_THREADS_PTHREADS
00062
00063 #include <pthread.h>
00064
00065 #ifdef GECODE_THREADS_OSX
00066
00067 #include <libkern/OSAtomic.h>
00068
00069 #endif
00070
00071 #endif
00072
00088 namespace Gecode { namespace Support {
00089
00099 class Mutex {
00100 private:
00101 #ifdef GECODE_THREADS_WINDOWS
00102
00103 CRITICAL_SECTION w_cs;
00104 #endif
00105 #ifdef GECODE_THREADS_PTHREADS
00106
00107 pthread_mutex_t p_m;
00108 #endif
00109 public:
00111 Mutex(void);
00113 void acquire(void);
00115 bool tryacquire(void);
00117 void release(void);
00119 ~Mutex(void);
00121 static void* operator new(size_t s);
00123 static void operator delete(void* p);
00124 private:
00126 Mutex(const Mutex&) {}
00128 void operator=(const Mutex&) {}
00129 };
00130
00131 #if defined(GECODE_THREADS_WINDOWS) || !defined(GECODE_THREADS_PTHREADS)
00132
00133 typedef Mutex FastMutex;
00134
00135 #endif
00136
00137 #ifdef GECODE_THREADS_PTHREADS
00138
00139 #if defined(GECODE_THREADS_OSX) || defined(GECODE_THREADS_PTHREADS_SPINLOCK)
00140
00155 class FastMutex {
00156 private:
00157 #ifdef GECODE_THREADS_OSX
00158
00159 OSSpinLock lck;
00160 #else
00161
00162 pthread_spinlock_t p_s;
00163 #endif
00164 public:
00166 FastMutex(void);
00168 void acquire(void);
00170 bool tryacquire(void);
00172 void release(void);
00174 ~FastMutex(void);
00176 static void* operator new(size_t s);
00178 static void operator delete(void* p);
00179 private:
00181 FastMutex(const FastMutex&) {}
00183 void operator=(const FastMutex&) {}
00184 };
00185
00186 #else
00187
00188 typedef Mutex FastMutex;
00189
00190 #endif
00191
00192 #endif
00193
00199 class Lock {
00200 private:
00202 Mutex& m;
00203 public:
00205 Lock(Mutex& m0);
00207 ~Lock(void);
00208 private:
00210 Lock(const Lock& l) : m(l.m) {}
00212 void operator=(const Lock&) {}
00213 };
00214
00223 class Event {
00224 private:
00225 #ifdef GECODE_THREADS_WINDOWS
00226
00227 HANDLE w_h;
00228 #endif
00229 #ifdef GECODE_THREADS_PTHREADS
00230
00231 pthread_mutex_t p_m;
00233 pthread_cond_t p_c;
00235 bool p_s;
00236 #endif
00237 public:
00239 Event(void);
00241 void signal(void);
00243 void wait(void);
00245 ~Event(void);
00246 private:
00248 Event(const Event&) {}
00250 void operator=(const Event&) {}
00251 };
00252
00258 class Runnable {
00259 private:
00261 bool d;
00262 public:
00264 Runnable(bool d=true);
00266 void todelete(bool d);
00268 bool todelete(void) const;
00270 virtual void run(void) = 0;
00272 virtual ~Runnable(void) {}
00274 static void* operator new(size_t s);
00276 static void operator delete(void* p);
00277 };
00278
00288 class Thread {
00289 public:
00291 class Run {
00292 public:
00294 Run* n;
00296 Runnable* r;
00298 Event e;
00300 Mutex m;
00302 GECODE_SUPPORT_EXPORT Run(Runnable* r);
00304 GECODE_SUPPORT_EXPORT void exec(void);
00306 void run(Runnable* r);
00308 static void* operator new(size_t s);
00310 static void operator delete(void* p);
00311 };
00313 GECODE_SUPPORT_EXPORT static Mutex* m(void);
00315 GECODE_SUPPORT_EXPORT static Run* idle;
00316 public:
00326 static void run(Runnable* r);
00328 static void sleep(unsigned int ms);
00330 static unsigned int npu(void);
00331 private:
00333 Thread(const Thread&) {}
00335 void operator=(const Thread&) {}
00336 };
00337
00338 }}
00339
00340