main page
modules
namespaces
classes
files
Gecode home
Generated on Sat Nov 9 2013 19:18:31 for Gecode by
doxygen
1.8.4
gecode
support
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: 2013-10-15 00:46:56 +0200 (Tue, 15 Oct 2013) $ by $Author: tack $
14
* $Revision: 14025 $
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
public
:
261
virtual
void
run
(
void
) = 0;
263
virtual
~Runnable
(
void
) {}
265
static
void
*
operator
new
(
size_t
s);
267
static
void
operator
delete
(
void
*
p
);
268
};
269
279
class
Thread
{
280
public
:
282
class
Run
{
283
public
:
285
Run
*
n
;
287
Runnable
*
r
;
289
Event
e
;
291
Mutex
m
;
293
GECODE_SUPPORT_EXPORT
Run
(
Runnable
*
r
);
295
GECODE_SUPPORT_EXPORT
void
exec
(
void
);
297
void
run
(
Runnable
*
r
);
299
static
void
*
operator
new
(
size_t
s);
301
static
void
operator
delete
(
void
*
p
);
302
};
304
GECODE_SUPPORT_EXPORT
static
Mutex
*
m
(
void
);
306
GECODE_SUPPORT_EXPORT
static
Run
*
idle
;
307
public
:
317
static
void
run
(
Runnable
*
r
);
319
static
void
sleep
(
unsigned
int
ms);
321
static
unsigned
int
npu
(
void
);
322
private
:
324
Thread
(
const
Thread
&) {}
326
void
operator=(
const
Thread
&) {}
327
};
328
329
}}
330
331
// STATISTICS: support-any