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 10 #ifndef _LIBCPP___STOP_TOKEN_ATOMIC_UNIQUE_GUARD_H 11 #define _LIBCPP___STOP_TOKEN_ATOMIC_UNIQUE_GUARD_H 12 13 #include <__bit/popcount.h> 14 #include <__config> 15 #include <atomic> 16 17 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) 18 # pragma GCC system_header 19 #endif 20 21 _LIBCPP_BEGIN_NAMESPACE_STD 22 23 #if _LIBCPP_STD_VER >= 20 24 25 // This class implements an RAII unique_lock without a mutex. 26 // It uses std::atomic<State>, 27 // where State contains a lock bit and might contain other data, 28 // and LockedBit is the value of State when the lock bit is set, e.g 1 << 2 29 template <class _State, _State _LockedBit> 30 class _LIBCPP_AVAILABILITY_SYNC __atomic_unique_lock { 31 static_assert(std::popcount(_LockedBit) == 1, "LockedBit must be an integer where only one bit is set"); 32 33 std::atomic<_State>& __state_; 34 bool __is_locked_; 35 36 public: 37 _LIBCPP_HIDE_FROM_ABI explicit __atomic_unique_lock(std::atomic<_State>& __state) noexcept 38 : __state_(__state), __is_locked_(true) { 39 __lock(); 40 } 41 42 template <class _Pred> 43 _LIBCPP_HIDE_FROM_ABI __atomic_unique_lock(std::atomic<_State>& __state, _Pred&& __give_up_locking) noexcept 44 : __state_(__state), __is_locked_(false) { 45 __is_locked_ = __lock_impl(__give_up_locking, __set_locked_bit, std::memory_order_acquire); 46 } 47 48 template <class _Pred, class _UnaryFunction> 49 _LIBCPP_HIDE_FROM_ABI __atomic_unique_lock( 50 std::atomic<_State>& __state, 51 _Pred&& __give_up_locking, 52 _UnaryFunction&& __state_after_lock, 53 std::memory_order __locked_ordering) noexcept 54 : __state_(__state), __is_locked_(false) { 55 __is_locked_ = __lock_impl(__give_up_locking, __state_after_lock, __locked_ordering); 56 } 57 58 __atomic_unique_lock(const __atomic_unique_lock&) = delete; 59 __atomic_unique_lock(__atomic_unique_lock&&) = delete; 60 __atomic_unique_lock& operator=(const __atomic_unique_lock&) = delete; 61 __atomic_unique_lock& operator=(__atomic_unique_lock&&) = delete; 62 63 _LIBCPP_HIDE_FROM_ABI ~__atomic_unique_lock() { 64 if (__is_locked_) { 65 __unlock(); 66 } 67 } 68 69 _LIBCPP_HIDE_FROM_ABI bool __owns_lock() const noexcept { return __is_locked_; } 70 71 _LIBCPP_HIDE_FROM_ABI void __lock() noexcept { 72 const auto __never_give_up_locking = [](_State) { return false; }; 73 // std::memory_order_acquire because we'd like to make sure that all the read operations after the lock can read the 74 // up-to-date values. 75 __lock_impl(__never_give_up_locking, __set_locked_bit, std::memory_order_acquire); 76 __is_locked_ = true; 77 } 78 79 _LIBCPP_HIDE_FROM_ABI void __unlock() noexcept { 80 // unset the _LockedBit. `memory_order_release` because we need to make sure all the write operations before calling 81 // `__unlock` will be made visible to other threads 82 __state_.fetch_and(static_cast<_State>(~_LockedBit), std::memory_order_release); 83 __state_.notify_all(); 84 __is_locked_ = false; 85 } 86 87 private: 88 template <class _Pred, class _UnaryFunction> 89 _LIBCPP_HIDE_FROM_ABI bool 90 __lock_impl(_Pred&& __give_up_locking, // while trying to lock the state, if the predicate returns true, give up 91 // locking and return 92 _UnaryFunction&& __state_after_lock, 93 std::memory_order __locked_ordering) noexcept { 94 // At this stage, until we exit the inner while loop, other than the atomic state, we are not reading any order 95 // dependent values that is written on other threads, or writing anything that needs to be seen on other threads. 96 // Therefore `memory_order_relaxed` is enough. 97 _State __current_state = __state_.load(std::memory_order_relaxed); 98 do { 99 while (true) { 100 if (__give_up_locking(__current_state)) { 101 // user provided early return condition. fail to lock 102 return false; 103 } else if ((__current_state & _LockedBit) != 0) { 104 // another thread has locked the state, we need to wait 105 __state_.wait(__current_state, std::memory_order_relaxed); 106 // when it is woken up by notifyAll or spuriously, the __state_ 107 // might have changed. reload the state 108 // Note that the new state's _LockedBit may or may not equal to 0 109 __current_state = __state_.load(std::memory_order_relaxed); 110 } else { 111 // at least for now, it is not locked. we can try `compare_exchange_weak` to lock it. 112 // Note that the variable `__current_state`'s lock bit has to be 0 at this point. 113 break; 114 } 115 } 116 } while (!__state_.compare_exchange_weak( 117 __current_state, // if __state_ has the same value of __current_state, lock bit must be zero before exchange and 118 // we are good to lock/exchange and return. If _state has a different value, because other 119 // threads locked it between the `break` statement above and this statement, exchange will fail 120 // and go back to the inner while loop above. 121 __state_after_lock(__current_state), // state after lock. Usually it should be __current_state | _LockedBit. 122 // Some use cases need to set other bits at the same time as an atomic 123 // operation therefore we accept a function 124 __locked_ordering, // sucessful exchange order. Usually it should be std::memory_order_acquire. 125 // Some use cases need more strict ordering therefore we accept it as a parameter 126 std::memory_order_relaxed // fail to exchange order. We don't need any ordering as we are going back to the 127 // inner while loop 128 )); 129 return true; 130 } 131 132 _LIBCPP_HIDE_FROM_ABI static constexpr auto __set_locked_bit = [](_State __state) { return __state | _LockedBit; }; 133 }; 134 135 #endif // _LIBCPP_STD_VER >= 20 136 137 _LIBCPP_END_NAMESPACE_STD 138 139 #endif // _LIBCPP___STOP_TOKEN_ATOMIC_UNIQUE_GUARD_H 140