14#include "coopsync_tbb/detail/macros.hpp"
15#include "coopsync_tbb/detail/wait_queue.hpp"
25template <std::ptrdiff_t N>
26class int_max_value_t {
27 static_assert(N > 0,
"N must be positive");
30 static const bool fits8 =
31 N <= static_cast<std::ptrdiff_t>(
32 std::numeric_limits<std::int_least8_t>::max());
34 static const bool fits16 =
35 N <= static_cast<std::ptrdiff_t>(
36 std::numeric_limits<std::int_least16_t>::max());
38 static const bool fits32 =
39 N <= static_cast<std::ptrdiff_t>(
40 std::numeric_limits<std::int_least32_t>::max());
42 static const bool fits64 =
43 N <= static_cast<std::ptrdiff_t>(
44 std::numeric_limits<std::int_least64_t>::max());
49 typename std::conditional<
52 typename std::conditional<
55 typename std::conditional<
58 typename std::conditional<
62 >::type>::type>::type>::type;
77template <std::ptrdiff_t LeastMaxValue =
78 std::numeric_limits<std::ptrdiff_t>::max()>
81 static_assert(LeastMaxValue >= 0,
"LeastMaxValue must be non-negative");
82 static_assert(LeastMaxValue <= std::numeric_limits<std::ptrdiff_t>::max(),
83 "LeastMaxValue must be less than or equal to "
84 "std::numeric_limits<std::ptrdiff_t>::max()");
114 void release(std::ptrdiff_t update = 1);
133 constexpr static std::ptrdiff_t
max() noexcept;
136 using storage_t = typename detail::int_max_value_t<LeastMaxValue>::type;
138 std::atomic<storage_t> m_counter;
139 detail::wait_queue m_waiters;
192 constexpr static std::ptrdiff_t
max() noexcept;
195 std::atomic<
bool> m_available;
196 detail::wait_queue m_waiters;
202template <std::ptrdiff_t LeastMaxValue>
204 std::ptrdiff_t desired)
205 : m_counter(desired) {
206 assert(desired >= 0);
207 assert(desired <=
max());
210template <std::ptrdiff_t LeastMaxValue>
212 assert(m_waiters.empty());
215template <std::ptrdiff_t LeastMaxValue>
216inline constexpr std::ptrdiff_t
218 return static_cast<std::ptrdiff_t
>(std::numeric_limits<storage_t>::max());
221template <std::ptrdiff_t LeastMaxValue>
223 auto current = m_counter.load(std::memory_order_acquire);
224 while (current > 0) {
225 if (m_counter.compare_exchange_weak(current, current - 1,
226 std::memory_order_acquire,
227 std::memory_order_relaxed)) {
234template <std::ptrdiff_t LeastMaxValue>
238 [
this] {
return m_counter.load(std::memory_order_acquire) == 0; });
242template <std::ptrdiff_t LeastMaxValue>
249 const auto prev = m_counter.fetch_add(
static_cast<storage_t
>(update),
250 std::memory_order_release);
252 max() -
static_cast<std::ptrdiff_t
>(prev));
255 m_waiters.resume_n(update);
void release(std::ptrdiff_t update=1)
Releases the semaphore incrementing the counter by the specified update. If there are tasks suspended...
counting_semaphore(std::ptrdiff_t desired)
Constructs a counting_semaphore with the specified initial count.
bool try_acquire()
Attempts to acquire the semaphore without suspending.
counting_semaphore(counting_semaphore &&)=delete
counting_semaphore is not move-constructible.
void acquire()
Acquires the semaphore. If the semaphore is already acquired the calling task is suspended until the ...
counting_semaphore & operator=(const counting_semaphore &)=delete
counting_semaphore is not copy-assignable.
counting_semaphore & operator=(counting_semaphore &&)=delete
counting_semaphore is not move-assignable.
~counting_semaphore()
Destroys the counting_semaphore.
counting_semaphore(const counting_semaphore &)=delete
counting_semaphore is not copy-constructible.
static constexpr std::ptrdiff_t max() noexcept
Returns the maximum value for the counter of the binary semaphore.
Definition semaphore.hpp:258
A counting semaphore that allows tasks to synchronize based on a counter. The semaphore is initialize...
Definition semaphore.hpp:79
void release(std::ptrdiff_t update=1)
Releases the semaphore, incrementing the counter by the specified update. If there are tasks suspende...
Definition semaphore.hpp:243
~counting_semaphore()
Destroys the counting_semaphore.
Definition semaphore.hpp:211
static constexpr std::ptrdiff_t max() noexcept
Returns the maximum value for the counter of the counting_semaphore.
Definition semaphore.hpp:217
counting_semaphore & operator=(counting_semaphore &&)=delete
counting_semaphore is not move-assignable.
counting_semaphore & operator=(const counting_semaphore &)=delete
counting_semaphore is not copy-assignable.
void acquire()
Acquires the semaphore, decrementing the counter by 1. If the counter is zero, the calling task is su...
Definition semaphore.hpp:235
bool try_acquire()
Attempts to acquire the semaphore without suspending.
Definition semaphore.hpp:222
counting_semaphore(std::ptrdiff_t desired)
Constructs a counting_semaphore with the specified initial count.
Definition semaphore.hpp:203
counting_semaphore(counting_semaphore &&)=delete
counting_semaphore is not move-constructible.
counting_semaphore(const counting_semaphore &)=delete
counting_semaphore is not copy-constructible.
Main namespace of the library, containing all public APIs.
Definition wait_for.hpp:11
counting_semaphore< 1 > binary_semaphore
An alias for counting_semaphore<1>.
Definition semaphore.hpp:200