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 48*7a6dacacSDimitry Andric#include <__config> 49*7a6dacacSDimitry Andric 50*7a6dacacSDimitry Andric#ifdef _LIBCPP_HAS_NO_THREADS 51*7a6dacacSDimitry Andric# error "<semaphore> is not supported since libc++ has been configured without support for threads." 52*7a6dacacSDimitry Andric#endif 53*7a6dacacSDimitry Andric 5481ad6265SDimitry Andric#include <__assert> // all public C++ headers provide the assertion handler 5506c3fb27SDimitry Andric#include <__atomic/atomic_base.h> 5606c3fb27SDimitry Andric#include <__atomic/atomic_sync.h> 5706c3fb27SDimitry Andric#include <__atomic/memory_order.h> 58e8d8bef9SDimitry Andric#include <__availability> 5981ad6265SDimitry Andric#include <__chrono/time_point.h> 605f757f3fSDimitry Andric#include <__thread/poll_with_backoff.h> 6104eeddc0SDimitry Andric#include <__thread/timed_backoff_policy.h> 625ffd83dbSDimitry Andric#include <__threading_support> 6306c3fb27SDimitry Andric#include <cstddef> 6481ad6265SDimitry Andric#include <limits> 6504eeddc0SDimitry Andric#include <version> 665ffd83dbSDimitry Andric 675ffd83dbSDimitry Andric#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) 685ffd83dbSDimitry Andric# pragma GCC system_header 695ffd83dbSDimitry Andric#endif 705ffd83dbSDimitry Andric 71e8d8bef9SDimitry Andric_LIBCPP_PUSH_MACROS 72e8d8bef9SDimitry Andric#include <__undef_macros> 73e8d8bef9SDimitry Andric 745ffd83dbSDimitry Andric#if _LIBCPP_STD_VER >= 14 755ffd83dbSDimitry Andric 765ffd83dbSDimitry Andric_LIBCPP_BEGIN_NAMESPACE_STD 775ffd83dbSDimitry Andric 785ffd83dbSDimitry Andric/* 795ffd83dbSDimitry Andric 80349cc55cSDimitry Andric__atomic_semaphore_base is the general-case implementation. 81e8d8bef9SDimitry AndricIt is a typical Dijkstra semaphore algorithm over atomics, wait and notify 825ffd83dbSDimitry Andricfunctions. It avoids contention against users' own use of those facilities. 835ffd83dbSDimitry Andric 845ffd83dbSDimitry Andric*/ 855ffd83dbSDimitry Andric 8606c3fb27SDimitry Andric# define _LIBCPP_SEMAPHORE_MAX (numeric_limits<ptrdiff_t>::max()) 8706c3fb27SDimitry Andric 88cb14a3feSDimitry Andricclass __atomic_semaphore_base { 89bdd1243dSDimitry Andric __atomic_base<ptrdiff_t> __a_; 905ffd83dbSDimitry Andric 915ffd83dbSDimitry Andricpublic: 92cb14a3feSDimitry Andric _LIBCPP_HIDE_FROM_ABI constexpr explicit __atomic_semaphore_base(ptrdiff_t __count) : __a_(__count) {} 93cb14a3feSDimitry Andric _LIBCPP_AVAILABILITY_SYNC _LIBCPP_HIDE_FROM_ABI void release(ptrdiff_t __update = 1) { 9406c3fb27SDimitry Andric auto __old = __a_.fetch_add(__update, memory_order_release); 95*7a6dacacSDimitry Andric _LIBCPP_ASSERT_ARGUMENT_WITHIN_DOMAIN( 96cb14a3feSDimitry Andric __update <= _LIBCPP_SEMAPHORE_MAX - __old, "update is greater than the expected value"); 9706c3fb27SDimitry Andric 98cb14a3feSDimitry Andric if (__old > 0) { 9906c3fb27SDimitry Andric // Nothing to do 100cb14a3feSDimitry Andric } else if (__update > 1) 101bdd1243dSDimitry Andric __a_.notify_all(); 1025ffd83dbSDimitry Andric else 103bdd1243dSDimitry Andric __a_.notify_one(); 1045ffd83dbSDimitry Andric } 105cb14a3feSDimitry Andric _LIBCPP_AVAILABILITY_SYNC _LIBCPP_HIDE_FROM_ABI void acquire() { 106fe6060f1SDimitry Andric auto const __test_fn = [this]() -> bool { 107bdd1243dSDimitry Andric auto __old = __a_.load(memory_order_relaxed); 108bdd1243dSDimitry Andric return (__old != 0) && __a_.compare_exchange_strong(__old, __old - 1, memory_order_acquire, memory_order_relaxed); 1095ffd83dbSDimitry Andric }; 110bdd1243dSDimitry Andric __cxx_atomic_wait(&__a_.__a_, __test_fn); 1115ffd83dbSDimitry Andric } 11206c3fb27SDimitry Andric template <class _Rep, class _Period> 113cb14a3feSDimitry Andric _LIBCPP_AVAILABILITY_SYNC _LIBCPP_HIDE_FROM_ABI bool 114cb14a3feSDimitry Andric try_acquire_for(chrono::duration<_Rep, _Period> const& __rel_time) { 11506c3fb27SDimitry Andric if (__rel_time == chrono::duration<_Rep, _Period>::zero()) 116349cc55cSDimitry Andric return try_acquire(); 117349cc55cSDimitry Andric auto const __test_fn = [this]() { return try_acquire(); }; 118bdd1243dSDimitry Andric return std::__libcpp_thread_poll_with_backoff(__test_fn, __libcpp_timed_backoff_policy(), __rel_time); 119349cc55cSDimitry Andric } 120cb14a3feSDimitry Andric _LIBCPP_AVAILABILITY_SYNC _LIBCPP_HIDE_FROM_ABI bool try_acquire() { 121bdd1243dSDimitry Andric auto __old = __a_.load(memory_order_acquire); 122349cc55cSDimitry Andric while (true) { 1235ffd83dbSDimitry Andric if (__old == 0) 1245ffd83dbSDimitry Andric return false; 125bdd1243dSDimitry Andric if (__a_.compare_exchange_strong(__old, __old - 1, memory_order_acquire, memory_order_relaxed)) 1265ffd83dbSDimitry Andric return true; 1275ffd83dbSDimitry Andric } 1285ffd83dbSDimitry Andric } 1295ffd83dbSDimitry Andric}; 1305ffd83dbSDimitry Andric 1315ffd83dbSDimitry Andrictemplate <ptrdiff_t __least_max_value = _LIBCPP_SEMAPHORE_MAX> 132cb14a3feSDimitry Andricclass counting_semaphore { 133bdd1243dSDimitry Andric __atomic_semaphore_base __semaphore_; 1345ffd83dbSDimitry Andric 1355ffd83dbSDimitry Andricpublic: 13606c3fb27SDimitry Andric static_assert(__least_max_value >= 0, "The least maximum value must be a positive number"); 13706c3fb27SDimitry Andric 138cb14a3feSDimitry Andric static constexpr ptrdiff_t max() noexcept { return __least_max_value; } 1395ffd83dbSDimitry Andric 140cb14a3feSDimitry Andric _LIBCPP_HIDE_FROM_ABI constexpr explicit counting_semaphore(ptrdiff_t __count) : __semaphore_(__count) { 141*7a6dacacSDimitry Andric _LIBCPP_ASSERT_ARGUMENT_WITHIN_DOMAIN( 14206c3fb27SDimitry Andric __count >= 0, 14306c3fb27SDimitry Andric "counting_semaphore::counting_semaphore(ptrdiff_t): counting_semaphore cannot be " 14406c3fb27SDimitry Andric "initialized with a negative value"); 145*7a6dacacSDimitry Andric _LIBCPP_ASSERT_ARGUMENT_WITHIN_DOMAIN( 14606c3fb27SDimitry Andric __count <= max(), 14706c3fb27SDimitry Andric "counting_semaphore::counting_semaphore(ptrdiff_t): counting_semaphore cannot be " 14806c3fb27SDimitry Andric "initialized with a value greater than max()"); 14906c3fb27SDimitry Andric } 1505ffd83dbSDimitry Andric ~counting_semaphore() = default; 1515ffd83dbSDimitry Andric 1525ffd83dbSDimitry Andric counting_semaphore(const counting_semaphore&) = delete; 1535ffd83dbSDimitry Andric counting_semaphore& operator=(const counting_semaphore&) = delete; 1545ffd83dbSDimitry Andric 155cb14a3feSDimitry Andric _LIBCPP_AVAILABILITY_SYNC _LIBCPP_HIDE_FROM_ABI void release(ptrdiff_t __update = 1) { 156*7a6dacacSDimitry Andric _LIBCPP_ASSERT_ARGUMENT_WITHIN_DOMAIN(__update >= 0, "counting_semaphore:release called with a negative value"); 157bdd1243dSDimitry Andric __semaphore_.release(__update); 1585ffd83dbSDimitry Andric } 159cb14a3feSDimitry Andric _LIBCPP_AVAILABILITY_SYNC _LIBCPP_HIDE_FROM_ABI void acquire() { __semaphore_.acquire(); } 16006c3fb27SDimitry Andric template <class _Rep, class _Period> 161cb14a3feSDimitry Andric _LIBCPP_AVAILABILITY_SYNC _LIBCPP_HIDE_FROM_ABI bool 162cb14a3feSDimitry Andric try_acquire_for(chrono::duration<_Rep, _Period> const& __rel_time) { 163bdd1243dSDimitry Andric return __semaphore_.try_acquire_for(chrono::duration_cast<chrono::nanoseconds>(__rel_time)); 1645ffd83dbSDimitry Andric } 165cb14a3feSDimitry Andric _LIBCPP_AVAILABILITY_SYNC _LIBCPP_HIDE_FROM_ABI bool try_acquire() { return __semaphore_.try_acquire(); } 16606c3fb27SDimitry Andric template <class _Clock, class _Duration> 167cb14a3feSDimitry Andric _LIBCPP_AVAILABILITY_SYNC _LIBCPP_HIDE_FROM_ABI bool 168cb14a3feSDimitry Andric try_acquire_until(chrono::time_point<_Clock, _Duration> const& __abs_time) { 16906c3fb27SDimitry Andric auto const __current = _Clock::now(); 17006c3fb27SDimitry Andric if (__current >= __abs_time) 1715ffd83dbSDimitry Andric return try_acquire(); 1725ffd83dbSDimitry Andric else 17306c3fb27SDimitry Andric return try_acquire_for(__abs_time - __current); 1745ffd83dbSDimitry Andric } 1755ffd83dbSDimitry Andric}; 1765ffd83dbSDimitry Andric 1775ffd83dbSDimitry Andricusing binary_semaphore = counting_semaphore<1>; 1785ffd83dbSDimitry Andric 1795ffd83dbSDimitry Andric_LIBCPP_END_NAMESPACE_STD 1805ffd83dbSDimitry Andric 1815ffd83dbSDimitry Andric#endif // _LIBCPP_STD_VER >= 14 1825ffd83dbSDimitry Andric 183e8d8bef9SDimitry Andric_LIBCPP_POP_MACROS 184e8d8bef9SDimitry Andric 18506c3fb27SDimitry Andric#if !defined(_LIBCPP_REMOVE_TRANSITIVE_INCLUDES) && _LIBCPP_STD_VER <= 20 18606c3fb27SDimitry Andric# include <atomic> 18706c3fb27SDimitry Andric#endif 18806c3fb27SDimitry Andric 1895ffd83dbSDimitry Andric#endif //_LIBCPP_SEMAPHORE 190