Generated on Tue Jul 18 2017 18:41:42 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: 2015-09-27 19:00:38 +0200 (Sun, 27 Sep 2015) $ by $Author: schulte $
11  * $Revision: 14762 $
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  /*
65  * Mutexes
66  *
67  */
68  forceinline void*
69  Mutex::operator new(size_t s) {
70  return Gecode::heap.ralloc(s);
71  }
72 
73  forceinline void
74  Mutex::operator delete(void* p) {
76  }
77 
78 #if defined(GECODE_THREADS_OSX) || defined(GECODE_THREADS_PTHREADS_SPINLOCK)
79 
80  /*
81  * Fast mutexes
82  *
83  */
84  forceinline void*
85  FastMutex::operator new(size_t s) {
86  return Gecode::heap.ralloc(s);
87  }
88 
89  forceinline void
90  FastMutex::operator delete(void* p) {
92  }
93 
94 #endif
95 
96  /*
97  * Locks
98  */
100  Lock::Lock(Mutex& m0) : m(m0) {
101  m.acquire();
102  }
104  Lock::~Lock(void) {
105  m.release();
106  }
107 
108 
109  /*
110  * Threads
111  */
112  inline void
114  m.acquire();
115  r = r0;
116  m.release();
117  e.signal();
118  }
119  inline void
121  m()->acquire();
122  if (idle != NULL) {
123  Run* i = idle;
124  idle = idle->n;
125  m()->release();
126  i->run(r);
127  } else {
128  m()->release();
129  (void) new Run(r);
130  }
131  }
132  forceinline void
133  Thread::Run::operator delete(void* p) {
134  heap.rfree(p);
135  }
136  forceinline void*
137  Thread::Run::operator new(size_t s) {
138  return heap.ralloc(s);
139  }
140 
141 }}
142 
143 // 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:258
static void run(Runnable *r)
Construct a new thread and run r.
Definition: thread.hpp:120
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
A mutex for mutual exclausion among several threads.
Definition: thread.hpp:99
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:113
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:100
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:784
Heap heap
The single global heap.
Definition: heap.cpp:48
#define forceinline
Definition: config.hpp:173
int n
Number of elements.
Definition: array.hpp:527
Gecode toplevel namespace
~Lock(void)
Leave lock.
Definition: thread.hpp:104
bool todelete(void) const
Return whether to be deleted upon termination.
Definition: thread.hpp:51