xref: /freebsd/contrib/llvm-project/libcxx/include/semaphore (revision cb14a3fe5122c879eae1fb480ed7ce82a699ddb6)
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
4906c3fb27SDimitry Andric#include <__atomic/atomic_base.h>
5006c3fb27SDimitry Andric#include <__atomic/atomic_sync.h>
5106c3fb27SDimitry Andric#include <__atomic/memory_order.h>
52e8d8bef9SDimitry Andric#include <__availability>
5381ad6265SDimitry Andric#include <__chrono/time_point.h>
54fe6060f1SDimitry Andric#include <__config>
555f757f3fSDimitry Andric#include <__thread/poll_with_backoff.h>
5604eeddc0SDimitry Andric#include <__thread/timed_backoff_policy.h>
575ffd83dbSDimitry Andric#include <__threading_support>
5806c3fb27SDimitry Andric#include <cstddef>
5981ad6265SDimitry Andric#include <limits>
6004eeddc0SDimitry Andric#include <version>
615ffd83dbSDimitry Andric
625ffd83dbSDimitry Andric#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
635ffd83dbSDimitry Andric#  pragma GCC system_header
645ffd83dbSDimitry Andric#endif
655ffd83dbSDimitry Andric
665ffd83dbSDimitry Andric#ifdef _LIBCPP_HAS_NO_THREADS
6781ad6265SDimitry Andric#  error "<semaphore> is not supported since libc++ has been configured without support for threads."
685ffd83dbSDimitry Andric#endif
695ffd83dbSDimitry Andric
70e8d8bef9SDimitry Andric_LIBCPP_PUSH_MACROS
71e8d8bef9SDimitry Andric#include <__undef_macros>
72e8d8bef9SDimitry Andric
735ffd83dbSDimitry Andric#if _LIBCPP_STD_VER >= 14
745ffd83dbSDimitry Andric
755ffd83dbSDimitry Andric_LIBCPP_BEGIN_NAMESPACE_STD
765ffd83dbSDimitry Andric
775ffd83dbSDimitry Andric/*
785ffd83dbSDimitry Andric
79349cc55cSDimitry Andric__atomic_semaphore_base is the general-case implementation.
80e8d8bef9SDimitry AndricIt is a typical Dijkstra semaphore algorithm over atomics, wait and notify
815ffd83dbSDimitry Andricfunctions. It avoids contention against users' own use of those facilities.
825ffd83dbSDimitry Andric
835ffd83dbSDimitry Andric*/
845ffd83dbSDimitry Andric
8506c3fb27SDimitry Andric#  define _LIBCPP_SEMAPHORE_MAX (numeric_limits<ptrdiff_t>::max())
8606c3fb27SDimitry Andric
87*cb14a3feSDimitry Andricclass __atomic_semaphore_base {
88bdd1243dSDimitry Andric  __atomic_base<ptrdiff_t> __a_;
895ffd83dbSDimitry Andric
905ffd83dbSDimitry Andricpublic:
91*cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI constexpr explicit __atomic_semaphore_base(ptrdiff_t __count) : __a_(__count) {}
92*cb14a3feSDimitry Andric  _LIBCPP_AVAILABILITY_SYNC _LIBCPP_HIDE_FROM_ABI void release(ptrdiff_t __update = 1) {
9306c3fb27SDimitry Andric    auto __old = __a_.fetch_add(__update, memory_order_release);
94*cb14a3feSDimitry Andric    _LIBCPP_ASSERT_UNCATEGORIZED(
95*cb14a3feSDimitry Andric        __update <= _LIBCPP_SEMAPHORE_MAX - __old, "update is greater than the expected value");
9606c3fb27SDimitry Andric
97*cb14a3feSDimitry Andric    if (__old > 0) {
9806c3fb27SDimitry Andric      // Nothing to do
99*cb14a3feSDimitry Andric    } else if (__update > 1)
100bdd1243dSDimitry Andric      __a_.notify_all();
1015ffd83dbSDimitry Andric    else
102bdd1243dSDimitry Andric      __a_.notify_one();
1035ffd83dbSDimitry Andric  }
104*cb14a3feSDimitry Andric  _LIBCPP_AVAILABILITY_SYNC _LIBCPP_HIDE_FROM_ABI void acquire() {
105fe6060f1SDimitry Andric    auto const __test_fn = [this]() -> bool {
106bdd1243dSDimitry Andric      auto __old = __a_.load(memory_order_relaxed);
107bdd1243dSDimitry Andric      return (__old != 0) && __a_.compare_exchange_strong(__old, __old - 1, memory_order_acquire, memory_order_relaxed);
1085ffd83dbSDimitry Andric    };
109bdd1243dSDimitry Andric    __cxx_atomic_wait(&__a_.__a_, __test_fn);
1105ffd83dbSDimitry Andric  }
11106c3fb27SDimitry Andric  template <class _Rep, class _Period>
112*cb14a3feSDimitry Andric  _LIBCPP_AVAILABILITY_SYNC _LIBCPP_HIDE_FROM_ABI bool
113*cb14a3feSDimitry Andric  try_acquire_for(chrono::duration<_Rep, _Period> const& __rel_time) {
11406c3fb27SDimitry Andric    if (__rel_time == chrono::duration<_Rep, _Period>::zero())
115349cc55cSDimitry Andric      return try_acquire();
116349cc55cSDimitry Andric    auto const __test_fn = [this]() { return try_acquire(); };
117bdd1243dSDimitry Andric    return std::__libcpp_thread_poll_with_backoff(__test_fn, __libcpp_timed_backoff_policy(), __rel_time);
118349cc55cSDimitry Andric  }
119*cb14a3feSDimitry Andric  _LIBCPP_AVAILABILITY_SYNC _LIBCPP_HIDE_FROM_ABI bool try_acquire() {
120bdd1243dSDimitry Andric    auto __old = __a_.load(memory_order_acquire);
121349cc55cSDimitry Andric    while (true) {
1225ffd83dbSDimitry Andric      if (__old == 0)
1235ffd83dbSDimitry Andric        return false;
124bdd1243dSDimitry Andric      if (__a_.compare_exchange_strong(__old, __old - 1, memory_order_acquire, memory_order_relaxed))
1255ffd83dbSDimitry Andric        return true;
1265ffd83dbSDimitry Andric    }
1275ffd83dbSDimitry Andric  }
1285ffd83dbSDimitry Andric};
1295ffd83dbSDimitry Andric
1305ffd83dbSDimitry Andrictemplate <ptrdiff_t __least_max_value = _LIBCPP_SEMAPHORE_MAX>
131*cb14a3feSDimitry Andricclass counting_semaphore {
132bdd1243dSDimitry Andric  __atomic_semaphore_base __semaphore_;
1335ffd83dbSDimitry Andric
1345ffd83dbSDimitry Andricpublic:
13506c3fb27SDimitry Andric  static_assert(__least_max_value >= 0, "The least maximum value must be a positive number");
13606c3fb27SDimitry Andric
137*cb14a3feSDimitry Andric  static constexpr ptrdiff_t max() noexcept { return __least_max_value; }
1385ffd83dbSDimitry Andric
139*cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI constexpr explicit counting_semaphore(ptrdiff_t __count) : __semaphore_(__count) {
14006c3fb27SDimitry Andric    _LIBCPP_ASSERT_UNCATEGORIZED(
14106c3fb27SDimitry Andric        __count >= 0,
14206c3fb27SDimitry Andric        "counting_semaphore::counting_semaphore(ptrdiff_t): counting_semaphore cannot be "
14306c3fb27SDimitry Andric        "initialized with a negative value");
14406c3fb27SDimitry Andric    _LIBCPP_ASSERT_UNCATEGORIZED(
14506c3fb27SDimitry Andric        __count <= max(),
14606c3fb27SDimitry Andric        "counting_semaphore::counting_semaphore(ptrdiff_t): counting_semaphore cannot be "
14706c3fb27SDimitry Andric        "initialized with a value greater than max()");
14806c3fb27SDimitry Andric  }
1495ffd83dbSDimitry Andric  ~counting_semaphore() = default;
1505ffd83dbSDimitry Andric
1515ffd83dbSDimitry Andric  counting_semaphore(const counting_semaphore&)            = delete;
1525ffd83dbSDimitry Andric  counting_semaphore& operator=(const counting_semaphore&) = delete;
1535ffd83dbSDimitry Andric
154*cb14a3feSDimitry Andric  _LIBCPP_AVAILABILITY_SYNC _LIBCPP_HIDE_FROM_ABI void release(ptrdiff_t __update = 1) {
15506c3fb27SDimitry Andric    _LIBCPP_ASSERT_UNCATEGORIZED(__update >= 0, "counting_semaphore:release called with a negative value");
156bdd1243dSDimitry Andric    __semaphore_.release(__update);
1575ffd83dbSDimitry Andric  }
158*cb14a3feSDimitry Andric  _LIBCPP_AVAILABILITY_SYNC _LIBCPP_HIDE_FROM_ABI void acquire() { __semaphore_.acquire(); }
15906c3fb27SDimitry Andric  template <class _Rep, class _Period>
160*cb14a3feSDimitry Andric  _LIBCPP_AVAILABILITY_SYNC _LIBCPP_HIDE_FROM_ABI bool
161*cb14a3feSDimitry Andric  try_acquire_for(chrono::duration<_Rep, _Period> const& __rel_time) {
162bdd1243dSDimitry Andric    return __semaphore_.try_acquire_for(chrono::duration_cast<chrono::nanoseconds>(__rel_time));
1635ffd83dbSDimitry Andric  }
164*cb14a3feSDimitry Andric  _LIBCPP_AVAILABILITY_SYNC _LIBCPP_HIDE_FROM_ABI bool try_acquire() { return __semaphore_.try_acquire(); }
16506c3fb27SDimitry Andric  template <class _Clock, class _Duration>
166*cb14a3feSDimitry Andric  _LIBCPP_AVAILABILITY_SYNC _LIBCPP_HIDE_FROM_ABI bool
167*cb14a3feSDimitry Andric  try_acquire_until(chrono::time_point<_Clock, _Duration> const& __abs_time) {
16806c3fb27SDimitry Andric    auto const __current = _Clock::now();
16906c3fb27SDimitry Andric    if (__current >= __abs_time)
1705ffd83dbSDimitry Andric      return try_acquire();
1715ffd83dbSDimitry Andric    else
17206c3fb27SDimitry Andric      return try_acquire_for(__abs_time - __current);
1735ffd83dbSDimitry Andric  }
1745ffd83dbSDimitry Andric};
1755ffd83dbSDimitry Andric
1765ffd83dbSDimitry Andricusing binary_semaphore = counting_semaphore<1>;
1775ffd83dbSDimitry Andric
1785ffd83dbSDimitry Andric_LIBCPP_END_NAMESPACE_STD
1795ffd83dbSDimitry Andric
1805ffd83dbSDimitry Andric#endif // _LIBCPP_STD_VER >= 14
1815ffd83dbSDimitry Andric
182e8d8bef9SDimitry Andric_LIBCPP_POP_MACROS
183e8d8bef9SDimitry 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