Olivier Giroux | 161e6e8 | 2020-02-18 09:58:34 -0500 | [diff] [blame] | 1 | // -*- C++ -*- |
Louis Dionne | 9bd9388 | 2021-11-17 16:25:01 -0500 | [diff] [blame] | 2 | //===----------------------------------------------------------------------===// |
Olivier Giroux | 161e6e8 | 2020-02-18 09:58:34 -0500 | [diff] [blame] | 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> |
Louis Dionne | caf2683 | 2022-01-10 09:43:29 -0500 | [diff] [blame] | 50 | #include <__thread/timed_backoff_policy.h> |
Olivier Giroux | 161e6e8 | 2020-02-18 09:58:34 -0500 | [diff] [blame] | 51 | #include <__threading_support> |
| 52 | #include <atomic> |
Mark de Wever | ce8f12c | 2021-12-22 18:14:14 +0100 | [diff] [blame] | 53 | #include <version> |
Olivier Giroux | 161e6e8 | 2020-02-18 09:58:34 -0500 | [diff] [blame] | 54 | |
| 55 | #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) |
Arthur O'Dwyer | 6eeaa00 | 2022-02-01 20:16:40 -0500 | [diff] [blame] | 56 | # pragma GCC system_header |
Olivier Giroux | 161e6e8 | 2020-02-18 09:58:34 -0500 | [diff] [blame] | 57 | #endif |
| 58 | |
| 59 | #ifdef _LIBCPP_HAS_NO_THREADS |
| 60 | # error <semaphore> is not supported on this single threaded system |
| 61 | #endif |
| 62 | |
Arthur O'Dwyer | 2422fa2 | 2020-12-02 18:55:01 -0500 | [diff] [blame] | 63 | _LIBCPP_PUSH_MACROS |
| 64 | #include <__undef_macros> |
| 65 | |
Louis Dionne | 1ab9531 | 2020-02-24 18:12:44 -0500 | [diff] [blame] | 66 | #if _LIBCPP_STD_VER >= 14 |
Olivier Giroux | 161e6e8 | 2020-02-18 09:58:34 -0500 | [diff] [blame] | 67 | |
| 68 | _LIBCPP_BEGIN_NAMESPACE_STD |
| 69 | |
| 70 | /* |
| 71 | |
Arthur O'Dwyer | 7a35d63 | 2021-09-20 18:03:49 -0400 | [diff] [blame] | 72 | __atomic_semaphore_base is the general-case implementation. |
Arthur O'Dwyer | df5df56 | 2020-12-12 11:57:32 -0500 | [diff] [blame] | 73 | It is a typical Dijkstra semaphore algorithm over atomics, wait and notify |
Olivier Giroux | 161e6e8 | 2020-02-18 09:58:34 -0500 | [diff] [blame] | 74 | functions. It avoids contention against users' own use of those facilities. |
| 75 | |
| 76 | */ |
| 77 | |
| 78 | class __atomic_semaphore_base |
| 79 | { |
| 80 | __atomic_base<ptrdiff_t> __a; |
| 81 | |
| 82 | public: |
| 83 | _LIBCPP_INLINE_VISIBILITY |
Arthur O'Dwyer | 227e00b | 2021-09-16 23:14:57 -0400 | [diff] [blame] | 84 | constexpr explicit __atomic_semaphore_base(ptrdiff_t __count) : __a(__count) |
Olivier Giroux | 161e6e8 | 2020-02-18 09:58:34 -0500 | [diff] [blame] | 85 | { |
| 86 | } |
Louis Dionne | 3dde4ab | 2020-02-24 10:09:29 -0500 | [diff] [blame] | 87 | _LIBCPP_AVAILABILITY_SYNC _LIBCPP_INLINE_VISIBILITY |
Olivier Giroux | 161e6e8 | 2020-02-18 09:58:34 -0500 | [diff] [blame] | 88 | void release(ptrdiff_t __update = 1) |
| 89 | { |
| 90 | if(0 < __a.fetch_add(__update, memory_order_release)) |
| 91 | ; |
| 92 | else if(__update > 1) |
| 93 | __a.notify_all(); |
| 94 | else |
| 95 | __a.notify_one(); |
| 96 | } |
Louis Dionne | 3dde4ab | 2020-02-24 10:09:29 -0500 | [diff] [blame] | 97 | _LIBCPP_AVAILABILITY_SYNC _LIBCPP_INLINE_VISIBILITY |
Olivier Giroux | 161e6e8 | 2020-02-18 09:58:34 -0500 | [diff] [blame] | 98 | void acquire() |
| 99 | { |
Louis Dionne | af6be62 | 2021-07-27 17:30:47 -0400 | [diff] [blame] | 100 | auto const __test_fn = [this]() -> bool { |
Olivier Giroux | 161e6e8 | 2020-02-18 09:58:34 -0500 | [diff] [blame] | 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); |
| 103 | }; |
| 104 | __cxx_atomic_wait(&__a.__a_, __test_fn); |
| 105 | } |
| 106 | template <class Rep, class Period> |
Louis Dionne | 3dde4ab | 2020-02-24 10:09:29 -0500 | [diff] [blame] | 107 | _LIBCPP_AVAILABILITY_SYNC _LIBCPP_INLINE_VISIBILITY |
Olivier Giroux | 161e6e8 | 2020-02-18 09:58:34 -0500 | [diff] [blame] | 108 | bool try_acquire_for(chrono::duration<Rep, Period> const& __rel_time) |
| 109 | { |
Arthur O'Dwyer | a95b840 | 2021-11-04 17:58:33 -0400 | [diff] [blame] | 110 | if (__rel_time == chrono::duration<Rep, Period>::zero()) |
| 111 | return try_acquire(); |
| 112 | auto const __test_fn = [this]() { return try_acquire(); }; |
Olivier Giroux | 161e6e8 | 2020-02-18 09:58:34 -0500 | [diff] [blame] | 113 | return __libcpp_thread_poll_with_backoff(__test_fn, __libcpp_timed_backoff_policy(), __rel_time); |
| 114 | } |
Arthur O'Dwyer | a95b840 | 2021-11-04 17:58:33 -0400 | [diff] [blame] | 115 | _LIBCPP_AVAILABILITY_SYNC _LIBCPP_INLINE_VISIBILITY |
| 116 | bool try_acquire() |
| 117 | { |
| 118 | auto __old = __a.load(memory_order_acquire); |
| 119 | while (true) { |
| 120 | if (__old == 0) |
| 121 | return false; |
| 122 | if (__a.compare_exchange_strong(__old, __old - 1, memory_order_acquire, memory_order_relaxed)) |
| 123 | return true; |
| 124 | } |
| 125 | } |
Olivier Giroux | 161e6e8 | 2020-02-18 09:58:34 -0500 | [diff] [blame] | 126 | }; |
| 127 | |
ogiroux | 945d52b | 2020-02-26 16:49:37 -0800 | [diff] [blame] | 128 | #define _LIBCPP_SEMAPHORE_MAX (numeric_limits<ptrdiff_t>::max()) |
| 129 | |
Olivier Giroux | 161e6e8 | 2020-02-18 09:58:34 -0500 | [diff] [blame] | 130 | template<ptrdiff_t __least_max_value = _LIBCPP_SEMAPHORE_MAX> |
| 131 | class counting_semaphore |
| 132 | { |
Arthur O'Dwyer | 7a35d63 | 2021-09-20 18:03:49 -0400 | [diff] [blame] | 133 | __atomic_semaphore_base __semaphore; |
Olivier Giroux | 161e6e8 | 2020-02-18 09:58:34 -0500 | [diff] [blame] | 134 | |
| 135 | public: |
| 136 | static constexpr ptrdiff_t max() noexcept { |
| 137 | return __least_max_value; |
| 138 | } |
| 139 | |
| 140 | _LIBCPP_INLINE_VISIBILITY |
Arthur O'Dwyer | 227e00b | 2021-09-16 23:14:57 -0400 | [diff] [blame] | 141 | constexpr explicit counting_semaphore(ptrdiff_t __count) : __semaphore(__count) { } |
Olivier Giroux | 161e6e8 | 2020-02-18 09:58:34 -0500 | [diff] [blame] | 142 | ~counting_semaphore() = default; |
| 143 | |
| 144 | counting_semaphore(const counting_semaphore&) = delete; |
| 145 | counting_semaphore& operator=(const counting_semaphore&) = delete; |
| 146 | |
Louis Dionne | 3dde4ab | 2020-02-24 10:09:29 -0500 | [diff] [blame] | 147 | _LIBCPP_AVAILABILITY_SYNC _LIBCPP_INLINE_VISIBILITY |
Olivier Giroux | 161e6e8 | 2020-02-18 09:58:34 -0500 | [diff] [blame] | 148 | void release(ptrdiff_t __update = 1) |
| 149 | { |
| 150 | __semaphore.release(__update); |
| 151 | } |
Louis Dionne | 3dde4ab | 2020-02-24 10:09:29 -0500 | [diff] [blame] | 152 | _LIBCPP_AVAILABILITY_SYNC _LIBCPP_INLINE_VISIBILITY |
Olivier Giroux | 161e6e8 | 2020-02-18 09:58:34 -0500 | [diff] [blame] | 153 | void acquire() |
| 154 | { |
| 155 | __semaphore.acquire(); |
| 156 | } |
| 157 | template<class Rep, class Period> |
Louis Dionne | 3dde4ab | 2020-02-24 10:09:29 -0500 | [diff] [blame] | 158 | _LIBCPP_AVAILABILITY_SYNC _LIBCPP_INLINE_VISIBILITY |
Olivier Giroux | 161e6e8 | 2020-02-18 09:58:34 -0500 | [diff] [blame] | 159 | bool try_acquire_for(chrono::duration<Rep, Period> const& __rel_time) |
| 160 | { |
| 161 | return __semaphore.try_acquire_for(chrono::duration_cast<chrono::nanoseconds>(__rel_time)); |
| 162 | } |
Louis Dionne | 3dde4ab | 2020-02-24 10:09:29 -0500 | [diff] [blame] | 163 | _LIBCPP_AVAILABILITY_SYNC _LIBCPP_INLINE_VISIBILITY |
Olivier Giroux | 161e6e8 | 2020-02-18 09:58:34 -0500 | [diff] [blame] | 164 | bool try_acquire() |
| 165 | { |
Arthur O'Dwyer | a95b840 | 2021-11-04 17:58:33 -0400 | [diff] [blame] | 166 | return __semaphore.try_acquire(); |
Olivier Giroux | 161e6e8 | 2020-02-18 09:58:34 -0500 | [diff] [blame] | 167 | } |
| 168 | template <class Clock, class Duration> |
Louis Dionne | 3dde4ab | 2020-02-24 10:09:29 -0500 | [diff] [blame] | 169 | _LIBCPP_AVAILABILITY_SYNC _LIBCPP_INLINE_VISIBILITY |
Olivier Giroux | 161e6e8 | 2020-02-18 09:58:34 -0500 | [diff] [blame] | 170 | bool try_acquire_until(chrono::time_point<Clock, Duration> const& __abs_time) |
| 171 | { |
| 172 | auto const current = Clock::now(); |
Arthur O'Dwyer | a95b840 | 2021-11-04 17:58:33 -0400 | [diff] [blame] | 173 | if (current >= __abs_time) |
Olivier Giroux | 161e6e8 | 2020-02-18 09:58:34 -0500 | [diff] [blame] | 174 | return try_acquire(); |
| 175 | else |
| 176 | return try_acquire_for(__abs_time - current); |
| 177 | } |
| 178 | }; |
| 179 | |
| 180 | using binary_semaphore = counting_semaphore<1>; |
| 181 | |
| 182 | _LIBCPP_END_NAMESPACE_STD |
| 183 | |
Louis Dionne | 1ab9531 | 2020-02-24 18:12:44 -0500 | [diff] [blame] | 184 | #endif // _LIBCPP_STD_VER >= 14 |
| 185 | |
Arthur O'Dwyer | 2422fa2 | 2020-12-02 18:55:01 -0500 | [diff] [blame] | 186 | _LIBCPP_POP_MACROS |
| 187 | |
Olivier Giroux | 161e6e8 | 2020-02-18 09:58:34 -0500 | [diff] [blame] | 188 | #endif //_LIBCPP_SEMAPHORE |