Olivier Giroux | 161e6e8 | 2020-02-18 09:58:34 -0500 | [diff] [blame] | 1 | // -*- C++ -*- |
| 2 | //===--------------------------- semaphore --------------------------------===// |
| 3 | // |
| 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 |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
| 10 | #ifndef _LIBCPP_SEMAPHORE |
| 11 | #define _LIBCPP_SEMAPHORE |
| 12 | |
| 13 | /* |
| 14 | semaphore synopsis |
| 15 | |
| 16 | namespace std { |
| 17 | |
| 18 | template<ptrdiff_t least_max_value = implementation-defined> |
| 19 | class counting_semaphore |
| 20 | { |
| 21 | public: |
| 22 | static constexpr ptrdiff_t max() noexcept; |
| 23 | |
| 24 | constexpr explicit counting_semaphore(ptrdiff_t desired); |
| 25 | ~counting_semaphore(); |
| 26 | |
| 27 | counting_semaphore(const counting_semaphore&) = delete; |
| 28 | counting_semaphore& operator=(const counting_semaphore&) = delete; |
| 29 | |
| 30 | void release(ptrdiff_t update = 1); |
| 31 | void acquire(); |
| 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); |
| 37 | |
| 38 | private: |
| 39 | ptrdiff_t counter; // exposition only |
| 40 | }; |
| 41 | |
| 42 | using binary_semaphore = counting_semaphore<1>; |
| 43 | |
| 44 | } |
| 45 | |
| 46 | */ |
| 47 | |
Louis Dionne | 73912b2 | 2020-11-04 15:01:25 -0500 | [diff] [blame] | 48 | #include <__availability> |
Arthur O'Dwyer | ef18160 | 2021-05-19 11:57:04 -0400 | [diff] [blame^] | 49 | #include <__config> |
Olivier Giroux | 161e6e8 | 2020-02-18 09:58:34 -0500 | [diff] [blame] | 50 | #include <__threading_support> |
| 51 | #include <atomic> |
Olivier Giroux | 161e6e8 | 2020-02-18 09:58:34 -0500 | [diff] [blame] | 52 | |
| 53 | #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) |
| 54 | #pragma GCC system_header |
| 55 | #endif |
| 56 | |
| 57 | #ifdef _LIBCPP_HAS_NO_THREADS |
| 58 | # error <semaphore> is not supported on this single threaded system |
| 59 | #endif |
| 60 | |
Arthur O'Dwyer | 2422fa2 | 2020-12-02 18:55:01 -0500 | [diff] [blame] | 61 | _LIBCPP_PUSH_MACROS |
| 62 | #include <__undef_macros> |
| 63 | |
Louis Dionne | 1ab9531 | 2020-02-24 18:12:44 -0500 | [diff] [blame] | 64 | #if _LIBCPP_STD_VER >= 14 |
Olivier Giroux | 161e6e8 | 2020-02-18 09:58:34 -0500 | [diff] [blame] | 65 | |
| 66 | _LIBCPP_BEGIN_NAMESPACE_STD |
| 67 | |
| 68 | /* |
| 69 | |
| 70 | __atomic_semaphore_base is the general-case implementation, to be used for |
| 71 | user-requested least-max values that exceed the OS implementation support |
| 72 | (incl. when the OS has no support of its own) and for binary semaphores. |
| 73 | |
Arthur O'Dwyer | df5df56 | 2020-12-12 11:57:32 -0500 | [diff] [blame] | 74 | It is a typical Dijkstra semaphore algorithm over atomics, wait and notify |
Olivier Giroux | 161e6e8 | 2020-02-18 09:58:34 -0500 | [diff] [blame] | 75 | functions. It avoids contention against users' own use of those facilities. |
| 76 | |
| 77 | */ |
| 78 | |
| 79 | class __atomic_semaphore_base |
| 80 | { |
| 81 | __atomic_base<ptrdiff_t> __a; |
| 82 | |
| 83 | public: |
| 84 | _LIBCPP_INLINE_VISIBILITY |
| 85 | __atomic_semaphore_base(ptrdiff_t __count) : __a(__count) |
| 86 | { |
| 87 | } |
Louis Dionne | 3dde4ab | 2020-02-24 10:09:29 -0500 | [diff] [blame] | 88 | _LIBCPP_AVAILABILITY_SYNC _LIBCPP_INLINE_VISIBILITY |
Olivier Giroux | 161e6e8 | 2020-02-18 09:58:34 -0500 | [diff] [blame] | 89 | void release(ptrdiff_t __update = 1) |
| 90 | { |
| 91 | if(0 < __a.fetch_add(__update, memory_order_release)) |
| 92 | ; |
| 93 | else if(__update > 1) |
| 94 | __a.notify_all(); |
| 95 | else |
| 96 | __a.notify_one(); |
| 97 | } |
Louis Dionne | 3dde4ab | 2020-02-24 10:09:29 -0500 | [diff] [blame] | 98 | _LIBCPP_AVAILABILITY_SYNC _LIBCPP_INLINE_VISIBILITY |
Olivier Giroux | 161e6e8 | 2020-02-18 09:58:34 -0500 | [diff] [blame] | 99 | void acquire() |
| 100 | { |
| 101 | auto const __test_fn = [=]() -> bool { |
| 102 | auto __old = __a.load(memory_order_relaxed); |
| 103 | return (__old != 0) && __a.compare_exchange_strong(__old, __old - 1, memory_order_acquire, memory_order_relaxed); |
| 104 | }; |
| 105 | __cxx_atomic_wait(&__a.__a_, __test_fn); |
| 106 | } |
| 107 | template <class Rep, class Period> |
Louis Dionne | 3dde4ab | 2020-02-24 10:09:29 -0500 | [diff] [blame] | 108 | _LIBCPP_AVAILABILITY_SYNC _LIBCPP_INLINE_VISIBILITY |
Olivier Giroux | 161e6e8 | 2020-02-18 09:58:34 -0500 | [diff] [blame] | 109 | bool try_acquire_for(chrono::duration<Rep, Period> const& __rel_time) |
| 110 | { |
| 111 | auto const __test_fn = [=]() -> bool { |
| 112 | auto __old = __a.load(memory_order_acquire); |
| 113 | while(1) { |
| 114 | if (__old == 0) |
| 115 | return false; |
| 116 | if(__a.compare_exchange_strong(__old, __old - 1, memory_order_acquire, memory_order_relaxed)) |
| 117 | return true; |
| 118 | } |
| 119 | }; |
| 120 | return __libcpp_thread_poll_with_backoff(__test_fn, __libcpp_timed_backoff_policy(), __rel_time); |
| 121 | } |
| 122 | }; |
| 123 | |
| 124 | #ifndef _LIBCPP_NO_NATIVE_SEMAPHORES |
| 125 | |
| 126 | /* |
| 127 | |
| 128 | __platform_semaphore_base a simple wrapper for the OS semaphore type. That |
| 129 | is, every call is routed to the OS in the most direct manner possible. |
| 130 | |
| 131 | */ |
| 132 | |
| 133 | class __platform_semaphore_base |
| 134 | { |
| 135 | __libcpp_semaphore_t __semaphore; |
| 136 | |
| 137 | public: |
| 138 | _LIBCPP_INLINE_VISIBILITY |
| 139 | __platform_semaphore_base(ptrdiff_t __count) : |
| 140 | __semaphore() |
| 141 | { |
| 142 | __libcpp_semaphore_init(&__semaphore, __count); |
| 143 | } |
| 144 | _LIBCPP_INLINE_VISIBILITY |
| 145 | ~__platform_semaphore_base() { |
| 146 | __libcpp_semaphore_destroy(&__semaphore); |
| 147 | } |
| 148 | _LIBCPP_INLINE_VISIBILITY |
| 149 | void release(ptrdiff_t __update) |
| 150 | { |
| 151 | for(; __update; --__update) |
| 152 | __libcpp_semaphore_post(&__semaphore); |
| 153 | } |
| 154 | _LIBCPP_INLINE_VISIBILITY |
| 155 | void acquire() |
| 156 | { |
| 157 | __libcpp_semaphore_wait(&__semaphore); |
| 158 | } |
| 159 | _LIBCPP_INLINE_VISIBILITY |
| 160 | bool try_acquire_for(chrono::nanoseconds __rel_time) |
| 161 | { |
| 162 | return __libcpp_semaphore_wait_timed(&__semaphore, __rel_time); |
| 163 | } |
| 164 | }; |
| 165 | |
| 166 | template<ptrdiff_t __least_max_value> |
| 167 | using __semaphore_base = |
| 168 | typename conditional<(__least_max_value > 1 && __least_max_value <= _LIBCPP_SEMAPHORE_MAX), |
| 169 | __platform_semaphore_base, |
| 170 | __atomic_semaphore_base>::type; |
| 171 | |
| 172 | #else |
| 173 | |
| 174 | template<ptrdiff_t __least_max_value> |
| 175 | using __semaphore_base = |
| 176 | __atomic_semaphore_base; |
| 177 | |
ogiroux | 945d52b | 2020-02-26 16:49:37 -0800 | [diff] [blame] | 178 | #define _LIBCPP_SEMAPHORE_MAX (numeric_limits<ptrdiff_t>::max()) |
| 179 | |
Olivier Giroux | 161e6e8 | 2020-02-18 09:58:34 -0500 | [diff] [blame] | 180 | #endif //_LIBCPP_NO_NATIVE_SEMAPHORES |
| 181 | |
| 182 | template<ptrdiff_t __least_max_value = _LIBCPP_SEMAPHORE_MAX> |
| 183 | class counting_semaphore |
| 184 | { |
| 185 | __semaphore_base<__least_max_value> __semaphore; |
| 186 | |
| 187 | public: |
| 188 | static constexpr ptrdiff_t max() noexcept { |
| 189 | return __least_max_value; |
| 190 | } |
| 191 | |
| 192 | _LIBCPP_INLINE_VISIBILITY |
| 193 | counting_semaphore(ptrdiff_t __count = 0) : __semaphore(__count) { } |
| 194 | ~counting_semaphore() = default; |
| 195 | |
| 196 | counting_semaphore(const counting_semaphore&) = delete; |
| 197 | counting_semaphore& operator=(const counting_semaphore&) = delete; |
| 198 | |
Louis Dionne | 3dde4ab | 2020-02-24 10:09:29 -0500 | [diff] [blame] | 199 | _LIBCPP_AVAILABILITY_SYNC _LIBCPP_INLINE_VISIBILITY |
Olivier Giroux | 161e6e8 | 2020-02-18 09:58:34 -0500 | [diff] [blame] | 200 | void release(ptrdiff_t __update = 1) |
| 201 | { |
| 202 | __semaphore.release(__update); |
| 203 | } |
Louis Dionne | 3dde4ab | 2020-02-24 10:09:29 -0500 | [diff] [blame] | 204 | _LIBCPP_AVAILABILITY_SYNC _LIBCPP_INLINE_VISIBILITY |
Olivier Giroux | 161e6e8 | 2020-02-18 09:58:34 -0500 | [diff] [blame] | 205 | void acquire() |
| 206 | { |
| 207 | __semaphore.acquire(); |
| 208 | } |
| 209 | template<class Rep, class Period> |
Louis Dionne | 3dde4ab | 2020-02-24 10:09:29 -0500 | [diff] [blame] | 210 | _LIBCPP_AVAILABILITY_SYNC _LIBCPP_INLINE_VISIBILITY |
Olivier Giroux | 161e6e8 | 2020-02-18 09:58:34 -0500 | [diff] [blame] | 211 | bool try_acquire_for(chrono::duration<Rep, Period> const& __rel_time) |
| 212 | { |
| 213 | return __semaphore.try_acquire_for(chrono::duration_cast<chrono::nanoseconds>(__rel_time)); |
| 214 | } |
Louis Dionne | 3dde4ab | 2020-02-24 10:09:29 -0500 | [diff] [blame] | 215 | _LIBCPP_AVAILABILITY_SYNC _LIBCPP_INLINE_VISIBILITY |
Olivier Giroux | 161e6e8 | 2020-02-18 09:58:34 -0500 | [diff] [blame] | 216 | bool try_acquire() |
| 217 | { |
| 218 | return try_acquire_for(chrono::nanoseconds::zero()); |
| 219 | } |
| 220 | template <class Clock, class Duration> |
Louis Dionne | 3dde4ab | 2020-02-24 10:09:29 -0500 | [diff] [blame] | 221 | _LIBCPP_AVAILABILITY_SYNC _LIBCPP_INLINE_VISIBILITY |
Olivier Giroux | 161e6e8 | 2020-02-18 09:58:34 -0500 | [diff] [blame] | 222 | bool try_acquire_until(chrono::time_point<Clock, Duration> const& __abs_time) |
| 223 | { |
| 224 | auto const current = Clock::now(); |
| 225 | if(current >= __abs_time) |
| 226 | return try_acquire(); |
| 227 | else |
| 228 | return try_acquire_for(__abs_time - current); |
| 229 | } |
| 230 | }; |
| 231 | |
| 232 | using binary_semaphore = counting_semaphore<1>; |
| 233 | |
| 234 | _LIBCPP_END_NAMESPACE_STD |
| 235 | |
Louis Dionne | 1ab9531 | 2020-02-24 18:12:44 -0500 | [diff] [blame] | 236 | #endif // _LIBCPP_STD_VER >= 14 |
| 237 | |
Arthur O'Dwyer | 2422fa2 | 2020-12-02 18:55:01 -0500 | [diff] [blame] | 238 | _LIBCPP_POP_MACROS |
| 239 | |
Olivier Giroux | 161e6e8 | 2020-02-18 09:58:34 -0500 | [diff] [blame] | 240 | #endif //_LIBCPP_SEMAPHORE |