Generated on Fri Aug 24 2012 04:52:14 for Gecode by doxygen 1.8.1.2
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: 2010-05-11 20:29:52 +1000 (Tue, 11 May 2010) $ by $Author: tack $
14  * $Revision: 10939 $
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 #endif
64 
80 namespace Gecode { namespace Support {
81 
91  class Mutex {
92  private:
93 #ifdef GECODE_THREADS_WINDOWS
94 
95  CRITICAL_SECTION w_cs;
96 #endif
97 #ifdef GECODE_THREADS_PTHREADS
98 
99  pthread_mutex_t p_m;
100 #endif
101  public:
103  Mutex(void);
105  void acquire(void);
107  bool tryacquire(void);
109  void release(void);
111  ~Mutex(void);
113  static void* operator new(size_t s);
115  static void operator delete(void* p);
116  private:
118  Mutex(const Mutex&) {}
120  void operator=(const Mutex&) {}
121  };
122 
128  class Lock {
129  private:
131  Mutex& m;
132  public:
134  Lock(Mutex& m0);
136  ~Lock(void);
137  private:
139  Lock(const Lock& l) : m(l.m) {}
141  void operator=(const Lock&) {}
142  };
143 
152  class Event {
153  private:
154 #ifdef GECODE_THREADS_WINDOWS
155 
156  HANDLE w_h;
157 #endif
158 #ifdef GECODE_THREADS_PTHREADS
159 
160  pthread_mutex_t p_m;
162  pthread_cond_t p_c;
164  bool p_s;
165 #endif
166  public:
168  Event(void);
170  void signal(void);
172  void wait(void);
174  ~Event(void);
175  private:
177  Event(const Event&) {}
179  void operator=(const Event&) {}
180  };
181 
187  class Runnable {
188  public:
190  virtual void run(void) = 0;
192  virtual ~Runnable(void) {}
194  static void* operator new(size_t s);
196  static void operator delete(void* p);
197  };
198 
208  class Thread {
209  public:
211  class Run {
212  public:
214  Run* n;
224  GECODE_SUPPORT_EXPORT void exec(void);
226  void run(Runnable* r);
228  static void* operator new(size_t s);
230  static void operator delete(void* p);
231  };
236  public:
246  static void run(Runnable* r);
248  static void sleep(unsigned int ms);
250  static unsigned int npu(void);
251  private:
253  Thread(const Thread&) {}
255  void operator=(const Thread&) {}
256  };
257 
258 }}
259 
260 #ifdef GECODE_THREADS_WINDOWS
262 #endif
263 #ifdef GECODE_THREADS_PTHREADS
265 #endif
266 #ifndef GECODE_HAS_THREADS
268 #endif
269 
271 
272 // STATISTICS: support-any