Generated on Sat Feb 7 2015 02:01:31 for Gecode by doxygen 1.8.9.1
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-10-15 00:46:56 +0200 (Tue, 15 Oct 2013) $ by $Author: tack $
14  * $Revision: 14025 $
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 #include <cstddef>
42 
43 #ifdef GECODE_THREADS_WINDOWS
44 
45 #ifndef NOMINMAX
46 # define NOMINMAX
47 #endif
48 
49 #ifndef _WIN32_WINNT
50 # define _WIN32_WINNT 0x400
51 #endif
52 
53 #ifndef WIN32_LEAN_AND_MEAN
54 # define WIN32_LEAN_AND_MEAN
55 #endif
56 
57 #include <windows.h>
58 
59 #endif
60 
61 #ifdef GECODE_THREADS_PTHREADS
62 
63 #include <pthread.h>
64 
65 #ifdef GECODE_THREADS_OSX
66 
67 #include <libkern/OSAtomic.h>
68 
69 #endif
70 
71 #endif
72 
88 namespace Gecode { namespace Support {
89 
99  class Mutex {
100  private:
101 #ifdef GECODE_THREADS_WINDOWS
102  CRITICAL_SECTION w_cs;
104 #endif
105 #ifdef GECODE_THREADS_PTHREADS
106  pthread_mutex_t p_m;
108 #endif
109  public:
111  Mutex(void);
113  void acquire(void);
115  bool tryacquire(void);
117  void release(void);
119  ~Mutex(void);
121  static void* operator new(size_t s);
123  static void operator delete(void* p);
124  private:
126  Mutex(const Mutex&) {}
128  void operator=(const Mutex&) {}
129  };
130 
131 #if defined(GECODE_THREADS_WINDOWS) || !defined(GECODE_THREADS_PTHREADS)
132 
133  typedef Mutex FastMutex;
134 
135 #endif
136 
137 #ifdef GECODE_THREADS_PTHREADS
138 
139 #if defined(GECODE_THREADS_OSX) || defined(GECODE_THREADS_PTHREADS_SPINLOCK)
140 
155  class FastMutex {
156  private:
157 #ifdef GECODE_THREADS_OSX
158  OSSpinLock lck;
160 #else
161  pthread_spinlock_t p_s;
163 #endif
164  public:
166  FastMutex(void);
168  void acquire(void);
170  bool tryacquire(void);
172  void release(void);
174  ~FastMutex(void);
176  static void* operator new(size_t s);
178  static void operator delete(void* p);
179  private:
181  FastMutex(const FastMutex&) {}
183  void operator=(const FastMutex&) {}
184  };
185 
186 #else
187 
188  typedef Mutex FastMutex;
189 
190 #endif
191 
192 #endif
193 
199  class Lock {
200  private:
202  Mutex& m;
203  public:
205  Lock(Mutex& m0);
207  ~Lock(void);
208  private:
210  Lock(const Lock& l) : m(l.m) {}
212  void operator=(const Lock&) {}
213  };
214 
223  class Event {
224  private:
225 #ifdef GECODE_THREADS_WINDOWS
226  HANDLE w_h;
228 #endif
229 #ifdef GECODE_THREADS_PTHREADS
230  pthread_mutex_t p_m;
233  pthread_cond_t p_c;
235  bool p_s;
236 #endif
237  public:
239  Event(void);
241  void signal(void);
243  void wait(void);
245  ~Event(void);
246  private:
248  Event(const Event&) {}
250  void operator=(const Event&) {}
251  };
252 
258  class Runnable {
259  public:
261  virtual void run(void) = 0;
263  virtual ~Runnable(void) {}
265  static void* operator new(size_t s);
267  static void operator delete(void* p);
268  };
269 
279  class Thread {
280  public:
282  class Run {
283  public:
285  Run* n;
295  GECODE_SUPPORT_EXPORT void exec(void);
297  void run(Runnable* r);
299  static void* operator new(size_t s);
301  static void operator delete(void* p);
302  };
304  GECODE_SUPPORT_EXPORT static Mutex* m(void);
307  public:
317  static void run(Runnable* r);
319  static void sleep(unsigned int ms);
321  static unsigned int npu(void);
322  private:
324  Thread(const Thread&) {}
326  void operator=(const Thread&) {}
327  };
328 
329 }}
330 
331 // STATISTICS: support-any
NNF * l
Left subtree.
Definition: bool-expr.cpp:244
static unsigned int npu(void)
Return number of processing units (1 if information not available)
Definition: none.hpp:80
Mutex m
Mutex for synchronization.
Definition: thread.hpp:291
virtual ~Runnable(void)
Destructor.
Definition: thread.hpp:263
An interface for objects that can be run by a thread.
Definition: thread.hpp:258
Mutex(void)
Initialize mutex.
Definition: none.hpp:44
static void run(Runnable *r)
Construct a new thread and run r.
Definition: thread.hpp:109
void acquire(void)
Acquire the mutex and possibly block.
Definition: none.hpp:46
void signal(void)
Signal the event.
Definition: none.hpp:63
Runnable * r
Runnable object to execute.
Definition: thread.hpp:287
A mutex for mutual exclausion among several threads.
Definition: thread.hpp:99
void release(void)
Release the mutex.
Definition: none.hpp:52
void run(Runnable *r)
Run a runnable object.
Definition: thread.hpp:102
int p
Number of positive literals for node type.
Definition: bool-expr.cpp:236
An event for synchronization.
Definition: thread.hpp:223
A lock as a scoped frontend for a mutex.
Definition: thread.hpp:199
NNF * r
Right subtree.
Definition: bool-expr.cpp:246
virtual void run(void)=0
The function that is executed when the thread starts.
Lock(Mutex &m0)
Enter lock.
Definition: thread.hpp:89
static void sleep(unsigned int ms)
Put current thread to sleep for ms milliseconds.
Definition: none.hpp:78
void exec(void)
Infinite loop for execution.
Definition: thread.cpp:54
bool tryacquire(void)
Try to acquire the mutex, return true if succesful.
Definition: none.hpp:48
~Event(void)
Delete event.
Definition: none.hpp:67
#define GECODE_SUPPORT_EXPORT
Definition: support.hh:75
~Mutex(void)
Delete mutex.
Definition: none.hpp:54
static Run * idle
Idle runners.
Definition: thread.hpp:306
Simple threads.
Definition: thread.hpp:279
Event(void)
Initialize event.
Definition: none.hpp:61
Run * n
Next idle thread.
Definition: thread.hpp:285
Mutex FastMutex
Definition: thread.hpp:188
Event e
Event to wait for next runnable object to execute.
Definition: thread.hpp:289
static Mutex * m(void)
Mutex for synchronization.
Definition: thread.cpp:46
Gecode toplevel namespace
~Lock(void)
Leave lock.
Definition: thread.hpp:93
void wait(void)
Wait until the event becomes signalled.
Definition: none.hpp:65
Run(Runnable *r)
Create a new thread.
Definition: none.hpp:74