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
80 FastMutex::acquire(
void) {
84 FastMutex::tryacquire(
void) {
85 return OSSpinLockTry(&lck);
88 FastMutex::release(
void) {
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]");
107 FastMutex::acquire(
void) {
108 if (pthread_spin_lock(&p_s) != 0)
109 throw OperatingSystemError(
"FastMutex::acquire[pthread_spin_lock]");
112 FastMutex::tryacquire(
void) {
113 return pthread_spin_trylock(&p_s) == 0;
116 FastMutex::release(
void) {
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]");
133 Event::Event(
void) : p_s(false) {
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]");
140 Event::signal(
void) {
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]");
163 Event::~Event(
void) {
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]");
175 Thread::sleep(
unsigned int ms) {
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;
Mutex(void)
Initialize mutex.
void acquire(void)
Acquire the mutex and possibly block.
void release(void)
Release the mutex.
int n
Number of negative literals for node type.
bool tryacquire(void)
Try to acquire the mutex, return true if succesful.
~Mutex(void)
Delete mutex.
Gecode toplevel namespace
void wait(Home home, FloatVar x, void(*c)(Space &home))
Execute c when x becomes assigned.