Olivier Giroux | 161e6e8 | 2020-02-18 09:58:34 -0500 | [diff] [blame] | 1 | // -*- C++ -*- |
Louis Dionne | 9bd9388 | 2021-11-17 16:25:01 -0500 | [diff] [blame] | 2 | //===----------------------------------------------------------------------===// |
Olivier Giroux | 161e6e8 | 2020-02-18 09:58:34 -0500 | [diff] [blame] | 3 | // |
| 4 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 5 | // See https://llvm.org/LICENSE.txt for license information. |
| 6 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
| 10 | #ifndef _LIBCPP_LATCH |
| 11 | #define _LIBCPP_LATCH |
| 12 | |
| 13 | /* |
| 14 | latch synopsis |
| 15 | |
| 16 | namespace std |
| 17 | { |
| 18 | |
| 19 | class latch |
| 20 | { |
| 21 | public: |
Marek Kurdej | 7f401b9 | 2020-12-06 15:36:18 +0100 | [diff] [blame] | 22 | static constexpr ptrdiff_t max() noexcept; |
| 23 | |
Olivier Giroux | 161e6e8 | 2020-02-18 09:58:34 -0500 | [diff] [blame] | 24 | constexpr explicit latch(ptrdiff_t __expected); |
| 25 | ~latch(); |
| 26 | |
| 27 | latch(const latch&) = delete; |
| 28 | latch& operator=(const latch&) = delete; |
| 29 | |
| 30 | void count_down(ptrdiff_t __update = 1); |
| 31 | bool try_wait() const noexcept; |
| 32 | void wait() const; |
| 33 | void arrive_and_wait(ptrdiff_t __update = 1); |
| 34 | |
| 35 | private: |
| 36 | ptrdiff_t __counter; // exposition only |
| 37 | }; |
| 38 | |
| 39 | } |
| 40 | |
| 41 | */ |
| 42 | |
Louis Dionne | 73912b2 | 2020-11-04 15:01:25 -0500 | [diff] [blame] | 43 | #include <__availability> |
Arthur O'Dwyer | ef18160 | 2021-05-19 11:57:04 -0400 | [diff] [blame] | 44 | #include <__config> |
Olivier Giroux | 161e6e8 | 2020-02-18 09:58:34 -0500 | [diff] [blame] | 45 | #include <atomic> |
Mark de Wever | ce8f12c | 2021-12-22 18:14:14 +0100 | [diff] [blame] | 46 | #include <version> |
Olivier Giroux | 161e6e8 | 2020-02-18 09:58:34 -0500 | [diff] [blame] | 47 | |
| 48 | #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) |
Arthur O'Dwyer | 6eeaa00 | 2022-02-01 20:16:40 -0500 | [diff] [blame] | 49 | # pragma GCC system_header |
Olivier Giroux | 161e6e8 | 2020-02-18 09:58:34 -0500 | [diff] [blame] | 50 | #endif |
| 51 | |
| 52 | #ifdef _LIBCPP_HAS_NO_THREADS |
| 53 | # error <latch> is not supported on this single threaded system |
| 54 | #endif |
| 55 | |
Arthur O'Dwyer | 2422fa2 | 2020-12-02 18:55:01 -0500 | [diff] [blame] | 56 | _LIBCPP_PUSH_MACROS |
| 57 | #include <__undef_macros> |
| 58 | |
Louis Dionne | 1ab9531 | 2020-02-24 18:12:44 -0500 | [diff] [blame] | 59 | #if _LIBCPP_STD_VER >= 14 |
Olivier Giroux | 161e6e8 | 2020-02-18 09:58:34 -0500 | [diff] [blame] | 60 | |
| 61 | _LIBCPP_BEGIN_NAMESPACE_STD |
| 62 | |
| 63 | class latch |
| 64 | { |
| 65 | __atomic_base<ptrdiff_t> __a; |
| 66 | |
| 67 | public: |
| 68 | static constexpr ptrdiff_t max() noexcept { |
| 69 | return numeric_limits<ptrdiff_t>::max(); |
| 70 | } |
| 71 | |
| 72 | inline _LIBCPP_INLINE_VISIBILITY |
| 73 | constexpr explicit latch(ptrdiff_t __expected) : __a(__expected) { } |
| 74 | |
| 75 | ~latch() = default; |
| 76 | latch(const latch&) = delete; |
| 77 | latch& operator=(const latch&) = delete; |
| 78 | |
Louis Dionne | 3dde4ab | 2020-02-24 10:09:29 -0500 | [diff] [blame] | 79 | inline _LIBCPP_AVAILABILITY_SYNC _LIBCPP_INLINE_VISIBILITY |
Olivier Giroux | 161e6e8 | 2020-02-18 09:58:34 -0500 | [diff] [blame] | 80 | void count_down(ptrdiff_t __update = 1) |
| 81 | { |
| 82 | auto const __old = __a.fetch_sub(__update, memory_order_release); |
| 83 | if(__old == __update) |
| 84 | __a.notify_all(); |
| 85 | } |
| 86 | inline _LIBCPP_INLINE_VISIBILITY |
| 87 | bool try_wait() const noexcept |
| 88 | { |
| 89 | return 0 == __a.load(memory_order_acquire); |
| 90 | } |
Louis Dionne | 3dde4ab | 2020-02-24 10:09:29 -0500 | [diff] [blame] | 91 | inline _LIBCPP_AVAILABILITY_SYNC _LIBCPP_INLINE_VISIBILITY |
Olivier Giroux | 161e6e8 | 2020-02-18 09:58:34 -0500 | [diff] [blame] | 92 | void wait() const |
| 93 | { |
| 94 | auto const __test_fn = [=]() -> bool { |
| 95 | return try_wait(); |
| 96 | }; |
| 97 | __cxx_atomic_wait(&__a.__a_, __test_fn); |
| 98 | } |
Louis Dionne | 3dde4ab | 2020-02-24 10:09:29 -0500 | [diff] [blame] | 99 | inline _LIBCPP_AVAILABILITY_SYNC _LIBCPP_INLINE_VISIBILITY |
Olivier Giroux | 161e6e8 | 2020-02-18 09:58:34 -0500 | [diff] [blame] | 100 | void arrive_and_wait(ptrdiff_t __update = 1) |
| 101 | { |
| 102 | count_down(__update); |
| 103 | wait(); |
| 104 | } |
| 105 | }; |
| 106 | |
| 107 | _LIBCPP_END_NAMESPACE_STD |
| 108 | |
Louis Dionne | 1ab9531 | 2020-02-24 18:12:44 -0500 | [diff] [blame] | 109 | #endif // _LIBCPP_STD_VER >= 14 |
| 110 | |
Arthur O'Dwyer | 2422fa2 | 2020-12-02 18:55:01 -0500 | [diff] [blame] | 111 | _LIBCPP_POP_MACROS |
| 112 | |
Olivier Giroux | 161e6e8 | 2020-02-18 09:58:34 -0500 | [diff] [blame] | 113 | #endif //_LIBCPP_LATCH |