2023-01-14 01:53:52 +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___ATOMIC_ATOMIC_FLAG_H
|
|
|
|
#define _LIBCPP___ATOMIC_ATOMIC_FLAG_H
|
|
|
|
|
|
|
|
#include <__atomic/atomic_sync.h>
|
|
|
|
#include <__atomic/contention_t.h>
|
|
|
|
#include <__atomic/cxx_atomic_impl.h>
|
|
|
|
#include <__atomic/memory_order.h>
|
|
|
|
#include <__chrono/duration.h>
|
|
|
|
#include <__config>
|
|
|
|
#include <__threading_support>
|
|
|
|
#include <cstdint>
|
|
|
|
|
|
|
|
#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
|
|
|
|
# pragma GCC system_header
|
|
|
|
#endif
|
|
|
|
|
|
|
|
_LIBCPP_BEGIN_NAMESPACE_STD
|
|
|
|
|
2023-02-17 12:07:52 +00:00
|
|
|
struct atomic_flag
|
2023-01-14 01:53:52 +00:00
|
|
|
{
|
|
|
|
__cxx_atomic_impl<_LIBCPP_ATOMIC_FLAG_TYPE> __a_;
|
|
|
|
|
2023-02-17 12:00:21 +00:00
|
|
|
_LIBCPP_HIDE_FROM_ABI
|
2023-01-14 01:53:52 +00:00
|
|
|
bool test(memory_order __m = memory_order_seq_cst) const volatile _NOEXCEPT
|
|
|
|
{return _LIBCPP_ATOMIC_FLAG_TYPE(true) == __cxx_atomic_load(&__a_, __m);}
|
2023-02-17 12:00:21 +00:00
|
|
|
_LIBCPP_HIDE_FROM_ABI
|
2023-01-14 01:53:52 +00:00
|
|
|
bool test(memory_order __m = memory_order_seq_cst) const _NOEXCEPT
|
|
|
|
{return _LIBCPP_ATOMIC_FLAG_TYPE(true) == __cxx_atomic_load(&__a_, __m);}
|
|
|
|
|
2023-02-17 12:00:21 +00:00
|
|
|
_LIBCPP_HIDE_FROM_ABI
|
2023-01-14 01:53:52 +00:00
|
|
|
bool test_and_set(memory_order __m = memory_order_seq_cst) volatile _NOEXCEPT
|
|
|
|
{return __cxx_atomic_exchange(&__a_, _LIBCPP_ATOMIC_FLAG_TYPE(true), __m);}
|
2023-02-17 12:00:21 +00:00
|
|
|
_LIBCPP_HIDE_FROM_ABI
|
2023-01-14 01:53:52 +00:00
|
|
|
bool test_and_set(memory_order __m = memory_order_seq_cst) _NOEXCEPT
|
|
|
|
{return __cxx_atomic_exchange(&__a_, _LIBCPP_ATOMIC_FLAG_TYPE(true), __m);}
|
2023-02-17 12:00:21 +00:00
|
|
|
_LIBCPP_HIDE_FROM_ABI
|
2023-01-14 01:53:52 +00:00
|
|
|
void clear(memory_order __m = memory_order_seq_cst) volatile _NOEXCEPT
|
|
|
|
{__cxx_atomic_store(&__a_, _LIBCPP_ATOMIC_FLAG_TYPE(false), __m);}
|
2023-02-17 12:00:21 +00:00
|
|
|
_LIBCPP_HIDE_FROM_ABI
|
2023-01-14 01:53:52 +00:00
|
|
|
void clear(memory_order __m = memory_order_seq_cst) _NOEXCEPT
|
|
|
|
{__cxx_atomic_store(&__a_, _LIBCPP_ATOMIC_FLAG_TYPE(false), __m);}
|
|
|
|
|
2023-02-17 12:00:21 +00:00
|
|
|
_LIBCPP_AVAILABILITY_SYNC _LIBCPP_HIDE_FROM_ABI
|
2023-01-14 01:53:52 +00:00
|
|
|
void wait(bool __v, memory_order __m = memory_order_seq_cst) const volatile _NOEXCEPT
|
|
|
|
{__cxx_atomic_wait(&__a_, _LIBCPP_ATOMIC_FLAG_TYPE(__v), __m);}
|
2023-02-17 12:00:21 +00:00
|
|
|
_LIBCPP_AVAILABILITY_SYNC _LIBCPP_HIDE_FROM_ABI
|
2023-01-14 01:53:52 +00:00
|
|
|
void wait(bool __v, memory_order __m = memory_order_seq_cst) const _NOEXCEPT
|
|
|
|
{__cxx_atomic_wait(&__a_, _LIBCPP_ATOMIC_FLAG_TYPE(__v), __m);}
|
2023-02-17 12:00:21 +00:00
|
|
|
_LIBCPP_AVAILABILITY_SYNC _LIBCPP_HIDE_FROM_ABI
|
2023-01-14 01:53:52 +00:00
|
|
|
void notify_one() volatile _NOEXCEPT
|
|
|
|
{__cxx_atomic_notify_one(&__a_);}
|
2023-02-17 12:00:21 +00:00
|
|
|
_LIBCPP_AVAILABILITY_SYNC _LIBCPP_HIDE_FROM_ABI
|
2023-01-14 01:53:52 +00:00
|
|
|
void notify_one() _NOEXCEPT
|
|
|
|
{__cxx_atomic_notify_one(&__a_);}
|
2023-02-17 12:00:21 +00:00
|
|
|
_LIBCPP_AVAILABILITY_SYNC _LIBCPP_HIDE_FROM_ABI
|
2023-01-14 01:53:52 +00:00
|
|
|
void notify_all() volatile _NOEXCEPT
|
|
|
|
{__cxx_atomic_notify_all(&__a_);}
|
2023-02-17 12:00:21 +00:00
|
|
|
_LIBCPP_AVAILABILITY_SYNC _LIBCPP_HIDE_FROM_ABI
|
2023-01-14 01:53:52 +00:00
|
|
|
void notify_all() _NOEXCEPT
|
|
|
|
{__cxx_atomic_notify_all(&__a_);}
|
|
|
|
|
|
|
|
#if _LIBCPP_STD_VER >= 20
|
2023-02-17 12:00:21 +00:00
|
|
|
_LIBCPP_HIDE_FROM_ABI constexpr
|
2023-01-14 01:53:52 +00:00
|
|
|
atomic_flag() _NOEXCEPT : __a_(false) {}
|
|
|
|
#else
|
|
|
|
atomic_flag() _NOEXCEPT = default;
|
|
|
|
#endif
|
|
|
|
|
2023-02-17 12:00:21 +00:00
|
|
|
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR
|
2023-01-14 01:53:52 +00:00
|
|
|
atomic_flag(bool __b) _NOEXCEPT : __a_(__b) {} // EXTENSION
|
|
|
|
|
|
|
|
atomic_flag(const atomic_flag&) = delete;
|
|
|
|
atomic_flag& operator=(const atomic_flag&) = delete;
|
|
|
|
atomic_flag& operator=(const atomic_flag&) volatile = delete;
|
|
|
|
|
2023-02-17 12:07:52 +00:00
|
|
|
};
|
2023-01-14 01:53:52 +00:00
|
|
|
|
2023-02-17 12:00:21 +00:00
|
|
|
inline _LIBCPP_HIDE_FROM_ABI
|
2023-01-14 01:53:52 +00:00
|
|
|
bool
|
|
|
|
atomic_flag_test(const volatile atomic_flag* __o) _NOEXCEPT
|
|
|
|
{
|
|
|
|
return __o->test();
|
|
|
|
}
|
|
|
|
|
2023-02-17 12:00:21 +00:00
|
|
|
inline _LIBCPP_HIDE_FROM_ABI
|
2023-01-14 01:53:52 +00:00
|
|
|
bool
|
|
|
|
atomic_flag_test(const atomic_flag* __o) _NOEXCEPT
|
|
|
|
{
|
|
|
|
return __o->test();
|
|
|
|
}
|
|
|
|
|
2023-02-17 12:00:21 +00:00
|
|
|
inline _LIBCPP_HIDE_FROM_ABI
|
2023-01-14 01:53:52 +00:00
|
|
|
bool
|
|
|
|
atomic_flag_test_explicit(const volatile atomic_flag* __o, memory_order __m) _NOEXCEPT
|
|
|
|
{
|
|
|
|
return __o->test(__m);
|
|
|
|
}
|
|
|
|
|
2023-02-17 12:00:21 +00:00
|
|
|
inline _LIBCPP_HIDE_FROM_ABI
|
2023-01-14 01:53:52 +00:00
|
|
|
bool
|
|
|
|
atomic_flag_test_explicit(const atomic_flag* __o, memory_order __m) _NOEXCEPT
|
|
|
|
{
|
|
|
|
return __o->test(__m);
|
|
|
|
}
|
|
|
|
|
2023-02-17 12:00:21 +00:00
|
|
|
inline _LIBCPP_HIDE_FROM_ABI
|
2023-01-14 01:53:52 +00:00
|
|
|
bool
|
|
|
|
atomic_flag_test_and_set(volatile atomic_flag* __o) _NOEXCEPT
|
|
|
|
{
|
|
|
|
return __o->test_and_set();
|
|
|
|
}
|
|
|
|
|
2023-02-17 12:00:21 +00:00
|
|
|
inline _LIBCPP_HIDE_FROM_ABI
|
2023-01-14 01:53:52 +00:00
|
|
|
bool
|
|
|
|
atomic_flag_test_and_set(atomic_flag* __o) _NOEXCEPT
|
|
|
|
{
|
|
|
|
return __o->test_and_set();
|
|
|
|
}
|
|
|
|
|
2023-02-17 12:00:21 +00:00
|
|
|
inline _LIBCPP_HIDE_FROM_ABI
|
2023-01-14 01:53:52 +00:00
|
|
|
bool
|
|
|
|
atomic_flag_test_and_set_explicit(volatile atomic_flag* __o, memory_order __m) _NOEXCEPT
|
|
|
|
{
|
|
|
|
return __o->test_and_set(__m);
|
|
|
|
}
|
|
|
|
|
2023-02-17 12:00:21 +00:00
|
|
|
inline _LIBCPP_HIDE_FROM_ABI
|
2023-01-14 01:53:52 +00:00
|
|
|
bool
|
|
|
|
atomic_flag_test_and_set_explicit(atomic_flag* __o, memory_order __m) _NOEXCEPT
|
|
|
|
{
|
|
|
|
return __o->test_and_set(__m);
|
|
|
|
}
|
|
|
|
|
2023-02-17 12:00:21 +00:00
|
|
|
inline _LIBCPP_HIDE_FROM_ABI
|
2023-01-14 01:53:52 +00:00
|
|
|
void
|
|
|
|
atomic_flag_clear(volatile atomic_flag* __o) _NOEXCEPT
|
|
|
|
{
|
|
|
|
__o->clear();
|
|
|
|
}
|
|
|
|
|
2023-02-17 12:00:21 +00:00
|
|
|
inline _LIBCPP_HIDE_FROM_ABI
|
2023-01-14 01:53:52 +00:00
|
|
|
void
|
|
|
|
atomic_flag_clear(atomic_flag* __o) _NOEXCEPT
|
|
|
|
{
|
|
|
|
__o->clear();
|
|
|
|
}
|
|
|
|
|
2023-02-17 12:00:21 +00:00
|
|
|
inline _LIBCPP_HIDE_FROM_ABI
|
2023-01-14 01:53:52 +00:00
|
|
|
void
|
|
|
|
atomic_flag_clear_explicit(volatile atomic_flag* __o, memory_order __m) _NOEXCEPT
|
|
|
|
{
|
|
|
|
__o->clear(__m);
|
|
|
|
}
|
|
|
|
|
2023-02-17 12:00:21 +00:00
|
|
|
inline _LIBCPP_HIDE_FROM_ABI
|
2023-01-14 01:53:52 +00:00
|
|
|
void
|
|
|
|
atomic_flag_clear_explicit(atomic_flag* __o, memory_order __m) _NOEXCEPT
|
|
|
|
{
|
|
|
|
__o->clear(__m);
|
|
|
|
}
|
|
|
|
|
2023-02-17 12:00:21 +00:00
|
|
|
inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_AVAILABILITY_SYNC
|
2023-01-14 01:53:52 +00:00
|
|
|
void
|
|
|
|
atomic_flag_wait(const volatile atomic_flag* __o, bool __v) _NOEXCEPT
|
|
|
|
{
|
|
|
|
__o->wait(__v);
|
|
|
|
}
|
|
|
|
|
2023-02-17 12:00:21 +00:00
|
|
|
inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_AVAILABILITY_SYNC
|
2023-01-14 01:53:52 +00:00
|
|
|
void
|
|
|
|
atomic_flag_wait(const atomic_flag* __o, bool __v) _NOEXCEPT
|
|
|
|
{
|
|
|
|
__o->wait(__v);
|
|
|
|
}
|
|
|
|
|
2023-02-17 12:00:21 +00:00
|
|
|
inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_AVAILABILITY_SYNC
|
2023-01-14 01:53:52 +00:00
|
|
|
void
|
|
|
|
atomic_flag_wait_explicit(const volatile atomic_flag* __o,
|
|
|
|
bool __v, memory_order __m) _NOEXCEPT
|
|
|
|
{
|
|
|
|
__o->wait(__v, __m);
|
|
|
|
}
|
|
|
|
|
2023-02-17 12:00:21 +00:00
|
|
|
inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_AVAILABILITY_SYNC
|
2023-01-14 01:53:52 +00:00
|
|
|
void
|
|
|
|
atomic_flag_wait_explicit(const atomic_flag* __o,
|
|
|
|
bool __v, memory_order __m) _NOEXCEPT
|
|
|
|
{
|
|
|
|
__o->wait(__v, __m);
|
|
|
|
}
|
|
|
|
|
2023-02-17 12:00:21 +00:00
|
|
|
inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_AVAILABILITY_SYNC
|
2023-01-14 01:53:52 +00:00
|
|
|
void
|
|
|
|
atomic_flag_notify_one(volatile atomic_flag* __o) _NOEXCEPT
|
|
|
|
{
|
|
|
|
__o->notify_one();
|
|
|
|
}
|
|
|
|
|
2023-02-17 12:00:21 +00:00
|
|
|
inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_AVAILABILITY_SYNC
|
2023-01-14 01:53:52 +00:00
|
|
|
void
|
|
|
|
atomic_flag_notify_one(atomic_flag* __o) _NOEXCEPT
|
|
|
|
{
|
|
|
|
__o->notify_one();
|
|
|
|
}
|
|
|
|
|
2023-02-17 12:00:21 +00:00
|
|
|
inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_AVAILABILITY_SYNC
|
2023-01-14 01:53:52 +00:00
|
|
|
void
|
|
|
|
atomic_flag_notify_all(volatile atomic_flag* __o) _NOEXCEPT
|
|
|
|
{
|
|
|
|
__o->notify_all();
|
|
|
|
}
|
|
|
|
|
2023-02-17 12:00:21 +00:00
|
|
|
inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_AVAILABILITY_SYNC
|
2023-01-14 01:53:52 +00:00
|
|
|
void
|
|
|
|
atomic_flag_notify_all(atomic_flag* __o) _NOEXCEPT
|
|
|
|
{
|
|
|
|
__o->notify_all();
|
|
|
|
}
|
|
|
|
|
|
|
|
_LIBCPP_END_NAMESPACE_STD
|
|
|
|
|
|
|
|
#endif // _LIBCPP___ATOMIC_ATOMIC_FLAG_H
|