Generated on Tue Jul 18 2017 18:41:42 for Gecode by doxygen 1.8.13
pthreads.hpp
Go to the documentation of this file.
1 /* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */
2 /*
3  * Main authors:
4  * Christian Schulte <schulte@gecode.org>
5  *
6  * Copyright:
7  * Christian Schulte, 2009
8  *
9  * Last modified:
10  * $Date: 2015-08-18 09:57:34 +0200 (Tue, 18 Aug 2015) $ by $Author: schulte $
11  * $Revision: 14653 $
12  *
13  * This file is part of Gecode, the generic constraint
14  * development environment:
15  * http://www.gecode.org
16  *
17  * Permission is hereby granted, free of charge, to any person obtaining
18  * a copy of this software and associated documentation files (the
19  * "Software"), to deal in the Software without restriction, including
20  * without limitation the rights to use, copy, modify, merge, publish,
21  * distribute, sublicense, and/or sell copies of the Software, and to
22  * permit persons to whom the Software is furnished to do so, subject to
23  * the following conditions:
24  *
25  * The above copyright notice and this permission notice shall be
26  * included in all copies or substantial portions of the Software.
27  *
28  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
29  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
30  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
31  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
32  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
33  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
34  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
35  *
36  */
37 
38 #ifdef GECODE_HAS_UNISTD_H
39 #include <unistd.h>
40 #endif
41 
42 #include <exception>
43 
44 namespace Gecode { namespace Support {
45 
46  /*
47  * Mutex
48  */
50  Mutex::Mutex(void) {
51  if (pthread_mutex_init(&p_m,NULL) != 0)
52  throw OperatingSystemError("Mutex::Mutex[pthread_mutex_init]");
53  }
54  forceinline void
55  Mutex::acquire(void) {
56  if (pthread_mutex_lock(&p_m) != 0)
57  throw OperatingSystemError("Mutex::acquire[pthread_mutex_lock]");
58  }
59  forceinline bool
60  Mutex::tryacquire(void) {
61  return pthread_mutex_trylock(&p_m) == 0;
62  }
63  forceinline void
64  Mutex::release(void) {
65  if (pthread_mutex_unlock(&p_m) != 0)
66  throw OperatingSystemError("Mutex::release[pthread_mutex_unlock]");
67  }
69  Mutex::~Mutex(void) {
70  if (pthread_mutex_destroy(&p_m) != 0) {
71  std::cerr << "Operating system error: "
72  << "Mutex::~Mutex[pthread_mutex_destroy]";
73  std::terminate();
74  }
75  }
76 
77 #ifdef GECODE_THREADS_OSX
78 
79  /*
80  * FastMutex
81  */
83  FastMutex::FastMutex(void) : lck(OS_SPINLOCK_INIT) {}
84  forceinline void
85  FastMutex::acquire(void) {
86  OSSpinLockLock(&lck);
87  }
88  forceinline bool
89  FastMutex::tryacquire(void) {
90  return OSSpinLockTry(&lck);
91  }
92  forceinline void
93  FastMutex::release(void) {
94  OSSpinLockUnlock(&lck);
95  }
97  FastMutex::~FastMutex(void) {}
98 
99 #endif
100 
101 #ifdef GECODE_THREADS_PTHREADS_SPINLOCK
102 
103  /*
104  * FastMutex
105  */
107  FastMutex::FastMutex(void) {
108  if (pthread_spin_init(&p_s,PTHREAD_PROCESS_PRIVATE) != 0)
109  throw OperatingSystemError("FastMutex::FastMutex[pthread_spin_init]");
110  }
111  forceinline void
112  FastMutex::acquire(void) {
113  if (pthread_spin_lock(&p_s) != 0)
114  throw OperatingSystemError("FastMutex::acquire[pthread_spin_lock]");
115  }
116  forceinline bool
117  FastMutex::tryacquire(void) {
118  return pthread_spin_trylock(&p_s) == 0;
119  }
120  forceinline void
121  FastMutex::release(void) {
122  if (pthread_spin_unlock(&p_s) != 0)
123  throw OperatingSystemError("FastMutex::release[pthread_spin_unlock]");
124  }
126  FastMutex::~FastMutex(void) {
127  if (pthread_spin_destroy(&p_s) != 0) {
128  std::cerr << "Operating system error: "
129  << "FastMutex::~FastMutex[pthread_spin_destroy]";
130  std::terminate();
131  }
132  }
133 
134 #endif
135 
136  /*
137  * Event
138  */
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]");
145  }
146  forceinline void
147  Event::signal(void) {
148  if (pthread_mutex_lock(&p_m) != 0)
149  throw OperatingSystemError("Event::signal[pthread_mutex_lock]");
150  if (!p_s) {
151  p_s = true;
152  if (pthread_cond_signal(&p_c) != 0)
153  throw OperatingSystemError("Event::signal[pthread_cond_signal]");
154  }
155  if (pthread_mutex_unlock(&p_m) != 0)
156  throw OperatingSystemError("Event::signal[pthread_mutex_unlock]");
157  }
158  forceinline void
159  Event::wait(void) {
160  if (pthread_mutex_lock(&p_m) != 0)
161  throw OperatingSystemError("Event::wait[pthread_mutex_lock]");
162  while (!p_s)
163  if (pthread_cond_wait(&p_c,&p_m) != 0)
164  throw OperatingSystemError("Event::wait[pthread_cond_wait]");
165  p_s = false;
166  if (pthread_mutex_unlock(&p_m) != 0)
167  throw OperatingSystemError("Event::wait[pthread_mutex_unlock]");
168  }
170  Event::~Event(void) {
171  if (pthread_cond_destroy(&p_c) != 0) {
172  std::cerr << "Operating system error: "
173  << "Event::~Event[pthread_cond_destroy]";
174  std::terminate();
175  }
176  if (pthread_mutex_destroy(&p_m) != 0) {
177  std::cerr << "Operating system error: "
178  << "Event::~Event[pthread_mutex_destroy]";
179  std::terminate();
180  }
181  }
182 
183 
184  /*
185  * Thread
186  */
187  forceinline void
188  Thread::sleep(unsigned int ms) {
189 #ifdef GECODE_HAS_UNISTD_H
190  unsigned int s = ms / 1000;
191  ms -= 1000 * s;
192  if (s > 0) {
193  // More than one million microseconds, use sleep
194  ::sleep(s);
195  }
196  usleep(ms * 1000);
197 #endif
198  }
199  forceinline unsigned int
200  Thread::npu(void) {
201 #ifdef GECODE_HAS_UNISTD_H
202  int n=static_cast<int>(sysconf(_SC_NPROCESSORS_ONLN));
203  return (n>1) ? n : 1;
204 #else
205  return 1;
206 #endif
207  }
208 
209 }}
210 
211 // STATISTICS: support-any
static unsigned int npu(void)
Return number of processing units (1 if information not available)
Definition: none.hpp:80
Mutex(void)
Initialize mutex.
Definition: none.hpp:44
void acquire(void)
Acquire the mutex and possibly block.
Definition: none.hpp:46
void signal(void)
Signal the event.
Definition: none.hpp:63
void release(void)
Release the mutex.
Definition: none.hpp:52
int n
Number of negative literals for node type.
Definition: bool-expr.cpp:238
static void sleep(unsigned int ms)
Put current thread to sleep for ms milliseconds.
Definition: none.hpp:78
bool tryacquire(void)
Try to acquire the mutex, return true if succesful.
Definition: none.hpp:48
~Event(void)
Delete event.
Definition: none.hpp:67
~Mutex(void)
Delete mutex.
Definition: none.hpp:54
Event(void)
Initialize event.
Definition: none.hpp:61
Mutex FastMutex
Definition: thread.hpp:188
#define forceinline
Definition: config.hpp:173
Gecode toplevel namespace
void wait(void)
Wait until the event becomes signalled.
Definition: none.hpp:65