38 #ifdef GECODE_HAS_UNISTD_H 44 namespace Gecode {
namespace Support {
51 if (pthread_mutex_init(&p_m,NULL) != 0)
52 throw OperatingSystemError(
"Mutex::Mutex[pthread_mutex_init]");
56 if (pthread_mutex_lock(&p_m) != 0)
57 throw OperatingSystemError(
"Mutex::acquire[pthread_mutex_lock]");
61 return pthread_mutex_trylock(&p_m) == 0;
65 if (pthread_mutex_unlock(&p_m) != 0)
66 throw OperatingSystemError(
"Mutex::release[pthread_mutex_unlock]");
70 if (pthread_mutex_destroy(&p_m) != 0) {
71 std::cerr <<
"Operating system error: " 72 <<
"Mutex::~Mutex[pthread_mutex_destroy]";
77 #ifdef GECODE_THREADS_OSX 90 return OSSpinLockTry(&lck);
94 OSSpinLockUnlock(&lck);
97 FastMutex::~FastMutex(
void) {}
101 #ifdef GECODE_THREADS_PTHREADS_SPINLOCK 108 if (pthread_spin_init(&p_s,PTHREAD_PROCESS_PRIVATE) != 0)
109 throw OperatingSystemError(
"FastMutex::FastMutex[pthread_spin_init]");
113 if (pthread_spin_lock(&p_s) != 0)
114 throw OperatingSystemError(
"FastMutex::acquire[pthread_spin_lock]");
118 return pthread_spin_trylock(&p_s) == 0;
122 if (pthread_spin_unlock(&p_s) != 0)
123 throw OperatingSystemError(
"FastMutex::release[pthread_spin_unlock]");
126 FastMutex::~FastMutex(
void) {
127 if (pthread_spin_destroy(&p_s) != 0) {
128 std::cerr <<
"Operating system error: " 129 <<
"FastMutex::~FastMutex[pthread_spin_destroy]";
141 if (pthread_mutex_init(&p_m,NULL) != 0)
142 throw OperatingSystemError(
"Event::Event[pthread_mutex_init]");
143 if (pthread_cond_init(&p_c,NULL) != 0)
144 throw OperatingSystemError(
"Event::Event[pthread_cond_init]");
148 if (pthread_mutex_lock(&p_m) != 0)
149 throw OperatingSystemError(
"Event::signal[pthread_mutex_lock]");
152 if (pthread_cond_signal(&p_c) != 0)
153 throw OperatingSystemError(
"Event::signal[pthread_cond_signal]");
155 if (pthread_mutex_unlock(&p_m) != 0)
156 throw OperatingSystemError(
"Event::signal[pthread_mutex_unlock]");
160 if (pthread_mutex_lock(&p_m) != 0)
161 throw OperatingSystemError(
"Event::wait[pthread_mutex_lock]");
163 if (pthread_cond_wait(&p_c,&p_m) != 0)
164 throw OperatingSystemError(
"Event::wait[pthread_cond_wait]");
166 if (pthread_mutex_unlock(&p_m) != 0)
167 throw OperatingSystemError(
"Event::wait[pthread_mutex_unlock]");
171 if (pthread_cond_destroy(&p_c) != 0) {
172 std::cerr <<
"Operating system error: " 173 <<
"Event::~Event[pthread_cond_destroy]";
176 if (pthread_mutex_destroy(&p_m) != 0) {
177 std::cerr <<
"Operating system error: " 178 <<
"Event::~Event[pthread_mutex_destroy]";
189 #ifdef GECODE_HAS_UNISTD_H 190 unsigned int s = ms / 1000;
201 #ifdef GECODE_HAS_UNISTD_H 202 int n=
static_cast<int>(sysconf(_SC_NPROCESSORS_ONLN));
203 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.