85#ifndef ALEPH_CONCURRENCY_UTILS_H
86#define ALEPH_CONCURRENCY_UTILS_H
90#include <condition_variable>
97#include <shared_mutex>
145 template <
typename T>
174 template <
typename Predicate>
176 std::unique_lock<std::mutex> & lock,
196 template <
typename U>
199 std::unique_lock<std::mutex> lock(
mutex_);
217 template <
typename U>
221 std::unique_lock<std::mutex> lock(
mutex_);
224 token, registration);
244 throw std::invalid_argument(
"BoundedChannel capacity must be greater than zero");
267 std::lock_guard<std::mutex> lock(
mutex_);
277 [[nodiscard]]
size_t size()
const
279 std::lock_guard<std::mutex> lock(
mutex_);
291 std::lock_guard<std::mutex> lock(
mutex_);
311 std::lock_guard<std::mutex> lock(
mutex_);
398 template <
typename... Args>
399 requires (
sizeof...(Args) == 0)
402 std::unique_lock<std::mutex> lock(
mutex_);
406 queue_.emplace_back(std::forward<Args>(args)...);
428 template <
typename First,
typename... Rest>
429 requires std::constructible_from<
T, First, Rest...>
430 bool emplace(First && first, Rest && ... rest)
432 std::unique_lock<std::mutex> lock(
mutex_);
436 queue_.emplace_back(std::forward<First>(first),
437 std::forward<Rest>(rest)...);
460 template <
typename First,
typename... Args>
462 (not std::constructible_from<
T, First, Args...>) &&
463 std::constructible_from<T, Args...>
464 bool emplace(First && token, Args && ... args)
467 std::unique_lock<std::mutex> lock(
mutex_);
470 token, registration);
473 queue_.emplace_back(std::forward<Args>(args)...);
490 std::lock_guard<std::mutex> lock(
mutex_);
509 std::lock_guard<std::mutex> lock(
mutex_);
532 std::unique_lock<std::mutex> lock(
mutex_);
562 std::unique_lock<std::mutex> lock(
mutex_);
565 token, registration);
588 [[nodiscard]] std::optional<T>
recv()
590 std::unique_lock<std::mutex> lock(
mutex_);
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));
600 result.emplace(
value);
604 if constexpr (std::is_nothrow_move_constructible_v<T> or
605 not std::is_copy_constructible_v<T>)
608 return static_cast<const std::optional<T> &
>(result);
629 std::unique_lock<std::mutex> lock(
mutex_);
632 token, registration);
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));
641 result.emplace(
value);
645 if constexpr (std::is_nothrow_move_constructible_v<T> or
646 not std::is_copy_constructible_v<T>)
649 return static_cast<const std::optional<T> &
>(result);
665 std::lock_guard<std::mutex> lock(
mutex_);
687 std::lock_guard<std::mutex> lock(
mutex_);
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));
696 result.emplace(
value);
699 if constexpr (std::is_nothrow_move_constructible_v<T> or
700 not std::is_copy_constructible_v<T>)
703 return static_cast<const std::optional<T> &
>(result);
725 template <
typename T,
typename Mutex = std::mutex>
759 :
lock_(std::move(other.lock_)),
ptr_(std::exchange(other.ptr_,
nullptr)) {}
779 [[nodiscard]]
T &
get() noexcept
781 assert(
ptr_ !=
nullptr);
797 assert(
ptr_ !=
nullptr);
832 :
lock_(std::move(other.lock_)),
ptr_(std::exchange(other.ptr_,
nullptr)) {}
852 [[nodiscard]]
const T &
get() const noexcept
854 assert(
ptr_ !=
nullptr);
870 assert(
ptr_ !=
nullptr);
913 template <
typename... Args>
952 template <
typename F>
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");
959 return std::invoke(std::forward<F>(f),
value_);
977 template <
typename F>
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");
984 return std::invoke(std::forward<F>(f), std::as_const(
value_));
1001 template <
typename T,
typename SharedMutex = std::shared_mutex>
1034 :
lock_(std::move(other.lock_)),
ptr_(std::exchange(other.ptr_,
nullptr)) {}
1054 [[nodiscard]]
const T &
get() const noexcept
1056 assert(
ptr_ !=
nullptr);
1072 assert(
ptr_ !=
nullptr);
1106 :
lock_(std::move(other.lock_)),
ptr_(std::exchange(other.ptr_,
nullptr)) {}
1128 assert(
ptr_ !=
nullptr);
1144 assert(
ptr_ !=
nullptr);
1186 template <
typename... Args>
1188 :
value_(
std::forward<Args>(args)...) {}
1223 template <
typename F>
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_));
1244 template <
typename F>
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_);
1273 template <
typename T>
1278 alignas(64) std::atomic<size_t>
head_{0};
1279 alignas(64) std::atomic<size_t>
tail_{0};
1282 [[nodiscard]]
size_t advance(
size_t idx)
const noexcept
1288 template <
typename... Args>
1291 const size_t tail =
tail_.load(std::memory_order_relaxed);
1293 if (
next ==
head_.load(std::memory_order_acquire))
1295 slots_[tail].emplace(std::forward<Args>(args)...);
1296 tail_.store(
next, std::memory_order_release);
1311 throw std::invalid_argument(
"SpscQueue capacity must be greater than zero");
1332 [[nodiscard]]
bool empty() const noexcept
1334 return head_.load(std::memory_order_acquire) ==
1335 tail_.load(std::memory_order_acquire);
1343 [[nodiscard]]
bool full() const noexcept
1345 const size_t tail =
tail_.load(std::memory_order_acquire);
1346 return advance(tail) ==
head_.load(std::memory_order_acquire);
1354 [[nodiscard]]
size_t size() const noexcept
1356 const size_t head =
head_.load(std::memory_order_acquire);
1357 const size_t tail =
tail_.load(std::memory_order_acquire);
1389 template <
typename... Args>
1405 const size_t head =
head_.load(std::memory_order_relaxed);
1406 if (head ==
tail_.load(std::memory_order_acquire))
1426 const size_t head =
head_.load(std::memory_order_relaxed);
1427 if (head ==
tail_.load(std::memory_order_acquire))
1428 return std::nullopt;
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));
1435 result.emplace(
value);
1438 if constexpr (std::is_nothrow_move_constructible_v<T> or
1439 not std::is_copy_constructible_v<T>)
1442 return static_cast<const std::optional<T> &
>(result);
1451 template <
typename T>
1460 template <
typename T,
typename Mutex = std::mutex>
1469 template <
typename T,
typename SharedMutex = std::shared_mutex>
1477 template <
typename T>
size_t size_t int32_t value
size_t size_t int32_t * out
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 ®istration)
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.
std::unique_lock< Mutex > lock_
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
void next()
Advance all underlying iterators (bounds-checked).
A modern, efficient thread pool for parallel task execution.