pthreads.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 #ifdef GECODE_HAS_UNISTD_H
00039 #include <unistd.h>
00040 #endif
00041
00042 namespace Gecode { namespace Support {
00043
00044
00045
00046
00047 forceinline
00048 Mutex::Mutex(void) {
00049 if (pthread_mutex_init(&p_m,NULL) != 0)
00050 throw OperatingSystemError("Mutex::Mutex[pthread_mutex_init]");
00051 }
00052 forceinline void
00053 Mutex::acquire(void) {
00054 if (pthread_mutex_lock(&p_m) != 0)
00055 throw OperatingSystemError("Mutex::acquire[pthread_mutex_lock]");
00056 }
00057 forceinline bool
00058 Mutex::tryacquire(void) {
00059 return pthread_mutex_trylock(&p_m) == 0;
00060 }
00061 forceinline void
00062 Mutex::release(void) {
00063 if (pthread_mutex_unlock(&p_m) != 0)
00064 throw OperatingSystemError("Mutex::release[pthread_mutex_unlock]");
00065 }
00066 forceinline
00067 Mutex::~Mutex(void) {
00068 if (pthread_mutex_destroy(&p_m) != 0)
00069 throw OperatingSystemError("Mutex::~Mutex[pthread_mutex_destroy]");
00070 }
00071
00072 #ifdef GECODE_THREADS_OSX
00073
00074
00075
00076
00077 forceinline
00078 FastMutex::FastMutex(void) : lck(OS_SPINLOCK_INIT) {}
00079 forceinline void
00080 FastMutex::acquire(void) {
00081 OSSpinLockLock(&lck);
00082 }
00083 forceinline bool
00084 FastMutex::tryacquire(void) {
00085 return OSSpinLockTry(&lck);
00086 }
00087 forceinline void
00088 FastMutex::release(void) {
00089 OSSpinLockUnlock(&lck);
00090 }
00091 forceinline
00092 FastMutex::~FastMutex(void) {}
00093
00094 #endif
00095
00096 #ifdef GECODE_THREADS_PTHREADS_SPINLOCK
00097
00098
00099
00100
00101 forceinline
00102 FastMutex::FastMutex(void) {
00103 if (pthread_spin_init(&p_s,PTHREAD_PROCESS_PRIVATE) != 0)
00104 throw OperatingSystemError("FastMutex::FastMutex[pthread_spin_init]");
00105 }
00106 forceinline void
00107 FastMutex::acquire(void) {
00108 if (pthread_spin_lock(&p_s) != 0)
00109 throw OperatingSystemError("FastMutex::acquire[pthread_spin_lock]");
00110 }
00111 forceinline bool
00112 FastMutex::tryacquire(void) {
00113 return pthread_spin_trylock(&p_s) == 0;
00114 }
00115 forceinline void
00116 FastMutex::release(void) {
00117 if (pthread_spin_unlock(&p_s) != 0)
00118 throw OperatingSystemError("FastMutex::release[pthread_spin_unlock]");
00119 }
00120 forceinline
00121 FastMutex::~FastMutex(void) {
00122 if (pthread_spin_destroy(&p_s) != 0)
00123 throw OperatingSystemError(
00124 "FastMutex::~FastMutex[pthread_spin_destroy]");
00125 }
00126
00127 #endif
00128
00129
00130
00131
00132 forceinline
00133 Event::Event(void) : p_s(false) {
00134 if (pthread_mutex_init(&p_m,NULL) != 0)
00135 throw OperatingSystemError("Event::Event[pthread_mutex_init]");
00136 if (pthread_cond_init(&p_c,NULL) != 0)
00137 throw OperatingSystemError("Event::Event[pthread_cond_init]");
00138 }
00139 forceinline void
00140 Event::signal(void) {
00141 if (pthread_mutex_lock(&p_m) != 0)
00142 throw OperatingSystemError("Event::signal[pthread_mutex_lock]");
00143 if (!p_s) {
00144 p_s = true;
00145 if (pthread_cond_signal(&p_c) != 0)
00146 throw OperatingSystemError("Event::signal[pthread_cond_signal]");
00147 }
00148 if (pthread_mutex_unlock(&p_m) != 0)
00149 throw OperatingSystemError("Event::signal[pthread_mutex_unlock]");
00150 }
00151 forceinline void
00152 Event::wait(void) {
00153 if (pthread_mutex_lock(&p_m) != 0)
00154 throw OperatingSystemError("Event::wait[pthread_mutex_lock]");
00155 while (!p_s)
00156 if (pthread_cond_wait(&p_c,&p_m) != 0)
00157 throw OperatingSystemError("Event::wait[pthread_cond_wait]");
00158 p_s = false;
00159 if (pthread_mutex_unlock(&p_m) != 0)
00160 throw OperatingSystemError("Event::wait[pthread_mutex_unlock]");
00161 }
00162 forceinline
00163 Event::~Event(void) {
00164 if (pthread_cond_destroy(&p_c) != 0)
00165 throw OperatingSystemError("Event::~Event[pthread_cond_destroy]");
00166 if (pthread_mutex_destroy(&p_m) != 0)
00167 throw OperatingSystemError("Event::~Event[pthread_mutex_destroy]");
00168 }
00169
00170
00171
00172
00173
00174 forceinline void
00175 Thread::sleep(unsigned int ms) {
00176 #ifdef GECODE_HAS_UNISTD_H
00177 unsigned int s = ms / 1000;
00178 ms -= 1000 * s;
00179 if (s > 0) {
00180
00181 ::sleep(s);
00182 }
00183 usleep(ms * 1000);
00184 #endif
00185 }
00186 forceinline unsigned int
00187 Thread::npu(void) {
00188 #ifdef GECODE_HAS_UNISTD_H
00189 int n=static_cast<int>(sysconf(_SC_NPROCESSORS_ONLN));
00190 return (n>1) ? n : 1;
00191 #else
00192 return 1;
00193 #endif
00194 }
00195
00196 }}
00197
00198