Generated on Sat Feb 7 2015 02:01:31 for Gecode by doxygen 1.8.9.1
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: 2013-07-01 07:36:45 +0200 (Mon, 01 Jul 2013) $ by $Author: tack $
11  * $Revision: 13741 $
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 namespace Gecode { namespace Support {
43 
44  /*
45  * Mutex
46  */
48  Mutex::Mutex(void) {
49  if (pthread_mutex_init(&p_m,NULL) != 0)
50  throw OperatingSystemError("Mutex::Mutex[pthread_mutex_init]");
51  }
52  forceinline void
53  Mutex::acquire(void) {
54  if (pthread_mutex_lock(&p_m) != 0)
55  throw OperatingSystemError("Mutex::acquire[pthread_mutex_lock]");
56  }
57  forceinline bool
58  Mutex::tryacquire(void) {
59  return pthread_mutex_trylock(&p_m) == 0;
60  }
61  forceinline void
62  Mutex::release(void) {
63  if (pthread_mutex_unlock(&p_m) != 0)
64  throw OperatingSystemError("Mutex::release[pthread_mutex_unlock]");
65  }
67  Mutex::~Mutex(void) {
68  if (pthread_mutex_destroy(&p_m) != 0)
69  throw OperatingSystemError("Mutex::~Mutex[pthread_mutex_destroy]");
70  }
71 
72 #ifdef GECODE_THREADS_OSX
73 
74  /*
75  * FastMutex
76  */
78  FastMutex::FastMutex(void) : lck(OS_SPINLOCK_INIT) {}
79  forceinline void
80  FastMutex::acquire(void) {
81  OSSpinLockLock(&lck);
82  }
83  forceinline bool
84  FastMutex::tryacquire(void) {
85  return OSSpinLockTry(&lck);
86  }
87  forceinline void
88  FastMutex::release(void) {
89  OSSpinLockUnlock(&lck);
90  }
92  FastMutex::~FastMutex(void) {}
93 
94 #endif
95 
96 #ifdef GECODE_THREADS_PTHREADS_SPINLOCK
97 
98  /*
99  * FastMutex
100  */
102  FastMutex::FastMutex(void) {
103  if (pthread_spin_init(&p_s,PTHREAD_PROCESS_PRIVATE) != 0)
104  throw OperatingSystemError("FastMutex::FastMutex[pthread_spin_init]");
105  }
106  forceinline void
107  FastMutex::acquire(void) {
108  if (pthread_spin_lock(&p_s) != 0)
109  throw OperatingSystemError("FastMutex::acquire[pthread_spin_lock]");
110  }
111  forceinline bool
112  FastMutex::tryacquire(void) {
113  return pthread_spin_trylock(&p_s) == 0;
114  }
115  forceinline void
116  FastMutex::release(void) {
117  if (pthread_spin_unlock(&p_s) != 0)
118  throw OperatingSystemError("FastMutex::release[pthread_spin_unlock]");
119  }
121  FastMutex::~FastMutex(void) {
122  if (pthread_spin_destroy(&p_s) != 0)
123  throw OperatingSystemError(
124  "FastMutex::~FastMutex[pthread_spin_destroy]");
125  }
126 
127 #endif
128 
129  /*
130  * Event
131  */
133  Event::Event(void) : p_s(false) {
134  if (pthread_mutex_init(&p_m,NULL) != 0)
135  throw OperatingSystemError("Event::Event[pthread_mutex_init]");
136  if (pthread_cond_init(&p_c,NULL) != 0)
137  throw OperatingSystemError("Event::Event[pthread_cond_init]");
138  }
139  forceinline void
140  Event::signal(void) {
141  if (pthread_mutex_lock(&p_m) != 0)
142  throw OperatingSystemError("Event::signal[pthread_mutex_lock]");
143  if (!p_s) {
144  p_s = true;
145  if (pthread_cond_signal(&p_c) != 0)
146  throw OperatingSystemError("Event::signal[pthread_cond_signal]");
147  }
148  if (pthread_mutex_unlock(&p_m) != 0)
149  throw OperatingSystemError("Event::signal[pthread_mutex_unlock]");
150  }
151  forceinline void
152  Event::wait(void) {
153  if (pthread_mutex_lock(&p_m) != 0)
154  throw OperatingSystemError("Event::wait[pthread_mutex_lock]");
155  while (!p_s)
156  if (pthread_cond_wait(&p_c,&p_m) != 0)
157  throw OperatingSystemError("Event::wait[pthread_cond_wait]");
158  p_s = false;
159  if (pthread_mutex_unlock(&p_m) != 0)
160  throw OperatingSystemError("Event::wait[pthread_mutex_unlock]");
161  }
163  Event::~Event(void) {
164  if (pthread_cond_destroy(&p_c) != 0)
165  throw OperatingSystemError("Event::~Event[pthread_cond_destroy]");
166  if (pthread_mutex_destroy(&p_m) != 0)
167  throw OperatingSystemError("Event::~Event[pthread_mutex_destroy]");
168  }
169 
170 
171  /*
172  * Thread
173  */
174  forceinline void
175  Thread::sleep(unsigned int ms) {
176 #ifdef GECODE_HAS_UNISTD_H
177  unsigned int s = ms / 1000;
178  ms -= 1000 * s;
179  if (s > 0) {
180  // More than one million microseconds, use sleep
181  ::sleep(s);
182  }
183  usleep(ms * 1000);
184 #endif
185  }
186  forceinline unsigned int
187  Thread::npu(void) {
188 #ifdef GECODE_HAS_UNISTD_H
189  int n=static_cast<int>(sysconf(_SC_NPROCESSORS_ONLN));
190  return (n>1) ? n : 1;
191 #else
192  return 1;
193 #endif
194  }
195 
196 }}
197 
198 // STATISTICS: support-any
Mutex(void)
Initialize mutex.
Definition: none.hpp:44
void acquire(void)
Acquire the mutex and possibly block.
Definition: none.hpp:46
void release(void)
Release the mutex.
Definition: none.hpp:52
int n
Number of negative literals for node type.
Definition: bool-expr.cpp:238
bool tryacquire(void)
Try to acquire the mutex, return true if succesful.
Definition: none.hpp:48
~Mutex(void)
Delete mutex.
Definition: none.hpp:54
Mutex FastMutex
Definition: thread.hpp:188
#define forceinline
Definition: config.hpp:132
Gecode toplevel namespace
void wait(Home home, FloatVar x, void(*c)(Space &home))
Execute c when x becomes assigned.
Definition: exec.cpp:44