Generated on Thu Jan 16 2025 00:00:00 for Gecode by doxygen 1.14.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.org>
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.org
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 */
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 void*
64 Mutex::operator new(size_t s) {
65 return Gecode::heap.ralloc(s);
66 }
67
68 forceinline void
69 Mutex::operator delete(void* p) {
70 Gecode::heap.rfree(p);
71 }
72
73#if ! ( defined(GECODE_THREADS_WINDOWS) || defined(GECODE_THREADS_OSX_UNFAIR) || !defined(GECODE_THREADS_PTHREADS_SPINLOCK) )
74
75 /*
76 * Fast mutexes
77 *
78 */
79 forceinline void*
80 FastMutex::operator new(size_t s) {
81 return Gecode::heap.ralloc(s);
82 }
83
84 forceinline void
85 FastMutex::operator delete(void* p) {
86 Gecode::heap.rfree(p);
87 }
88
89#endif
90
91 /*
92 * Locks
93 */
95 Lock::Lock(Mutex& m0) : m(m0) {
96 m.acquire();
97 }
100 m.release();
101 }
102
103
104 /*
105 * Threads
106 */
107 inline void
109 m.acquire();
110 r = r0;
111 m.release();
112 e.signal();
113 }
114 inline void
116 m()->acquire();
117 if (idle != NULL) {
118 Run* i = idle;
119 idle = idle->n;
120 m()->release();
121 i->run(r);
122 } else {
123 m()->release();
124 (void) new Run(r);
125 }
126 }
127 forceinline void
128 Thread::Run::operator delete(void* p) {
129 heap.rfree(p);
130 }
131 forceinline void*
132 Thread::Run::operator new(size_t s) {
133 return heap.ralloc(s);
134 }
135
136}}
137
138// STATISTICS: support-any
~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
void release(void)
Release the mutex.
Definition none.hpp:48
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
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:108
Event e
Event to wait for next runnable object to execute.
Definition thread.hpp:306
Runnable * r
Runnable object to execute.
Definition thread.hpp:304
Mutex m
Mutex for synchronization.
Definition thread.hpp:308
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 Mutex * m(void)
Mutex for synchronization.
Definition thread.cpp:42
Heap heap
The single global heap.
Definition heap.cpp:44
Support algorithms and datastructures
Gecode toplevel namespace
Post propagator for SetVar SetOpType SetVar SetRelType r
Definition set.hh:773
#define forceinline
Definition config.hpp:194