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 <__assert> // all public C++ headers provide the assertion handler 49#include <__availability> 50#include <__chrono/time_point.h> 51#include <__config> 52#include <__thread/timed_backoff_policy.h> 53#include <__threading_support> 54#include <atomic> 55#include <version> 56 57#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) 58# pragma GCC system_header 59#endif 60 61#ifdef _LIBCPP_HAS_NO_THREADS 62# error <semaphore> is not supported on this single threaded system 63#endif 64 65_LIBCPP_PUSH_MACROS 66#include <__undef_macros> 67 68#if _LIBCPP_STD_VER >= 14 69 70_LIBCPP_BEGIN_NAMESPACE_STD 71 72/* 73 74__atomic_semaphore_base is the general-case implementation. 75It is a typical Dijkstra semaphore algorithm over atomics, wait and notify 76functions. It avoids contention against users' own use of those facilities. 77 78*/ 79 80class __atomic_semaphore_base 81{ 82 __atomic_base<ptrdiff_t> __a; 83 84public: 85 _LIBCPP_INLINE_VISIBILITY 86 constexpr explicit __atomic_semaphore_base(ptrdiff_t __count) : __a(__count) 87 { 88 } 89 _LIBCPP_AVAILABILITY_SYNC _LIBCPP_INLINE_VISIBILITY 90 void release(ptrdiff_t __update = 1) 91 { 92 if(0 < __a.fetch_add(__update, memory_order_release)) 93 ; 94 else if(__update > 1) 95 __a.notify_all(); 96 else 97 __a.notify_one(); 98 } 99 _LIBCPP_AVAILABILITY_SYNC _LIBCPP_INLINE_VISIBILITY 100 void acquire() 101 { 102 auto const __test_fn = [this]() -> bool { 103 auto __old = __a.load(memory_order_relaxed); 104 return (__old != 0) && __a.compare_exchange_strong(__old, __old - 1, memory_order_acquire, memory_order_relaxed); 105 }; 106 __cxx_atomic_wait(&__a.__a_, __test_fn); 107 } 108 template <class Rep, class Period> 109 _LIBCPP_AVAILABILITY_SYNC _LIBCPP_INLINE_VISIBILITY 110 bool try_acquire_for(chrono::duration<Rep, Period> const& __rel_time) 111 { 112 if (__rel_time == chrono::duration<Rep, Period>::zero()) 113 return try_acquire(); 114 auto const __test_fn = [this]() { return try_acquire(); }; 115 return __libcpp_thread_poll_with_backoff(__test_fn, __libcpp_timed_backoff_policy(), __rel_time); 116 } 117 _LIBCPP_AVAILABILITY_SYNC _LIBCPP_INLINE_VISIBILITY 118 bool try_acquire() 119 { 120 auto __old = __a.load(memory_order_acquire); 121 while (true) { 122 if (__old == 0) 123 return false; 124 if (__a.compare_exchange_strong(__old, __old - 1, memory_order_acquire, memory_order_relaxed)) 125 return true; 126 } 127 } 128}; 129 130#define _LIBCPP_SEMAPHORE_MAX (numeric_limits<ptrdiff_t>::max()) 131 132template<ptrdiff_t __least_max_value = _LIBCPP_SEMAPHORE_MAX> 133class counting_semaphore 134{ 135 __atomic_semaphore_base __semaphore; 136 137public: 138 static constexpr ptrdiff_t max() noexcept { 139 return __least_max_value; 140 } 141 142 _LIBCPP_INLINE_VISIBILITY 143 constexpr explicit counting_semaphore(ptrdiff_t __count) : __semaphore(__count) { } 144 ~counting_semaphore() = default; 145 146 counting_semaphore(const counting_semaphore&) = delete; 147 counting_semaphore& operator=(const counting_semaphore&) = delete; 148 149 _LIBCPP_AVAILABILITY_SYNC _LIBCPP_INLINE_VISIBILITY 150 void release(ptrdiff_t __update = 1) 151 { 152 __semaphore.release(__update); 153 } 154 _LIBCPP_AVAILABILITY_SYNC _LIBCPP_INLINE_VISIBILITY 155 void acquire() 156 { 157 __semaphore.acquire(); 158 } 159 template<class Rep, class Period> 160 _LIBCPP_AVAILABILITY_SYNC _LIBCPP_INLINE_VISIBILITY 161 bool try_acquire_for(chrono::duration<Rep, Period> const& __rel_time) 162 { 163 return __semaphore.try_acquire_for(chrono::duration_cast<chrono::nanoseconds>(__rel_time)); 164 } 165 _LIBCPP_AVAILABILITY_SYNC _LIBCPP_INLINE_VISIBILITY 166 bool try_acquire() 167 { 168 return __semaphore.try_acquire(); 169 } 170 template <class Clock, class Duration> 171 _LIBCPP_AVAILABILITY_SYNC _LIBCPP_INLINE_VISIBILITY 172 bool try_acquire_until(chrono::time_point<Clock, Duration> const& __abs_time) 173 { 174 auto const current = Clock::now(); 175 if (current >= __abs_time) 176 return try_acquire(); 177 else 178 return try_acquire_for(__abs_time - current); 179 } 180}; 181 182using binary_semaphore = counting_semaphore<1>; 183 184_LIBCPP_END_NAMESPACE_STD 185 186#endif // _LIBCPP_STD_VER >= 14 187 188_LIBCPP_POP_MACROS 189 190#endif //_LIBCPP_SEMAPHORE 191