Generated on Tue Oct 22 2013 00:49:08 for Gecode by doxygen 1.8.4
thread.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  * Bugfixes provided by:
10  * David Rijsman <david.rijsman@quintiq.com>
11  *
12  * Last modified:
13  * $Date: 2013-07-15 02:49:56 +0200 (Mon, 15 Jul 2013) $ by $Author: tack $
14  * $Revision: 13879 $
15  *
16  * This file is part of Gecode, the generic constraint
17  * development environment:
18  * http://www.gecode.org
19  *
20  * Permission is hereby granted, free of charge, to any person obtaining
21  * a copy of this software and associated documentation files (the
22  * "Software"), to deal in the Software without restriction, including
23  * without limitation the rights to use, copy, modify, merge, publish,
24  * distribute, sublicense, and/or sell copies of the Software, and to
25  * permit persons to whom the Software is furnished to do so, subject to
26  * the following conditions:
27  *
28  * The above copyright notice and this permission notice shall be
29  * included in all copies or substantial portions of the Software.
30  *
31  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
32  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
33  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
34  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
35  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
36  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
37  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
38  *
39  */
40 
41 #ifdef GECODE_THREADS_WINDOWS
42 
43 #ifndef NOMINMAX
44 # define NOMINMAX
45 #endif
46 
47 #ifndef _WIN32_WINNT
48 # define _WIN32_WINNT 0x400
49 #endif
50 
51 #ifndef WIN32_LEAN_AND_MEAN
52 # define WIN32_LEAN_AND_MEAN
53 #endif
54 
55 #include <windows.h>
56 
57 #endif
58 
59 #ifdef GECODE_THREADS_PTHREADS
60 
61 #include <pthread.h>
62 
63 #ifdef GECODE_THREADS_OSX
64 
65 #include <libkern/OSAtomic.h>
66 
67 #endif
68 
69 #endif
70 
86 namespace Gecode { namespace Support {
87 
97  class Mutex {
98  private:
99 #ifdef GECODE_THREADS_WINDOWS
100  CRITICAL_SECTION w_cs;
102 #endif
103 #ifdef GECODE_THREADS_PTHREADS
104  pthread_mutex_t p_m;
106 #endif
107  public:
109  Mutex(void);
111  void acquire(void);
113  bool tryacquire(void);
115  void release(void);
117  ~Mutex(void);
119  static void* operator new(size_t s);
121  static void operator delete(void* p);
122  private:
124  Mutex(const Mutex&) {}
126  void operator=(const Mutex&) {}
127  };
128 
129 #ifdef GECODE_THREADS_WINDOWS
130 
131  typedef Mutex FastMutex;
132 
133 #endif
134 
135 #ifdef GECODE_THREADS_PTHREADS
136 
137 #if defined(GECODE_THREADS_OSX) || defined(GECODE_THREADS_PTHREADS_SPINLOCK)
138 
153  class FastMutex {
154  private:
155 #ifdef GECODE_THREADS_OSX
156  OSSpinLock lck;
158 #else
159  pthread_spinlock_t p_s;
161 #endif
162  public:
164  FastMutex(void);
166  void acquire(void);
168  bool tryacquire(void);
170  void release(void);
172  ~FastMutex(void);
174  static void* operator new(size_t s);
176  static void operator delete(void* p);
177  private:
179  FastMutex(const FastMutex&) {}
181  void operator=(const FastMutex&) {}
182  };
183 
184 #else
185 
186  typedef Mutex FastMutex;
187 
188 #endif
189 
190 #endif
191 
197  class Lock {
198  private:
200  Mutex& m;
201  public:
203  Lock(Mutex& m0);
205  ~Lock(void);
206  private:
208  Lock(const Lock& l) : m(l.m) {}
210  void operator=(const Lock&) {}
211  };
212 
221  class Event {
222  private:
223 #ifdef GECODE_THREADS_WINDOWS
224  HANDLE w_h;
226 #endif
227 #ifdef GECODE_THREADS_PTHREADS
228  pthread_mutex_t p_m;
231  pthread_cond_t p_c;
233  bool p_s;
234 #endif
235  public:
237  Event(void);
239  void signal(void);
241  void wait(void);
243  ~Event(void);
244  private:
246  Event(const Event&) {}
248  void operator=(const Event&) {}
249  };
250 
256  class Runnable {
257  public:
259  virtual void run(void) = 0;
261  virtual ~Runnable(void) {}
263  static void* operator new(size_t s);
265  static void operator delete(void* p);
266  };
267 
277  class Thread {
278  public:
280  class Run {
281  public:
283  Run* n;
293  GECODE_SUPPORT_EXPORT void exec(void);
295  void run(Runnable* r);
297  static void* operator new(size_t s);
299  static void operator delete(void* p);
300  };
302  GECODE_SUPPORT_EXPORT static Mutex* m(void);
305  public:
315  static void run(Runnable* r);
317  static void sleep(unsigned int ms);
319  static unsigned int npu(void);
320  private:
322  Thread(const Thread&) {}
324  void operator=(const Thread&) {}
325  };
326 
327 }}
328 
329 // STATISTICS: support-any