1// -*- 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 <__config> 49 50#if !defined(_LIBCPP_HAS_NO_THREADS) 51 52# include <__assert> 53# include <__atomic/atomic_base.h> 54# include <__atomic/atomic_sync.h> 55# include <__atomic/memory_order.h> 56# include <__chrono/time_point.h> 57# include <__thread/poll_with_backoff.h> 58# include <__thread/support.h> 59# include <__thread/timed_backoff_policy.h> 60# include <cstddef> 61# include <limits> 62# include <version> 63 64# if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) 65# pragma GCC system_header 66# endif 67 68_LIBCPP_PUSH_MACROS 69# include <__undef_macros> 70 71# if _LIBCPP_STD_VER >= 14 72 73_LIBCPP_BEGIN_NAMESPACE_STD 74 75/* 76 77__atomic_semaphore_base is the general-case implementation. 78It is a typical Dijkstra semaphore algorithm over atomics, wait and notify 79functions. It avoids contention against users' own use of those facilities. 80 81*/ 82 83# define _LIBCPP_SEMAPHORE_MAX (numeric_limits<ptrdiff_t>::max()) 84 85class __atomic_semaphore_base { 86 __atomic_base<ptrdiff_t> __a_; 87 88public: 89 _LIBCPP_HIDE_FROM_ABI constexpr explicit __atomic_semaphore_base(ptrdiff_t __count) : __a_(__count) {} 90 _LIBCPP_AVAILABILITY_SYNC _LIBCPP_HIDE_FROM_ABI void release(ptrdiff_t __update = 1) { 91 auto __old = __a_.fetch_add(__update, memory_order_release); 92 _LIBCPP_ASSERT_ARGUMENT_WITHIN_DOMAIN( 93 __update <= _LIBCPP_SEMAPHORE_MAX - __old, "update is greater than the expected value"); 94 if (__old == 0) { 95 __a_.notify_all(); 96 } 97 } 98 _LIBCPP_AVAILABILITY_SYNC _LIBCPP_HIDE_FROM_ABI void acquire() { 99 std::__atomic_wait_unless( 100 __a_, [this](ptrdiff_t& __old) { return __try_acquire_impl(__old); }, memory_order_relaxed); 101 } 102 template <class _Rep, class _Period> 103 _LIBCPP_AVAILABILITY_SYNC _LIBCPP_HIDE_FROM_ABI bool 104 try_acquire_for(chrono::duration<_Rep, _Period> const& __rel_time) { 105 if (__rel_time == chrono::duration<_Rep, _Period>::zero()) 106 return try_acquire(); 107 auto const __poll_fn = [this]() { return try_acquire(); }; 108 return std::__libcpp_thread_poll_with_backoff(__poll_fn, __libcpp_timed_backoff_policy(), __rel_time); 109 } 110 _LIBCPP_AVAILABILITY_SYNC _LIBCPP_HIDE_FROM_ABI bool try_acquire() { 111 auto __old = __a_.load(memory_order_relaxed); 112 return __try_acquire_impl(__old); 113 } 114 115private: 116 _LIBCPP_AVAILABILITY_SYNC _LIBCPP_HIDE_FROM_ABI bool __try_acquire_impl(ptrdiff_t& __old) { 117 while (true) { 118 if (__old == 0) 119 return false; 120 if (__a_.compare_exchange_weak(__old, __old - 1, memory_order_acquire, memory_order_relaxed)) 121 return true; 122 } 123 } 124}; 125 126template <ptrdiff_t __least_max_value = _LIBCPP_SEMAPHORE_MAX> 127class _LIBCPP_DEPRECATED_ATOMIC_SYNC counting_semaphore { 128 __atomic_semaphore_base __semaphore_; 129 130public: 131 static_assert(__least_max_value >= 0, "The least maximum value must be a positive number"); 132 133 static constexpr ptrdiff_t max() noexcept { return __least_max_value; } 134 135 _LIBCPP_HIDE_FROM_ABI constexpr explicit counting_semaphore(ptrdiff_t __count) : __semaphore_(__count) { 136 _LIBCPP_ASSERT_ARGUMENT_WITHIN_DOMAIN( 137 __count >= 0, 138 "counting_semaphore::counting_semaphore(ptrdiff_t): counting_semaphore cannot be " 139 "initialized with a negative value"); 140 _LIBCPP_ASSERT_ARGUMENT_WITHIN_DOMAIN( 141 __count <= max(), 142 "counting_semaphore::counting_semaphore(ptrdiff_t): counting_semaphore cannot be " 143 "initialized with a value greater than max()"); 144 } 145 ~counting_semaphore() = default; 146 147 counting_semaphore(const counting_semaphore&) = delete; 148 counting_semaphore& operator=(const counting_semaphore&) = delete; 149 150 _LIBCPP_AVAILABILITY_SYNC _LIBCPP_HIDE_FROM_ABI void release(ptrdiff_t __update = 1) { 151 _LIBCPP_ASSERT_ARGUMENT_WITHIN_DOMAIN(__update >= 0, "counting_semaphore:release called with a negative value"); 152 __semaphore_.release(__update); 153 } 154 _LIBCPP_AVAILABILITY_SYNC _LIBCPP_HIDE_FROM_ABI void acquire() { __semaphore_.acquire(); } 155 template <class _Rep, class _Period> 156 _LIBCPP_AVAILABILITY_SYNC _LIBCPP_HIDE_FROM_ABI bool 157 try_acquire_for(chrono::duration<_Rep, _Period> const& __rel_time) { 158 return __semaphore_.try_acquire_for(chrono::duration_cast<chrono::nanoseconds>(__rel_time)); 159 } 160 _LIBCPP_AVAILABILITY_SYNC _LIBCPP_HIDE_FROM_ABI bool try_acquire() { return __semaphore_.try_acquire(); } 161 template <class _Clock, class _Duration> 162 _LIBCPP_AVAILABILITY_SYNC _LIBCPP_HIDE_FROM_ABI bool 163 try_acquire_until(chrono::time_point<_Clock, _Duration> const& __abs_time) { 164 auto const __current = _Clock::now(); 165 if (__current >= __abs_time) 166 return try_acquire(); 167 else 168 return try_acquire_for(__abs_time - __current); 169 } 170}; 171 172_LIBCPP_SUPPRESS_DEPRECATED_PUSH 173using binary_semaphore _LIBCPP_DEPRECATED_ATOMIC_SYNC = counting_semaphore<1>; 174_LIBCPP_SUPPRESS_DEPRECATED_POP 175 176_LIBCPP_END_NAMESPACE_STD 177 178# endif // _LIBCPP_STD_VER >= 14 179 180_LIBCPP_POP_MACROS 181 182#endif // !defined(_LIBCPP_HAS_NO_THREADS) 183 184#if !defined(_LIBCPP_REMOVE_TRANSITIVE_INCLUDES) && _LIBCPP_STD_VER <= 20 185# include <atomic> 186#endif 187 188#endif //_LIBCPP_SEMAPHORE 189