Generated on for Gecode by doxygen 1.17.0
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.dev>
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.dev
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#include <atomic>
39
40#ifdef GECODE_HAS_THREADS
41#include <thread>
42#include <mutex>
43#include <condition_variable>
44
45#ifdef GECODE_USE_OSX_UNFAIR_MUTEX
46#include <os/lock.h>
47#include <libkern/OSAtomic.h>
48#endif
49
50#endif
51
66
67namespace Gecode { namespace Support {
68
78 class Mutex {
79 private:
80#ifdef GECODE_HAS_THREADS
81#ifndef GECODE_USE_OSX_UNFAIR_MUTEX
83 std::mutex m;
84#else
86 os_unfair_lock l;
87#endif
88#endif
89 public:
91 Mutex(void);
93 void acquire(void);
95 bool tryacquire(void);
97 void release(void);
99 static void* operator new(size_t s);
101 static void operator delete(void* p);
102 private:
104 Mutex(const Mutex&) {}
106 void operator=(const Mutex&) {}
107 };
108
114 class Lock {
115 private:
117 Mutex& m;
118 public:
120 Lock(Mutex& m0);
122 ~Lock(void);
123 private:
125 Lock(const Lock& l) : m(l.m) {}
127 void operator=(const Lock&) {}
128 };
129
138 class Event {
139 private:
140#ifdef GECODE_HAS_THREADS
142 std::mutex m;
144 std::condition_variable c;
145#endif
147 bool s;
148 public:
150 Event(void);
152 void signal(void);
154 void wait(void);
155 private:
157 Event(const Event&) {}
159 void operator=(const Event&) {}
160 };
161
169 public:
171 virtual ~Terminator() {}
173 virtual void terminated(void) = 0;
174 };
175
181 class Runnable {
182 private:
184 bool d;
185 public:
187 Runnable(bool d=true);
189 void todelete(bool d);
191 bool todelete(void) const;
193 virtual Terminator* terminator(void) const { return nullptr; }
195 virtual void run(void) = 0;
197 virtual ~Runnable(void) {}
199 static void* operator new(size_t s);
201 static void operator delete(void* p);
202 };
203
213 class Thread {
214 public:
216 class Run {
217 public:
221 std::atomic<Runnable*> r;
229 void run(Runnable* r);
231 static void* operator new(size_t s);
233 static void operator delete(void* p);
234 };
235
239 public:
249 static void run(Runnable* r);
251 static void sleep(unsigned int ms);
253 static unsigned int npu(void);
258 private:
260 Thread(const Thread&) {}
262 void operator=(const Thread&) {}
263 };
264
265}}
266
267// STATISTICS: support-any
An event for synchronization.
Definition thread.hpp:138
void wait(void)
Wait until the event becomes signalled.
Definition thread.hpp:143
Event(void)
Initialize event.
Definition thread.hpp:131
void signal(void)
Signal the event.
Definition thread.hpp:133
~Lock(void)
Leave lock.
Definition thread.hpp:123
Lock(Mutex &m0)
Enter lock.
Definition thread.hpp:119
A mutex for mutual exclausion among several threads.
Definition thread.hpp:78
Mutex(void)
Initialize mutex.
Definition thread.hpp:64
void release(void)
Release the mutex.
Definition thread.hpp:94
bool tryacquire(void)
Try to acquire the mutex, return true if successful.
Definition thread.hpp:81
void acquire(void)
Acquire the mutex and possibly block.
Definition thread.hpp:70
An interface for objects that can be run by a thread.
Definition thread.hpp:181
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 Terminator * terminator(void) const
Return terminator object.
Definition thread.hpp:193
virtual ~Runnable(void)
Destructor.
Definition thread.hpp:197
An interface for objects that can be called after a thread has terminated (after running the thread's...
Definition thread.hpp:168
virtual ~Terminator()
Destructor.
Definition thread.hpp:171
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:172
void exec(void)
Infinite loop for execution.
Event e
Event to wait for next runnable object to execute.
Definition thread.hpp:223
Run * n
Next idle thread.
Definition thread.hpp:219
std::atomic< Runnable * > r
Runnable object to execute.
Definition thread.hpp:221
Run(Runnable *r)
Create a new thread.
Simple threads.
Definition thread.hpp:213
static void releaseGlobalMutex(Mutex *m)
release globally acquired mutex m
static void run(Runnable *r)
Construct a new thread and run r.
Definition thread.hpp:177
static Mutex * m(void)
Mutex for synchronization.
static void sleep(unsigned int ms)
Put current thread to sleep for ms milliseconds.
Definition thread.hpp:156
static void acquireGlobalMutex(Mutex *m)
acquire mutex m globally and possibly lock
static Run * idle
Idle runners.
Definition thread.hpp:238
static unsigned int npu(void)
Return number of processing units (1 if information not available).
Definition thread.hpp:164
Support algorithms and datastructures
Gecode toplevel namespace
#define GECODE_SUPPORT_EXPORT
Definition support.hh:75