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
85 FastMutex::acquire(
void) {
89 FastMutex::tryacquire(
void) {
90 return OSSpinLockTry(&lck);
93 FastMutex::release(
void) {
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]");
112 FastMutex::acquire(
void) {
113 if (pthread_spin_lock(&p_s) != 0)
114 throw OperatingSystemError(
"FastMutex::acquire[pthread_spin_lock]");
117 FastMutex::tryacquire(
void) {
118 return pthread_spin_trylock(&p_s) == 0;
121 FastMutex::release(
void) {
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]";
140 Event::Event(
void) : p_s(false) {
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]");
147 Event::signal(
void) {
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]");
170 Event::~Event(
void) {
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]";
188 Thread::sleep(
unsigned int ms) {
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;