Warning, /include/c++/v1/__cxx03/latch is written in an unsupported language. File is not indexed.
0001 // -*- C++ -*-
0002 //===----------------------------------------------------------------------===//
0003 //
0004 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0005 // See https://llvm.org/LICENSE.txt for license information.
0006 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0007 //
0008 //===----------------------------------------------------------------------===//
0009
0010 #ifndef _LIBCPP___CXX03_LATCH
0011 #define _LIBCPP___CXX03_LATCH
0012
0013 /*
0014 latch synopsis
0015
0016 namespace std
0017 {
0018
0019 class latch
0020 {
0021 public:
0022 static constexpr ptrdiff_t max() noexcept;
0023
0024 constexpr explicit latch(ptrdiff_t __expected);
0025 ~latch();
0026
0027 latch(const latch&) = delete;
0028 latch& operator=(const latch&) = delete;
0029
0030 void count_down(ptrdiff_t __update = 1);
0031 bool try_wait() const noexcept;
0032 void wait() const;
0033 void arrive_and_wait(ptrdiff_t __update = 1);
0034
0035 private:
0036 ptrdiff_t __counter; // exposition only
0037 };
0038
0039 }
0040
0041 */
0042
0043 #include <__cxx03/__config>
0044
0045 #if !defined(_LIBCPP_HAS_NO_THREADS)
0046
0047 # include <__cxx03/__assert>
0048 # include <__cxx03/__atomic/atomic_base.h>
0049 # include <__cxx03/__atomic/atomic_sync.h>
0050 # include <__cxx03/__atomic/memory_order.h>
0051 # include <__cxx03/cstddef>
0052 # include <__cxx03/limits>
0053 # include <__cxx03/version>
0054
0055 # if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
0056 # pragma GCC system_header
0057 # endif
0058
0059 _LIBCPP_PUSH_MACROS
0060 # include <__cxx03/__undef_macros>
0061
0062 # if _LIBCPP_STD_VER >= 14
0063
0064 _LIBCPP_BEGIN_NAMESPACE_STD
0065
0066 class _LIBCPP_DEPRECATED_ATOMIC_SYNC latch {
0067 __atomic_base<ptrdiff_t> __a_;
0068
0069 public:
0070 static _LIBCPP_HIDE_FROM_ABI constexpr ptrdiff_t max() noexcept { return numeric_limits<ptrdiff_t>::max(); }
0071
0072 inline _LIBCPP_HIDE_FROM_ABI constexpr explicit latch(ptrdiff_t __expected) : __a_(__expected) {
0073 _LIBCPP_ASSERT_ARGUMENT_WITHIN_DOMAIN(
0074 __expected >= 0,
0075 "latch::latch(ptrdiff_t): latch cannot be "
0076 "initialized with a negative value");
0077 _LIBCPP_ASSERT_ARGUMENT_WITHIN_DOMAIN(
0078 __expected <= max(),
0079 "latch::latch(ptrdiff_t): latch cannot be "
0080 "initialized with a value greater than max()");
0081 }
0082
0083 _LIBCPP_HIDE_FROM_ABI ~latch() = default;
0084 latch(const latch&) = delete;
0085 latch& operator=(const latch&) = delete;
0086
0087 inline _LIBCPP_AVAILABILITY_SYNC _LIBCPP_HIDE_FROM_ABI void count_down(ptrdiff_t __update = 1) {
0088 _LIBCPP_ASSERT_ARGUMENT_WITHIN_DOMAIN(__update >= 0, "latch::count_down called with a negative value");
0089 auto const __old = __a_.fetch_sub(__update, memory_order_release);
0090 _LIBCPP_ASSERT_ARGUMENT_WITHIN_DOMAIN(
0091 __update <= __old,
0092 "latch::count_down called with a value greater "
0093 "than the internal counter");
0094 if (__old == __update)
0095 __a_.notify_all();
0096 }
0097 inline _LIBCPP_HIDE_FROM_ABI bool try_wait() const noexcept {
0098 auto __value = __a_.load(memory_order_acquire);
0099 return try_wait_impl(__value);
0100 }
0101 inline _LIBCPP_AVAILABILITY_SYNC _LIBCPP_HIDE_FROM_ABI void wait() const {
0102 std::__atomic_wait_unless(
0103 __a_, [this](ptrdiff_t& __value) -> bool { return try_wait_impl(__value); }, memory_order_acquire);
0104 }
0105 inline _LIBCPP_AVAILABILITY_SYNC _LIBCPP_HIDE_FROM_ABI void arrive_and_wait(ptrdiff_t __update = 1) {
0106 _LIBCPP_ASSERT_ARGUMENT_WITHIN_DOMAIN(__update >= 0, "latch::arrive_and_wait called with a negative value");
0107 // other preconditions on __update are checked in count_down()
0108
0109 count_down(__update);
0110 wait();
0111 }
0112
0113 private:
0114 _LIBCPP_HIDE_FROM_ABI bool try_wait_impl(ptrdiff_t& __value) const noexcept { return __value == 0; }
0115 };
0116
0117 _LIBCPP_END_NAMESPACE_STD
0118
0119 # endif // _LIBCPP_STD_VER >= 14
0120
0121 _LIBCPP_POP_MACROS
0122
0123 #endif // !defined(_LIBCPP_HAS_NO_THREADS)
0124
0125 #if !defined(_LIBCPP_REMOVE_TRANSITIVE_INCLUDES) && _LIBCPP_STD_VER <= 20
0126 # include <__cxx03/atomic>
0127 #endif
0128
0129 #endif //_LIBCPP___CXX03_LATCH