2 //===--------------------------- semaphore --------------------------------===//
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>;
49 #include <__threading_support>
53 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
54 #pragma GCC system_header
57 #ifdef _LIBCPP_HAS_NO_THREADS
58 # error <semaphore> is not supported on this single threaded system
61 #if _LIBCPP_STD_VER >= 14
63 _LIBCPP_BEGIN_NAMESPACE_STD
67 __atomic_semaphore_base is the general-case implementation, to be used for
68 user-requested least-max values that exceed the OS implementation support
69 (incl. when the OS has no support of its own) and for binary semaphores.
71 It is a typical Dijsktra semaphore algorithm over atomics, wait and notify
72 functions. It avoids contention against users' own use of those facilities.
76 class __atomic_semaphore_base
78 __atomic_base<ptrdiff_t> __a;
81 _LIBCPP_INLINE_VISIBILITY
82 __atomic_semaphore_base(ptrdiff_t __count) : __a(__count)
85 _LIBCPP_AVAILABILITY_SYNC _LIBCPP_INLINE_VISIBILITY
86 void release(ptrdiff_t __update = 1)
88 if(0 < __a.fetch_add(__update, memory_order_release))
95 _LIBCPP_AVAILABILITY_SYNC _LIBCPP_INLINE_VISIBILITY
98 auto const __test_fn = [=]() -> bool {
99 auto __old = __a.load(memory_order_relaxed);
100 return (__old != 0) && __a.compare_exchange_strong(__old, __old - 1, memory_order_acquire, memory_order_relaxed);
102 __cxx_atomic_wait(&__a.__a_, __test_fn);
104 template <class Rep, class Period>
105 _LIBCPP_AVAILABILITY_SYNC _LIBCPP_INLINE_VISIBILITY
106 bool try_acquire_for(chrono::duration<Rep, Period> const& __rel_time)
108 auto const __test_fn = [=]() -> bool {
109 auto __old = __a.load(memory_order_acquire);
113 if(__a.compare_exchange_strong(__old, __old - 1, memory_order_acquire, memory_order_relaxed))
117 return __libcpp_thread_poll_with_backoff(__test_fn, __libcpp_timed_backoff_policy(), __rel_time);
121 #ifndef _LIBCPP_NO_NATIVE_SEMAPHORES
125 __platform_semaphore_base a simple wrapper for the OS semaphore type. That
126 is, every call is routed to the OS in the most direct manner possible.
130 class __platform_semaphore_base
132 __libcpp_semaphore_t __semaphore;
135 _LIBCPP_INLINE_VISIBILITY
136 __platform_semaphore_base(ptrdiff_t __count) :
139 __libcpp_semaphore_init(&__semaphore, __count);
141 _LIBCPP_INLINE_VISIBILITY
142 ~__platform_semaphore_base() {
143 __libcpp_semaphore_destroy(&__semaphore);
145 _LIBCPP_INLINE_VISIBILITY
146 void release(ptrdiff_t __update)
148 for(; __update; --__update)
149 __libcpp_semaphore_post(&__semaphore);
151 _LIBCPP_INLINE_VISIBILITY
154 __libcpp_semaphore_wait(&__semaphore);
156 _LIBCPP_INLINE_VISIBILITY
157 bool try_acquire_for(chrono::nanoseconds __rel_time)
159 return __libcpp_semaphore_wait_timed(&__semaphore, __rel_time);
163 template<ptrdiff_t __least_max_value>
164 using __semaphore_base =
165 typename conditional<(__least_max_value > 1 && __least_max_value <= _LIBCPP_SEMAPHORE_MAX),
166 __platform_semaphore_base,
167 __atomic_semaphore_base>::type;
171 template<ptrdiff_t __least_max_value>
172 using __semaphore_base =
173 __atomic_semaphore_base;
175 #define _LIBCPP_SEMAPHORE_MAX (numeric_limits<ptrdiff_t>::max())
177 #endif //_LIBCPP_NO_NATIVE_SEMAPHORES
179 template<ptrdiff_t __least_max_value = _LIBCPP_SEMAPHORE_MAX>
180 class counting_semaphore
182 __semaphore_base<__least_max_value> __semaphore;
185 static constexpr ptrdiff_t max() noexcept {
186 return __least_max_value;
189 _LIBCPP_INLINE_VISIBILITY
190 counting_semaphore(ptrdiff_t __count = 0) : __semaphore(__count) { }
191 ~counting_semaphore() = default;
193 counting_semaphore(const counting_semaphore&) = delete;
194 counting_semaphore& operator=(const counting_semaphore&) = delete;
196 _LIBCPP_AVAILABILITY_SYNC _LIBCPP_INLINE_VISIBILITY
197 void release(ptrdiff_t __update = 1)
199 __semaphore.release(__update);
201 _LIBCPP_AVAILABILITY_SYNC _LIBCPP_INLINE_VISIBILITY
204 __semaphore.acquire();
206 template<class Rep, class Period>
207 _LIBCPP_AVAILABILITY_SYNC _LIBCPP_INLINE_VISIBILITY
208 bool try_acquire_for(chrono::duration<Rep, Period> const& __rel_time)
210 return __semaphore.try_acquire_for(chrono::duration_cast<chrono::nanoseconds>(__rel_time));
212 _LIBCPP_AVAILABILITY_SYNC _LIBCPP_INLINE_VISIBILITY
215 return try_acquire_for(chrono::nanoseconds::zero());
217 template <class Clock, class Duration>
218 _LIBCPP_AVAILABILITY_SYNC _LIBCPP_INLINE_VISIBILITY
219 bool try_acquire_until(chrono::time_point<Clock, Duration> const& __abs_time)
221 auto const current = Clock::now();
222 if(current >= __abs_time)
223 return try_acquire();
225 return try_acquire_for(__abs_time - current);
229 using binary_semaphore = counting_semaphore<1>;
231 _LIBCPP_END_NAMESPACE_STD
233 #endif // _LIBCPP_STD_VER >= 14
235 #endif //_LIBCPP_SEMAPHORE