2 //===----------------------------------------------------------------------===//
4 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5 // See https://llvm.org/LICENSE.txt for license information.
6 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
8 //===----------------------------------------------------------------------===//
10 #ifndef _LIBCPP_SEMAPHORE
11 #define _LIBCPP_SEMAPHORE
18 template<ptrdiff_t least_max_value = implementation-defined>
19 class counting_semaphore
22 static constexpr ptrdiff_t max() noexcept;
24 constexpr explicit counting_semaphore(ptrdiff_t desired);
25 ~counting_semaphore();
27 counting_semaphore(const counting_semaphore&) = delete;
28 counting_semaphore& operator=(const counting_semaphore&) = delete;
30 void release(ptrdiff_t update = 1);
32 bool try_acquire() noexcept;
33 template<class Rep, class Period>
34 bool try_acquire_for(const chrono::duration<Rep, Period>& rel_time);
35 template<class Clock, class Duration>
36 bool try_acquire_until(const chrono::time_point<Clock, Duration>& abs_time);
39 ptrdiff_t counter; // exposition only
42 using binary_semaphore = counting_semaphore<1>;
48 #include <__assert> // all public C++ headers provide the assertion handler
49 #include <__atomic/atomic_base.h>
50 #include <__atomic/atomic_sync.h>
51 #include <__atomic/memory_order.h>
52 #include <__availability>
53 #include <__chrono/time_point.h>
55 #include <__thread/poll_with_backoff.h>
56 #include <__thread/timed_backoff_policy.h>
57 #include <__threading_support>
62 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
63 # pragma GCC system_header
66 #ifdef _LIBCPP_HAS_NO_THREADS
67 # error "<semaphore> is not supported since libc++ has been configured without support for threads."
71 #include <__undef_macros>
73 #if _LIBCPP_STD_VER >= 14
75 _LIBCPP_BEGIN_NAMESPACE_STD
79 __atomic_semaphore_base is the general-case implementation.
80 It is a typical Dijkstra semaphore algorithm over atomics, wait and notify
81 functions. It avoids contention against users' own use of those facilities.
85 #define _LIBCPP_SEMAPHORE_MAX (numeric_limits<ptrdiff_t>::max())
87 class __atomic_semaphore_base
89 __atomic_base<ptrdiff_t> __a_;
92 _LIBCPP_INLINE_VISIBILITY
93 constexpr explicit __atomic_semaphore_base(ptrdiff_t __count) : __a_(__count)
96 _LIBCPP_AVAILABILITY_SYNC _LIBCPP_INLINE_VISIBILITY
97 void release(ptrdiff_t __update = 1)
99 auto __old = __a_.fetch_add(__update, memory_order_release);
100 _LIBCPP_ASSERT_UNCATEGORIZED(__update <= _LIBCPP_SEMAPHORE_MAX - __old, "update is greater than the expected value");
106 else if (__update > 1)
111 _LIBCPP_AVAILABILITY_SYNC _LIBCPP_INLINE_VISIBILITY
114 auto const __test_fn = [this]() -> bool {
115 auto __old = __a_.load(memory_order_relaxed);
116 return (__old != 0) && __a_.compare_exchange_strong(__old, __old - 1, memory_order_acquire, memory_order_relaxed);
118 __cxx_atomic_wait(&__a_.__a_, __test_fn);
120 template <class _Rep, class _Period>
121 _LIBCPP_AVAILABILITY_SYNC _LIBCPP_INLINE_VISIBILITY
122 bool try_acquire_for(chrono::duration<_Rep, _Period> const& __rel_time)
124 if (__rel_time == chrono::duration<_Rep, _Period>::zero())
125 return try_acquire();
126 auto const __test_fn = [this]() { return try_acquire(); };
127 return std::__libcpp_thread_poll_with_backoff(__test_fn, __libcpp_timed_backoff_policy(), __rel_time);
129 _LIBCPP_AVAILABILITY_SYNC _LIBCPP_INLINE_VISIBILITY
132 auto __old = __a_.load(memory_order_acquire);
136 if (__a_.compare_exchange_strong(__old, __old - 1, memory_order_acquire, memory_order_relaxed))
142 template<ptrdiff_t __least_max_value = _LIBCPP_SEMAPHORE_MAX>
143 class counting_semaphore
145 __atomic_semaphore_base __semaphore_;
148 static_assert(__least_max_value >= 0, "The least maximum value must be a positive number");
150 static constexpr ptrdiff_t max() noexcept {
151 return __least_max_value;
154 _LIBCPP_INLINE_VISIBILITY
155 constexpr explicit counting_semaphore(ptrdiff_t __count) : __semaphore_(__count)
157 _LIBCPP_ASSERT_UNCATEGORIZED(
159 "counting_semaphore::counting_semaphore(ptrdiff_t): counting_semaphore cannot be "
160 "initialized with a negative value");
161 _LIBCPP_ASSERT_UNCATEGORIZED(
163 "counting_semaphore::counting_semaphore(ptrdiff_t): counting_semaphore cannot be "
164 "initialized with a value greater than max()");
166 ~counting_semaphore() = default;
168 counting_semaphore(const counting_semaphore&) = delete;
169 counting_semaphore& operator=(const counting_semaphore&) = delete;
171 _LIBCPP_AVAILABILITY_SYNC _LIBCPP_INLINE_VISIBILITY
172 void release(ptrdiff_t __update = 1)
174 _LIBCPP_ASSERT_UNCATEGORIZED(__update >= 0, "counting_semaphore:release called with a negative value");
175 __semaphore_.release(__update);
177 _LIBCPP_AVAILABILITY_SYNC _LIBCPP_INLINE_VISIBILITY
180 __semaphore_.acquire();
182 template<class _Rep, class _Period>
183 _LIBCPP_AVAILABILITY_SYNC _LIBCPP_INLINE_VISIBILITY
184 bool try_acquire_for(chrono::duration<_Rep, _Period> const& __rel_time)
186 return __semaphore_.try_acquire_for(chrono::duration_cast<chrono::nanoseconds>(__rel_time));
188 _LIBCPP_AVAILABILITY_SYNC _LIBCPP_INLINE_VISIBILITY
191 return __semaphore_.try_acquire();
193 template <class _Clock, class _Duration>
194 _LIBCPP_AVAILABILITY_SYNC _LIBCPP_INLINE_VISIBILITY
195 bool try_acquire_until(chrono::time_point<_Clock, _Duration> const& __abs_time)
197 auto const __current = _Clock::now();
198 if (__current >= __abs_time)
199 return try_acquire();
201 return try_acquire_for(__abs_time - __current);
205 using binary_semaphore = counting_semaphore<1>;
207 _LIBCPP_END_NAMESPACE_STD
209 #endif // _LIBCPP_STD_VER >= 14
213 #if !defined(_LIBCPP_REMOVE_TRANSITIVE_INCLUDES) && _LIBCPP_STD_VER <= 20
217 #endif //_LIBCPP_SEMAPHORE