xref: /freebsd/contrib/llvm-project/libcxx/include/latch (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_LATCH
115ffd83dbSDimitry Andric#define _LIBCPP_LATCH
125ffd83dbSDimitry Andric
135ffd83dbSDimitry Andric/*
145ffd83dbSDimitry Andric    latch synopsis
155ffd83dbSDimitry Andric
165ffd83dbSDimitry Andricnamespace std
175ffd83dbSDimitry Andric{
185ffd83dbSDimitry Andric
195ffd83dbSDimitry Andric  class latch
205ffd83dbSDimitry Andric  {
215ffd83dbSDimitry Andric  public:
22e8d8bef9SDimitry Andric    static constexpr ptrdiff_t max() noexcept;
23e8d8bef9SDimitry Andric
245ffd83dbSDimitry Andric    constexpr explicit latch(ptrdiff_t __expected);
255ffd83dbSDimitry Andric    ~latch();
265ffd83dbSDimitry Andric
275ffd83dbSDimitry Andric    latch(const latch&) = delete;
285ffd83dbSDimitry Andric    latch& operator=(const latch&) = delete;
295ffd83dbSDimitry Andric
305ffd83dbSDimitry Andric    void count_down(ptrdiff_t __update = 1);
315ffd83dbSDimitry Andric    bool try_wait() const noexcept;
325ffd83dbSDimitry Andric    void wait() const;
335ffd83dbSDimitry Andric    void arrive_and_wait(ptrdiff_t __update = 1);
345ffd83dbSDimitry Andric
355ffd83dbSDimitry Andric  private:
365ffd83dbSDimitry Andric    ptrdiff_t __counter; // exposition only
375ffd83dbSDimitry Andric  };
385ffd83dbSDimitry Andric
395ffd83dbSDimitry Andric}
405ffd83dbSDimitry Andric
415ffd83dbSDimitry Andric*/
425ffd83dbSDimitry Andric
4381ad6265SDimitry Andric#include <__assert> // all public C++ headers provide the assertion handler
4406c3fb27SDimitry Andric#include <__atomic/atomic_base.h>
4506c3fb27SDimitry Andric#include <__atomic/atomic_sync.h>
4606c3fb27SDimitry Andric#include <__atomic/memory_order.h>
47e8d8bef9SDimitry Andric#include <__availability>
48fe6060f1SDimitry Andric#include <__config>
4906c3fb27SDimitry Andric#include <cstddef>
5081ad6265SDimitry Andric#include <limits>
5104eeddc0SDimitry Andric#include <version>
525ffd83dbSDimitry Andric
535ffd83dbSDimitry Andric#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
545ffd83dbSDimitry Andric#  pragma GCC system_header
555ffd83dbSDimitry Andric#endif
565ffd83dbSDimitry Andric
575ffd83dbSDimitry Andric#ifdef _LIBCPP_HAS_NO_THREADS
5881ad6265SDimitry Andric#  error "<latch> is not supported since libc++ has been configured without support for threads."
595ffd83dbSDimitry Andric#endif
605ffd83dbSDimitry Andric
61e8d8bef9SDimitry Andric_LIBCPP_PUSH_MACROS
62e8d8bef9SDimitry Andric#include <__undef_macros>
63e8d8bef9SDimitry Andric
645ffd83dbSDimitry Andric#if _LIBCPP_STD_VER >= 14
655ffd83dbSDimitry Andric
665ffd83dbSDimitry Andric_LIBCPP_BEGIN_NAMESPACE_STD
675ffd83dbSDimitry Andric
68*cb14a3feSDimitry Andricclass latch {
69bdd1243dSDimitry Andric  __atomic_base<ptrdiff_t> __a_;
705ffd83dbSDimitry Andric
715ffd83dbSDimitry Andricpublic:
72*cb14a3feSDimitry Andric  static _LIBCPP_HIDE_FROM_ABI constexpr ptrdiff_t max() noexcept { return numeric_limits<ptrdiff_t>::max(); }
735ffd83dbSDimitry Andric
74*cb14a3feSDimitry Andric  inline _LIBCPP_HIDE_FROM_ABI constexpr explicit latch(ptrdiff_t __expected) : __a_(__expected) {
75*cb14a3feSDimitry Andric    _LIBCPP_ASSERT_UNCATEGORIZED(
76*cb14a3feSDimitry Andric        __expected >= 0,
7706c3fb27SDimitry Andric        "latch::latch(ptrdiff_t): latch cannot be "
7806c3fb27SDimitry Andric        "initialized with a negative value");
79*cb14a3feSDimitry Andric    _LIBCPP_ASSERT_UNCATEGORIZED(
80*cb14a3feSDimitry Andric        __expected <= max(),
8106c3fb27SDimitry Andric        "latch::latch(ptrdiff_t): latch cannot be "
8206c3fb27SDimitry Andric        "initialized with a value greater than max()");
8306c3fb27SDimitry Andric  }
845ffd83dbSDimitry Andric
8506c3fb27SDimitry Andric  _LIBCPP_HIDE_FROM_ABI ~latch() = default;
865ffd83dbSDimitry Andric  latch(const latch&)            = delete;
875ffd83dbSDimitry Andric  latch& operator=(const latch&) = delete;
885ffd83dbSDimitry Andric
89*cb14a3feSDimitry Andric  inline _LIBCPP_AVAILABILITY_SYNC _LIBCPP_HIDE_FROM_ABI void count_down(ptrdiff_t __update = 1) {
90*cb14a3feSDimitry Andric    _LIBCPP_ASSERT_UNCATEGORIZED(__update >= 0, "latch::count_down called with a negative value");
91bdd1243dSDimitry Andric    auto const __old = __a_.fetch_sub(__update, memory_order_release);
9206c3fb27SDimitry Andric    _LIBCPP_ASSERT_UNCATEGORIZED(
93*cb14a3feSDimitry Andric        __update <= __old,
94*cb14a3feSDimitry Andric        "latch::count_down called with a value greater "
9506c3fb27SDimitry Andric        "than the internal counter");
965ffd83dbSDimitry Andric    if (__old == __update)
97bdd1243dSDimitry Andric      __a_.notify_all();
985ffd83dbSDimitry Andric  }
99*cb14a3feSDimitry Andric  inline _LIBCPP_HIDE_FROM_ABI bool try_wait() const noexcept { return 0 == __a_.load(memory_order_acquire); }
100*cb14a3feSDimitry Andric  inline _LIBCPP_AVAILABILITY_SYNC _LIBCPP_HIDE_FROM_ABI void wait() const {
101*cb14a3feSDimitry Andric    __cxx_atomic_wait(&__a_.__a_, [this]() -> bool { return try_wait(); });
1025ffd83dbSDimitry Andric  }
103*cb14a3feSDimitry Andric  inline _LIBCPP_AVAILABILITY_SYNC _LIBCPP_HIDE_FROM_ABI void arrive_and_wait(ptrdiff_t __update = 1) {
104*cb14a3feSDimitry Andric    _LIBCPP_ASSERT_UNCATEGORIZED(__update >= 0, "latch::arrive_and_wait called with a negative value");
10506c3fb27SDimitry Andric    // other preconditions on __update are checked in count_down()
10606c3fb27SDimitry Andric
1075ffd83dbSDimitry Andric    count_down(__update);
1085ffd83dbSDimitry Andric    wait();
1095ffd83dbSDimitry Andric  }
1105ffd83dbSDimitry Andric};
1115ffd83dbSDimitry Andric
1125ffd83dbSDimitry Andric_LIBCPP_END_NAMESPACE_STD
1135ffd83dbSDimitry Andric
1145ffd83dbSDimitry Andric#endif // _LIBCPP_STD_VER >= 14
1155ffd83dbSDimitry Andric
116e8d8bef9SDimitry Andric_LIBCPP_POP_MACROS
117e8d8bef9SDimitry Andric
11806c3fb27SDimitry Andric#if !defined(_LIBCPP_REMOVE_TRANSITIVE_INCLUDES) && _LIBCPP_STD_VER <= 20
11906c3fb27SDimitry Andric#  include <atomic>
12006c3fb27SDimitry Andric#endif
12106c3fb27SDimitry Andric
1225ffd83dbSDimitry Andric#endif //_LIBCPP_LATCH
123