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 void
00048 Thread::sleep(unsigned int ms) {
00049 #ifdef GECODE_HAS_UNISTD_H
00050 unsigned int s = ms / 1000;
00051 ms -= 1000 * s;
00052 if (s > 0) {
00053
00054 ::sleep(s);
00055 }
00056 usleep(ms * 1000);
00057 #endif
00058 }
00059 forceinline unsigned int
00060 Thread::npu(void) {
00061 #ifdef GECODE_HAS_UNISTD_H
00062 int n=sysconf(_SC_NPROCESSORS_ONLN);
00063 return (n>1) ? n : 1;
00064 #else
00065 return 1;
00066 #endif
00067 }
00068 forceinline
00069 Thread::~Thread(void) {
00070 }
00071
00072
00073
00074
00075
00076 forceinline
00077 Mutex::Mutex(void) {
00078 if (pthread_mutex_init(&p_m,NULL) != 0)
00079 throw OperatingSystemError("Mutex::Mutex[pthread_mutex_init]");
00080 }
00081 forceinline void
00082 Mutex::acquire(void) {
00083 if (pthread_mutex_lock(&p_m) != 0)
00084 throw OperatingSystemError("Mutex::acquire[pthread_mutex_lock]");
00085 }
00086 forceinline bool
00087 Mutex::tryacquire(void) {
00088 return pthread_mutex_trylock(&p_m) == 0;
00089 }
00090 forceinline void
00091 Mutex::release(void) {
00092 if (pthread_mutex_unlock(&p_m) != 0)
00093 throw OperatingSystemError("Mutex::release[pthread_mutex_unlock]");
00094 }
00095 forceinline
00096 Mutex::~Mutex(void) {
00097 if (pthread_mutex_destroy(&p_m) != 0)
00098 throw OperatingSystemError("Mutex::~Mutex[pthread_mutex_destroy]");
00099 }
00100
00101
00102
00103
00104
00105 forceinline
00106 Event::Event(void) : p_s(false) {
00107 if (pthread_mutex_init(&p_m,NULL) != 0)
00108 throw OperatingSystemError("Event::Event[pthread_mutex_init]");
00109 if (pthread_cond_init(&p_c,NULL) != 0)
00110 throw OperatingSystemError("Event::Event[pthread_cond_init]");
00111 }
00112 forceinline void
00113 Event::signal(void) {
00114 if (pthread_mutex_lock(&p_m) != 0)
00115 throw OperatingSystemError("Event::signal[pthread_mutex_lock]");
00116 if (!p_s) {
00117 p_s = true;
00118 if (pthread_cond_signal(&p_c) != 0)
00119 throw OperatingSystemError("Event::signal[pthread_cond_signal]");
00120 }
00121 if (pthread_mutex_unlock(&p_m) != 0)
00122 throw OperatingSystemError("Event::signal[pthread_mutex_unlock]");
00123 }
00124 forceinline void
00125 Event::wait(void) {
00126 if (pthread_mutex_lock(&p_m) != 0)
00127 throw OperatingSystemError("Event::wait[pthread_mutex_lock]");
00128 while (!p_s)
00129 if (pthread_cond_wait(&p_c,&p_m) != 0)
00130 throw OperatingSystemError("Event::wait[pthread_cond_wait]");
00131 p_s = false;
00132 if (pthread_mutex_unlock(&p_m) != 0)
00133 throw OperatingSystemError("Event::wait[pthread_mutex_unlock]");
00134 }
00135 forceinline
00136 Event::~Event(void) {
00137 if (pthread_cond_destroy(&p_c) != 0)
00138 throw OperatingSystemError("Event::~Event[pthread_cond_destroy]");
00139 if (pthread_mutex_destroy(&p_m) != 0)
00140 throw OperatingSystemError("Event::~Event[pthread_mutex_destroy]");
00141 }
00142
00143
00144 }}
00145
00146