Generated on Tue Jan 28 2020 00:00:00 for Gecode by doxygen 1.8.17
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: 2016-04-19 17:19:45 +0200 (Tue, 19 Apr 2016) $ by $Author: schulte $
14  * $Revision: 14967 $
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  private:
261  bool d;
262  public:
264  Runnable(bool d=true);
266  void todelete(bool d);
268  bool todelete(void) const;
270  virtual void run(void) = 0;
272  virtual ~Runnable(void) {}
274  static void* operator new(size_t s);
276  static void operator delete(void* p);
277  };
278 
288  class Thread {
289  public:
291  class Run {
292  public:
294  Run* n;
304  GECODE_SUPPORT_EXPORT void exec(void);
306  void run(Runnable* r);
308  static void* operator new(size_t s);
310  static void operator delete(void* p);
311  };
313  GECODE_SUPPORT_EXPORT static Mutex* m(void);
316  public:
326  static void run(Runnable* r);
328  static void sleep(unsigned int ms);
330  static unsigned int npu(void);
331  private:
333  Thread(const Thread&) {}
335  void operator=(const Thread&) {}
336  };
337 
338 }}
339 
340 // STATISTICS: support-any
bool todelete(void) const
Return whether to be deleted upon termination.
Definition: thread.hpp:51
void exec(void)
Infinite loop for execution.
Definition: thread.cpp:54
An event for synchronization.
Definition: thread.hpp:223
static Mutex * m(void)
Mutex for synchronization.
Definition: thread.cpp:46
Run * n
Next idle thread.
Definition: thread.hpp:294
void acquire(void)
Acquire the mutex and possibly block.
Definition: none.hpp:46
virtual ~Runnable(void)
Destructor.
Definition: thread.hpp:272
~Lock(void)
Leave lock.
Definition: thread.hpp:104
Mutex FastMutex
Definition: thread.hpp:188
Event e
Event to wait for next runnable object to execute.
Definition: thread.hpp:298
A real thread.
Definition: thread.hpp:291
Event(void)
Initialize event.
Definition: none.hpp:61
A lock as a scoped frontend for a mutex.
Definition: thread.hpp:199
Gecode toplevel namespace
Run(Runnable *r)
Create a new thread.
Definition: none.hpp:74
~Event(void)
Delete event.
Definition: none.hpp:67
Mutex(void)
Initialize mutex.
Definition: none.hpp:44
bool tryacquire(void)
Try to acquire the mutex, return true if succesful.
Definition: none.hpp:48
virtual void run(void)=0
The function that is executed when the thread starts.
static void run(Runnable *r)
Construct a new thread and run r.
Definition: thread.hpp:120
Post propagator for SetVar SetOpType SetVar SetRelType r
Definition: set.hh:784
Runnable(bool d=true)
Initialize, d defines whether object is deleted when terminated.
Definition: thread.hpp:44
Simple threads.
Definition: thread.hpp:288
~Mutex(void)
Delete mutex.
Definition: none.hpp:54
Mutex m
Mutex for synchronization.
Definition: thread.hpp:300
NNF * l
Left subtree.
Definition: bool-expr.cpp:244
void signal(void)
Signal the event.
Definition: none.hpp:63
static Run * idle
Idle runners.
Definition: thread.hpp:315
Runnable * r
Runnable object to execute.
Definition: thread.hpp:296
Lock(Mutex &m0)
Enter lock.
Definition: thread.hpp:100
static void sleep(unsigned int ms)
Put current thread to sleep for ms milliseconds.
Definition: none.hpp:78
#define GECODE_SUPPORT_EXPORT
Definition: support.hh:75
An interface for objects that can be run by a thread.
Definition: thread.hpp:258
void release(void)
Release the mutex.
Definition: none.hpp:52
A mutex for mutual exclausion among several threads.
Definition: thread.hpp:99
void wait(void)
Wait until the event becomes signalled.
Definition: none.hpp:65
void run(Runnable *r)
Run a runnable object.
Definition: thread.hpp:113
int p
Number of positive literals for node type.
Definition: bool-expr.cpp:236
static unsigned int npu(void)
Return number of processing units (1 if information not available)
Definition: none.hpp:80