latch 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. // -*- C++ -*-
  2. //===----------------------------------------------------------------------===//
  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. #ifndef _LIBCPP_LATCH
  10. #define _LIBCPP_LATCH
  11. /*
  12. latch synopsis
  13. namespace std
  14. {
  15. class latch
  16. {
  17. public:
  18. static constexpr ptrdiff_t max() noexcept;
  19. constexpr explicit latch(ptrdiff_t __expected);
  20. ~latch();
  21. latch(const latch&) = delete;
  22. latch& operator=(const latch&) = delete;
  23. void count_down(ptrdiff_t __update = 1);
  24. bool try_wait() const noexcept;
  25. void wait() const;
  26. void arrive_and_wait(ptrdiff_t __update = 1);
  27. private:
  28. ptrdiff_t __counter; // exposition only
  29. };
  30. }
  31. */
  32. #include <__availability>
  33. #include <__config>
  34. #include <atomic>
  35. #include <version>
  36. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  37. # pragma GCC system_header
  38. #endif
  39. #ifdef _LIBCPP_HAS_NO_THREADS
  40. # error <latch> is not supported on this single threaded system
  41. #endif
  42. _LIBCPP_PUSH_MACROS
  43. #include <__undef_macros>
  44. #if _LIBCPP_STD_VER >= 14
  45. _LIBCPP_BEGIN_NAMESPACE_STD
  46. class latch
  47. {
  48. __atomic_base<ptrdiff_t> __a;
  49. public:
  50. static constexpr ptrdiff_t max() noexcept {
  51. return numeric_limits<ptrdiff_t>::max();
  52. }
  53. inline _LIBCPP_INLINE_VISIBILITY
  54. constexpr explicit latch(ptrdiff_t __expected) : __a(__expected) { }
  55. ~latch() = default;
  56. latch(const latch&) = delete;
  57. latch& operator=(const latch&) = delete;
  58. inline _LIBCPP_AVAILABILITY_SYNC _LIBCPP_INLINE_VISIBILITY
  59. void count_down(ptrdiff_t __update = 1)
  60. {
  61. auto const __old = __a.fetch_sub(__update, memory_order_release);
  62. if(__old == __update)
  63. __a.notify_all();
  64. }
  65. inline _LIBCPP_INLINE_VISIBILITY
  66. bool try_wait() const noexcept
  67. {
  68. return 0 == __a.load(memory_order_acquire);
  69. }
  70. inline _LIBCPP_AVAILABILITY_SYNC _LIBCPP_INLINE_VISIBILITY
  71. void wait() const
  72. {
  73. auto const __test_fn = [=]() -> bool {
  74. return try_wait();
  75. };
  76. __cxx_atomic_wait(&__a.__a_, __test_fn);
  77. }
  78. inline _LIBCPP_AVAILABILITY_SYNC _LIBCPP_INLINE_VISIBILITY
  79. void arrive_and_wait(ptrdiff_t __update = 1)
  80. {
  81. count_down(__update);
  82. wait();
  83. }
  84. };
  85. _LIBCPP_END_NAMESPACE_STD
  86. #endif // _LIBCPP_STD_VER >= 14
  87. _LIBCPP_POP_MACROS
  88. #endif //_LIBCPP_LATCH