2020-02-18 14:58:34 +00:00
|
|
|
// -*- C++ -*-
|
2021-11-17 21:25:01 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
2020-02-18 14:58:34 +00:00
|
|
|
//
|
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#ifndef _LIBCPP_LATCH
|
|
|
|
#define _LIBCPP_LATCH
|
|
|
|
|
|
|
|
/*
|
|
|
|
latch synopsis
|
|
|
|
|
|
|
|
namespace std
|
|
|
|
{
|
|
|
|
|
|
|
|
class latch
|
|
|
|
{
|
|
|
|
public:
|
2020-12-06 14:36:18 +00:00
|
|
|
static constexpr ptrdiff_t max() noexcept;
|
|
|
|
|
2020-02-18 14:58:34 +00:00
|
|
|
constexpr explicit latch(ptrdiff_t __expected);
|
|
|
|
~latch();
|
|
|
|
|
|
|
|
latch(const latch&) = delete;
|
|
|
|
latch& operator=(const latch&) = delete;
|
|
|
|
|
|
|
|
void count_down(ptrdiff_t __update = 1);
|
|
|
|
bool try_wait() const noexcept;
|
|
|
|
void wait() const;
|
|
|
|
void arrive_and_wait(ptrdiff_t __update = 1);
|
|
|
|
|
|
|
|
private:
|
|
|
|
ptrdiff_t __counter; // exposition only
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
2024-01-16 18:13:40 +00:00
|
|
|
#include <__config>
|
|
|
|
|
|
|
|
#ifdef _LIBCPP_HAS_NO_THREADS
|
|
|
|
# error "<latch> is not supported since libc++ has been configured without support for threads."
|
|
|
|
#endif
|
|
|
|
|
2022-03-25 16:55:36 +00:00
|
|
|
#include <__assert> // all public C++ headers provide the assertion handler
|
2023-01-31 18:23:30 +00:00
|
|
|
#include <__atomic/atomic_base.h>
|
|
|
|
#include <__atomic/atomic_sync.h>
|
|
|
|
#include <__atomic/memory_order.h>
|
2020-11-04 20:01:25 +00:00
|
|
|
#include <__availability>
|
2023-01-31 18:23:30 +00:00
|
|
|
#include <cstddef>
|
2022-05-10 19:27:52 +00:00
|
|
|
#include <limits>
|
2021-12-22 17:14:14 +00:00
|
|
|
#include <version>
|
2020-02-18 14:58:34 +00:00
|
|
|
|
|
|
|
#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
|
2022-02-02 01:16:40 +00:00
|
|
|
# pragma GCC system_header
|
2020-02-18 14:58:34 +00:00
|
|
|
#endif
|
|
|
|
|
2020-12-02 23:55:01 +00:00
|
|
|
_LIBCPP_PUSH_MACROS
|
|
|
|
#include <__undef_macros>
|
|
|
|
|
2020-02-24 23:12:44 +00:00
|
|
|
#if _LIBCPP_STD_VER >= 14
|
2020-02-18 14:58:34 +00:00
|
|
|
|
|
|
|
_LIBCPP_BEGIN_NAMESPACE_STD
|
|
|
|
|
|
|
|
class latch {
|
2022-09-02 14:19:07 +00:00
|
|
|
__atomic_base<ptrdiff_t> __a_;
|
2020-02-18 14:58:34 +00:00
|
|
|
|
|
|
|
public:
|
2023-01-23 09:27:14 +00:00
|
|
|
static _LIBCPP_HIDE_FROM_ABI constexpr ptrdiff_t max() noexcept { return numeric_limits<ptrdiff_t>::max(); }
|
2023-12-18 19:01:33 +00:00
|
|
|
|
2023-07-05 21:33:18 +00:00
|
|
|
inline _LIBCPP_HIDE_FROM_ABI constexpr explicit latch(ptrdiff_t __expected) : __a_(__expected) {
|
|
|
|
_LIBCPP_ASSERT_UNCATEGORIZED(
|
|
|
|
__expected >= 0,
|
|
|
|
"latch::latch(ptrdiff_t): latch cannot be "
|
|
|
|
"initialized with a negative value");
|
|
|
|
_LIBCPP_ASSERT_UNCATEGORIZED(
|
|
|
|
__expected <= max(),
|
|
|
|
"latch::latch(ptrdiff_t): latch cannot be "
|
|
|
|
"initialized with a value greater than max()");
|
|
|
|
}
|
2023-12-18 19:01:33 +00:00
|
|
|
|
2023-01-23 09:27:14 +00:00
|
|
|
_LIBCPP_HIDE_FROM_ABI ~latch() = default;
|
2020-02-18 14:58:34 +00:00
|
|
|
latch(const latch&) = delete;
|
|
|
|
latch& operator=(const latch&) = delete;
|
2023-12-18 19:01:33 +00:00
|
|
|
|
2020-02-24 15:09:29 +00:00
|
|
|
inline _LIBCPP_AVAILABILITY_SYNC _LIBCPP_HIDE_FROM_ABI void count_down(ptrdiff_t __update = 1) {
|
2023-07-05 21:33:18 +00:00
|
|
|
_LIBCPP_ASSERT_UNCATEGORIZED(__update >= 0, "latch::count_down called with a negative value");
|
2022-09-02 14:19:07 +00:00
|
|
|
auto const __old = __a_.fetch_sub(__update, memory_order_release);
|
2023-07-05 21:33:18 +00:00
|
|
|
_LIBCPP_ASSERT_UNCATEGORIZED(
|
|
|
|
__update <= __old,
|
|
|
|
"latch::count_down called with a value greater "
|
|
|
|
"than the internal counter");
|
|
|
|
if (__old == __update)
|
2022-09-02 14:19:07 +00:00
|
|
|
__a_.notify_all();
|
2020-02-18 14:58:34 +00:00
|
|
|
}
|
2022-09-02 14:19:07 +00:00
|
|
|
inline _LIBCPP_HIDE_FROM_ABI bool try_wait() const noexcept { return 0 == __a_.load(memory_order_acquire); }
|
2020-02-24 15:09:29 +00:00
|
|
|
inline _LIBCPP_AVAILABILITY_SYNC _LIBCPP_HIDE_FROM_ABI void wait() const {
|
2023-07-04 18:42:44 +00:00
|
|
|
__cxx_atomic_wait(&__a_.__a_, [this]() -> bool { return try_wait(); });
|
2020-02-18 14:58:34 +00:00
|
|
|
}
|
2020-02-24 15:09:29 +00:00
|
|
|
inline _LIBCPP_AVAILABILITY_SYNC _LIBCPP_HIDE_FROM_ABI void arrive_and_wait(ptrdiff_t __update = 1) {
|
2023-07-05 21:33:18 +00:00
|
|
|
_LIBCPP_ASSERT_UNCATEGORIZED(__update >= 0, "latch::arrive_and_wait called with a negative value");
|
|
|
|
// other preconditions on __update are checked in count_down()
|
2023-12-18 19:01:33 +00:00
|
|
|
|
2020-02-18 14:58:34 +00:00
|
|
|
count_down(__update);
|
|
|
|
wait();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
_LIBCPP_END_NAMESPACE_STD
|
|
|
|
|
2020-02-24 23:12:44 +00:00
|
|
|
#endif // _LIBCPP_STD_VER >= 14
|
|
|
|
|
2020-12-02 23:55:01 +00:00
|
|
|
_LIBCPP_POP_MACROS
|
|
|
|
|
2023-01-31 18:23:30 +00:00
|
|
|
#if !defined(_LIBCPP_REMOVE_TRANSITIVE_INCLUDES) && _LIBCPP_STD_VER <= 20
|
|
|
|
# include <atomic>
|
|
|
|
#endif
|
|
|
|
|
2020-02-18 14:58:34 +00:00
|
|
|
#endif //_LIBCPP_LATCH
|