ORIGINAL
Loading...
Searching...
No Matches
semaphores.h
Go to the documentation of this file.
1
25#ifndef ORIGINAL_SEMAPHORES_H
26#define ORIGINAL_SEMAPHORES_H
27#include "config.h"
28#include "error.h"
29#include "mutex.h"
30#include "condition.h"
31
32
33namespace original {
47 template<u_integer MAX_CNT = 1>
48 class semaphore {
49 u_integer count_;
50 mutex mutex_;
51 condition condition_;
52
53 public:
59
66
73 void acquire();
74
79 bool tryAcquire();
80
87
97
104
112
119 };
120
128 template<>
129 class semaphore<0> {
130 u_integer count_;
131 mutex mutex_;
132 condition condition_;
133
134 public:
138 semaphore();
139
145
150 void acquire();
151
156 bool tryAcquire();
157
163 bool acquireFor(const time::duration& timeout);
164
170 void release(u_integer increase = 1);
171 };
172
181 template<u_integer MAX_CNT>
183 semaphore<MAX_CNT>& semaphore_;
184
185 public:
192
198 };
199}
200
201template<original::u_integer MAX_CNT>
203
204template<original::u_integer MAX_CNT>
206 if (init_count > MAX_CNT) {
207 throw valueError("Init count is " + printable::formatString(init_count) +
208 ", that is larger than the max count " + printable::formatString(MAX_CNT));
209 }
210}
211
212template<original::u_integer MAX_CNT>
214 {
215 uniqueLock lock{this->mutex_};
216 this->condition_.wait(this->mutex_, [this]{
217 return this->count_ > 0;
218 });
219 this->count_ -= 1;
220 }
221 this->condition_.notify();
222}
223
224template<original::u_integer MAX_CNT>
226 {
227 uniqueLock lock{this->mutex_};
228 if (this->count_ == 0) {
229 return false;
230 }
231 this->count_ -= 1;
232 }
233 this->condition_.notify();
234 return true;
235}
236
237template<original::u_integer MAX_CNT>
239 bool success;
240 {
241 uniqueLock lock{this->mutex_};
242 success = this->condition_.waitFor(this->mutex_, timeout, [this]{
243 return this->count_ > 0;
244 });
245 if (success){
246 this->count_ -= 1;
247 }
248 }
249 if (success) {
250 this->condition_.notify();
251 }
252 return success;
253}
254
255template<original::u_integer MAX_CNT>
257 if (increase > MAX_CNT) {
258 throw valueError("Increase is larger than max count " + printable::formatString(MAX_CNT));
259 }
260 {
261 uniqueLock lock{this->mutex_};
262 this->condition_.wait(this->mutex_, [this, increase] {
263 return this->count_ + increase <= MAX_CNT;
264 });
265 this->count_ += increase;
266 }
267 this->condition_.notifySome(increase);
268}
269
270template<original::u_integer MAX_CNT>
272 {
273 uniqueLock lock{this->mutex_};
274 if (this->count_ + increase > MAX_CNT) {
275 return false;
276 }
277 this->count_ += increase;
278 }
279 this->condition_.notifySome(increase);
280 return true;
281}
282
283template<original::u_integer MAX_CNT>
285 bool success;
286 {
287 uniqueLock lock{this->mutex_};
288 success = this->condition_.waitFor(this->mutex_, timeout, [this, increase]{
289 return this->count_ + increase <= MAX_CNT;
290 });
291 if (success){
292 this->count_ += increase;
293 }
294 }
295 if (success){
296 this->condition_.notifySome(increase);
297 }
298 return success;
299}
300
301template<original::u_integer MAX_CNT>
303 return this->releaseFor(1, timeout);
304}
305
307
309
311 uniqueLock lock{this->mutex_};
312 this->condition_.wait(this->mutex_, [this]{
313 return this->count_ > 0;
314 });
315 this->count_ -= 1;
316}
317
319 uniqueLock lock{this->mutex_};
320 if (this->count_ == 0) {
321 return false;
322 }
323 this->count_ -= 1;
324 return true;
325}
326
328 uniqueLock lock{this->mutex_};
329 const bool success = this->condition_.waitFor(this->mutex_, timeout, [this]{
330 return this->count_ > 0;
331 });
332 if (success){
333 this->count_ -= 1;
334 }
335 return success;
336}
337
339 {
340 uniqueLock lock{this->mutex_};
341 this->count_ += increase;
342 }
343 this->condition_.notifySome(increase);
344}
345
346template<original::u_integer MAX_CNT>
350
351template<original::u_integer MAX_CNT>
353 this->semaphore_.release();
354}
355
356#endif //ORIGINAL_SEMAPHORES_H
Definition condition.h:209
Definition mutex.h:241
Unique ownership smart pointer with move semantics.
Definition ownerPtr.h:37
static std::string formatString(const TYPE &t)
Universal value-to-string conversion with type-specific formatting.
Definition printable.h:339
RAII wrapper for automatic semaphore management.
Definition semaphores.h:182
~semaphoreGuard()
Destructor that releases the semaphore.
Definition semaphores.h:352
semaphoreGuard(semaphore< MAX_CNT > &sem)
Constructs a guard and acquires the semaphore.
Definition semaphores.h:347
Counting semaphore with maximum count constraint.
Definition semaphores.h:48
void release(u_integer increase=1)
Releases resources (increments count)
Definition semaphores.h:256
void acquire()
Acquires one resource (decrements count)
Definition semaphores.h:213
bool acquireFor(time::duration timeout)
Attempts to acquire one resource with timeout.
Definition semaphores.h:238
semaphore(u_integer init_count)
Constructs a semaphore with specified initial count.
Definition semaphores.h:205
bool releaseFor(u_integer increase, time::duration timeout)
Attempts to release resources with timeout.
Definition semaphores.h:284
semaphore()
Constructs a semaphore with maximum count.
Definition semaphores.h:202
bool tryRelease(u_integer increase=1)
Attempts to release resources without blocking.
Definition semaphores.h:271
bool tryAcquire()
Attempts to acquire one resource without blocking.
Definition semaphores.h:225
bool releaseFor(const time::duration &timeout)
Attempts to release one resource with timeout.
Definition semaphores.h:302
Represents a time duration with nanosecond precision.
Definition zeit.h:143
RAII wrapper for single mutex locking.
Definition mutex.h:280
Exception for invalid parameter values.
Definition error.h:219
Condition variable implementation for thread synchronization.
Platform-independent type definitions and compiler/platform detection.
Custom exception classes and callback validation utilities.
Cross-platform mutex and lock management utilities.
Main namespace for the project Original.
Definition algorithms.h:21