CoopSync for TBB 0.1.0
Cooperative synchronization primitives for TBB resumable tasks
Loading...
Searching...
No Matches
semaphore.hpp
1// SPDX-FileCopyrightText: 2026 CERN
2//
3// SPDX-License-Identifier: Apache-2.0
4
5#pragma once
6
7#include <atomic>
8#include <cassert>
9#include <cstddef>
10#include <cstdint>
11#include <limits>
12#include <type_traits>
13
14#include "coopsync_tbb/detail/macros.hpp"
15#include "coopsync_tbb/detail/wait_queue.hpp"
16
17namespace coopsync_tbb {
18
19namespace detail {
20
25template <std::ptrdiff_t N>
26class int_max_value_t {
27 static_assert(N > 0, "N must be positive");
28
29 private:
30 static const bool fits8 =
31 N <= static_cast<std::ptrdiff_t>(
32 std::numeric_limits<std::int_least8_t>::max());
33
34 static const bool fits16 =
35 N <= static_cast<std::ptrdiff_t>(
36 std::numeric_limits<std::int_least16_t>::max());
37
38 static const bool fits32 =
39 N <= static_cast<std::ptrdiff_t>(
40 std::numeric_limits<std::int_least32_t>::max());
41
42 static const bool fits64 =
43 N <= static_cast<std::ptrdiff_t>(
44 std::numeric_limits<std::int_least64_t>::max());
45
46 public:
47 // clang-format off
48 using type =
49 typename std::conditional<
50 fits8,
51 std::int_least8_t,
52 typename std::conditional<
53 fits16,
54 std::int_least16_t,
55 typename std::conditional<
56 fits32,
57 std::int_least32_t,
58 typename std::conditional<
59 fits64,
60 std::int_least64_t,
61 std::ptrdiff_t
62 >::type>::type>::type>::type;
63 // clang-format on
64};
65} // namespace detail
66
74// @tparam LeastMaxValue The least maximum value for the counter of the
75// counting_semaphore. Must be non-negative and less than or equal to
76// max().
77template <std::ptrdiff_t LeastMaxValue =
78 std::numeric_limits<std::ptrdiff_t>::max()>
79class COOPSYNC_TBB_EXPORT counting_semaphore {
80 public:
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()");
85
89 explicit counting_semaphore(std::ptrdiff_t desired);
90
93
96
99
102
108
114 void release(std::ptrdiff_t update = 1);
115
122 void acquire();
123
127 COOPSYNC_TBB_NODISCARD bool try_acquire();
128
133 constexpr static std::ptrdiff_t max() noexcept;
134
135 private:
136 using storage_t = typename detail::int_max_value_t<LeastMaxValue>::type;
137
138 std::atomic<storage_t> m_counter;
139 detail::wait_queue m_waiters;
140};
141
145template <>
146class COOPSYNC_TBB_EXPORT counting_semaphore<1> {
147 public:
150 explicit counting_semaphore(std::ptrdiff_t desired);
151
154
157
160
163
169
175 void release(std::ptrdiff_t update = 1);
176
182 void acquire();
183
187 COOPSYNC_TBB_NODISCARD bool try_acquire();
188
192 constexpr static std::ptrdiff_t max() noexcept;
193
194 private:
195 std::atomic<bool> m_available;
196 detail::wait_queue m_waiters;
197};
198
201
202template <std::ptrdiff_t LeastMaxValue>
204 std::ptrdiff_t desired)
205 : m_counter(desired) {
206 assert(desired >= 0); // LCOV_EXCL_LINE
207 assert(desired <= max()); // LCOV_EXCL_LINE
208}
209
210template <std::ptrdiff_t LeastMaxValue>
212 assert(m_waiters.empty()); // LCOV_EXCL_LINE
213}
214
215template <std::ptrdiff_t LeastMaxValue>
216inline constexpr std::ptrdiff_t
218 return static_cast<std::ptrdiff_t>(std::numeric_limits<storage_t>::max());
219}
220
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)) {
228 return true;
229 }
230 }
231 return false;
232}
233
234template <std::ptrdiff_t LeastMaxValue>
236 while (!try_acquire()) {
237 m_waiters.wait_if(
238 [this] { return m_counter.load(std::memory_order_acquire) == 0; });
239 }
240}
241
242template <std::ptrdiff_t LeastMaxValue>
243inline void counting_semaphore<LeastMaxValue>::release(std::ptrdiff_t update) {
244 assert(update >= 0); // LCOV_EXCL_LINE
245 if (update == 0) {
246 return;
247 }
248
249 const auto prev = m_counter.fetch_add(static_cast<storage_t>(update),
250 std::memory_order_release);
251 assert(update <= // LCOV_EXCL_LINE
252 max() - static_cast<std::ptrdiff_t>(prev));
253
254 // Wake up to `update` waiters; any unused permits stay in m_counter.
255 m_waiters.resume_n(update);
256}
257
258inline constexpr std::ptrdiff_t counting_semaphore<1>::max() noexcept {
259 return 1;
260}
261
262} // namespace coopsync_tbb
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