blob: 971ca2f70adc2f7903a44c1c5a0e828a7b19b112 [file] [log] [blame]
Yi Kong878f9942023-12-13 12:55:04 +09001// -*- C++ -*-
2//===----------------------------------------------------------------------===//
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
16namespace std {
17
18template<ptrdiff_t least_max_value = implementation-defined>
19class counting_semaphore
20{
21public:
22static constexpr ptrdiff_t max() noexcept;
23
24constexpr explicit counting_semaphore(ptrdiff_t desired);
25~counting_semaphore();
26
27counting_semaphore(const counting_semaphore&) = delete;
28counting_semaphore& operator=(const counting_semaphore&) = delete;
29
30void release(ptrdiff_t update = 1);
31void acquire();
32bool try_acquire() noexcept;
33template<class Rep, class Period>
34 bool try_acquire_for(const chrono::duration<Rep, Period>& rel_time);
35template<class Clock, class Duration>
36 bool try_acquire_until(const chrono::time_point<Clock, Duration>& abs_time);
37
38private:
39ptrdiff_t counter; // exposition only
40};
41
42using binary_semaphore = counting_semaphore<1>;
43
44}
45
46*/
47
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>
54#include <__config>
55#include <__thread/timed_backoff_policy.h>
56#include <__threading_support>
57#include <cstddef>
58#include <limits>
59#include <version>
60
61#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
62# pragma GCC system_header
63#endif
64
65#ifdef _LIBCPP_HAS_NO_THREADS
66# error "<semaphore> is not supported since libc++ has been configured without support for threads."
67#endif
68
69_LIBCPP_PUSH_MACROS
70#include <__undef_macros>
71
72#if _LIBCPP_STD_VER >= 14
73
74_LIBCPP_BEGIN_NAMESPACE_STD
75
76/*
77
78__atomic_semaphore_base is the general-case implementation.
79It is a typical Dijkstra semaphore algorithm over atomics, wait and notify
80functions. It avoids contention against users' own use of those facilities.
81
82*/
83
84#define _LIBCPP_SEMAPHORE_MAX (numeric_limits<ptrdiff_t>::max())
85
86class __atomic_semaphore_base
87{
88 __atomic_base<ptrdiff_t> __a_;
89
90public:
91 _LIBCPP_INLINE_VISIBILITY
92 constexpr explicit __atomic_semaphore_base(ptrdiff_t __count) : __a_(__count)
93 {
94 }
95 _LIBCPP_AVAILABILITY_SYNC _LIBCPP_INLINE_VISIBILITY
96 void release(ptrdiff_t __update = 1)
97 {
98 auto __old = __a_.fetch_add(__update, memory_order_release);
99 _LIBCPP_ASSERT_UNCATEGORIZED(__update <= _LIBCPP_SEMAPHORE_MAX - __old, "update is greater than the expected value");
100
101 if (__old > 0)
102 {
103 // Nothing to do
104 }
105 else if (__update > 1)
106 __a_.notify_all();
107 else
108 __a_.notify_one();
109 }
110 _LIBCPP_AVAILABILITY_SYNC _LIBCPP_INLINE_VISIBILITY
111 void acquire()
112 {
113 auto const __test_fn = [this]() -> bool {
114 auto __old = __a_.load(memory_order_relaxed);
115 return (__old != 0) && __a_.compare_exchange_strong(__old, __old - 1, memory_order_acquire, memory_order_relaxed);
116 };
117 __cxx_atomic_wait(&__a_.__a_, __test_fn);
118 }
119 template <class _Rep, class _Period>
120 _LIBCPP_AVAILABILITY_SYNC _LIBCPP_INLINE_VISIBILITY
121 bool try_acquire_for(chrono::duration<_Rep, _Period> const& __rel_time)
122 {
123 if (__rel_time == chrono::duration<_Rep, _Period>::zero())
124 return try_acquire();
125 auto const __test_fn = [this]() { return try_acquire(); };
126 return std::__libcpp_thread_poll_with_backoff(__test_fn, __libcpp_timed_backoff_policy(), __rel_time);
127 }
128 _LIBCPP_AVAILABILITY_SYNC _LIBCPP_INLINE_VISIBILITY
129 bool try_acquire()
130 {
131 auto __old = __a_.load(memory_order_acquire);
132 while (true) {
133 if (__old == 0)
134 return false;
135 if (__a_.compare_exchange_strong(__old, __old - 1, memory_order_acquire, memory_order_relaxed))
136 return true;
137 }
138 }
139};
140
141template<ptrdiff_t __least_max_value = _LIBCPP_SEMAPHORE_MAX>
142class counting_semaphore
143{
144 __atomic_semaphore_base __semaphore_;
145
146public:
147 static_assert(__least_max_value >= 0, "The least maximum value must be a positive number");
148
149 static constexpr ptrdiff_t max() noexcept {
150 return __least_max_value;
151 }
152
153 _LIBCPP_INLINE_VISIBILITY
154 constexpr explicit counting_semaphore(ptrdiff_t __count) : __semaphore_(__count)
155 {
156 _LIBCPP_ASSERT_UNCATEGORIZED(
157 __count >= 0,
158 "counting_semaphore::counting_semaphore(ptrdiff_t): counting_semaphore cannot be "
159 "initialized with a negative value");
160 _LIBCPP_ASSERT_UNCATEGORIZED(
161 __count <= max(),
162 "counting_semaphore::counting_semaphore(ptrdiff_t): counting_semaphore cannot be "
163 "initialized with a value greater than max()");
164 }
165 ~counting_semaphore() = default;
166
167 counting_semaphore(const counting_semaphore&) = delete;
168 counting_semaphore& operator=(const counting_semaphore&) = delete;
169
170 _LIBCPP_AVAILABILITY_SYNC _LIBCPP_INLINE_VISIBILITY
171 void release(ptrdiff_t __update = 1)
172 {
173 _LIBCPP_ASSERT_UNCATEGORIZED(__update >= 0, "counting_semaphore:release called with a negative value");
174 __semaphore_.release(__update);
175 }
176 _LIBCPP_AVAILABILITY_SYNC _LIBCPP_INLINE_VISIBILITY
177 void acquire()
178 {
179 __semaphore_.acquire();
180 }
181 template<class _Rep, class _Period>
182 _LIBCPP_AVAILABILITY_SYNC _LIBCPP_INLINE_VISIBILITY
183 bool try_acquire_for(chrono::duration<_Rep, _Period> const& __rel_time)
184 {
185 return __semaphore_.try_acquire_for(chrono::duration_cast<chrono::nanoseconds>(__rel_time));
186 }
187 _LIBCPP_AVAILABILITY_SYNC _LIBCPP_INLINE_VISIBILITY
188 bool try_acquire()
189 {
190 return __semaphore_.try_acquire();
191 }
192 template <class _Clock, class _Duration>
193 _LIBCPP_AVAILABILITY_SYNC _LIBCPP_INLINE_VISIBILITY
194 bool try_acquire_until(chrono::time_point<_Clock, _Duration> const& __abs_time)
195 {
196 auto const __current = _Clock::now();
197 if (__current >= __abs_time)
198 return try_acquire();
199 else
200 return try_acquire_for(__abs_time - __current);
201 }
202};
203
204using binary_semaphore = counting_semaphore<1>;
205
206_LIBCPP_END_NAMESPACE_STD
207
208#endif // _LIBCPP_STD_VER >= 14
209
210_LIBCPP_POP_MACROS
211
212#if !defined(_LIBCPP_REMOVE_TRANSITIVE_INCLUDES) && _LIBCPP_STD_VER <= 20
213# include <atomic>
214#endif
215
216#endif //_LIBCPP_SEMAPHORE