Generated on Sat Apr 10 2021 00:00:00 for Gecode by doxygen 1.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  * This file is part of Gecode, the generic constraint
13  * development environment:
14  * http://www.gecode.org
15  *
16  * Permission is hereby granted, free of charge, to any person obtaining
17  * a copy of this software and associated documentation files (the
18  * "Software"), to deal in the Software without restriction, including
19  * without limitation the rights to use, copy, modify, merge, publish,
20  * distribute, sublicense, and/or sell copies of the Software, and to
21  * permit persons to whom the Software is furnished to do so, subject to
22  * the following conditions:
23  *
24  * The above copyright notice and this permission notice shall be
25  * included in all copies or substantial portions of the Software.
26  *
27  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
28  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
29  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
30  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
31  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
32  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
33  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
34  *
35  */
36 
37 #include <cstddef>
38 
39 #ifdef GECODE_THREADS_WINDOWS
40 
41 #ifndef NOMINMAX
42 # define NOMINMAX
43 #endif
44 
45 #ifndef _WIN32_WINNT
46 # define _WIN32_WINNT 0x400
47 #endif
48 
49 #ifndef WIN32_LEAN_AND_MEAN
50 # define WIN32_LEAN_AND_MEAN
51 #endif
52 
53 #include <windows.h>
54 
55 #endif
56 
57 #ifdef GECODE_THREADS_PTHREADS
58 
59 #include <pthread.h>
60 
61 #ifdef GECODE_THREADS_OSX_UNFAIR
62 
63 #include <os/lock.h>
64 #include <libkern/OSAtomic.h>
65 
66 #endif
67 
68 #endif
69 
85 namespace Gecode { namespace Support {
86 
96  class Mutex {
97  private:
98 #if defined(GECODE_THREADS_WINDOWS)
100  CRITICAL_SECTION w_cs;
101 #elif defined(GECODE_THREADS_OSX_UNFAIR)
103  union {
104  os_unfair_lock unfair_lck;
105 #pragma clang diagnostic push
106 #pragma clang diagnostic ignored "-Wdeprecated-declarations"
107  OSSpinLock spin_lck;
108 #pragma clang diagnostic pop
109  } u;
110 #elif defined(GECODE_THREADS_PTHREADS)
112  pthread_mutex_t p_m;
113 #else
114 #error No suitable mutex implementation found
115 #endif
116  public:
118  Mutex(void);
120  void acquire(void);
122  bool tryacquire(void);
124  void release(void);
126  ~Mutex(void);
128  static void* operator new(size_t s);
130  static void operator delete(void* p);
131  private:
133  Mutex(const Mutex&) {}
135  void operator=(const Mutex&) {}
136  };
137 
138 #ifndef GECODE_THREADS_PTHREADS_SPINLOCK
139 
140  typedef Mutex FastMutex;
141 
142 #else
143 
158  class FastMutex {
159  private:
161  pthread_spinlock_t p_s;
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 #endif
185 
191  class Lock {
192  private:
194  Mutex& m;
195  public:
197  Lock(Mutex& m0);
199  ~Lock(void);
200  private:
202  Lock(const Lock& l) : m(l.m) {}
204  void operator=(const Lock&) {}
205  };
206 
215  class Event {
216  private:
217 #ifdef GECODE_THREADS_WINDOWS
219  HANDLE w_h;
220 #endif
221 #ifdef GECODE_THREADS_PTHREADS
223  pthread_mutex_t p_m;
225  pthread_cond_t p_c;
227  bool p_s;
228 #endif
229  public:
231  Event(void);
233  void signal(void);
235  void wait(void);
237  ~Event(void);
238  private:
240  Event(const Event&) {}
242  void operator=(const Event&) {}
243  };
244 
251  class Terminator {
252  public:
254  virtual ~Terminator() {}
256  virtual void terminated(void) = 0;
257  };
258 
264  class Runnable {
265  private:
267  bool d;
268  public:
270  Runnable(bool d=true);
272  void todelete(bool d);
274  bool todelete(void) const;
276  virtual Terminator* terminator(void) const { return NULL; }
278  virtual void run(void) = 0;
280  virtual ~Runnable(void) {}
282  static void* operator new(size_t s);
284  static void operator delete(void* p);
285  };
286 
296  class Thread {
297  public:
299  class Run {
300  public:
302  Run* n;
312  GECODE_SUPPORT_EXPORT void exec(void);
314  void run(Runnable* r);
316  static void* operator new(size_t s);
318  static void operator delete(void* p);
319  };
321  GECODE_SUPPORT_EXPORT static Mutex* m(void);
324  public:
334  static void run(Runnable* r);
336  static void sleep(unsigned int ms);
338  static unsigned int npu(void);
339  private:
341  Thread(const Thread&) {}
343  void operator=(const Thread&) {}
344  };
345 
346 }}
347 
348 // STATISTICS: support-any
NNF * l
Left subtree.
Definition: bool-expr.cpp:240
int p
Number of positive literals for node type.
Definition: bool-expr.cpp:232
union Gecode::@602::NNF::@65 u
Union depending on nodetype t.
NNF * r
Right subtree.
Definition: bool-expr.cpp:242
An event for synchronization.
Definition: thread.hpp:215
void wait(void)
Wait until the event becomes signalled.
Definition: none.hpp:61
Event(void)
Initialize event.
Definition: none.hpp:57
~Event(void)
Delete event.
Definition: none.hpp:63
void signal(void)
Signal the event.
Definition: none.hpp:59
A lock as a scoped frontend for a mutex.
Definition: thread.hpp:191
~Lock(void)
Leave lock.
Definition: thread.hpp:99
Lock(Mutex &m0)
Enter lock.
Definition: thread.hpp:95
A mutex for mutual exclausion among several threads.
Definition: thread.hpp:96
Mutex(void)
Initialize mutex.
Definition: none.hpp:40
~Mutex(void)
Delete mutex.
Definition: none.hpp:50
void release(void)
Release the mutex.
Definition: none.hpp:48
bool tryacquire(void)
Try to acquire the mutex, return true if succesful.
Definition: none.hpp:44
void acquire(void)
Acquire the mutex and possibly block.
Definition: none.hpp:42
An interface for objects that can be run by a thread.
Definition: thread.hpp:264
virtual Terminator * terminator(void) const
Return terminator object.
Definition: thread.hpp:276
Runnable(bool d=true)
Initialize, d defines whether object is deleted when terminated.
Definition: thread.hpp:40
bool todelete(void) const
Return whether to be deleted upon termination.
Definition: thread.hpp:47
virtual void run(void)=0
The function that is executed when the thread starts.
virtual ~Runnable(void)
Destructor.
Definition: thread.hpp:280
An interface for objects that can be called after a thread has terminated (after running the thread's...
Definition: thread.hpp:251
virtual ~Terminator()
Destructor.
Definition: thread.hpp:254
virtual void terminated(void)=0
The function that is called when the thread has terminated.
void run(Runnable *r)
Run a runnable object.
Definition: thread.hpp:108
void exec(void)
Infinite loop for execution.
Definition: thread.cpp:50
Event e
Event to wait for next runnable object to execute.
Definition: thread.hpp:306
Run * n
Next idle thread.
Definition: thread.hpp:302
Runnable * r
Runnable object to execute.
Definition: thread.hpp:304
Run(Runnable *r)
Create a new thread.
Definition: none.hpp:70
Mutex m
Mutex for synchronization.
Definition: thread.hpp:308
Simple threads.
Definition: thread.hpp:296
static Run * idle
Idle runners.
Definition: thread.hpp:323
static void run(Runnable *r)
Construct a new thread and run r.
Definition: thread.hpp:115
static void sleep(unsigned int ms)
Put current thread to sleep for ms milliseconds.
Definition: none.hpp:74
static Mutex * m(void)
Mutex for synchronization.
Definition: thread.cpp:42
static unsigned int npu(void)
Return number of processing units (1 if information not available)
Definition: none.hpp:76
Mutex FastMutex
Definition: thread.hpp:140
#define GECODE_SUPPORT_EXPORT
Definition: support.hh:71