blob: cd27534d3e85b7dcc8afd347e78c3be1f45ab2c9 [file] [log] [blame]
Olivier Giroux161e6e82020-02-18 09:58:34 -05001// -*- 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
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 <__config>
49#include <__threading_support>
50#include <atomic>
51#include <cassert>
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
Louis Dionne1ab95312020-02-24 18:12:44 -050061#if _LIBCPP_STD_VER >= 14
Olivier Giroux161e6e82020-02-18 09:58:34 -050062
63_LIBCPP_BEGIN_NAMESPACE_STD
64
65/*
66
67__atomic_semaphore_base is the general-case implementation, to be used for
68user-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.
70
71It is a typical Dijsktra semaphore algorithm over atomics, wait and notify
72functions. It avoids contention against users' own use of those facilities.
73
74*/
75
76class __atomic_semaphore_base
77{
78 __atomic_base<ptrdiff_t> __a;
79
80public:
81 _LIBCPP_INLINE_VISIBILITY
82 __atomic_semaphore_base(ptrdiff_t __count) : __a(__count)
83 {
84 }
85 _LIBCPP_INLINE_VISIBILITY
86 void release(ptrdiff_t __update = 1)
87 {
88 if(0 < __a.fetch_add(__update, memory_order_release))
89 ;
90 else if(__update > 1)
91 __a.notify_all();
92 else
93 __a.notify_one();
94 }
95 _LIBCPP_INLINE_VISIBILITY
96 void acquire()
97 {
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);
101 };
102 __cxx_atomic_wait(&__a.__a_, __test_fn);
103 }
104 template <class Rep, class Period>
105 _LIBCPP_INLINE_VISIBILITY
106 bool try_acquire_for(chrono::duration<Rep, Period> const& __rel_time)
107 {
108 auto const __test_fn = [=]() -> bool {
109 auto __old = __a.load(memory_order_acquire);
110 while(1) {
111 if (__old == 0)
112 return false;
113 if(__a.compare_exchange_strong(__old, __old - 1, memory_order_acquire, memory_order_relaxed))
114 return true;
115 }
116 };
117 return __libcpp_thread_poll_with_backoff(__test_fn, __libcpp_timed_backoff_policy(), __rel_time);
118 }
119};
120
121#ifndef _LIBCPP_NO_NATIVE_SEMAPHORES
122
123/*
124
125__platform_semaphore_base a simple wrapper for the OS semaphore type. That
126is, every call is routed to the OS in the most direct manner possible.
127
128*/
129
130class __platform_semaphore_base
131{
132 __libcpp_semaphore_t __semaphore;
133
134public:
135 _LIBCPP_INLINE_VISIBILITY
136 __platform_semaphore_base(ptrdiff_t __count) :
137 __semaphore()
138 {
139 __libcpp_semaphore_init(&__semaphore, __count);
140 }
141 _LIBCPP_INLINE_VISIBILITY
142 ~__platform_semaphore_base() {
143 __libcpp_semaphore_destroy(&__semaphore);
144 }
145 _LIBCPP_INLINE_VISIBILITY
146 void release(ptrdiff_t __update)
147 {
148 for(; __update; --__update)
149 __libcpp_semaphore_post(&__semaphore);
150 }
151 _LIBCPP_INLINE_VISIBILITY
152 void acquire()
153 {
154 __libcpp_semaphore_wait(&__semaphore);
155 }
156 _LIBCPP_INLINE_VISIBILITY
157 bool try_acquire_for(chrono::nanoseconds __rel_time)
158 {
159 return __libcpp_semaphore_wait_timed(&__semaphore, __rel_time);
160 }
161};
162
163template<ptrdiff_t __least_max_value>
164using __semaphore_base =
165 typename conditional<(__least_max_value > 1 && __least_max_value <= _LIBCPP_SEMAPHORE_MAX),
166 __platform_semaphore_base,
167 __atomic_semaphore_base>::type;
168
169#else
170
171template<ptrdiff_t __least_max_value>
172using __semaphore_base =
173 __atomic_semaphore_base;
174
175#endif //_LIBCPP_NO_NATIVE_SEMAPHORES
176
177template<ptrdiff_t __least_max_value = _LIBCPP_SEMAPHORE_MAX>
178class counting_semaphore
179{
180 __semaphore_base<__least_max_value> __semaphore;
181
182public:
183 static constexpr ptrdiff_t max() noexcept {
184 return __least_max_value;
185 }
186
187 _LIBCPP_INLINE_VISIBILITY
188 counting_semaphore(ptrdiff_t __count = 0) : __semaphore(__count) { }
189 ~counting_semaphore() = default;
190
191 counting_semaphore(const counting_semaphore&) = delete;
192 counting_semaphore& operator=(const counting_semaphore&) = delete;
193
194 _LIBCPP_INLINE_VISIBILITY
195 void release(ptrdiff_t __update = 1)
196 {
197 __semaphore.release(__update);
198 }
199 _LIBCPP_INLINE_VISIBILITY
200 void acquire()
201 {
202 __semaphore.acquire();
203 }
204 template<class Rep, class Period>
205 _LIBCPP_INLINE_VISIBILITY
206 bool try_acquire_for(chrono::duration<Rep, Period> const& __rel_time)
207 {
208 return __semaphore.try_acquire_for(chrono::duration_cast<chrono::nanoseconds>(__rel_time));
209 }
210 _LIBCPP_INLINE_VISIBILITY
211 bool try_acquire()
212 {
213 return try_acquire_for(chrono::nanoseconds::zero());
214 }
215 template <class Clock, class Duration>
216 _LIBCPP_INLINE_VISIBILITY
217 bool try_acquire_until(chrono::time_point<Clock, Duration> const& __abs_time)
218 {
219 auto const current = Clock::now();
220 if(current >= __abs_time)
221 return try_acquire();
222 else
223 return try_acquire_for(__abs_time - current);
224 }
225};
226
227using binary_semaphore = counting_semaphore<1>;
228
229_LIBCPP_END_NAMESPACE_STD
230
Louis Dionne1ab95312020-02-24 18:12:44 -0500231#endif // _LIBCPP_STD_VER >= 14
232
Olivier Giroux161e6e82020-02-18 09:58:34 -0500233#endif //_LIBCPP_SEMAPHORE