38 #ifdef GECODE_HAS_UNISTD_H 42 namespace Gecode {
namespace Support {
49 if (pthread_mutex_init(&p_m,NULL) != 0)
50 throw OperatingSystemError(
"Mutex::Mutex[pthread_mutex_init]");
54 if (pthread_mutex_lock(&p_m) != 0)
55 throw OperatingSystemError(
"Mutex::acquire[pthread_mutex_lock]");
59 return pthread_mutex_trylock(&p_m) == 0;
63 if (pthread_mutex_unlock(&p_m) != 0)
64 throw OperatingSystemError(
"Mutex::release[pthread_mutex_unlock]");
68 if (pthread_mutex_destroy(&p_m) != 0)
69 throw OperatingSystemError(
"Mutex::~Mutex[pthread_mutex_destroy]");
72 #ifdef GECODE_THREADS_OSX 85 return OSSpinLockTry(&lck);
89 OSSpinLockUnlock(&lck);
92 FastMutex::~FastMutex(
void) {}
96 #ifdef GECODE_THREADS_PTHREADS_SPINLOCK 103 if (pthread_spin_init(&p_s,PTHREAD_PROCESS_PRIVATE) != 0)
104 throw OperatingSystemError(
"FastMutex::FastMutex[pthread_spin_init]");
108 if (pthread_spin_lock(&p_s) != 0)
109 throw OperatingSystemError(
"FastMutex::acquire[pthread_spin_lock]");
113 return pthread_spin_trylock(&p_s) == 0;
117 if (pthread_spin_unlock(&p_s) != 0)
118 throw OperatingSystemError(
"FastMutex::release[pthread_spin_unlock]");
121 FastMutex::~FastMutex(
void) {
122 if (pthread_spin_destroy(&p_s) != 0)
123 throw OperatingSystemError(
124 "FastMutex::~FastMutex[pthread_spin_destroy]");
134 if (pthread_mutex_init(&p_m,NULL) != 0)
135 throw OperatingSystemError(
"Event::Event[pthread_mutex_init]");
136 if (pthread_cond_init(&p_c,NULL) != 0)
137 throw OperatingSystemError(
"Event::Event[pthread_cond_init]");
141 if (pthread_mutex_lock(&p_m) != 0)
142 throw OperatingSystemError(
"Event::signal[pthread_mutex_lock]");
145 if (pthread_cond_signal(&p_c) != 0)
146 throw OperatingSystemError(
"Event::signal[pthread_cond_signal]");
148 if (pthread_mutex_unlock(&p_m) != 0)
149 throw OperatingSystemError(
"Event::signal[pthread_mutex_unlock]");
153 if (pthread_mutex_lock(&p_m) != 0)
154 throw OperatingSystemError(
"Event::wait[pthread_mutex_lock]");
156 if (pthread_cond_wait(&p_c,&p_m) != 0)
157 throw OperatingSystemError(
"Event::wait[pthread_cond_wait]");
159 if (pthread_mutex_unlock(&p_m) != 0)
160 throw OperatingSystemError(
"Event::wait[pthread_mutex_unlock]");
164 if (pthread_cond_destroy(&p_c) != 0)
165 throw OperatingSystemError(
"Event::~Event[pthread_cond_destroy]");
166 if (pthread_mutex_destroy(&p_m) != 0)
167 throw OperatingSystemError(
"Event::~Event[pthread_mutex_destroy]");
176 #ifdef GECODE_HAS_UNISTD_H 177 unsigned int s = ms / 1000;
188 #ifdef GECODE_HAS_UNISTD_H 189 int n=
static_cast<int>(sysconf(_SC_NPROCESSORS_ONLN));
190 return (n>1) ?
n : 1;
static unsigned int npu(void)
Return number of processing units (1 if information not available)
Mutex(void)
Initialize mutex.
void acquire(void)
Acquire the mutex and possibly block.
void signal(void)
Signal the event.
void release(void)
Release the mutex.
int n
Number of negative literals for node type.
static void sleep(unsigned int ms)
Put current thread to sleep for ms milliseconds.
bool tryacquire(void)
Try to acquire the mutex, return true if succesful.
~Event(void)
Delete event.
~Mutex(void)
Delete mutex.
Event(void)
Initialize event.
Gecode toplevel namespace
void wait(void)
Wait until the event becomes signalled.