34 #ifdef GECODE_HAS_UNISTD_H 40 namespace Gecode {
namespace Support {
42 #ifdef GECODE_THREADS_OSX_UNFAIR 44 #pragma clang diagnostic push 45 #pragma clang diagnostic ignored "-Wdeprecated-declarations" 51 if (&os_unfair_lock_lock != NULL)
52 u.unfair_lck = OS_UNFAIR_LOCK_INIT;
54 u.spin_lck = OS_SPINLOCK_INIT;
58 if (&os_unfair_lock_lock != NULL) {
59 os_unfair_lock_lock(&
u.unfair_lck);
61 OSSpinLockLock(&
u.spin_lck);
66 if (&os_unfair_lock_trylock != NULL)
67 return os_unfair_lock_trylock(&
u.unfair_lck);
69 return OSSpinLockTry(&
u.spin_lck);
73 if (&os_unfair_lock_unlock != NULL)
74 os_unfair_lock_unlock(&
u.unfair_lck);
76 OSSpinLockUnlock(&
u.spin_lck);
81 #pragma clang diagnostic pop 90 if (pthread_mutex_init(&p_m,NULL) != 0)
91 throw OperatingSystemError(
"Mutex::Mutex[pthread_mutex_init]");
95 if (pthread_mutex_lock(&p_m) != 0)
96 throw OperatingSystemError(
"Mutex::acquire[pthread_mutex_lock]");
100 return pthread_mutex_trylock(&p_m) == 0;
104 if (pthread_mutex_unlock(&p_m) != 0)
105 throw OperatingSystemError(
"Mutex::release[pthread_mutex_unlock]");
109 if (pthread_mutex_destroy(&p_m) != 0) {
110 std::cerr <<
"Operating system error: " 111 <<
"Mutex::~Mutex[pthread_mutex_destroy]";
117 #ifdef GECODE_THREADS_PTHREADS_SPINLOCK 124 if (pthread_spin_init(&p_s,PTHREAD_PROCESS_PRIVATE) != 0)
125 throw OperatingSystemError(
"FastMutex::FastMutex[pthread_spin_init]");
129 if (pthread_spin_lock(&p_s) != 0)
130 throw OperatingSystemError(
"FastMutex::acquire[pthread_spin_lock]");
134 return pthread_spin_trylock(&p_s) == 0;
138 if (pthread_spin_unlock(&p_s) != 0)
139 throw OperatingSystemError(
"FastMutex::release[pthread_spin_unlock]");
142 FastMutex::~FastMutex(
void) {
143 if (pthread_spin_destroy(&p_s) != 0) {
144 std::cerr <<
"Operating system error: " 145 <<
"FastMutex::~FastMutex[pthread_spin_destroy]";
157 if (pthread_mutex_init(&p_m,NULL) != 0)
158 throw OperatingSystemError(
"Event::Event[pthread_mutex_init]");
159 if (pthread_cond_init(&p_c,NULL) != 0)
160 throw OperatingSystemError(
"Event::Event[pthread_cond_init]");
164 if (pthread_mutex_lock(&p_m) != 0)
165 throw OperatingSystemError(
"Event::signal[pthread_mutex_lock]");
168 if (pthread_cond_signal(&p_c) != 0)
169 throw OperatingSystemError(
"Event::signal[pthread_cond_signal]");
171 if (pthread_mutex_unlock(&p_m) != 0)
172 throw OperatingSystemError(
"Event::signal[pthread_mutex_unlock]");
176 if (pthread_mutex_lock(&p_m) != 0)
177 throw OperatingSystemError(
"Event::wait[pthread_mutex_lock]");
179 if (pthread_cond_wait(&p_c,&p_m) != 0)
180 throw OperatingSystemError(
"Event::wait[pthread_cond_wait]");
182 if (pthread_mutex_unlock(&p_m) != 0)
183 throw OperatingSystemError(
"Event::wait[pthread_mutex_unlock]");
187 if (pthread_cond_destroy(&p_c) != 0) {
188 std::cerr <<
"Operating system error: " 189 <<
"Event::~Event[pthread_cond_destroy]";
192 if (pthread_mutex_destroy(&p_m) != 0) {
193 std::cerr <<
"Operating system error: " 194 <<
"Event::~Event[pthread_mutex_destroy]";
205 #ifdef GECODE_HAS_UNISTD_H 206 unsigned int s = ms / 1000;
217 #ifdef GECODE_HAS_UNISTD_H 218 int n=
static_cast<int>(sysconf(_SC_NPROCESSORS_ONLN));
219 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.
union Gecode::@585::NNF::@62 u
Union depending on nodetype t.
~Mutex(void)
Delete mutex.
Event(void)
Initialize event.
Gecode toplevel namespace
void wait(void)
Wait until the event becomes signalled.