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
00073
00074
00075
00076 forceinline
00077 Event::Event(void) : p_s(false) {
00078 if (pthread_mutex_init(&p_m,NULL) != 0)
00079 throw OperatingSystemError("Event::Event[pthread_mutex_init]");
00080 if (pthread_cond_init(&p_c,NULL) != 0)
00081 throw OperatingSystemError("Event::Event[pthread_cond_init]");
00082 }
00083 forceinline void
00084 Event::signal(void) {
00085 if (pthread_mutex_lock(&p_m) != 0)
00086 throw OperatingSystemError("Event::signal[pthread_mutex_lock]");
00087 if (!p_s) {
00088 p_s = true;
00089 if (pthread_cond_signal(&p_c) != 0)
00090 throw OperatingSystemError("Event::signal[pthread_cond_signal]");
00091 }
00092 if (pthread_mutex_unlock(&p_m) != 0)
00093 throw OperatingSystemError("Event::signal[pthread_mutex_unlock]");
00094 }
00095 forceinline void
00096 Event::wait(void) {
00097 if (pthread_mutex_lock(&p_m) != 0)
00098 throw OperatingSystemError("Event::wait[pthread_mutex_lock]");
00099 while (!p_s)
00100 if (pthread_cond_wait(&p_c,&p_m) != 0)
00101 throw OperatingSystemError("Event::wait[pthread_cond_wait]");
00102 p_s = false;
00103 if (pthread_mutex_unlock(&p_m) != 0)
00104 throw OperatingSystemError("Event::wait[pthread_mutex_unlock]");
00105 }
00106 forceinline
00107 Event::~Event(void) {
00108 if (pthread_cond_destroy(&p_c) != 0)
00109 throw OperatingSystemError("Event::~Event[pthread_cond_destroy]");
00110 if (pthread_mutex_destroy(&p_m) != 0)
00111 throw OperatingSystemError("Event::~Event[pthread_mutex_destroy]");
00112 }
00113
00114
00115
00116
00117
00118 forceinline void
00119 Thread::sleep(unsigned int ms) {
00120 #ifdef GECODE_HAS_UNISTD_H
00121 unsigned int s = ms / 1000;
00122 ms -= 1000 * s;
00123 if (s > 0) {
00124
00125 ::sleep(s);
00126 }
00127 usleep(ms * 1000);
00128 #endif
00129 }
00130 forceinline unsigned int
00131 Thread::npu(void) {
00132 #ifdef GECODE_HAS_UNISTD_H
00133 int n=static_cast<int>(sysconf(_SC_NPROCESSORS_ONLN));
00134 return (n>1) ? n : 1;
00135 #else
00136 return 1;
00137 #endif
00138 }
00139
00140 }}
00141
00142