Generated on for Gecode by doxygen 1.17.0
worker-control.cpp
Go to the documentation of this file.
1/* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */
2/*
3 * Main author:
4 * Mikael Zayenz Lagerkvist <lagerkvist@gecode.dev>
5 *
6 * Copyright:
7 * Mikael Zayenz Lagerkvist, 2026
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#include <gecode/search.hh>
35
36namespace Gecode { namespace Search {
37
39 public:
45
46 std::atomic<unsigned int> requested;
47 std::atomic<unsigned int> capacity;
48 std::atomic<unsigned long long int> generation;
49 std::atomic<bool> fast_admit;
53
54 State(unsigned int workers)
55 : RefCount(1U), requested(workers), capacity(0U), generation(0U),
57 ~State(void) {
58 delete [] events;
59 }
60 };
61
63 : state(nullptr) {}
64
66 : state(new State(requested)) {}
67
69 : state(control.state) {
70 if (state != nullptr)
71 state->inc();
72 }
73
76 if (control.state != nullptr)
77 control.state->inc();
78 if ((state != nullptr) && state->dec())
79 delete state;
80 state = control.state;
81 return *this;
82 }
83
85 if ((state != nullptr) && state->dec())
86 delete state;
87 }
88
89 bool
90 WorkerControl::engaged(void) const noexcept {
91 return state != nullptr;
92 }
93
94 unsigned int
95 WorkerControl::requested(void) const noexcept {
96 return (state == nullptr) ? 0U :
97 state->requested.load(std::memory_order_acquire);
98 }
99
100 void
101 WorkerControl::request(unsigned int workers) {
102 if (state == nullptr)
103 throw UninitializedWorkerControl("WorkerControl::request");
104#ifndef GECODE_HAS_THREADS
105 if (workers == 0U)
106 throw InvalidWorkerRequest("WorkerControl::request");
107#endif
108 bool changed = false;
109 {
110 Support::Lock lock(state->mutex);
111 unsigned int capacity =
112 state->capacity.load(std::memory_order_relaxed);
113 if ((capacity != 0U) && (workers > capacity))
114 throw InvalidWorkerRequest("WorkerControl::request");
115 unsigned int old =
116 state->requested.load(std::memory_order_relaxed);
117 if (old != workers) {
118 state->fast_admit.store(false,std::memory_order_release);
119 state->requested.store(workers,std::memory_order_release);
120 (void) state->generation.fetch_add(1U,std::memory_order_release);
121 changed = true;
122 }
123 }
124 if (changed)
126 }
127
128 unsigned int
129 WorkerControl::capacity(void) const noexcept {
130 return (state == nullptr) ? 0U :
131 state->capacity.load(std::memory_order_acquire);
132 }
133
134 void
136 unsigned int capacity) {
137 if (control.state == nullptr)
138 return;
139 WorkerControl::State* state = control.state;
140 Support::Lock lock(state->mutex);
142 throw WorkerControlInUse("WorkerControlAccess::attach");
143 const unsigned int retained_capacity =
144 state->capacity.load(std::memory_order_relaxed);
145#ifndef GECODE_HAS_THREADS
146 if (state->requested.load(std::memory_order_relaxed) == 0U)
147 throw InvalidWorkerRequest("WorkerControlAccess::attach");
148#endif
149 if ((capacity == 0U) ||
150 ((retained_capacity != 0U) && (retained_capacity != capacity)) ||
151 (state->requested.load(std::memory_order_relaxed) > capacity))
152 throw InvalidWorkerRequest("WorkerControlAccess::attach");
153 if (state->events == nullptr)
154 state->events = new Support::Event[capacity];
155 state->capacity.store(capacity,std::memory_order_release);
156 state->fast_admit.store(
157 state->requested.load(std::memory_order_relaxed) == capacity,
158 std::memory_order_release);
160 }
161
162 void
164 if (control.state == nullptr)
165 return;
166 WorkerControl::State* state = control.state;
167 {
168 Support::Lock lock(state->mutex);
171 }
172 signal_all(control);
173 }
174
175 void
177 if (control.state == nullptr)
178 return;
179 WorkerControl::State* state = control.state;
180 Support::Lock lock(state->mutex);
182 throw WorkerControlInUse("WorkerControlAccess::prepare_reuse");
184 }
185
186 unsigned long long int
188 return (control.state == nullptr) ? 0U :
189 control.state->generation.load(std::memory_order_acquire);
190 }
191
192 const std::atomic<bool>*
194 return (control.state == nullptr) ? nullptr :
195 &control.state->fast_admit;
196 }
197
198 void
200 WorkerControl& control, unsigned long long int generation) {
201 if (control.state == nullptr)
202 return;
203 WorkerControl::State* state = control.state;
204 Support::Lock lock(state->mutex);
205 if ((state->generation.load(std::memory_order_relaxed) == generation) &&
206 (state->requested.load(std::memory_order_relaxed) ==
207 state->capacity.load(std::memory_order_relaxed)))
208 state->fast_admit.store(true,std::memory_order_release);
209 }
210
211 bool
213 const WorkerControl& y) {
214 return x.state == y.state;
215 }
216
217 unsigned int
219 return (control.state == nullptr) ? 0U :
220 control.state->requested.load(std::memory_order_acquire);
221 }
222
223 void
225 unsigned int& requested,
226 unsigned long long int& generation) {
227 WorkerControl::State* state = control.state;
228 if (state == nullptr) {
229 requested = 0U;
230 generation = 0U;
231 return;
232 }
233 Support::Lock lock(state->mutex);
234 requested = state->requested.load(std::memory_order_relaxed);
235 generation = state->generation.load(std::memory_order_relaxed);
236 }
237
238 void
239 WorkerControlAccess::wait(WorkerControl& control, unsigned int worker) {
240 WorkerControl::State* state = control.state;
241 if (state == nullptr)
242 return;
243 Support::Event* event = nullptr;
244 {
245 Support::Lock lock(state->mutex);
246 unsigned int capacity =
247 state->capacity.load(std::memory_order_relaxed);
248 assert((state->events == nullptr) || (worker < capacity));
249 if ((state->events != nullptr) && (worker < capacity))
250 event = &state->events[worker];
251 }
252 if (event != nullptr)
253 event->wait();
254 }
255
256 void
257 WorkerControlAccess::signal(WorkerControl& control, unsigned int worker) {
258 WorkerControl::State* state = control.state;
259 if (state == nullptr)
260 return;
261 Support::Event* event = nullptr;
262 {
263 Support::Lock lock(state->mutex);
264 unsigned int capacity =
265 state->capacity.load(std::memory_order_relaxed);
266 assert((state->events == nullptr) || (worker < capacity));
267 if ((state->events != nullptr) && (worker < capacity))
268 event = &state->events[worker];
269 }
270 if (event != nullptr)
271 event->signal();
272 }
273
274 void
276 WorkerControl::State* state = control.state;
277 if (state == nullptr)
278 return;
279 Support::Event* events;
280 unsigned int capacity;
281 {
282 Support::Lock lock(state->mutex);
283 events = state->events;
284 capacity = state->capacity.load(std::memory_order_relaxed);
285 }
286 if (events != nullptr)
287 for (unsigned int i=0U; i<capacity; i++)
288 events[i].signal();
289 }
290
291}}
292
293// STATISTICS: search-other
Exception: Invalid requested number of search workers
Definition exception.hpp:66
Exception: Request through an empty worker control
Definition exception.hpp:72
static void signal(WorkerControl &control, unsigned int worker)
Signal the event belonging to worker worker.
static void signal_all(WorkerControl &control)
Signal every worker event.
static bool same_identity(const WorkerControl &x, const WorkerControl &y)
Return whether x and y share one control identity.
static unsigned long long int generation(const WorkerControl &control)
Return the request generation.
static void prepare_reuse(WorkerControl &control)
Prepare control for another engine with the same capacity.
static void snapshot(const WorkerControl &control, unsigned int &requested, unsigned long long int &generation)
Return a consistent request and generation snapshot.
static void wait(WorkerControl &control, unsigned int worker)
Wait for the event belonging to worker worker.
static void detach(WorkerControl &control)
Detach control from its leaf engine.
static const std::atomic< bool > * fast_admission(const WorkerControl &control)
Return the unchanged-capacity fast-admission flag.
static void attach(WorkerControl &control, unsigned int capacity)
Attach control to a leaf engine with fixed capacity.
static unsigned int requested(const WorkerControl &control)
Return the current request.
Exception: Worker control is already bound or used in an unsupported engine
Definition exception.hpp:78
std::atomic< unsigned long long int > generation
std::atomic< unsigned int > requested
std::atomic< unsigned int > capacity
External control for the requested number of search workers.
Definition search.hh:773
WorkerControl & operator=(const WorkerControl &control)
Assignment operator.
WorkerControl(void) noexcept
Construct an empty handle.
bool engaged(void) const noexcept
Whether this handle is engaged.
unsigned int requested(void) const noexcept
Return the requested number of workers (zero also denotes an empty handle).
void request(unsigned int workers)
Request workers workers (zero pauses the engine).
unsigned int capacity(void) const noexcept
Return the fixed worker capacity, or zero before attachment.
An event for synchronization.
Definition thread.hpp:138
void wait(void)
Wait until the event becomes signalled.
Definition thread.hpp:143
void signal(void)
Signal the event.
Definition thread.hpp:133
A lock as a scoped frontend for a mutex.
Definition thread.hpp:114
A mutex for mutual exclausion among several threads.
Definition thread.hpp:78
A class for thread-safe reference counting.
Definition ref-count.hpp:39
RefCount(unsigned long int n)
Initialize.
Definition ref-count.hpp:55
const int capacity[n_warehouses]
Capacity of a single warehouse.
Search engines
Gecode toplevel namespace