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