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  * Last modified:
10  * $Date$ by $Author$
11  * $Revision$
12  *
13  * This file is part of Gecode, the generic constraint
14  * development environment:
15  * http://www.gecode.org
16  *
17  * Permission is hereby granted, free of charge, to any person obtaining
18  * a copy of this software and associated documentation files (the
19  * "Software"), to deal in the Software without restriction, including
20  * without limitation the rights to use, copy, modify, merge, publish,
21  * distribute, sublicense, and/or sell copies of the Software, and to
22  * permit persons to whom the Software is furnished to do so, subject to
23  * the following conditions:
24  *
25  * The above copyright notice and this permission notice shall be
26  * included in all copies or substantial portions of the Software.
27  *
28  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
29  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
30  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
31  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
32  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
33  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
34  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
35  *
36  */
37 
38 namespace Gecode { namespace Support {
39 
40  /*
41  * Runnable objects
42  */
45  : d(d0) {}
46  forceinline void
47  Runnable::todelete(bool d0) {
48  d=d0;
49  }
50  forceinline bool
51  Runnable::todelete(void) const {
52  return d;
53  }
54  forceinline void
55  Runnable::operator delete(void* p) {
56  heap.rfree(p);
57  }
58  forceinline void*
59  Runnable::operator new(size_t s) {
60  return heap.ralloc(s);
61  }
62 
63  /*
64  * Mutexes
65  *
66  */
67  forceinline void*
68  Mutex::operator new(size_t s) {
69  return Gecode::heap.ralloc(s);
70  }
71 
72  forceinline void
73  Mutex::operator delete(void* p) {
75  }
76 
77 #if ! ( defined(GECODE_THREADS_WINDOWS) || defined(GECODE_THREADS_OSX_UNFAIR) || !defined(GECODE_THREADS_PTHREADS_SPINLOCK) )
78 
79  /*
80  * Fast mutexes
81  *
82  */
83  forceinline void*
84  FastMutex::operator new(size_t s) {
85  return Gecode::heap.ralloc(s);
86  }
87 
88  forceinline void
89  FastMutex::operator delete(void* p) {
91  }
92 
93 #endif
94 
95  /*
96  * Locks
97  */
99  Lock::Lock(Mutex& m0) : m(m0) {
100  m.acquire();
101  }
103  Lock::~Lock(void) {
104  m.release();
105  }
106 
107 
108  /*
109  * Threads
110  */
111  inline void
113  m.acquire();
114  r = r0;
115  m.release();
116  e.signal();
117  }
118  inline void
120  m()->acquire();
121  if (idle != NULL) {
122  Run* i = idle;
123  idle = idle->n;
124  m()->release();
125  i->run(r);
126  } else {
127  m()->release();
128  (void) new Run(r);
129  }
130  }
131  forceinline void
132  Thread::Run::operator delete(void* p) {
133  heap.rfree(p);
134  }
135  forceinline void*
136  Thread::Run::operator new(size_t s) {
137  return heap.ralloc(s);
138  }
139 
140 }}
141 
142 // STATISTICS: support-any
void rfree(void *p)
Free memory block starting at p.
Definition: heap.hpp:375
An interface for objects that can be run by a thread.
Definition: thread.hpp:267
static void run(Runnable *r)
Construct a new thread and run r.
Definition: thread.hpp:119
void acquire(void)
Acquire the mutex and possibly block.
Definition: none.hpp:46
void * ralloc(size_t s)
Allocate s bytes from heap.
Definition: heap.hpp:361
#define forceinline
Definition: config.hpp:182
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
void run(Runnable *r)
Run a runnable object.
Definition: thread.hpp:112
int p
Number of positive literals for node type.
Definition: bool-expr.cpp:236
Gecode::IntArgs i(4, 1, 2, 3, 4)
Lock(Mutex &m0)
Enter lock.
Definition: thread.hpp:99
Runnable(bool d=true)
Initialize, d defines whether object is deleted when terminated.
Definition: thread.hpp:44
Post propagator for SetVar SetOpType SetVar SetRelType r
Definition: set.hh:769
Heap heap
The single global heap.
Definition: heap.cpp:48
int n
Number of elements.
Definition: array.hpp:515
Gecode toplevel namespace
~Lock(void)
Leave lock.
Definition: thread.hpp:103
bool todelete(void) const
Return whether to be deleted upon termination.
Definition: thread.hpp:51