Generated on Thu Apr 5 2018 19:44:19 for Gecode by doxygen 1.8.13
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$ by $Author$
14  * $Revision$
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 #ifdef GECODE_THREADS_OSX_UNFAIR
72 
73 #include <os/lock.h>
74 
75 #endif
76 
77 #endif
78 
94 namespace Gecode { namespace Support {
95 
105  class Mutex {
106  private:
107 #if defined(GECODE_THREADS_WINDOWS)
108  CRITICAL_SECTION w_cs;
110 #elif defined(GECODE_THREADS_OSX_UNFAIR)
111  os_unfair_lock lck;
113 #elif defined(GECODE_THREADS_PTHREADS)
114  pthread_mutex_t p_m;
116 #else
117 #error No suitable mutex implementation found
118 #endif
119  public:
121  Mutex(void);
123  void acquire(void);
125  bool tryacquire(void);
127  void release(void);
129  ~Mutex(void);
131  static void* operator new(size_t s);
133  static void operator delete(void* p);
134  private:
136  Mutex(const Mutex&) {}
138  void operator=(const Mutex&) {}
139  };
140 
141 #ifndef GECODE_THREADS_PTHREADS_SPINLOCK
142 
143  typedef Mutex FastMutex;
144 
145 #else
146 
161  class FastMutex {
162  private:
164  pthread_spinlock_t p_s;
165  public:
167  FastMutex(void);
169  void acquire(void);
171  bool tryacquire(void);
173  void release(void);
175  ~FastMutex(void);
177  static void* operator new(size_t s);
179  static void operator delete(void* p);
180  private:
182  FastMutex(const FastMutex&) {}
184  void operator=(const FastMutex&) {}
185  };
186 
187 #endif
188 
194  class Lock {
195  private:
197  Mutex& m;
198  public:
200  Lock(Mutex& m0);
202  ~Lock(void);
203  private:
205  Lock(const Lock& l) : m(l.m) {}
207  void operator=(const Lock&) {}
208  };
209 
218  class Event {
219  private:
220 #ifdef GECODE_THREADS_WINDOWS
221  HANDLE w_h;
223 #endif
224 #ifdef GECODE_THREADS_PTHREADS
225  pthread_mutex_t p_m;
228  pthread_cond_t p_c;
230  bool p_s;
231 #endif
232  public:
234  Event(void);
236  void signal(void);
238  void wait(void);
240  ~Event(void);
241  private:
243  Event(const Event&) {}
245  void operator=(const Event&) {}
246  };
247 
254  class Terminator {
255  public:
257  virtual ~Terminator() {}
259  virtual void terminated(void) = 0;
260  };
261 
267  class Runnable {
268  private:
270  bool d;
271  public:
273  Runnable(bool d=true);
275  void todelete(bool d);
277  bool todelete(void) const;
279  virtual Terminator* terminator(void) const { return NULL; }
281  virtual void run(void) = 0;
283  virtual ~Runnable(void) {}
285  static void* operator new(size_t s);
287  static void operator delete(void* p);
288  };
289 
299  class Thread {
300  public:
302  class Run {
303  public:
305  Run* n;
315  GECODE_SUPPORT_EXPORT void exec(void);
317  void run(Runnable* r);
319  static void* operator new(size_t s);
321  static void operator delete(void* p);
322  };
324  GECODE_SUPPORT_EXPORT static Mutex* m(void);
327  public:
337  static void run(Runnable* r);
339  static void sleep(unsigned int ms);
341  static unsigned int npu(void);
342  private:
344  Thread(const Thread&) {}
346  void operator=(const Thread&) {}
347  };
348 
349 }}
350 
351 // STATISTICS: support-any
NNF * l
Left subtree.
Definition: bool-expr.cpp:244
Mutex m
Mutex for synchronization.
Definition: thread.hpp:311
virtual ~Runnable(void)
Destructor.
Definition: thread.hpp:283
An interface for objects that can be run by a thread.
Definition: thread.hpp:267
Mutex(void)
Initialize mutex.
Definition: none.hpp:44
void acquire(void)
Acquire the mutex and possibly block.
Definition: none.hpp:46
Runnable * r
Runnable object to execute.
Definition: thread.hpp:307
virtual ~Terminator()
Destructor.
Definition: thread.hpp:257
A mutex for mutual exclausion among several threads.
Definition: thread.hpp:105
Gecode::IntSet d(v, 7)
void release(void)
Release the mutex.
Definition: none.hpp:52
int p
Number of positive literals for node type.
Definition: bool-expr.cpp:236
An event for synchronization.
Definition: thread.hpp:218
A lock as a scoped frontend for a mutex.
Definition: thread.hpp:194
virtual Terminator * terminator(void) const
Return terminator object.
Definition: thread.hpp:279
void wait(Home home, FloatVar x, std::function< void(Space &home)> c)
Execute c when x becomes assigned.
Definition: exec.cpp:43
bool tryacquire(void)
Try to acquire the mutex, return true if succesful.
Definition: none.hpp:48
#define GECODE_SUPPORT_EXPORT
Definition: support.hh:75
~Mutex(void)
Delete mutex.
Definition: none.hpp:54
static Run * idle
Idle runners.
Definition: thread.hpp:326
Simple threads.
Definition: thread.hpp:299
Post propagator for SetVar SetOpType SetVar SetRelType r
Definition: set.hh:769
Run * n
Next idle thread.
Definition: thread.hpp:305
Mutex FastMutex
Definition: thread.hpp:143
Event e
Event to wait for next runnable object to execute.
Definition: thread.hpp:309
An interface for objects that can be called after a thread has terminated (after running the thread&#39;s...
Definition: thread.hpp:254
Gecode toplevel namespace