File indexing completed on 2026-05-03 08:13:23
0001
0002
0003
0004
0005
0006
0007
0008
0009 #ifndef _LIBCPP___CXX03___ATOMIC_ATOMIC_FLAG_H
0010 #define _LIBCPP___CXX03___ATOMIC_ATOMIC_FLAG_H
0011
0012 #include <__cxx03/__atomic/atomic_sync.h>
0013 #include <__cxx03/__atomic/contention_t.h>
0014 #include <__cxx03/__atomic/cxx_atomic_impl.h>
0015 #include <__cxx03/__atomic/memory_order.h>
0016 #include <__cxx03/__chrono/duration.h>
0017 #include <__cxx03/__config>
0018 #include <__cxx03/__memory/addressof.h>
0019 #include <__cxx03/__thread/support.h>
0020 #include <__cxx03/cstdint>
0021
0022 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
0023 # pragma GCC system_header
0024 #endif
0025
0026 _LIBCPP_BEGIN_NAMESPACE_STD
0027
0028 struct atomic_flag {
0029 __cxx_atomic_impl<_LIBCPP_ATOMIC_FLAG_TYPE> __a_;
0030
0031 _LIBCPP_HIDE_FROM_ABI bool test(memory_order __m = memory_order_seq_cst) const volatile _NOEXCEPT {
0032 return _LIBCPP_ATOMIC_FLAG_TYPE(true) == __cxx_atomic_load(&__a_, __m);
0033 }
0034 _LIBCPP_HIDE_FROM_ABI bool test(memory_order __m = memory_order_seq_cst) const _NOEXCEPT {
0035 return _LIBCPP_ATOMIC_FLAG_TYPE(true) == __cxx_atomic_load(&__a_, __m);
0036 }
0037
0038 _LIBCPP_HIDE_FROM_ABI bool test_and_set(memory_order __m = memory_order_seq_cst) volatile _NOEXCEPT {
0039 return __cxx_atomic_exchange(&__a_, _LIBCPP_ATOMIC_FLAG_TYPE(true), __m);
0040 }
0041 _LIBCPP_HIDE_FROM_ABI bool test_and_set(memory_order __m = memory_order_seq_cst) _NOEXCEPT {
0042 return __cxx_atomic_exchange(&__a_, _LIBCPP_ATOMIC_FLAG_TYPE(true), __m);
0043 }
0044 _LIBCPP_HIDE_FROM_ABI void clear(memory_order __m = memory_order_seq_cst) volatile _NOEXCEPT {
0045 __cxx_atomic_store(&__a_, _LIBCPP_ATOMIC_FLAG_TYPE(false), __m);
0046 }
0047 _LIBCPP_HIDE_FROM_ABI void clear(memory_order __m = memory_order_seq_cst) _NOEXCEPT {
0048 __cxx_atomic_store(&__a_, _LIBCPP_ATOMIC_FLAG_TYPE(false), __m);
0049 }
0050
0051 _LIBCPP_DEPRECATED_ATOMIC_SYNC _LIBCPP_AVAILABILITY_SYNC _LIBCPP_HIDE_FROM_ABI void
0052 wait(bool __v, memory_order __m = memory_order_seq_cst) const volatile _NOEXCEPT {
0053 std::__atomic_wait(*this, _LIBCPP_ATOMIC_FLAG_TYPE(__v), __m);
0054 }
0055 _LIBCPP_DEPRECATED_ATOMIC_SYNC _LIBCPP_AVAILABILITY_SYNC _LIBCPP_HIDE_FROM_ABI void
0056 wait(bool __v, memory_order __m = memory_order_seq_cst) const _NOEXCEPT {
0057 std::__atomic_wait(*this, _LIBCPP_ATOMIC_FLAG_TYPE(__v), __m);
0058 }
0059 _LIBCPP_DEPRECATED_ATOMIC_SYNC _LIBCPP_AVAILABILITY_SYNC _LIBCPP_HIDE_FROM_ABI void notify_one() volatile _NOEXCEPT {
0060 std::__atomic_notify_one(*this);
0061 }
0062 _LIBCPP_DEPRECATED_ATOMIC_SYNC _LIBCPP_AVAILABILITY_SYNC _LIBCPP_HIDE_FROM_ABI void notify_one() _NOEXCEPT {
0063 std::__atomic_notify_one(*this);
0064 }
0065 _LIBCPP_AVAILABILITY_SYNC _LIBCPP_HIDE_FROM_ABI void notify_all() volatile _NOEXCEPT {
0066 std::__atomic_notify_all(*this);
0067 }
0068 _LIBCPP_DEPRECATED_ATOMIC_SYNC _LIBCPP_AVAILABILITY_SYNC _LIBCPP_HIDE_FROM_ABI void notify_all() _NOEXCEPT {
0069 std::__atomic_notify_all(*this);
0070 }
0071
0072 #if _LIBCPP_STD_VER >= 20
0073 _LIBCPP_HIDE_FROM_ABI constexpr atomic_flag() _NOEXCEPT : __a_(false) {}
0074 #else
0075 atomic_flag() _NOEXCEPT = default;
0076 #endif
0077
0078 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR atomic_flag(bool __b) _NOEXCEPT : __a_(__b) {}
0079
0080 atomic_flag(const atomic_flag&) = delete;
0081 atomic_flag& operator=(const atomic_flag&) = delete;
0082 atomic_flag& operator=(const atomic_flag&) volatile = delete;
0083 };
0084
0085 template <>
0086 struct __atomic_waitable_traits<atomic_flag> {
0087 static _LIBCPP_HIDE_FROM_ABI _LIBCPP_ATOMIC_FLAG_TYPE __atomic_load(const atomic_flag& __a, memory_order __order) {
0088 return std::__cxx_atomic_load(&__a.__a_, __order);
0089 }
0090
0091 static _LIBCPP_HIDE_FROM_ABI _LIBCPP_ATOMIC_FLAG_TYPE
0092 __atomic_load(const volatile atomic_flag& __a, memory_order __order) {
0093 return std::__cxx_atomic_load(&__a.__a_, __order);
0094 }
0095
0096 static _LIBCPP_HIDE_FROM_ABI const __cxx_atomic_impl<_LIBCPP_ATOMIC_FLAG_TYPE>*
0097 __atomic_contention_address(const atomic_flag& __a) {
0098 return std::addressof(__a.__a_);
0099 }
0100
0101 static _LIBCPP_HIDE_FROM_ABI const volatile __cxx_atomic_impl<_LIBCPP_ATOMIC_FLAG_TYPE>*
0102 __atomic_contention_address(const volatile atomic_flag& __a) {
0103 return std::addressof(__a.__a_);
0104 }
0105 };
0106
0107 inline _LIBCPP_HIDE_FROM_ABI bool atomic_flag_test(const volatile atomic_flag* __o) _NOEXCEPT { return __o->test(); }
0108
0109 inline _LIBCPP_HIDE_FROM_ABI bool atomic_flag_test(const atomic_flag* __o) _NOEXCEPT { return __o->test(); }
0110
0111 inline _LIBCPP_HIDE_FROM_ABI bool
0112 atomic_flag_test_explicit(const volatile atomic_flag* __o, memory_order __m) _NOEXCEPT {
0113 return __o->test(__m);
0114 }
0115
0116 inline _LIBCPP_HIDE_FROM_ABI bool atomic_flag_test_explicit(const atomic_flag* __o, memory_order __m) _NOEXCEPT {
0117 return __o->test(__m);
0118 }
0119
0120 inline _LIBCPP_HIDE_FROM_ABI bool atomic_flag_test_and_set(volatile atomic_flag* __o) _NOEXCEPT {
0121 return __o->test_and_set();
0122 }
0123
0124 inline _LIBCPP_HIDE_FROM_ABI bool atomic_flag_test_and_set(atomic_flag* __o) _NOEXCEPT { return __o->test_and_set(); }
0125
0126 inline _LIBCPP_HIDE_FROM_ABI bool
0127 atomic_flag_test_and_set_explicit(volatile atomic_flag* __o, memory_order __m) _NOEXCEPT {
0128 return __o->test_and_set(__m);
0129 }
0130
0131 inline _LIBCPP_HIDE_FROM_ABI bool atomic_flag_test_and_set_explicit(atomic_flag* __o, memory_order __m) _NOEXCEPT {
0132 return __o->test_and_set(__m);
0133 }
0134
0135 inline _LIBCPP_HIDE_FROM_ABI void atomic_flag_clear(volatile atomic_flag* __o) _NOEXCEPT { __o->clear(); }
0136
0137 inline _LIBCPP_HIDE_FROM_ABI void atomic_flag_clear(atomic_flag* __o) _NOEXCEPT { __o->clear(); }
0138
0139 inline _LIBCPP_HIDE_FROM_ABI void atomic_flag_clear_explicit(volatile atomic_flag* __o, memory_order __m) _NOEXCEPT {
0140 __o->clear(__m);
0141 }
0142
0143 inline _LIBCPP_HIDE_FROM_ABI void atomic_flag_clear_explicit(atomic_flag* __o, memory_order __m) _NOEXCEPT {
0144 __o->clear(__m);
0145 }
0146
0147 inline _LIBCPP_DEPRECATED_ATOMIC_SYNC _LIBCPP_HIDE_FROM_ABI _LIBCPP_AVAILABILITY_SYNC void
0148 atomic_flag_wait(const volatile atomic_flag* __o, bool __v) _NOEXCEPT {
0149 __o->wait(__v);
0150 }
0151
0152 inline _LIBCPP_DEPRECATED_ATOMIC_SYNC _LIBCPP_HIDE_FROM_ABI _LIBCPP_AVAILABILITY_SYNC void
0153 atomic_flag_wait(const atomic_flag* __o, bool __v) _NOEXCEPT {
0154 __o->wait(__v);
0155 }
0156
0157 inline _LIBCPP_DEPRECATED_ATOMIC_SYNC _LIBCPP_HIDE_FROM_ABI _LIBCPP_AVAILABILITY_SYNC void
0158 atomic_flag_wait_explicit(const volatile atomic_flag* __o, bool __v, memory_order __m) _NOEXCEPT {
0159 __o->wait(__v, __m);
0160 }
0161
0162 inline _LIBCPP_DEPRECATED_ATOMIC_SYNC _LIBCPP_HIDE_FROM_ABI _LIBCPP_AVAILABILITY_SYNC void
0163 atomic_flag_wait_explicit(const atomic_flag* __o, bool __v, memory_order __m) _NOEXCEPT {
0164 __o->wait(__v, __m);
0165 }
0166
0167 inline _LIBCPP_DEPRECATED_ATOMIC_SYNC _LIBCPP_HIDE_FROM_ABI _LIBCPP_AVAILABILITY_SYNC void
0168 atomic_flag_notify_one(volatile atomic_flag* __o) _NOEXCEPT {
0169 __o->notify_one();
0170 }
0171
0172 inline _LIBCPP_DEPRECATED_ATOMIC_SYNC _LIBCPP_HIDE_FROM_ABI _LIBCPP_AVAILABILITY_SYNC void
0173 atomic_flag_notify_one(atomic_flag* __o) _NOEXCEPT {
0174 __o->notify_one();
0175 }
0176
0177 inline _LIBCPP_DEPRECATED_ATOMIC_SYNC _LIBCPP_HIDE_FROM_ABI _LIBCPP_AVAILABILITY_SYNC void
0178 atomic_flag_notify_all(volatile atomic_flag* __o) _NOEXCEPT {
0179 __o->notify_all();
0180 }
0181
0182 inline _LIBCPP_DEPRECATED_ATOMIC_SYNC _LIBCPP_HIDE_FROM_ABI _LIBCPP_AVAILABILITY_SYNC void
0183 atomic_flag_notify_all(atomic_flag* __o) _NOEXCEPT {
0184 __o->notify_all();
0185 }
0186
0187 _LIBCPP_END_NAMESPACE_STD
0188
0189 #endif