Aleph-w 7.10.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
concurrency_utils.H
Go to the documentation of this file.
1/*
2 Aleph_w
3
4 Data structures & Algorithms
5 version 2.0.0b
6 https://github.com/lrleon/Aleph-w
7
8 This file is part of Aleph-w library
9
10 Copyright (c) 2002-2026 Leandro Rabindranath Leon
11
12 Permission is hereby granted, free of charge, to any person obtaining a copy
13 of this software and associated documentation files (the "Software"), to deal
14 in the Software without restriction, including without limitation the rights
15 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
16 copies of the Software, and to permit persons to whom the Software is
17 furnished to do so, subject to the following conditions:
18
19 The above copyright notice and this permission notice shall be included in all
20 copies or substantial portions of the Software.
21
22 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
25 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28 SOFTWARE.
29*/
30
85#ifndef ALEPH_CONCURRENCY_UTILS_H
86#define ALEPH_CONCURRENCY_UTILS_H
87
88#include <atomic>
89#include <cassert>
90#include <condition_variable>
91#include <concepts>
92#include <cstddef>
93#include <deque>
94#include <functional>
95#include <mutex>
96#include <optional>
97#include <shared_mutex>
98#include <stdexcept>
99#include <type_traits>
100#include <utility>
101#include <vector>
102
103#include <thread_pool.H>
104
105namespace Aleph
106{
145 template <typename T>
147 {
148 mutable std::mutex mutex_;
149 std::condition_variable not_empty_;
150 std::condition_variable not_full_;
151 std::deque<T> queue_;
152 size_t capacity_;
153 bool closed_ = false;
154
174 template <typename Predicate>
175 void wait_with_cancellation(std::condition_variable & cv,
176 std::unique_lock<std::mutex> & lock,
177 Predicate pred,
178 const CancellationToken & token,
180 {
181 if (pred())
182 return;
184 registration = token.notify_on_cancel(cv, mutex_);
185 cv.wait(lock, [&] { return pred() or token.stop_requested(); });
186 if (not pred())
188 }
189
196 template <typename U>
197 bool send_impl(U && value)
198 {
199 std::unique_lock<std::mutex> lock(mutex_);
200 not_full_.wait(lock, [&] { return closed_ or queue_.size() < capacity_; });
201 if (closed_)
202 return false;
203 queue_.push_back(std::forward<U>(value));
204 lock.unlock();
205 not_empty_.notify_one();
206 return true;
207 }
208
217 template <typename U>
218 bool send_impl(U && value, const CancellationToken & token)
219 {
221 std::unique_lock<std::mutex> lock(mutex_);
223 [&] { return closed_ or queue_.size() < capacity_; },
224 token, registration);
225 if (closed_)
226 return false;
227 queue_.push_back(std::forward<U>(value));
228 lock.unlock();
229 not_empty_.notify_one();
230 return true;
231 }
232
233 public:
240 explicit BoundedChannel(size_t capacity)
242 {
243 if (capacity_ == 0)
244 throw std::invalid_argument("BoundedChannel capacity must be greater than zero");
245 }
246
251
257 [[nodiscard]] size_t capacity() const noexcept { return capacity_; }
258
265 [[nodiscard]] bool is_closed() const
266 {
267 std::lock_guard<std::mutex> lock(mutex_);
268 return closed_;
269 }
270
277 [[nodiscard]] size_t size() const
278 {
279 std::lock_guard<std::mutex> lock(mutex_);
280 return queue_.size();
281 }
282
289 [[nodiscard]] bool empty() const
290 {
291 std::lock_guard<std::mutex> lock(mutex_);
292 return queue_.empty();
293 }
294
309 void close()
310 {
311 std::lock_guard<std::mutex> lock(mutex_);
312 if (closed_)
313 return;
314 closed_ = true;
315 not_empty_.notify_all();
316 not_full_.notify_all();
317 }
318
331 bool send(const T & value) { return send_impl(value); }
332
345 bool send(T && value) { return send_impl(std::move(value)); }
346
362 bool send(const T & value, const CancellationToken & token)
363 {
364 return send_impl(value, token);
365 }
366
382 bool send(T && value, const CancellationToken & token)
383 {
384 return send_impl(std::move(value), token);
385 }
386
398 template <typename... Args>
399 requires (sizeof...(Args) == 0)
400 bool emplace(Args && ... args)
401 {
402 std::unique_lock<std::mutex> lock(mutex_);
403 not_full_.wait(lock, [&] { return closed_ or queue_.size() < capacity_; });
404 if (closed_)
405 return false;
406 queue_.emplace_back(std::forward<Args>(args)...);
407 lock.unlock();
408 not_empty_.notify_one();
409 return true;
410 }
411
428 template <typename First, typename... Rest>
429 requires std::constructible_from<T, First, Rest...>
430 bool emplace(First && first, Rest && ... rest)
431 {
432 std::unique_lock<std::mutex> lock(mutex_);
433 not_full_.wait(lock, [&] { return closed_ or queue_.size() < capacity_; });
434 if (closed_)
435 return false;
436 queue_.emplace_back(std::forward<First>(first),
437 std::forward<Rest>(rest)...);
438 lock.unlock();
439 not_empty_.notify_one();
440 return true;
441 }
442
460 template <typename First, typename... Args>
461 requires std::same_as<std::remove_cvref_t<First>, CancellationToken> &&
462 (not std::constructible_from<T, First, Args...>) &&
463 std::constructible_from<T, Args...>
464 bool emplace(First && token, Args && ... args)
465 {
467 std::unique_lock<std::mutex> lock(mutex_);
469 [&] { return closed_ or queue_.size() < capacity_; },
470 token, registration);
471 if (closed_)
472 return false;
473 queue_.emplace_back(std::forward<Args>(args)...);
474 lock.unlock();
475 not_empty_.notify_one();
476 return true;
477 }
478
488 bool try_send(const T & value)
489 {
490 std::lock_guard<std::mutex> lock(mutex_);
491 if (closed_ or queue_.size() >= capacity_)
492 return false;
493 queue_.push_back(value);
494 not_empty_.notify_one();
495 return true;
496 }
497
507 bool try_send(T && value)
508 {
509 std::lock_guard<std::mutex> lock(mutex_);
510 if (closed_ or queue_.size() >= capacity_)
511 return false;
512 queue_.push_back(std::move(value));
513 not_empty_.notify_one();
514 return true;
515 }
516
530 bool recv(T & out)
531 {
532 std::unique_lock<std::mutex> lock(mutex_);
533 not_empty_.wait(lock, [&] { return closed_ or not queue_.empty(); });
534 if (queue_.empty())
535 return false;
536 T value(std::move_if_noexcept(queue_.front()));
537 out = std::move(value);
538 queue_.pop_front();
539 lock.unlock();
540 not_full_.notify_one();
541 return true;
542 }
543
559 bool recv(T & out, const CancellationToken & token)
560 {
562 std::unique_lock<std::mutex> lock(mutex_);
564 [&] { return closed_ or not queue_.empty(); },
565 token, registration);
566 if (queue_.empty())
567 return false;
568 T value(std::move_if_noexcept(queue_.front()));
569 out = std::move(value);
570 queue_.pop_front();
571 lock.unlock();
572 not_full_.notify_one();
573 return true;
574 }
575
588 [[nodiscard]] std::optional<T> recv()
589 {
590 std::unique_lock<std::mutex> lock(mutex_);
591 not_empty_.wait(lock, [&] { return closed_ or not queue_.empty(); });
592 if (queue_.empty())
593 return std::nullopt;
594 T value(std::move_if_noexcept(queue_.front()));
595 std::optional<T> result;
596 if constexpr (std::is_nothrow_move_constructible_v<T> or
597 not std::is_copy_constructible_v<T>)
598 result.emplace(std::move(value));
599 else
600 result.emplace(value);
601 queue_.pop_front();
602 lock.unlock();
603 not_full_.notify_one();
604 if constexpr (std::is_nothrow_move_constructible_v<T> or
605 not std::is_copy_constructible_v<T>)
606 return result;
607 else
608 return static_cast<const std::optional<T> &>(result);
609 }
610
626 [[nodiscard]] std::optional<T> recv(const CancellationToken & token)
627 {
629 std::unique_lock<std::mutex> lock(mutex_);
631 [&] { return closed_ or not queue_.empty(); },
632 token, registration);
633 if (queue_.empty())
634 return std::nullopt;
635 T value(std::move_if_noexcept(queue_.front()));
636 std::optional<T> result;
637 if constexpr (std::is_nothrow_move_constructible_v<T> or
638 not std::is_copy_constructible_v<T>)
639 result.emplace(std::move(value));
640 else
641 result.emplace(value);
642 queue_.pop_front();
643 lock.unlock();
644 not_full_.notify_one();
645 if constexpr (std::is_nothrow_move_constructible_v<T> or
646 not std::is_copy_constructible_v<T>)
647 return result;
648 else
649 return static_cast<const std::optional<T> &>(result);
650 }
651
663 bool try_recv(T & out)
664 {
665 std::lock_guard<std::mutex> lock(mutex_);
666 if (queue_.empty())
667 return false;
668 T value(std::move_if_noexcept(queue_.front()));
669 out = std::move(value);
670 queue_.pop_front();
671 not_full_.notify_one();
672 return true;
673 }
674
685 [[nodiscard]] std::optional<T> try_recv()
686 {
687 std::lock_guard<std::mutex> lock(mutex_);
688 if (queue_.empty())
689 return std::nullopt;
690 T value(std::move_if_noexcept(queue_.front()));
691 std::optional<T> result;
692 if constexpr (std::is_nothrow_move_constructible_v<T> or
693 not std::is_copy_constructible_v<T>)
694 result.emplace(std::move(value));
695 else
696 result.emplace(value);
697 queue_.pop_front();
698 not_full_.notify_one();
699 if constexpr (std::is_nothrow_move_constructible_v<T> or
700 not std::is_copy_constructible_v<T>)
701 return result;
702 else
703 return static_cast<const std::optional<T> &>(result);
704 }
705 };
706
725 template <typename T, typename Mutex = std::mutex>
727 {
728 public:
737 {
738 std::unique_lock<Mutex> lock_;
740
741 public:
747 LockedPtr(Mutex & mutex, T & value)
748 : lock_(mutex), ptr_(&value) {}
749
758 LockedPtr(LockedPtr && other) noexcept
759 : lock_(std::move(other.lock_)), ptr_(std::exchange(other.ptr_, nullptr)) {}
760
766 LockedPtr & operator = (LockedPtr && other) noexcept
767 {
768 LockedPtr taken(std::move(other)); // releases the old lock on return
769 lock_.swap(taken.lock_);
770 std::swap(ptr_, taken.ptr_);
771 return *this;
772 }
773
779 [[nodiscard]] T & get() noexcept
780 {
781 assert(ptr_ != nullptr);
782 return *ptr_;
783 }
789 [[nodiscard]] T * operator -> () noexcept { return ptr_; }
795 [[nodiscard]] T & operator * () noexcept
796 {
797 assert(ptr_ != nullptr);
798 return *ptr_;
799 }
800 };
801
810 {
811 std::unique_lock<Mutex> lock_;
812 const T *ptr_;
813
814 public:
820 ConstLockedPtr(Mutex & mutex, const T & value)
821 : lock_(mutex), ptr_(&value) {}
822
832 : lock_(std::move(other.lock_)), ptr_(std::exchange(other.ptr_, nullptr)) {}
833
840 {
841 ConstLockedPtr taken(std::move(other)); // releases the old lock on return
842 lock_.swap(taken.lock_);
843 std::swap(ptr_, taken.ptr_);
844 return *this;
845 }
846
852 [[nodiscard]] const T & get() const noexcept
853 {
854 assert(ptr_ != nullptr);
855 return *ptr_;
856 }
862 [[nodiscard]] const T * operator -> () const noexcept { return ptr_; }
868 [[nodiscard]] const T & operator * () const noexcept
869 {
870 assert(ptr_ != nullptr);
871 return *ptr_;
872 }
873 };
874
875 private:
876 mutable Mutex mutex_;
877 T value_ = T(); // not indeterminate when built by default
878
879 public:
883 Synchronized() requires std::default_initializable<T> = default;
884
886 Synchronized(const Synchronized &) = delete;
888 Synchronized & operator = (const Synchronized &) = delete;
892 Synchronized & operator = (Synchronized &&) = delete;
893
898 explicit Synchronized(const T & value)
899 : value_(value) {}
900
905 explicit Synchronized(T && value)
906 : value_(std::move(value)) {}
907
913 template <typename... Args>
914 explicit Synchronized(std::in_place_t, Args && ... args)
915 : value_(std::forward<Args>(args)...) {}
916
923 [[nodiscard]] LockedPtr lock() { return LockedPtr(mutex_, value_); }
924
932 [[nodiscard]] ConstLockedPtr lock() const
933 {
935 }
936
952 template <typename F>
953 decltype(auto) with_lock(F && f)
954 {
955 using Result = std::invoke_result_t<F, T &>;
956 static_assert(not std::is_reference_v<Result>,
957 "Synchronized::with_lock callback must not return a reference");
958 std::unique_lock<Mutex> lock(mutex_);
959 return std::invoke(std::forward<F>(f), value_);
960 }
961
977 template <typename F>
978 decltype(auto) with_lock(F && f) const
979 {
980 using Result = std::invoke_result_t<F, const T &>;
981 static_assert(not std::is_reference_v<Result>,
982 "Synchronized::with_lock callback must not return a reference");
983 std::unique_lock<Mutex> lock(mutex_);
984 return std::invoke(std::forward<F>(f), std::as_const(value_));
985 }
986 };
987
1001 template <typename T, typename SharedMutex = std::shared_mutex>
1003 {
1004 public:
1012 {
1013 std::shared_lock<SharedMutex> lock_;
1014 const T *ptr_;
1015
1016 public:
1022 ReadLockedPtr(SharedMutex & mutex, const T & value)
1023 : lock_(mutex), ptr_(&value) {}
1024
1033 ReadLockedPtr(ReadLockedPtr && other) noexcept
1034 : lock_(std::move(other.lock_)), ptr_(std::exchange(other.ptr_, nullptr)) {}
1035
1042 {
1043 ReadLockedPtr taken(std::move(other)); // releases the old lock on return
1044 lock_.swap(taken.lock_);
1045 std::swap(ptr_, taken.ptr_);
1046 return *this;
1047 }
1048
1054 [[nodiscard]] const T & get() const noexcept
1055 {
1056 assert(ptr_ != nullptr);
1057 return *ptr_;
1058 }
1064 [[nodiscard]] const T * operator -> () const noexcept { return ptr_; }
1070 [[nodiscard]] const T & operator * () const noexcept
1071 {
1072 assert(ptr_ != nullptr);
1073 return *ptr_;
1074 }
1075 };
1076
1084 {
1085 std::unique_lock<SharedMutex> lock_;
1087
1088 public:
1094 WriteLockedPtr(SharedMutex & mutex, T & value)
1095 : lock_(mutex), ptr_(&value) {}
1096
1106 : lock_(std::move(other.lock_)), ptr_(std::exchange(other.ptr_, nullptr)) {}
1107
1114 {
1115 WriteLockedPtr taken(std::move(other)); // releases the old lock on return
1116 lock_.swap(taken.lock_);
1117 std::swap(ptr_, taken.ptr_);
1118 return *this;
1119 }
1120
1126 [[nodiscard]] T & get() noexcept
1127 {
1128 assert(ptr_ != nullptr);
1129 return *ptr_;
1130 }
1136 [[nodiscard]] T * operator -> () noexcept { return ptr_; }
1142 [[nodiscard]] T & operator * () noexcept
1143 {
1144 assert(ptr_ != nullptr);
1145 return *ptr_;
1146 }
1147 };
1148
1149 private:
1150 mutable SharedMutex mutex_;
1151 T value_ = T(); // not indeterminate when built by default
1152
1153 public:
1157 RwSynchronized() requires std::default_initializable<T> = default;
1158
1162 RwSynchronized & operator = (const RwSynchronized &) = delete;
1166 RwSynchronized & operator = (RwSynchronized &&) = delete;
1167
1172 explicit RwSynchronized(const T & value)
1173 : value_(value) {}
1174
1180 : value_(std::move(value)) {}
1181
1186 template <typename... Args>
1187 explicit RwSynchronized(std::in_place_t, Args && ... args)
1188 : value_(std::forward<Args>(args)...) {}
1189
1195 [[nodiscard]] ReadLockedPtr read() const
1196 {
1197 return ReadLockedPtr(mutex_, value_);
1198 }
1199
1207 [[nodiscard]] WriteLockedPtr write()
1208 {
1209 return WriteLockedPtr(mutex_, value_);
1210 }
1211
1223 template <typename F>
1224 decltype(auto) with_read_lock(F && f) const
1225 {
1226 using Result = std::invoke_result_t<F, const T &>;
1227 static_assert(not std::is_reference_v<Result>,
1228 "RwSynchronized::with_read_lock callback must not return a reference");
1229 std::shared_lock<SharedMutex> lock(mutex_);
1230 return std::invoke(std::forward<F>(f), std::as_const(value_));
1231 }
1232
1244 template <typename F>
1245 decltype(auto) with_write_lock(F && f)
1246 {
1247 using Result = std::invoke_result_t<F, T &>;
1248 static_assert(not std::is_reference_v<Result>,
1249 "RwSynchronized::with_write_lock callback must not return a reference");
1250 std::unique_lock<SharedMutex> lock(mutex_);
1251 return std::invoke(std::forward<F>(f), value_);
1252 }
1253 };
1254
1273 template <typename T>
1275 {
1276 std::vector<std::optional<T>> slots_;
1278 alignas(64) std::atomic<size_t> head_{0};
1279 alignas(64) std::atomic<size_t> tail_{0};
1280
1282 [[nodiscard]] size_t advance(size_t idx) const noexcept
1283 {
1284 return (idx + 1) % storage_size_;
1285 }
1286
1288 template <typename... Args>
1289 bool emplace_impl(Args && ... args)
1290 {
1291 const size_t tail = tail_.load(std::memory_order_relaxed);
1292 const size_t next = advance(tail);
1293 if (next == head_.load(std::memory_order_acquire))
1294 return false;
1295 slots_[tail].emplace(std::forward<Args>(args)...);
1296 tail_.store(next, std::memory_order_release);
1297 return true;
1298 }
1299
1300 public:
1307 explicit SpscQueue(size_t capacity)
1309 {
1310 if (capacity == 0)
1311 throw std::invalid_argument("SpscQueue capacity must be greater than zero");
1312 }
1313
1315 SpscQueue(const SpscQueue &) = delete;
1317 SpscQueue & operator = (const SpscQueue &) = delete;
1318
1325 [[nodiscard]] size_t capacity() const noexcept { return storage_size_ - 1; }
1326
1332 [[nodiscard]] bool empty() const noexcept
1333 {
1334 return head_.load(std::memory_order_acquire) ==
1335 tail_.load(std::memory_order_acquire);
1336 }
1337
1343 [[nodiscard]] bool full() const noexcept
1344 {
1345 const size_t tail = tail_.load(std::memory_order_acquire);
1346 return advance(tail) == head_.load(std::memory_order_acquire);
1347 }
1348
1354 [[nodiscard]] size_t size() const noexcept
1355 {
1356 const size_t head = head_.load(std::memory_order_acquire);
1357 const size_t tail = tail_.load(std::memory_order_acquire);
1358 if (tail >= head)
1359 return tail - head;
1360 return storage_size_ - head + tail;
1361 }
1362
1370 bool try_push(const T & value) { return emplace_impl(value); }
1371
1379 bool try_push(T && value) { return emplace_impl(std::move(value)); }
1380
1389 template <typename... Args>
1390 bool emplace(Args && ... args)
1391 {
1392 return emplace_impl(std::forward<Args>(args)...);
1393 }
1394
1403 bool try_pop(T & out)
1404 {
1405 const size_t head = head_.load(std::memory_order_relaxed);
1406 if (head == tail_.load(std::memory_order_acquire))
1407 return false;
1408 T value(std::move_if_noexcept(*slots_[head]));
1409 out = std::move(value);
1410 slots_[head].reset();
1411 head_.store(advance(head), std::memory_order_release);
1412 return true;
1413 }
1414
1424 [[nodiscard]] std::optional<T> try_pop()
1425 {
1426 const size_t head = head_.load(std::memory_order_relaxed);
1427 if (head == tail_.load(std::memory_order_acquire))
1428 return std::nullopt;
1429 T value(std::move_if_noexcept(*slots_[head]));
1430 std::optional<T> result;
1431 if constexpr (std::is_nothrow_move_constructible_v<T> or
1432 not std::is_copy_constructible_v<T>)
1433 result.emplace(std::move(value));
1434 else
1435 result.emplace(value);
1436 slots_[head].reset();
1437 head_.store(advance(head), std::memory_order_release);
1438 if constexpr (std::is_nothrow_move_constructible_v<T> or
1439 not std::is_copy_constructible_v<T>)
1440 return result;
1441 else
1442 return static_cast<const std::optional<T> &>(result);
1443 }
1444 };
1445
1451 template <typename T>
1453
1460 template <typename T, typename Mutex = std::mutex>
1462
1469 template <typename T, typename SharedMutex = std::shared_mutex>
1471
1477 template <typename T>
1479}
1480
1481#endif
size_t size_t int32_t value
Definition ca-c-api.h:116
size_t size_t int32_t * out
Definition ca-c-api.h:120
Bounded blocking channel for producer-consumer workflows.
std::optional< T > recv(const CancellationToken &token)
Cancellation-aware receive of the next item from the channel.
bool emplace(Args &&... args)
Construct an element in-place at the back of the queue (no args).
size_t size() const
Return the number of currently queued items.
bool emplace(First &&token, Args &&... args)
Cancellation-aware in-place construction.
bool is_closed() const
Check whether the channel has been closed.
bool empty() const
Check whether the channel currently holds no queued items.
BoundedChannel(const BoundedChannel &)=delete
Deleted copy constructor.
size_t capacity() const noexcept
Return the maximum number of queued items.
bool recv(T &out)
Receive an item from the channel into out.
bool try_send(const T &value)
Attempt to send a value without blocking.
void wait_with_cancellation(std::condition_variable &cv, std::unique_lock< std::mutex > &lock, Predicate pred, const CancellationToken &token, CancellationToken::ConditionVariableRegistration &registration)
Wait on a condition variable with cancellation support.
bool try_send(T &&value)
Attempt to send a value without blocking (move).
std::optional< T > try_recv()
Attempt to receive an item without blocking.
bool try_recv(T &out)
Attempt to receive an item into out without blocking.
bool send_impl(U &&value)
Common implementation for send operations.
bool send(const T &value, const CancellationToken &token)
Cancellation-aware blocking send (copy).
std::condition_variable not_empty_
void close()
Close the channel.
bool send(T &&value)
Send a value by moving it into the channel.
bool recv(T &out, const CancellationToken &token)
Cancellation-aware blocking receive into out.
std::condition_variable not_full_
BoundedChannel(size_t capacity)
Construct a channel with a fixed capacity.
bool emplace(First &&first, Rest &&... rest)
Construct an element in-place at the back of the queue.
bool send_impl(U &&value, const CancellationToken &token)
Common implementation for cancellation-aware send operations.
bool send(T &&value, const CancellationToken &token)
Cancellation-aware blocking send (move).
std::optional< T > recv()
Receive the next item from the channel.
BoundedChannel & operator=(const BoundedChannel &)=delete
Deleted copy assignment operator.
bool send(const T &value)
Send a value by copying it into the channel.
RAII guard for condition variable cancellation registration.
Read-only cooperative cancellation token.
ConditionVariableRegistration notify_on_cancel(std::condition_variable &cv) const
Request notification of a condition variable on cancellation.
bool stop_requested() const noexcept
Return true if cancellation has been requested.
void throw_if_cancellation_requested() const
Throw operation_canceled if cancellation was requested.
RAII guard for shared (read-only) access.
ReadLockedPtr & operator=(ReadLockedPtr &&other) noexcept
Release the lock of this guard and take the lock and the access of another, which is left with neithe...
std::shared_lock< SharedMutex > lock_
ReadLockedPtr(SharedMutex &mutex, const T &value)
Construct a guard and acquire a shared lock.
const T * operator->() const noexcept
Access members of the protected value.
const T & get() const noexcept
Access the protected value.
const T & operator*() const noexcept
Access the protected value.
ReadLockedPtr(ReadLockedPtr &&other) noexcept
Take the lock and the access of another guard, which is left with neither.
RAII guard for exclusive (write) access.
T & operator*() noexcept
Access the protected value.
std::unique_lock< SharedMutex > lock_
WriteLockedPtr(SharedMutex &mutex, T &value)
Construct a guard and acquire an exclusive lock.
WriteLockedPtr & operator=(WriteLockedPtr &&other) noexcept
Release the lock of this guard and take the lock and the access of another, which is left with neithe...
WriteLockedPtr(WriteLockedPtr &&other) noexcept
Take the lock and the access of another guard, which is left with neither.
T * operator->() noexcept
Access members of the protected value.
T & get() noexcept
Access the protected value.
Read/write-lock protected shared object wrapper.
RwSynchronized()=default
Value-initialize the protected value: zero for a scalar.
decltype(auto) with_read_lock(F &&f) const
Execute a callback with shared (read-only) access.
RwSynchronized(std::in_place_t, Args &&... args)
Construct the protected value in-place.
ReadLockedPtr read() const
Acquire a shared lock and return a guard.
WriteLockedPtr write()
Acquire an exclusive lock and return a guard.
decltype(auto) with_write_lock(F &&f)
Execute a callback with exclusive (write) access.
RwSynchronized(T &&value)
Construct by moving from value.
Bounded single-producer/single-consumer queue.
bool try_pop(T &out)
Attempt to pop an item from the queue into out.
std::atomic< size_t > head_
SpscQueue & operator=(const SpscQueue &)=delete
Deleted copy assignment operator.
bool empty() const noexcept
Check if the queue is empty.
SpscQueue(size_t capacity)
Construct a queue with the specified capacity.
bool emplace_impl(Args &&... args)
Internal implementation for push/emplace.
SpscQueue(const SpscQueue &)=delete
Deleted copy constructor.
size_t capacity() const noexcept
Return the queue's capacity.
bool try_push(T &&value)
Attempt to push an item (move) into the queue.
std::vector< std::optional< T > > slots_
bool full() const noexcept
Check if the queue is full.
size_t size() const noexcept
Return the number of items currently in the queue.
size_t advance(size_t idx) const noexcept
Compute the next index in the circular buffer.
bool emplace(Args &&... args)
Construct an item in-place at the tail of the queue.
bool try_push(const T &value)
Attempt to push an item (copy) into the queue.
std::optional< T > try_pop()
Attempt to pop an item from the queue.
std::atomic< size_t > tail_
RAII guard for read-only access to the synchronized value.
ConstLockedPtr(ConstLockedPtr &&other) noexcept
Take the lock and the access of another guard, which is left with neither.
ConstLockedPtr(Mutex &mutex, const T &value)
Construct a guard and acquire the lock.
ConstLockedPtr & operator=(ConstLockedPtr &&other) noexcept
Release the lock of this guard and take the lock and the access of another, which is left with neithe...
const T * operator->() const noexcept
Access members of the protected value.
const T & operator*() const noexcept
Access the protected value.
const T & get() const noexcept
Access the protected value.
RAII guard for exclusive access to the synchronized value.
LockedPtr(Mutex &mutex, T &value)
Construct a guard and acquire the lock.
LockedPtr(LockedPtr &&other) noexcept
Take the lock and the access of another guard, which is left with neither.
T * operator->() noexcept
Access members of the protected value.
LockedPtr & operator=(LockedPtr &&other) noexcept
Release the lock of this guard and take the lock and the access of another, which is left with neithe...
T & operator*() noexcept
Access the protected value.
std::unique_lock< Mutex > lock_
T & get() noexcept
Access the protected value.
Mutex-protected shared object wrapper.
decltype(auto) with_lock(F &&f)
Execute a callback with exclusive access to the value.
Synchronized()=default
Value-initialize the protected value: zero for a scalar.
ConstLockedPtr lock() const
Acquire an exclusive lock (for const access) and return a guard.
Synchronized(std::in_place_t, Args &&... args)
Construct the protected value in-place.
decltype(auto) with_lock(F &&f) const
Execute a callback with read-only access to the value.
LockedPtr lock()
Acquire an exclusive lock and return a guard.
Synchronized(T &&value)
Construct by moving from value.
Freq_Node * pred
Predecessor node in level-order traversal.
Main namespace for Aleph-w library functions.
std::decay_t< typename HeadC::Item_Type > T
Definition ah-zip.H:152
void next()
Advance all underlying iterators (bounds-checked).
Definition ah-zip.H:236
STL namespace.
A modern, efficient thread pool for parallel task execution.