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