30#ifndef _GLIBCXX_SEMAPHORE_BASE_H
31#define _GLIBCXX_SEMAPHORE_BASE_H 1
33#pragma GCC system_header
36#if __cpp_lib_atomic_wait
41#ifdef _GLIBCXX_HAVE_POSIX_SEMAPHORE
45# include <semaphore.h>
51namespace std _GLIBCXX_VISIBILITY(default)
53_GLIBCXX_BEGIN_NAMESPACE_VERSION
55#ifdef _GLIBCXX_HAVE_POSIX_SEMAPHORE
56 struct __platform_semaphore
58 using __clock_t = chrono::system_clock;
60 static constexpr ptrdiff_t _S_max = SEM_VALUE_MAX;
62 static constexpr ptrdiff_t _S_max = _POSIX_SEM_VALUE_MAX;
65 explicit __platform_semaphore(ptrdiff_t __count)
noexcept
67 sem_init(&_M_semaphore, 0, __count);
70 __platform_semaphore(
const __platform_semaphore&) =
delete;
71 __platform_semaphore& operator=(
const __platform_semaphore&) =
delete;
73 ~__platform_semaphore()
74 { sem_destroy(&_M_semaphore); }
76 _GLIBCXX_ALWAYS_INLINE
void
81 auto __err = sem_wait(&_M_semaphore);
82 if (__err && (errno == EINTR))
91 _GLIBCXX_ALWAYS_INLINE
bool
92 _M_try_acquire() noexcept
96 auto __err = sem_trywait(&_M_semaphore);
97 if (__err && (errno == EINTR))
99 else if (__err && (errno == EAGAIN))
109 _GLIBCXX_ALWAYS_INLINE
void
110 _M_release(std::ptrdiff_t __update)
noexcept
112 for(; __update != 0; --__update)
114 auto __err = sem_post(&_M_semaphore);
121 _M_try_acquire_until_impl(
const chrono::time_point<__clock_t>& __atime)
125 auto __s = chrono::time_point_cast<chrono::seconds>(__atime);
126 auto __ns = chrono::duration_cast<chrono::nanoseconds>(__atime - __s);
128 struct timespec __ts =
130 static_cast<std::time_t
>(__s.time_since_epoch().count()),
131 static_cast<long>(__ns.count())
136 if (
auto __err = sem_timedwait(&_M_semaphore, &__ts))
140 else if (errno == ETIMEDOUT || errno == EINVAL)
151 template<
typename _Clock,
typename _Duration>
153 _M_try_acquire_until(
const chrono::time_point<_Clock,
154 _Duration>& __atime)
noexcept
156 if constexpr (std::is_same_v<__clock_t, _Clock>)
158 return _M_try_acquire_until_impl(__atime);
162 const typename _Clock::time_point __c_entry = _Clock::now();
163 const auto __s_entry = __clock_t::now();
164 const auto __delta = __atime - __c_entry;
165 const auto __s_atime = __s_entry + __delta;
166 if (_M_try_acquire_until_impl(__s_atime))
172 return (_Clock::now() < __atime);
176 template<
typename _Rep,
typename _Period>
177 _GLIBCXX_ALWAYS_INLINE
bool
178 _M_try_acquire_for(
const chrono::duration<_Rep, _Period>& __rtime)
180 {
return _M_try_acquire_until(__clock_t::now() + __rtime); }
187#if __cpp_lib_atomic_wait
188 struct __atomic_semaphore
191 explicit __atomic_semaphore(__detail::__platform_wait_t __count) noexcept
192 : _M_counter(__count)
194 __glibcxx_assert(__count >= 0 && __count <= _S_max);
197 __atomic_semaphore(
const __atomic_semaphore&) =
delete;
198 __atomic_semaphore& operator=(
const __atomic_semaphore&) =
delete;
200 static _GLIBCXX_ALWAYS_INLINE
bool
201 _S_do_try_acquire(__detail::__platform_wait_t* __counter)
noexcept
203 auto __old = __atomic_impl::load(__counter, memory_order::acquire);
207 return __atomic_impl::compare_exchange_strong(__counter,
209 memory_order::acquire,
210 memory_order::relaxed);
213 _GLIBCXX_ALWAYS_INLINE
void
214 _M_acquire() noexcept
217 [
this] {
return _S_do_try_acquire(&this->_M_counter); };
218 std::__atomic_wait_address_bare(&_M_counter, __pred);
222 _M_try_acquire() noexcept
225 [
this] {
return _S_do_try_acquire(&this->_M_counter); };
226 return std::__detail::__atomic_spin(__pred);
229 template<
typename _Clock,
typename _Duration>
230 _GLIBCXX_ALWAYS_INLINE
bool
231 _M_try_acquire_until(
const chrono::time_point<_Clock,
232 _Duration>& __atime)
noexcept
235 [
this] {
return _S_do_try_acquire(&this->_M_counter); };
237 return __atomic_wait_address_until_bare(&_M_counter, __pred, __atime);
240 template<
typename _Rep,
typename _Period>
241 _GLIBCXX_ALWAYS_INLINE
bool
242 _M_try_acquire_for(
const chrono::duration<_Rep, _Period>& __rtime)
246 [
this] {
return _S_do_try_acquire(&this->_M_counter); };
248 return __atomic_wait_address_for_bare(&_M_counter, __pred, __rtime);
251 _GLIBCXX_ALWAYS_INLINE
void
252 _M_release(ptrdiff_t __update)
noexcept
254 if (0 < __atomic_impl::fetch_add(&_M_counter, __update, memory_order_release))
257 __atomic_notify_address_bare(&_M_counter,
true);
259 __atomic_notify_address_bare(&_M_counter,
true);
265 alignas(__detail::__platform_wait_alignment)
266 __detail::__platform_wait_t _M_counter;
272#if defined __cpp_lib_atomic_wait && !_GLIBCXX_USE_POSIX_SEMAPHORE
273 using __semaphore_impl = __atomic_semaphore;
274#elif _GLIBCXX_HAVE_POSIX_SEMAPHORE
275 using __semaphore_impl = __platform_semaphore;
278_GLIBCXX_END_NAMESPACE_VERSION
void terminate() noexcept
ISO C++ entities toplevel namespace is std.
__numeric_traits_integer< _Tp > __int_traits
Convenience alias for __numeric_traits<integer-type>.