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 <__availability>
50 #include <__thread/timed_backoff_policy.h>
51 #include <__threading_support>
55 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
56 #pragma GCC system_header
59 #ifdef _LIBCPP_HAS_NO_THREADS
60 # error <semaphore> is not supported on this single threaded system
64 #include <__undef_macros>
66 #if _LIBCPP_STD_VER >= 14
68 _LIBCPP_BEGIN_NAMESPACE_STD
72 __atomic_semaphore_base is the general-case implementation.
73 It is a typical Dijkstra semaphore algorithm over atomics, wait and notify
74 functions. It avoids contention against users' own use of those facilities.
78 class __atomic_semaphore_base
80 __atomic_base<ptrdiff_t> __a;
83 _LIBCPP_INLINE_VISIBILITY
84 constexpr explicit __atomic_semaphore_base(ptrdiff_t __count) : __a(__count)
87 _LIBCPP_AVAILABILITY_SYNC _LIBCPP_INLINE_VISIBILITY
88 void release(ptrdiff_t __update = 1)
90 if(0 < __a.fetch_add(__update, memory_order_release))
97 _LIBCPP_AVAILABILITY_SYNC _LIBCPP_INLINE_VISIBILITY
100 auto const __test_fn = [this]() -> bool {
101 auto __old = __a.load(memory_order_relaxed);
102 return (__old != 0) && __a.compare_exchange_strong(__old, __old - 1, memory_order_acquire, memory_order_relaxed);
104 __cxx_atomic_wait(&__a.__a_, __test_fn);
106 template <class Rep, class Period>
107 _LIBCPP_AVAILABILITY_SYNC _LIBCPP_INLINE_VISIBILITY
108 bool try_acquire_for(chrono::duration<Rep, Period> const& __rel_time)
110 if (__rel_time == chrono::duration<Rep, Period>::zero())
111 return try_acquire();
112 auto const __test_fn = [this]() { return try_acquire(); };
113 return __libcpp_thread_poll_with_backoff(__test_fn, __libcpp_timed_backoff_policy(), __rel_time);
115 _LIBCPP_AVAILABILITY_SYNC _LIBCPP_INLINE_VISIBILITY
118 auto __old = __a.load(memory_order_acquire);
122 if (__a.compare_exchange_strong(__old, __old - 1, memory_order_acquire, memory_order_relaxed))
128 #define _LIBCPP_SEMAPHORE_MAX (numeric_limits<ptrdiff_t>::max())
130 template<ptrdiff_t __least_max_value = _LIBCPP_SEMAPHORE_MAX>
131 class counting_semaphore
133 __atomic_semaphore_base __semaphore;
136 static constexpr ptrdiff_t max() noexcept {
137 return __least_max_value;
140 _LIBCPP_INLINE_VISIBILITY
141 constexpr explicit counting_semaphore(ptrdiff_t __count) : __semaphore(__count) { }
142 ~counting_semaphore() = default;
144 counting_semaphore(const counting_semaphore&) = delete;
145 counting_semaphore& operator=(const counting_semaphore&) = delete;
147 _LIBCPP_AVAILABILITY_SYNC _LIBCPP_INLINE_VISIBILITY
148 void release(ptrdiff_t __update = 1)
150 __semaphore.release(__update);
152 _LIBCPP_AVAILABILITY_SYNC _LIBCPP_INLINE_VISIBILITY
155 __semaphore.acquire();
157 template<class Rep, class Period>
158 _LIBCPP_AVAILABILITY_SYNC _LIBCPP_INLINE_VISIBILITY
159 bool try_acquire_for(chrono::duration<Rep, Period> const& __rel_time)
161 return __semaphore.try_acquire_for(chrono::duration_cast<chrono::nanoseconds>(__rel_time));
163 _LIBCPP_AVAILABILITY_SYNC _LIBCPP_INLINE_VISIBILITY
166 return __semaphore.try_acquire();
168 template <class Clock, class Duration>
169 _LIBCPP_AVAILABILITY_SYNC _LIBCPP_INLINE_VISIBILITY
170 bool try_acquire_until(chrono::time_point<Clock, Duration> const& __abs_time)
172 auto const current = Clock::now();
173 if (current >= __abs_time)
174 return try_acquire();
176 return try_acquire_for(__abs_time - current);
180 using binary_semaphore = counting_semaphore<1>;
182 _LIBCPP_END_NAMESPACE_STD
184 #endif // _LIBCPP_STD_VER >= 14
188 #endif //_LIBCPP_SEMAPHORE