15ffd83dbSDimitry Andric// -*- C++ -*- 2349cc55cSDimitry Andric//===----------------------------------------------------------------------===// 35ffd83dbSDimitry Andric// 45ffd83dbSDimitry Andric// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 55ffd83dbSDimitry Andric// See https://llvm.org/LICENSE.txt for license information. 65ffd83dbSDimitry Andric// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 75ffd83dbSDimitry Andric// 85ffd83dbSDimitry Andric//===----------------------------------------------------------------------===// 95ffd83dbSDimitry Andric 105ffd83dbSDimitry Andric#ifndef _LIBCPP_SEMAPHORE 115ffd83dbSDimitry Andric#define _LIBCPP_SEMAPHORE 125ffd83dbSDimitry Andric 135ffd83dbSDimitry Andric/* 145ffd83dbSDimitry Andric semaphore synopsis 155ffd83dbSDimitry Andric 165ffd83dbSDimitry Andricnamespace std { 175ffd83dbSDimitry Andric 185ffd83dbSDimitry Andrictemplate<ptrdiff_t least_max_value = implementation-defined> 195ffd83dbSDimitry Andricclass counting_semaphore 205ffd83dbSDimitry Andric{ 215ffd83dbSDimitry Andricpublic: 225ffd83dbSDimitry Andricstatic constexpr ptrdiff_t max() noexcept; 235ffd83dbSDimitry Andric 245ffd83dbSDimitry Andricconstexpr explicit counting_semaphore(ptrdiff_t desired); 255ffd83dbSDimitry Andric~counting_semaphore(); 265ffd83dbSDimitry Andric 275ffd83dbSDimitry Andriccounting_semaphore(const counting_semaphore&) = delete; 285ffd83dbSDimitry Andriccounting_semaphore& operator=(const counting_semaphore&) = delete; 295ffd83dbSDimitry Andric 305ffd83dbSDimitry Andricvoid release(ptrdiff_t update = 1); 315ffd83dbSDimitry Andricvoid acquire(); 325ffd83dbSDimitry Andricbool try_acquire() noexcept; 335ffd83dbSDimitry Andrictemplate<class Rep, class Period> 345ffd83dbSDimitry Andric bool try_acquire_for(const chrono::duration<Rep, Period>& rel_time); 355ffd83dbSDimitry Andrictemplate<class Clock, class Duration> 365ffd83dbSDimitry Andric bool try_acquire_until(const chrono::time_point<Clock, Duration>& abs_time); 375ffd83dbSDimitry Andric 385ffd83dbSDimitry Andricprivate: 395ffd83dbSDimitry Andricptrdiff_t counter; // exposition only 405ffd83dbSDimitry Andric}; 415ffd83dbSDimitry Andric 425ffd83dbSDimitry Andricusing binary_semaphore = counting_semaphore<1>; 435ffd83dbSDimitry Andric 445ffd83dbSDimitry Andric} 455ffd83dbSDimitry Andric 465ffd83dbSDimitry Andric*/ 475ffd83dbSDimitry Andric 487a6dacacSDimitry Andric#include <__config> 497a6dacacSDimitry Andric 50*0fca6ea1SDimitry Andric#if !defined(_LIBCPP_HAS_NO_THREADS) 517a6dacacSDimitry Andric 52*0fca6ea1SDimitry Andric# include <__assert> 5306c3fb27SDimitry Andric# include <__atomic/atomic_base.h> 5406c3fb27SDimitry Andric# include <__atomic/atomic_sync.h> 5506c3fb27SDimitry Andric# include <__atomic/memory_order.h> 5681ad6265SDimitry Andric# include <__chrono/time_point.h> 575f757f3fSDimitry Andric# include <__thread/poll_with_backoff.h> 58*0fca6ea1SDimitry Andric# include <__thread/support.h> 5904eeddc0SDimitry Andric# include <__thread/timed_backoff_policy.h> 6006c3fb27SDimitry Andric# include <cstddef> 6181ad6265SDimitry Andric# include <limits> 6204eeddc0SDimitry Andric# include <version> 635ffd83dbSDimitry Andric 645ffd83dbSDimitry Andric# if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) 655ffd83dbSDimitry Andric# pragma GCC system_header 665ffd83dbSDimitry Andric# endif 675ffd83dbSDimitry Andric 68e8d8bef9SDimitry Andric_LIBCPP_PUSH_MACROS 69e8d8bef9SDimitry Andric# include <__undef_macros> 70e8d8bef9SDimitry Andric 715ffd83dbSDimitry Andric# if _LIBCPP_STD_VER >= 14 725ffd83dbSDimitry Andric 735ffd83dbSDimitry Andric_LIBCPP_BEGIN_NAMESPACE_STD 745ffd83dbSDimitry Andric 755ffd83dbSDimitry Andric/* 765ffd83dbSDimitry Andric 77349cc55cSDimitry Andric__atomic_semaphore_base is the general-case implementation. 78e8d8bef9SDimitry AndricIt is a typical Dijkstra semaphore algorithm over atomics, wait and notify 795ffd83dbSDimitry Andricfunctions. It avoids contention against users' own use of those facilities. 805ffd83dbSDimitry Andric 815ffd83dbSDimitry Andric*/ 825ffd83dbSDimitry Andric 8306c3fb27SDimitry Andric# define _LIBCPP_SEMAPHORE_MAX (numeric_limits<ptrdiff_t>::max()) 8406c3fb27SDimitry Andric 85cb14a3feSDimitry Andricclass __atomic_semaphore_base { 86bdd1243dSDimitry Andric __atomic_base<ptrdiff_t> __a_; 875ffd83dbSDimitry Andric 885ffd83dbSDimitry Andricpublic: 89cb14a3feSDimitry Andric _LIBCPP_HIDE_FROM_ABI constexpr explicit __atomic_semaphore_base(ptrdiff_t __count) : __a_(__count) {} 90cb14a3feSDimitry Andric _LIBCPP_AVAILABILITY_SYNC _LIBCPP_HIDE_FROM_ABI void release(ptrdiff_t __update = 1) { 9106c3fb27SDimitry Andric auto __old = __a_.fetch_add(__update, memory_order_release); 927a6dacacSDimitry Andric _LIBCPP_ASSERT_ARGUMENT_WITHIN_DOMAIN( 93cb14a3feSDimitry Andric __update <= _LIBCPP_SEMAPHORE_MAX - __old, "update is greater than the expected value"); 94*0fca6ea1SDimitry Andric if (__old == 0) { 95bdd1243dSDimitry Andric __a_.notify_all(); 96*0fca6ea1SDimitry Andric } 975ffd83dbSDimitry Andric } 98cb14a3feSDimitry Andric _LIBCPP_AVAILABILITY_SYNC _LIBCPP_HIDE_FROM_ABI void acquire() { 99*0fca6ea1SDimitry Andric std::__atomic_wait_unless( 100*0fca6ea1SDimitry Andric __a_, [this](ptrdiff_t& __old) { return __try_acquire_impl(__old); }, memory_order_relaxed); 1015ffd83dbSDimitry Andric } 10206c3fb27SDimitry Andric template <class _Rep, class _Period> 103cb14a3feSDimitry Andric _LIBCPP_AVAILABILITY_SYNC _LIBCPP_HIDE_FROM_ABI bool 104cb14a3feSDimitry Andric try_acquire_for(chrono::duration<_Rep, _Period> const& __rel_time) { 10506c3fb27SDimitry Andric if (__rel_time == chrono::duration<_Rep, _Period>::zero()) 106349cc55cSDimitry Andric return try_acquire(); 107*0fca6ea1SDimitry Andric auto const __poll_fn = [this]() { return try_acquire(); }; 108*0fca6ea1SDimitry Andric return std::__libcpp_thread_poll_with_backoff(__poll_fn, __libcpp_timed_backoff_policy(), __rel_time); 109349cc55cSDimitry Andric } 110cb14a3feSDimitry Andric _LIBCPP_AVAILABILITY_SYNC _LIBCPP_HIDE_FROM_ABI bool try_acquire() { 111*0fca6ea1SDimitry Andric auto __old = __a_.load(memory_order_relaxed); 112*0fca6ea1SDimitry Andric return __try_acquire_impl(__old); 113*0fca6ea1SDimitry Andric } 114*0fca6ea1SDimitry Andric 115*0fca6ea1SDimitry Andricprivate: 116*0fca6ea1SDimitry Andric _LIBCPP_AVAILABILITY_SYNC _LIBCPP_HIDE_FROM_ABI bool __try_acquire_impl(ptrdiff_t& __old) { 117349cc55cSDimitry Andric while (true) { 1185ffd83dbSDimitry Andric if (__old == 0) 1195ffd83dbSDimitry Andric return false; 120*0fca6ea1SDimitry Andric if (__a_.compare_exchange_weak(__old, __old - 1, memory_order_acquire, memory_order_relaxed)) 1215ffd83dbSDimitry Andric return true; 1225ffd83dbSDimitry Andric } 1235ffd83dbSDimitry Andric } 1245ffd83dbSDimitry Andric}; 1255ffd83dbSDimitry Andric 1265ffd83dbSDimitry Andrictemplate <ptrdiff_t __least_max_value = _LIBCPP_SEMAPHORE_MAX> 127*0fca6ea1SDimitry Andricclass _LIBCPP_DEPRECATED_ATOMIC_SYNC counting_semaphore { 128bdd1243dSDimitry Andric __atomic_semaphore_base __semaphore_; 1295ffd83dbSDimitry Andric 1305ffd83dbSDimitry Andricpublic: 13106c3fb27SDimitry Andric static_assert(__least_max_value >= 0, "The least maximum value must be a positive number"); 13206c3fb27SDimitry Andric 133cb14a3feSDimitry Andric static constexpr ptrdiff_t max() noexcept { return __least_max_value; } 1345ffd83dbSDimitry Andric 135cb14a3feSDimitry Andric _LIBCPP_HIDE_FROM_ABI constexpr explicit counting_semaphore(ptrdiff_t __count) : __semaphore_(__count) { 1367a6dacacSDimitry Andric _LIBCPP_ASSERT_ARGUMENT_WITHIN_DOMAIN( 13706c3fb27SDimitry Andric __count >= 0, 13806c3fb27SDimitry Andric "counting_semaphore::counting_semaphore(ptrdiff_t): counting_semaphore cannot be " 13906c3fb27SDimitry Andric "initialized with a negative value"); 1407a6dacacSDimitry Andric _LIBCPP_ASSERT_ARGUMENT_WITHIN_DOMAIN( 14106c3fb27SDimitry Andric __count <= max(), 14206c3fb27SDimitry Andric "counting_semaphore::counting_semaphore(ptrdiff_t): counting_semaphore cannot be " 14306c3fb27SDimitry Andric "initialized with a value greater than max()"); 14406c3fb27SDimitry Andric } 1455ffd83dbSDimitry Andric ~counting_semaphore() = default; 1465ffd83dbSDimitry Andric 1475ffd83dbSDimitry Andric counting_semaphore(const counting_semaphore&) = delete; 1485ffd83dbSDimitry Andric counting_semaphore& operator=(const counting_semaphore&) = delete; 1495ffd83dbSDimitry Andric 150cb14a3feSDimitry Andric _LIBCPP_AVAILABILITY_SYNC _LIBCPP_HIDE_FROM_ABI void release(ptrdiff_t __update = 1) { 1517a6dacacSDimitry Andric _LIBCPP_ASSERT_ARGUMENT_WITHIN_DOMAIN(__update >= 0, "counting_semaphore:release called with a negative value"); 152bdd1243dSDimitry Andric __semaphore_.release(__update); 1535ffd83dbSDimitry Andric } 154cb14a3feSDimitry Andric _LIBCPP_AVAILABILITY_SYNC _LIBCPP_HIDE_FROM_ABI void acquire() { __semaphore_.acquire(); } 15506c3fb27SDimitry Andric template <class _Rep, class _Period> 156cb14a3feSDimitry Andric _LIBCPP_AVAILABILITY_SYNC _LIBCPP_HIDE_FROM_ABI bool 157cb14a3feSDimitry Andric try_acquire_for(chrono::duration<_Rep, _Period> const& __rel_time) { 158bdd1243dSDimitry Andric return __semaphore_.try_acquire_for(chrono::duration_cast<chrono::nanoseconds>(__rel_time)); 1595ffd83dbSDimitry Andric } 160cb14a3feSDimitry Andric _LIBCPP_AVAILABILITY_SYNC _LIBCPP_HIDE_FROM_ABI bool try_acquire() { return __semaphore_.try_acquire(); } 16106c3fb27SDimitry Andric template <class _Clock, class _Duration> 162cb14a3feSDimitry Andric _LIBCPP_AVAILABILITY_SYNC _LIBCPP_HIDE_FROM_ABI bool 163cb14a3feSDimitry Andric try_acquire_until(chrono::time_point<_Clock, _Duration> const& __abs_time) { 16406c3fb27SDimitry Andric auto const __current = _Clock::now(); 16506c3fb27SDimitry Andric if (__current >= __abs_time) 1665ffd83dbSDimitry Andric return try_acquire(); 1675ffd83dbSDimitry Andric else 16806c3fb27SDimitry Andric return try_acquire_for(__abs_time - __current); 1695ffd83dbSDimitry Andric } 1705ffd83dbSDimitry Andric}; 1715ffd83dbSDimitry Andric 172*0fca6ea1SDimitry Andric_LIBCPP_SUPPRESS_DEPRECATED_PUSH 173*0fca6ea1SDimitry Andricusing binary_semaphore _LIBCPP_DEPRECATED_ATOMIC_SYNC = counting_semaphore<1>; 174*0fca6ea1SDimitry Andric_LIBCPP_SUPPRESS_DEPRECATED_POP 1755ffd83dbSDimitry Andric 1765ffd83dbSDimitry Andric_LIBCPP_END_NAMESPACE_STD 1775ffd83dbSDimitry Andric 1785ffd83dbSDimitry Andric# endif // _LIBCPP_STD_VER >= 14 1795ffd83dbSDimitry Andric 180e8d8bef9SDimitry Andric_LIBCPP_POP_MACROS 181e8d8bef9SDimitry Andric 182*0fca6ea1SDimitry Andric#endif // !defined(_LIBCPP_HAS_NO_THREADS) 183*0fca6ea1SDimitry Andric 18406c3fb27SDimitry Andric#if !defined(_LIBCPP_REMOVE_TRANSITIVE_INCLUDES) && _LIBCPP_STD_VER <= 20 18506c3fb27SDimitry Andric# include <atomic> 18606c3fb27SDimitry Andric#endif 18706c3fb27SDimitry Andric 1885ffd83dbSDimitry Andric#endif //_LIBCPP_SEMAPHORE 189