blob: 1128b36d9527ddcf54a50d8f02ad2adb91f01c3d [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
Louis Dionne73912b22020-11-04 15:01:25 -050048#include <__availability>
Arthur O'Dwyeref181602021-05-19 11:57:04 -040049#include <__config>
Olivier Giroux161e6e82020-02-18 09:58:34 -050050#include <__threading_support>
51#include <atomic>
Olivier Giroux161e6e82020-02-18 09:58:34 -050052
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'Dwyer2422fa22020-12-02 18:55:01 -050061_LIBCPP_PUSH_MACROS
62#include <__undef_macros>
63
Louis Dionne1ab95312020-02-24 18:12:44 -050064#if _LIBCPP_STD_VER >= 14
Olivier Giroux161e6e82020-02-18 09:58:34 -050065
66_LIBCPP_BEGIN_NAMESPACE_STD
67
68/*
69
Arthur O'Dwyer7a35d632021-09-20 18:03:49 -040070__atomic_semaphore_base is the general-case implementation.
Arthur O'Dwyerdf5df562020-12-12 11:57:32 -050071It is a typical Dijkstra semaphore algorithm over atomics, wait and notify
Olivier Giroux161e6e82020-02-18 09:58:34 -050072functions. 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
Arthur O'Dwyer227e00b2021-09-16 23:14:57 -040082 constexpr explicit __atomic_semaphore_base(ptrdiff_t __count) : __a(__count)
Olivier Giroux161e6e82020-02-18 09:58:34 -050083 {
84 }
Louis Dionne3dde4ab2020-02-24 10:09:29 -050085 _LIBCPP_AVAILABILITY_SYNC _LIBCPP_INLINE_VISIBILITY
Olivier Giroux161e6e82020-02-18 09:58:34 -050086 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 }
Louis Dionne3dde4ab2020-02-24 10:09:29 -050095 _LIBCPP_AVAILABILITY_SYNC _LIBCPP_INLINE_VISIBILITY
Olivier Giroux161e6e82020-02-18 09:58:34 -050096 void acquire()
97 {
Louis Dionneaf6be622021-07-27 17:30:47 -040098 auto const __test_fn = [this]() -> bool {
Olivier Giroux161e6e82020-02-18 09:58:34 -050099 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>
Louis Dionne3dde4ab2020-02-24 10:09:29 -0500105 _LIBCPP_AVAILABILITY_SYNC _LIBCPP_INLINE_VISIBILITY
Olivier Giroux161e6e82020-02-18 09:58:34 -0500106 bool try_acquire_for(chrono::duration<Rep, Period> const& __rel_time)
107 {
Louis Dionneaf6be622021-07-27 17:30:47 -0400108 auto const __test_fn = [this]() -> bool {
Olivier Giroux161e6e82020-02-18 09:58:34 -0500109 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
ogiroux945d52b2020-02-26 16:49:37 -0800121#define _LIBCPP_SEMAPHORE_MAX (numeric_limits<ptrdiff_t>::max())
122
Olivier Giroux161e6e82020-02-18 09:58:34 -0500123template<ptrdiff_t __least_max_value = _LIBCPP_SEMAPHORE_MAX>
124class counting_semaphore
125{
Arthur O'Dwyer7a35d632021-09-20 18:03:49 -0400126 __atomic_semaphore_base __semaphore;
Olivier Giroux161e6e82020-02-18 09:58:34 -0500127
128public:
129 static constexpr ptrdiff_t max() noexcept {
130 return __least_max_value;
131 }
132
133 _LIBCPP_INLINE_VISIBILITY
Arthur O'Dwyer227e00b2021-09-16 23:14:57 -0400134 constexpr explicit counting_semaphore(ptrdiff_t __count) : __semaphore(__count) { }
Olivier Giroux161e6e82020-02-18 09:58:34 -0500135 ~counting_semaphore() = default;
136
137 counting_semaphore(const counting_semaphore&) = delete;
138 counting_semaphore& operator=(const counting_semaphore&) = delete;
139
Louis Dionne3dde4ab2020-02-24 10:09:29 -0500140 _LIBCPP_AVAILABILITY_SYNC _LIBCPP_INLINE_VISIBILITY
Olivier Giroux161e6e82020-02-18 09:58:34 -0500141 void release(ptrdiff_t __update = 1)
142 {
143 __semaphore.release(__update);
144 }
Louis Dionne3dde4ab2020-02-24 10:09:29 -0500145 _LIBCPP_AVAILABILITY_SYNC _LIBCPP_INLINE_VISIBILITY
Olivier Giroux161e6e82020-02-18 09:58:34 -0500146 void acquire()
147 {
148 __semaphore.acquire();
149 }
150 template<class Rep, class Period>
Louis Dionne3dde4ab2020-02-24 10:09:29 -0500151 _LIBCPP_AVAILABILITY_SYNC _LIBCPP_INLINE_VISIBILITY
Olivier Giroux161e6e82020-02-18 09:58:34 -0500152 bool try_acquire_for(chrono::duration<Rep, Period> const& __rel_time)
153 {
154 return __semaphore.try_acquire_for(chrono::duration_cast<chrono::nanoseconds>(__rel_time));
155 }
Louis Dionne3dde4ab2020-02-24 10:09:29 -0500156 _LIBCPP_AVAILABILITY_SYNC _LIBCPP_INLINE_VISIBILITY
Olivier Giroux161e6e82020-02-18 09:58:34 -0500157 bool try_acquire()
158 {
159 return try_acquire_for(chrono::nanoseconds::zero());
160 }
161 template <class Clock, class Duration>
Louis Dionne3dde4ab2020-02-24 10:09:29 -0500162 _LIBCPP_AVAILABILITY_SYNC _LIBCPP_INLINE_VISIBILITY
Olivier Giroux161e6e82020-02-18 09:58:34 -0500163 bool try_acquire_until(chrono::time_point<Clock, Duration> const& __abs_time)
164 {
165 auto const current = Clock::now();
166 if(current >= __abs_time)
167 return try_acquire();
168 else
169 return try_acquire_for(__abs_time - current);
170 }
171};
172
173using binary_semaphore = counting_semaphore<1>;
174
175_LIBCPP_END_NAMESPACE_STD
176
Louis Dionne1ab95312020-02-24 18:12:44 -0500177#endif // _LIBCPP_STD_VER >= 14
178
Arthur O'Dwyer2422fa22020-12-02 18:55:01 -0500179_LIBCPP_POP_MACROS
180
Olivier Giroux161e6e82020-02-18 09:58:34 -0500181#endif //_LIBCPP_SEMAPHORE