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 4881ad6265SDimitry Andric#include <__assert> // all public C++ headers provide the assertion handler 49*06c3fb27SDimitry Andric#include <__atomic/atomic_base.h> 50*06c3fb27SDimitry Andric#include <__atomic/atomic_sync.h> 51*06c3fb27SDimitry Andric#include <__atomic/memory_order.h> 52e8d8bef9SDimitry Andric#include <__availability> 5381ad6265SDimitry Andric#include <__chrono/time_point.h> 54fe6060f1SDimitry Andric#include <__config> 5504eeddc0SDimitry Andric#include <__thread/timed_backoff_policy.h> 565ffd83dbSDimitry Andric#include <__threading_support> 57*06c3fb27SDimitry Andric#include <cstddef> 5881ad6265SDimitry Andric#include <limits> 5904eeddc0SDimitry Andric#include <version> 605ffd83dbSDimitry Andric 615ffd83dbSDimitry Andric#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) 625ffd83dbSDimitry Andric# pragma GCC system_header 635ffd83dbSDimitry Andric#endif 645ffd83dbSDimitry Andric 655ffd83dbSDimitry Andric#ifdef _LIBCPP_HAS_NO_THREADS 6681ad6265SDimitry Andric# error "<semaphore> is not supported since libc++ has been configured without support for threads." 675ffd83dbSDimitry Andric#endif 685ffd83dbSDimitry Andric 69e8d8bef9SDimitry Andric_LIBCPP_PUSH_MACROS 70e8d8bef9SDimitry Andric#include <__undef_macros> 71e8d8bef9SDimitry Andric 725ffd83dbSDimitry Andric#if _LIBCPP_STD_VER >= 14 735ffd83dbSDimitry Andric 745ffd83dbSDimitry Andric_LIBCPP_BEGIN_NAMESPACE_STD 755ffd83dbSDimitry Andric 765ffd83dbSDimitry Andric/* 775ffd83dbSDimitry Andric 78349cc55cSDimitry Andric__atomic_semaphore_base is the general-case implementation. 79e8d8bef9SDimitry AndricIt is a typical Dijkstra semaphore algorithm over atomics, wait and notify 805ffd83dbSDimitry Andricfunctions. It avoids contention against users' own use of those facilities. 815ffd83dbSDimitry Andric 825ffd83dbSDimitry Andric*/ 835ffd83dbSDimitry Andric 84*06c3fb27SDimitry Andric#define _LIBCPP_SEMAPHORE_MAX (numeric_limits<ptrdiff_t>::max()) 85*06c3fb27SDimitry Andric 865ffd83dbSDimitry Andricclass __atomic_semaphore_base 875ffd83dbSDimitry Andric{ 88bdd1243dSDimitry Andric __atomic_base<ptrdiff_t> __a_; 895ffd83dbSDimitry Andric 905ffd83dbSDimitry Andricpublic: 915ffd83dbSDimitry Andric _LIBCPP_INLINE_VISIBILITY 92bdd1243dSDimitry Andric constexpr explicit __atomic_semaphore_base(ptrdiff_t __count) : __a_(__count) 935ffd83dbSDimitry Andric { 945ffd83dbSDimitry Andric } 955ffd83dbSDimitry Andric _LIBCPP_AVAILABILITY_SYNC _LIBCPP_INLINE_VISIBILITY 965ffd83dbSDimitry Andric void release(ptrdiff_t __update = 1) 975ffd83dbSDimitry Andric { 98*06c3fb27SDimitry Andric auto __old = __a_.fetch_add(__update, memory_order_release); 99*06c3fb27SDimitry Andric _LIBCPP_ASSERT_UNCATEGORIZED(__update <= _LIBCPP_SEMAPHORE_MAX - __old, "update is greater than the expected value"); 100*06c3fb27SDimitry Andric 101*06c3fb27SDimitry Andric if (__old > 0) 102*06c3fb27SDimitry Andric { 103*06c3fb27SDimitry Andric // Nothing to do 104*06c3fb27SDimitry Andric } 1055ffd83dbSDimitry Andric else if (__update > 1) 106bdd1243dSDimitry Andric __a_.notify_all(); 1075ffd83dbSDimitry Andric else 108bdd1243dSDimitry Andric __a_.notify_one(); 1095ffd83dbSDimitry Andric } 1105ffd83dbSDimitry Andric _LIBCPP_AVAILABILITY_SYNC _LIBCPP_INLINE_VISIBILITY 1115ffd83dbSDimitry Andric void acquire() 1125ffd83dbSDimitry Andric { 113fe6060f1SDimitry Andric auto const __test_fn = [this]() -> bool { 114bdd1243dSDimitry Andric auto __old = __a_.load(memory_order_relaxed); 115bdd1243dSDimitry Andric return (__old != 0) && __a_.compare_exchange_strong(__old, __old - 1, memory_order_acquire, memory_order_relaxed); 1165ffd83dbSDimitry Andric }; 117bdd1243dSDimitry Andric __cxx_atomic_wait(&__a_.__a_, __test_fn); 1185ffd83dbSDimitry Andric } 119*06c3fb27SDimitry Andric template <class _Rep, class _Period> 1205ffd83dbSDimitry Andric _LIBCPP_AVAILABILITY_SYNC _LIBCPP_INLINE_VISIBILITY 121*06c3fb27SDimitry Andric bool try_acquire_for(chrono::duration<_Rep, _Period> const& __rel_time) 1225ffd83dbSDimitry Andric { 123*06c3fb27SDimitry Andric if (__rel_time == chrono::duration<_Rep, _Period>::zero()) 124349cc55cSDimitry Andric return try_acquire(); 125349cc55cSDimitry Andric auto const __test_fn = [this]() { return try_acquire(); }; 126bdd1243dSDimitry Andric return std::__libcpp_thread_poll_with_backoff(__test_fn, __libcpp_timed_backoff_policy(), __rel_time); 127349cc55cSDimitry Andric } 128349cc55cSDimitry Andric _LIBCPP_AVAILABILITY_SYNC _LIBCPP_INLINE_VISIBILITY 129349cc55cSDimitry Andric bool try_acquire() 130349cc55cSDimitry Andric { 131bdd1243dSDimitry Andric auto __old = __a_.load(memory_order_acquire); 132349cc55cSDimitry Andric while (true) { 1335ffd83dbSDimitry Andric if (__old == 0) 1345ffd83dbSDimitry Andric return false; 135bdd1243dSDimitry Andric if (__a_.compare_exchange_strong(__old, __old - 1, memory_order_acquire, memory_order_relaxed)) 1365ffd83dbSDimitry Andric return true; 1375ffd83dbSDimitry Andric } 1385ffd83dbSDimitry Andric } 1395ffd83dbSDimitry Andric}; 1405ffd83dbSDimitry Andric 1415ffd83dbSDimitry Andrictemplate<ptrdiff_t __least_max_value = _LIBCPP_SEMAPHORE_MAX> 1425ffd83dbSDimitry Andricclass counting_semaphore 1435ffd83dbSDimitry Andric{ 144bdd1243dSDimitry Andric __atomic_semaphore_base __semaphore_; 1455ffd83dbSDimitry Andric 1465ffd83dbSDimitry Andricpublic: 147*06c3fb27SDimitry Andric static_assert(__least_max_value >= 0, "The least maximum value must be a positive number"); 148*06c3fb27SDimitry Andric 1495ffd83dbSDimitry Andric static constexpr ptrdiff_t max() noexcept { 1505ffd83dbSDimitry Andric return __least_max_value; 1515ffd83dbSDimitry Andric } 1525ffd83dbSDimitry Andric 1535ffd83dbSDimitry Andric _LIBCPP_INLINE_VISIBILITY 154*06c3fb27SDimitry Andric constexpr explicit counting_semaphore(ptrdiff_t __count) : __semaphore_(__count) 155*06c3fb27SDimitry Andric { 156*06c3fb27SDimitry Andric _LIBCPP_ASSERT_UNCATEGORIZED( 157*06c3fb27SDimitry Andric __count >= 0, 158*06c3fb27SDimitry Andric "counting_semaphore::counting_semaphore(ptrdiff_t): counting_semaphore cannot be " 159*06c3fb27SDimitry Andric "initialized with a negative value"); 160*06c3fb27SDimitry Andric _LIBCPP_ASSERT_UNCATEGORIZED( 161*06c3fb27SDimitry Andric __count <= max(), 162*06c3fb27SDimitry Andric "counting_semaphore::counting_semaphore(ptrdiff_t): counting_semaphore cannot be " 163*06c3fb27SDimitry Andric "initialized with a value greater than max()"); 164*06c3fb27SDimitry Andric } 1655ffd83dbSDimitry Andric ~counting_semaphore() = default; 1665ffd83dbSDimitry Andric 1675ffd83dbSDimitry Andric counting_semaphore(const counting_semaphore&) = delete; 1685ffd83dbSDimitry Andric counting_semaphore& operator=(const counting_semaphore&) = delete; 1695ffd83dbSDimitry Andric 1705ffd83dbSDimitry Andric _LIBCPP_AVAILABILITY_SYNC _LIBCPP_INLINE_VISIBILITY 1715ffd83dbSDimitry Andric void release(ptrdiff_t __update = 1) 1725ffd83dbSDimitry Andric { 173*06c3fb27SDimitry Andric _LIBCPP_ASSERT_UNCATEGORIZED(__update >= 0, "counting_semaphore:release called with a negative value"); 174bdd1243dSDimitry Andric __semaphore_.release(__update); 1755ffd83dbSDimitry Andric } 1765ffd83dbSDimitry Andric _LIBCPP_AVAILABILITY_SYNC _LIBCPP_INLINE_VISIBILITY 1775ffd83dbSDimitry Andric void acquire() 1785ffd83dbSDimitry Andric { 179bdd1243dSDimitry Andric __semaphore_.acquire(); 1805ffd83dbSDimitry Andric } 181*06c3fb27SDimitry Andric template<class _Rep, class _Period> 1825ffd83dbSDimitry Andric _LIBCPP_AVAILABILITY_SYNC _LIBCPP_INLINE_VISIBILITY 183*06c3fb27SDimitry Andric bool try_acquire_for(chrono::duration<_Rep, _Period> const& __rel_time) 1845ffd83dbSDimitry Andric { 185bdd1243dSDimitry Andric return __semaphore_.try_acquire_for(chrono::duration_cast<chrono::nanoseconds>(__rel_time)); 1865ffd83dbSDimitry Andric } 1875ffd83dbSDimitry Andric _LIBCPP_AVAILABILITY_SYNC _LIBCPP_INLINE_VISIBILITY 1885ffd83dbSDimitry Andric bool try_acquire() 1895ffd83dbSDimitry Andric { 190bdd1243dSDimitry Andric return __semaphore_.try_acquire(); 1915ffd83dbSDimitry Andric } 192*06c3fb27SDimitry Andric template <class _Clock, class _Duration> 1935ffd83dbSDimitry Andric _LIBCPP_AVAILABILITY_SYNC _LIBCPP_INLINE_VISIBILITY 194*06c3fb27SDimitry Andric bool try_acquire_until(chrono::time_point<_Clock, _Duration> const& __abs_time) 1955ffd83dbSDimitry Andric { 196*06c3fb27SDimitry Andric auto const __current = _Clock::now(); 197*06c3fb27SDimitry Andric if (__current >= __abs_time) 1985ffd83dbSDimitry Andric return try_acquire(); 1995ffd83dbSDimitry Andric else 200*06c3fb27SDimitry Andric return try_acquire_for(__abs_time - __current); 2015ffd83dbSDimitry Andric } 2025ffd83dbSDimitry Andric}; 2035ffd83dbSDimitry Andric 2045ffd83dbSDimitry Andricusing binary_semaphore = counting_semaphore<1>; 2055ffd83dbSDimitry Andric 2065ffd83dbSDimitry Andric_LIBCPP_END_NAMESPACE_STD 2075ffd83dbSDimitry Andric 2085ffd83dbSDimitry Andric#endif // _LIBCPP_STD_VER >= 14 2095ffd83dbSDimitry Andric 210e8d8bef9SDimitry Andric_LIBCPP_POP_MACROS 211e8d8bef9SDimitry Andric 212*06c3fb27SDimitry Andric#if !defined(_LIBCPP_REMOVE_TRANSITIVE_INCLUDES) && _LIBCPP_STD_VER <= 20 213*06c3fb27SDimitry Andric# include <atomic> 214*06c3fb27SDimitry Andric#endif 215*06c3fb27SDimitry Andric 2165ffd83dbSDimitry Andric#endif //_LIBCPP_SEMAPHORE 217