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 43*7a6dacacSDimitry Andric#include <__config> 44*7a6dacacSDimitry Andric 45*7a6dacacSDimitry Andric#ifdef _LIBCPP_HAS_NO_THREADS 46*7a6dacacSDimitry Andric# error "<latch> is not supported since libc++ has been configured without support for threads." 47*7a6dacacSDimitry Andric#endif 48*7a6dacacSDimitry Andric 4981ad6265SDimitry Andric#include <__assert> // all public C++ headers provide the assertion handler 5006c3fb27SDimitry Andric#include <__atomic/atomic_base.h> 5106c3fb27SDimitry Andric#include <__atomic/atomic_sync.h> 5206c3fb27SDimitry Andric#include <__atomic/memory_order.h> 53e8d8bef9SDimitry Andric#include <__availability> 5406c3fb27SDimitry Andric#include <cstddef> 5581ad6265SDimitry Andric#include <limits> 5604eeddc0SDimitry Andric#include <version> 575ffd83dbSDimitry Andric 585ffd83dbSDimitry Andric#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) 595ffd83dbSDimitry Andric# pragma GCC system_header 605ffd83dbSDimitry Andric#endif 615ffd83dbSDimitry Andric 62e8d8bef9SDimitry Andric_LIBCPP_PUSH_MACROS 63e8d8bef9SDimitry Andric#include <__undef_macros> 64e8d8bef9SDimitry Andric 655ffd83dbSDimitry Andric#if _LIBCPP_STD_VER >= 14 665ffd83dbSDimitry Andric 675ffd83dbSDimitry Andric_LIBCPP_BEGIN_NAMESPACE_STD 685ffd83dbSDimitry Andric 69cb14a3feSDimitry Andricclass latch { 70bdd1243dSDimitry Andric __atomic_base<ptrdiff_t> __a_; 715ffd83dbSDimitry Andric 725ffd83dbSDimitry Andricpublic: 73cb14a3feSDimitry Andric static _LIBCPP_HIDE_FROM_ABI constexpr ptrdiff_t max() noexcept { return numeric_limits<ptrdiff_t>::max(); } 745ffd83dbSDimitry Andric 75cb14a3feSDimitry Andric inline _LIBCPP_HIDE_FROM_ABI constexpr explicit latch(ptrdiff_t __expected) : __a_(__expected) { 76*7a6dacacSDimitry Andric _LIBCPP_ASSERT_ARGUMENT_WITHIN_DOMAIN( 77cb14a3feSDimitry Andric __expected >= 0, 7806c3fb27SDimitry Andric "latch::latch(ptrdiff_t): latch cannot be " 7906c3fb27SDimitry Andric "initialized with a negative value"); 80*7a6dacacSDimitry Andric _LIBCPP_ASSERT_ARGUMENT_WITHIN_DOMAIN( 81cb14a3feSDimitry Andric __expected <= max(), 8206c3fb27SDimitry Andric "latch::latch(ptrdiff_t): latch cannot be " 8306c3fb27SDimitry Andric "initialized with a value greater than max()"); 8406c3fb27SDimitry Andric } 855ffd83dbSDimitry Andric 8606c3fb27SDimitry Andric _LIBCPP_HIDE_FROM_ABI ~latch() = default; 875ffd83dbSDimitry Andric latch(const latch&) = delete; 885ffd83dbSDimitry Andric latch& operator=(const latch&) = delete; 895ffd83dbSDimitry Andric 90cb14a3feSDimitry Andric inline _LIBCPP_AVAILABILITY_SYNC _LIBCPP_HIDE_FROM_ABI void count_down(ptrdiff_t __update = 1) { 91*7a6dacacSDimitry Andric _LIBCPP_ASSERT_ARGUMENT_WITHIN_DOMAIN(__update >= 0, "latch::count_down called with a negative value"); 92bdd1243dSDimitry Andric auto const __old = __a_.fetch_sub(__update, memory_order_release); 93*7a6dacacSDimitry Andric _LIBCPP_ASSERT_ARGUMENT_WITHIN_DOMAIN( 94cb14a3feSDimitry Andric __update <= __old, 95cb14a3feSDimitry Andric "latch::count_down called with a value greater " 9606c3fb27SDimitry Andric "than the internal counter"); 975ffd83dbSDimitry Andric if (__old == __update) 98bdd1243dSDimitry Andric __a_.notify_all(); 995ffd83dbSDimitry Andric } 100cb14a3feSDimitry Andric inline _LIBCPP_HIDE_FROM_ABI bool try_wait() const noexcept { return 0 == __a_.load(memory_order_acquire); } 101cb14a3feSDimitry Andric inline _LIBCPP_AVAILABILITY_SYNC _LIBCPP_HIDE_FROM_ABI void wait() const { 102cb14a3feSDimitry Andric __cxx_atomic_wait(&__a_.__a_, [this]() -> bool { return try_wait(); }); 1035ffd83dbSDimitry Andric } 104cb14a3feSDimitry Andric inline _LIBCPP_AVAILABILITY_SYNC _LIBCPP_HIDE_FROM_ABI void arrive_and_wait(ptrdiff_t __update = 1) { 105*7a6dacacSDimitry Andric _LIBCPP_ASSERT_ARGUMENT_WITHIN_DOMAIN(__update >= 0, "latch::arrive_and_wait called with a negative value"); 10606c3fb27SDimitry Andric // other preconditions on __update are checked in count_down() 10706c3fb27SDimitry Andric 1085ffd83dbSDimitry Andric count_down(__update); 1095ffd83dbSDimitry Andric wait(); 1105ffd83dbSDimitry Andric } 1115ffd83dbSDimitry Andric}; 1125ffd83dbSDimitry Andric 1135ffd83dbSDimitry Andric_LIBCPP_END_NAMESPACE_STD 1145ffd83dbSDimitry Andric 1155ffd83dbSDimitry Andric#endif // _LIBCPP_STD_VER >= 14 1165ffd83dbSDimitry Andric 117e8d8bef9SDimitry Andric_LIBCPP_POP_MACROS 118e8d8bef9SDimitry Andric 11906c3fb27SDimitry Andric#if !defined(_LIBCPP_REMOVE_TRANSITIVE_INCLUDES) && _LIBCPP_STD_VER <= 20 12006c3fb27SDimitry Andric# include <atomic> 12106c3fb27SDimitry Andric#endif 12206c3fb27SDimitry Andric 1235ffd83dbSDimitry Andric#endif //_LIBCPP_LATCH 124