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 #ifdef GECODE_THREADS_WINDOWS
00042
00043 #ifndef NOMINMAX
00044 # define NOMINMAX
00045 #endif
00046
00047 #ifndef _WIN32_WINNT
00048 # define _WIN32_WINNT 0x400
00049 #endif
00050
00051 #ifndef WIN32_LEAN_AND_MEAN
00052 # define WIN32_LEAN_AND_MEAN
00053 #endif
00054
00055 #include <windows.h>
00056
00057 #endif
00058
00059 #ifdef GECODE_THREADS_PTHREADS
00060
00061 #include <pthread.h>
00062
00063 #endif
00064
00080 namespace Gecode { namespace Support {
00081
00091 class Mutex {
00092 private:
00093 #ifdef GECODE_THREADS_WINDOWS
00094
00095 CRITICAL_SECTION w_cs;
00096 #endif
00097 #ifdef GECODE_THREADS_PTHREADS
00098
00099 pthread_mutex_t p_m;
00100 #endif
00101 public:
00103 Mutex(void);
00105 void acquire(void);
00107 bool tryacquire(void);
00109 void release(void);
00111 ~Mutex(void);
00113 static void* operator new(size_t s);
00115 static void operator delete(void* p);
00116 private:
00118 Mutex(const Mutex&) {}
00120 void operator=(const Mutex&) {}
00121 };
00122
00128 class Lock {
00129 private:
00131 Mutex& m;
00132 public:
00134 Lock(Mutex& m0);
00136 ~Lock(void);
00137 private:
00139 Lock(const Lock& l) : m(l.m) {}
00141 void operator=(const Lock&) {}
00142 };
00143
00152 class Event {
00153 private:
00154 #ifdef GECODE_THREADS_WINDOWS
00155
00156 HANDLE w_h;
00157 #endif
00158 #ifdef GECODE_THREADS_PTHREADS
00159
00160 pthread_mutex_t p_m;
00162 pthread_cond_t p_c;
00164 bool p_s;
00165 #endif
00166 public:
00168 Event(void);
00170 void signal(void);
00172 void wait(void);
00174 ~Event(void);
00175 private:
00177 Event(const Event&) {}
00179 void operator=(const Event&) {}
00180 };
00181
00187 class Runnable {
00188 public:
00190 virtual void run(void) = 0;
00192 virtual ~Runnable(void) {}
00194 static void* operator new(size_t s);
00196 static void operator delete(void* p);
00197 };
00198
00208 class Thread {
00209 public:
00211 class Run {
00212 public:
00214 Run* n;
00216 Runnable* r;
00218 Event e;
00220 Mutex m;
00222 GECODE_SUPPORT_EXPORT Run(Runnable* r);
00224 GECODE_SUPPORT_EXPORT void exec(void);
00226 void run(Runnable* r);
00228 static void* operator new(size_t s);
00230 static void operator delete(void* p);
00231 };
00233 GECODE_SUPPORT_EXPORT static Mutex m;
00235 GECODE_SUPPORT_EXPORT static Run* idle;
00236 public:
00246 static void run(Runnable* r);
00248 static void sleep(unsigned int ms);
00250 static unsigned int npu(void);
00251 private:
00253 Thread(const Thread&) {}
00255 void operator=(const Thread&) {}
00256 };
00257
00258 }}
00259
00260 #ifdef GECODE_THREADS_WINDOWS
00261 #include <gecode/support/thread/windows.hpp>
00262 #endif
00263 #ifdef GECODE_THREADS_PTHREADS
00264 #include <gecode/support/thread/pthreads.hpp>
00265 #endif
00266 #ifndef GECODE_HAS_THREADS
00267 #include <gecode/support/thread/none.hpp>
00268 #endif
00269
00270 #include <gecode/support/thread/thread.hpp>
00271
00272