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 * This file is part of Gecode, the generic constraint
10 * development environment:
11 * http://www.gecode.dev
12 *
13 * Permission is hereby granted, free of charge, to any person obtaining
14 * a copy of this software and associated documentation files (the
15 * "Software"), to deal in the Software without restriction, including
16 * without limitation the rights to use, copy, modify, merge, publish,
17 * distribute, sublicense, and/or sell copies of the Software, and to
18 * permit persons to whom the Software is furnished to do so, subject to
19 * the following conditions:
20 *
21 * The above copyright notice and this permission notice shall be
22 * included in all copies or substantial portions of the Software.
23 *
24 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
28 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
29 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
30 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31 *
32 */
33
34namespace Gecode { namespace Support {
35
36 /*
37 * Runnable objects
38 */
39 forceinline
41 : d(d0) {}
42 forceinline void
44 d=d0;
45 }
46 forceinline bool
47 Runnable::todelete(void) const {
48 return d;
49 }
50 forceinline void
51 Runnable::operator delete(void* p) {
52 heap.rfree(p);
53 }
54 forceinline void*
55 Runnable::operator new(size_t s) {
56 return heap.ralloc(s);
57 }
58
59 /*
60 * Mutexes
61 *
62 */
63 forceinline
65#if defined(GECODE_HAS_THREADS) && defined(GECODE_USE_OSX_UNFAIR_MUTEX)
66 l = OS_UNFAIR_LOCK_INIT;
67#endif
68 }
69 forceinline void
71#ifdef GECODE_HAS_THREADS
72#ifndef GECODE_USE_OSX_UNFAIR_MUTEX
73 m.lock();
74#else
75 os_unfair_lock_lock(&l);
76#endif
77#endif
78 }
79
80 forceinline bool
82#ifdef GECODE_HAS_THREADS
83#ifndef GECODE_USE_OSX_UNFAIR_MUTEX
84 return m.try_lock();
85#else
86 return os_unfair_lock_trylock(&l);
87#endif
88#else
89 return true;
90#endif
91 }
92
93 forceinline void
95#ifdef GECODE_HAS_THREADS
96#ifndef GECODE_USE_OSX_UNFAIR_MUTEX
97 m.unlock();
98#else
99 os_unfair_lock_unlock(&l);
100#endif
101#endif
102 }
103
104 forceinline void*
105 Mutex::operator new(size_t s) {
106 return Gecode::heap.ralloc(s);
107 }
108
109 forceinline void
110 Mutex::operator delete(void* p) {
111 Gecode::heap.rfree(p);
112 }
113
114
115 /*
116 * Locks
117 */
118 forceinline
119 Lock::Lock(Mutex& m0) : m(m0) {
120 m.acquire();
121 }
122 forceinline
124 m.release();
125 }
126
127 /*
128 * Events
129 */
130 forceinline
131 Event::Event(void) : s(false) {}
132 forceinline void
134#ifdef GECODE_HAS_THREADS
135 std::unique_lock<std::mutex> l(m);
136 if (!s) {
137 s = true;
138 c.notify_one();
139 }
140#endif
141 }
142 forceinline void
144#ifdef GECODE_HAS_THREADS
145 std::unique_lock<std::mutex> l(m);
146 c.wait(l, [this]{return this->s;});
147 s = false;
148#endif
149 }
150
151
152 /*
153 * Threads
154 */
155 forceinline void
156 Thread::sleep(unsigned int ms) {
157#ifdef GECODE_HAS_THREADS
158 std::this_thread::sleep_for(std::chrono::milliseconds(ms));
159#else
160 (void) ms;
161#endif
162 }
163 forceinline unsigned int
165#ifdef GECODE_HAS_THREADS
166 return std::thread::hardware_concurrency();
167#else
168 return 1;
169#endif
170 }
171 inline void
173 r.store(r0);
174 e.signal();
175 }
176 inline void
178 m()->acquire();
179 if (idle != nullptr) {
180 Run* i = idle;
181 idle = idle->n;
182 m()->release();
183 i->run(r);
184 } else {
185 m()->release();
186 (void) new Run(r);
187 }
188 }
189 forceinline void
190 Thread::Run::operator delete(void* p) {
191 heap.rfree(p);
192 }
193 forceinline void*
194 Thread::Run::operator new(size_t s) {
195 return heap.ralloc(s);
196 }
197
198}}
199
200// STATISTICS: support-any
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
void run(Runnable *r)
Run a runnable object.
Definition thread.hpp:172
Event e
Event to wait for next runnable object to execute.
Definition thread.hpp:223
std::atomic< Runnable * > r
Runnable object to execute.
Definition thread.hpp:221
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 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
Heap heap
The single global heap.
Support algorithms and datastructures
Gecode toplevel namespace