38 #ifdef GECODE_HAS_UNISTD_H 44 namespace Gecode {
namespace Support {
46 #ifdef GECODE_THREADS_OSX_UNFAIR 55 os_unfair_lock_lock(&lck);
59 return os_unfair_lock_trylock(&lck);
63 os_unfair_lock_unlock(&lck);
75 if (pthread_mutex_init(&p_m,NULL) != 0)
76 throw OperatingSystemError(
"Mutex::Mutex[pthread_mutex_init]");
80 if (pthread_mutex_lock(&p_m) != 0)
81 throw OperatingSystemError(
"Mutex::acquire[pthread_mutex_lock]");
85 return pthread_mutex_trylock(&p_m) == 0;
89 if (pthread_mutex_unlock(&p_m) != 0)
90 throw OperatingSystemError(
"Mutex::release[pthread_mutex_unlock]");
94 if (pthread_mutex_destroy(&p_m) != 0) {
95 std::cerr <<
"Operating system error: " 96 <<
"Mutex::~Mutex[pthread_mutex_destroy]";
102 #ifdef GECODE_THREADS_PTHREADS_SPINLOCK 109 if (pthread_spin_init(&p_s,PTHREAD_PROCESS_PRIVATE) != 0)
110 throw OperatingSystemError(
"FastMutex::FastMutex[pthread_spin_init]");
114 if (pthread_spin_lock(&p_s) != 0)
115 throw OperatingSystemError(
"FastMutex::acquire[pthread_spin_lock]");
119 return pthread_spin_trylock(&p_s) == 0;
123 if (pthread_spin_unlock(&p_s) != 0)
124 throw OperatingSystemError(
"FastMutex::release[pthread_spin_unlock]");
127 FastMutex::~FastMutex(
void) {
128 if (pthread_spin_destroy(&p_s) != 0) {
129 std::cerr <<
"Operating system error: " 130 <<
"FastMutex::~FastMutex[pthread_spin_destroy]";
142 if (pthread_mutex_init(&p_m,NULL) != 0)
143 throw OperatingSystemError(
"Event::Event[pthread_mutex_init]");
144 if (pthread_cond_init(&p_c,NULL) != 0)
145 throw OperatingSystemError(
"Event::Event[pthread_cond_init]");
149 if (pthread_mutex_lock(&p_m) != 0)
150 throw OperatingSystemError(
"Event::signal[pthread_mutex_lock]");
153 if (pthread_cond_signal(&p_c) != 0)
154 throw OperatingSystemError(
"Event::signal[pthread_cond_signal]");
156 if (pthread_mutex_unlock(&p_m) != 0)
157 throw OperatingSystemError(
"Event::signal[pthread_mutex_unlock]");
161 if (pthread_mutex_lock(&p_m) != 0)
162 throw OperatingSystemError(
"Event::wait[pthread_mutex_lock]");
164 if (pthread_cond_wait(&p_c,&p_m) != 0)
165 throw OperatingSystemError(
"Event::wait[pthread_cond_wait]");
167 if (pthread_mutex_unlock(&p_m) != 0)
168 throw OperatingSystemError(
"Event::wait[pthread_mutex_unlock]");
172 if (pthread_cond_destroy(&p_c) != 0) {
173 std::cerr <<
"Operating system error: " 174 <<
"Event::~Event[pthread_cond_destroy]";
177 if (pthread_mutex_destroy(&p_m) != 0) {
178 std::cerr <<
"Operating system error: " 179 <<
"Event::~Event[pthread_mutex_destroy]";
190 #ifdef GECODE_HAS_UNISTD_H 191 unsigned int s = ms / 1000;
202 #ifdef GECODE_HAS_UNISTD_H 203 int n=
static_cast<int>(sysconf(_SC_NPROCESSORS_ONLN));
204 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.