1#ifndef MSTL_CORE_ASYNC_ATOMIC_HPP__
2#define MSTL_CORE_ASYNC_ATOMIC_HPP__
23#define __MSTL_ATOMIC_CONSTRUCTIONS \
25 ~atomic() noexcept = default; \
26 atomic(const atomic&) = delete; \
27 atomic& operator =(const atomic&) = delete; \
28 atomic& operator =(const atomic&) volatile = delete; \
29 atomic(atomic&&) noexcept = default; \
30 atomic& operator =(atomic&&) noexcept = default;
44 static constexpr int min_align = (
sizeof(T) & (
sizeof(T) - 1)) ||
sizeof(T) > 16 ? 0 :
sizeof(T);
45 static constexpr int align_inner = min_align >
alignof(T) ? min_align : alignof(T);
47 alignas(align_inner) T value_;
49 static_assert(is_trivially_copyable_v<T>,
"atomic requires a trivially copyable type");
50 static_assert(
sizeof(T) > 0,
"Incomplete or zero-sized types are not supported");
51 static_assert(is_copy_constructible_v<T>,
"atomic need copy constructible T");
52 static_assert(is_move_constructible_v<T>,
"atomic need move constructible T");
53 static_assert(is_copy_assignable_v<T>,
"atomic copy move assignable T");
54 static_assert(is_move_assignable_v<T>,
"atomic need move assignable T");
60 __MSTL_ATOMIC_CONSTRUCTIONS
73 operator T() const noexcept {
80 operator T() const volatile noexcept {
89 T operator =(T value)
noexcept {
97 T operator =(T value)
volatile noexcept {
262struct atomic<T*> : atomic_base<T*> {
263 __MSTL_ATOMIC_CONSTRUCTIONS
265 explicit atomic(T* value) noexcept : atomic_base<T*>(value) {}
267 using atomic_base<T*>::operator =;
276 __MSTL_ATOMIC_CONSTRUCTIONS
299 __MSTL_ATOMIC_CONSTRUCTIONS
305 constexpr atomic(
const bool value) noexcept
313 bool operator =(
const bool value)
noexcept {
314 return base_.operator =(value);
320 bool operator =(
const bool value)
volatile noexcept {
321 return base_.operator =(value);
328 operator bool() const noexcept {
335 operator bool() const volatile noexcept {
344 return base_.is_lock_free();
351 return base_.is_lock_free();
360 base_.store(value, mo);
367 base_.store(value, mo);
376 return base_.load(mo);
383 return base_.load(mo);
393 return base_.exchange(value, mo);
400 return base_.exchange(value, mo);
413 return base_.compare_exchange_weak(value1, value2,
success, failure);
421 return base_.compare_exchange_weak(value1, value2,
success, failure);
433 return base_.compare_exchange_weak(value1, value2, mo);
441 return base_.compare_exchange_weak(value1, value2, mo);
454 return base_.compare_exchange_strong(value1, value2,
success, failure);
462 return base_.compare_exchange_strong(value1, value2,
success, failure);
474 return base_.compare_exchange_strong(value1, value2, mo);
482 return base_.compare_exchange_strong(value1, value2, mo);
511struct atomic<char> : atomic_base<char> {
512 using integral_type = char;
513 using base_type = atomic_base<char>;
515 __MSTL_ATOMIC_CONSTRUCTIONS
517 constexpr atomic(
const integral_type value) noexcept : base_type(value) {}
519 using base_type::operator integral_type;
520 using base_type::operator =;
522 static constexpr bool is_always_lock_free =
true;
527struct atomic<signed char> : atomic_base<signed char> {
528 using integral_type =
signed char;
529 using base_type = atomic_base<signed char>;
531 __MSTL_ATOMIC_CONSTRUCTIONS
533 constexpr atomic(
const integral_type value) noexcept : base_type(value) {}
535 using base_type::operator integral_type;
536 using base_type::operator =;
538 static constexpr bool is_always_lock_free =
true;
543struct atomic<unsigned char> : atomic_base<unsigned char> {
544 using integral_type =
unsigned char;
545 using base_type = atomic_base<unsigned char>;
547 __MSTL_ATOMIC_CONSTRUCTIONS
549 constexpr atomic(
const integral_type value) noexcept : base_type(value) {}
551 using base_type::operator integral_type;
552 using base_type::operator =;
554 static constexpr bool is_always_lock_free =
true;
559struct atomic<short> : atomic_base<short> {
560 using integral_type = short;
561 using base_type = atomic_base<short>;
563 __MSTL_ATOMIC_CONSTRUCTIONS
565 constexpr atomic(
const integral_type value) noexcept : base_type(value) {}
567 using base_type::operator integral_type;
568 using base_type::operator =;
570 static constexpr bool is_always_lock_free =
true;
575struct atomic<unsigned short> : atomic_base<unsigned short> {
576 using integral_type =
unsigned short;
577 using base_type = atomic_base<unsigned short>;
579 __MSTL_ATOMIC_CONSTRUCTIONS
581 constexpr atomic(
const integral_type value) noexcept : base_type(value) {}
583 using base_type::operator integral_type;
584 using base_type::operator =;
586 static constexpr bool is_always_lock_free =
true;
591struct atomic<int> : atomic_base<int> {
592 using integral_type = int;
593 using base_type = atomic_base<int>;
595 __MSTL_ATOMIC_CONSTRUCTIONS
597 constexpr atomic(
const integral_type value) noexcept : base_type(value) {}
599 using base_type::operator integral_type;
600 using base_type::operator =;
602 static constexpr bool is_always_lock_free =
true;
607struct atomic<unsigned int> : atomic_base<unsigned int> {
608 using integral_type =
unsigned int;
609 using base_type = atomic_base<unsigned int>;
611 __MSTL_ATOMIC_CONSTRUCTIONS
613 constexpr atomic(
const integral_type value) noexcept : base_type(value) {}
615 using base_type::operator integral_type;
616 using base_type::operator =;
618 static constexpr bool is_always_lock_free =
true;
623struct atomic<long> : atomic_base<long> {
624 using integral_type = long;
625 using base_type = atomic_base<long>;
627 __MSTL_ATOMIC_CONSTRUCTIONS
629 constexpr atomic(
const integral_type value) noexcept : base_type(value) {}
631 using base_type::operator integral_type;
632 using base_type::operator =;
634 static constexpr bool is_always_lock_free =
true;
639struct atomic<unsigned long> : atomic_base<unsigned long> {
640 using integral_type =
unsigned long;
641 using base_type = atomic_base<unsigned long>;
643 __MSTL_ATOMIC_CONSTRUCTIONS
645 constexpr atomic(
const integral_type value) noexcept : base_type(value) {}
647 using base_type::operator integral_type;
648 using base_type::operator =;
650 static constexpr bool is_always_lock_free =
true;
655struct atomic<long long> : atomic_base<long long> {
656 using integral_type =
long long;
657 using base_type = atomic_base<long long>;
659 __MSTL_ATOMIC_CONSTRUCTIONS
661 constexpr atomic(
const integral_type value) noexcept : base_type(value) {}
663 using base_type::operator integral_type;
664 using base_type::operator =;
666 static constexpr bool is_always_lock_free =
true;
671struct atomic<unsigned long long> : atomic_base<unsigned long long> {
672 using integral_type =
unsigned long long;
673 using base_type = atomic_base<unsigned long long>;
675 __MSTL_ATOMIC_CONSTRUCTIONS
677 constexpr atomic(
const integral_type value) noexcept : base_type(value) {}
679 using base_type::operator integral_type;
680 using base_type::operator =;
682 static constexpr bool is_always_lock_free =
true;
687struct atomic<wchar_t> : atomic_base<wchar_t> {
688 using integral_type = wchar_t;
689 using base_type = atomic_base<wchar_t>;
691 __MSTL_ATOMIC_CONSTRUCTIONS
693 constexpr atomic(
const integral_type value) noexcept : base_type(value) {}
695 using base_type::operator integral_type;
696 using base_type::operator =;
698 static constexpr bool is_always_lock_free =
true;
701#ifdef MSTL_STANDARD_20__
705 using integral_type = char8_t;
708 __MSTL_ATOMIC_CONSTRUCTIONS
710 constexpr atomic(
const integral_type value) noexcept : base_type(value) {}
712 using base_type::operator integral_type;
713 using base_type::operator =;
721struct atomic<char16_t> : atomic_base<char16_t> {
722 using integral_type = char16_t;
723 using base_type = atomic_base<char16_t>;
725 __MSTL_ATOMIC_CONSTRUCTIONS
727 constexpr atomic(
const integral_type value) noexcept : base_type(value) {}
729 using base_type::operator integral_type;
730 using base_type::operator =;
732 static constexpr bool is_always_lock_free =
true;
737struct atomic<char32_t> : atomic_base<char32_t> {
738 using integral_type = char32_t;
739 using base_type = atomic_base<char32_t>;
741 __MSTL_ATOMIC_CONSTRUCTIONS
743 constexpr atomic(
const integral_type value) noexcept : base_type(value) {}
745 using base_type::operator integral_type;
746 using base_type::operator =;
748 static constexpr bool is_always_lock_free =
true;
753struct atomic<float> : atomic_float_base<float> {
754 __MSTL_ATOMIC_CONSTRUCTIONS
756 constexpr atomic(
const float value) noexcept
757 : atomic_float_base<float>(value) {}
759 using atomic_float_base<
float>::operator =;
764struct atomic<double> : atomic_float_base<double> {
765 __MSTL_ATOMIC_CONSTRUCTIONS
767 constexpr atomic(
const double value) noexcept
768 : atomic_float_base<double>(value) {}
770 using atomic_float_base<
double>::operator =;
775struct atomic<long double> : atomic_float_base<long double> {
776 __MSTL_ATOMIC_CONSTRUCTIONS
778 constexpr atomic(
const long double value) noexcept
779 : atomic_float_base<long double>(value) {}
781 using atomic_float_base<
long double>::operator =;
784#undef __MSTL_ATOMIC_CONSTRUCTIONS
804#ifdef MSTL_STANDARD_20__
MSTL_NODISCARD constexpr T * addressof(T &x) noexcept
获取对象的地址
atomic< bool > atomic_bool
布尔原子类型
atomic< ptrdiff_t > atomic_ptrdiff_t
指针差值类型原子类型
atomic< unsigned char > atomic_uchar
无符号字符原子类型
atomic< char16_t > atomic_char16_t
UTF-16字符原子类型
atomic< intptr_t > atomic_intptr_t
有符号指针整数原子类型
atomic< uint64_t > atomic_uint64_t
64位无符号整数原子类型
atomic< short > atomic_short
短整型原子类型
atomic< uint16_t > atomic_uint16_t
16位无符号整数原子类型
atomic< unsigned short > atomic_ushort
无符号短整型原子类型
atomic< uint32_t > atomic_uint32_t
32位无符号整数原子类型
atomic< float32_t > atomic_float32
32位浮点数原子类型
atomic< float > atomic_float
单精度浮点数原子类型
MSTL_ALWAYS_INLINE_INLINE remove_volatile_t< T > atomic_exchange_any(T *ptr, remove_volatile_t< T > desired, memory_order mo) noexcept
通用原子交换操作
atomic< uintmax_t > atomic_uintmax_t
最大无符号整数原子类型
MSTL_ALWAYS_INLINE_INLINE remove_volatile_t< T > atomic_load_any(const T *ptr, memory_order mo) noexcept
通用原子加载操作
atomic< intmax_t > atomic_intmax_t
最大有符号整数原子类型
atomic< size_t > atomic_size_t
大小类型原子类型
atomic< long double > atomic_ldouble
扩展精度浮点数原子类型
atomic< decimal_t > atomic_decimal
十进制浮点数原子类型
atomic< float64_t > atomic_float64
64位浮点数原子类型
atomic< int16_t > atomic_int16_t
16位有符号整数原子类型
atomic< long long > atomic_llong
长长整型原子类型
atomic< int64_t > atomic_int64_t
64位有符号整数原子类型
atomic< int > atomic_int
整型原子类型
atomic< unsigned long > atomic_ulong
无符号长整型原子类型
atomic< char32_t > atomic_char32_t
UTF-32字符原子类型
atomic< signed char > atomic_schar
有符号字符原子类型
atomic< unsigned int > atomic_uint
无符号整型原子类型
atomic< double > atomic_double
双精度浮点数原子类型
atomic< uint8_t > atomic_uint8_t
8位无符号整数原子类型
MSTL_ALWAYS_INLINE_INLINE void atomic_store_any(T *ptr, remove_volatile_t< T > value, const memory_order mo) noexcept
通用原子存储操作
atomic< long > atomic_long
长整型原子类型
atomic< int32_t > atomic_int32_t
32位有符号整数原子类型
atomic< unsigned long long > atomic_ullong
无符号长长整型原子类型
atomic< int8_t > atomic_int8_t
8位有符号整数原子类型
MSTL_ALWAYS_INLINE_INLINE bool atomic_cmpexch_weak_any(volatile T *ptr, remove_volatile_t< T > *expected, remove_volatile_t< T > *desired, const memory_order success, const memory_order failure) noexcept
通用弱比较交换操作
MSTL_ALWAYS_INLINE_INLINE bool atomic_cmpexch_strong_any(volatile T *ptr, remove_volatile_t< T > *expected, remove_volatile_t< T > *desired, const memory_order success, const memory_order failure) noexcept
通用强比较交换操作
atomic< uintptr_t > atomic_uintptr_t
无符号指针整数原子类型
atomic< wchar_t > atomic_wchar_t
宽字符原子类型
atomic< char > atomic_char
字符原子类型
constexpr bool is_valid_cmpexch_failure_order(const memory_order mo) noexcept
检查比较交换失败内存顺序是否有效
MSTL_INLINE17 constexpr auto memory_order_seq_cst
顺序一致性内存顺序常量
constexpr memory_order cmpexch_failure_order(const memory_order mo) noexcept
获取原子比较交换操作失败时的内存顺序
#define _MSTL
全局命名空间MSTL前缀
#define MSTL_END_NAMESPACE__
结束全局命名空间MSTL
#define MSTL_BEGIN_NAMESPACE__
开始全局命名空间MSTL
static constexpr bool is_always_lock_free
是否总是无锁
bool is_lock_free() const noexcept
检查是否支持无锁操作
bool compare_exchange_strong(bool &value1, const bool value2, const memory_order mo=memory_order_seq_cst) noexcept
简化版强比较交换操作
void store(const bool value, const memory_order mo=memory_order_seq_cst) volatile noexcept
volatile版本的原子存储操作
void store(const bool value, const memory_order mo=memory_order_seq_cst) noexcept
原子存储操作
bool compare_exchange_weak(bool &value1, const bool value2, const memory_order success, const memory_order failure) noexcept
弱比较交换操作
bool load(const memory_order mo=memory_order_seq_cst) const volatile noexcept
volatile版本的原子加载操作
bool exchange(const bool value, const memory_order mo=memory_order_seq_cst) noexcept
原子交换操作
bool compare_exchange_strong(bool &value1, const bool value2, const memory_order mo=memory_order_seq_cst) volatile noexcept
volatile版本的简化版强比较交换操作
bool compare_exchange_weak(bool &value1, const bool value2, const memory_order mo=memory_order_seq_cst) volatile noexcept
volatile版本的简化版弱比较交换操作
void wait(const bool old, const memory_order mo=memory_order_seq_cst) const noexcept
等待值改变
bool compare_exchange_strong(bool &value1, const bool value2, const memory_order success, const memory_order failure) noexcept
强比较交换操作
constexpr atomic(const bool value) noexcept
构造函数
void notify_one() noexcept
通知一个等待线程
bool is_lock_free() const volatile noexcept
volatile版本的检查是否支持无锁操作
bool exchange(const bool value, const memory_order mo=memory_order_seq_cst) volatile noexcept
volatile版本的原子交换操作
bool compare_exchange_weak(bool &value1, const bool value2, const memory_order mo=memory_order_seq_cst) noexcept
简化版弱比较交换操作
bool compare_exchange_weak(bool &value1, const bool value2, const memory_order success, const memory_order failure) volatile noexcept
volatile版本的弱比较交换操作
bool compare_exchange_strong(bool &value1, const bool value2, const memory_order success, const memory_order failure) volatile noexcept
volatile版本的强比较交换操作
void notify_all() noexcept
通知所有等待线程
bool load(const memory_order mo=memory_order_seq_cst) const noexcept
原子加载操作
bool compare_exchange_strong(T &expected, T value, const memory_order mo=memory_order_seq_cst) noexcept
简化版强比较交换操作
T load(const memory_order mo=memory_order_seq_cst) const noexcept
原子加载操作
static constexpr bool is_always_lock_free
bool is_lock_free() const noexcept
检查是否支持无锁操作
bool compare_exchange_strong(T &expected, T desired, const memory_order success, const memory_order failure) noexcept
强比较交换操作
void store(T value, const memory_order mo=memory_order_seq_cst) volatile noexcept
volatile版本的原子存储操作
void store(T value, const memory_order mo=memory_order_seq_cst) noexcept
原子存储操作
T exchange(T value, const memory_order mo=memory_order_seq_cst) noexcept
原子交换操作
constexpr atomic(T value) noexcept
构造函数
bool compare_exchange_weak(T &expected, T desired, const memory_order mo=memory_order_seq_cst) noexcept
简化版弱比较交换操作
bool compare_exchange_weak(T &expected, T desired, const memory_order success, const memory_order failure) volatile noexcept
volatile版本的弱比较交换操作
bool compare_exchange_weak(T &expected, T desired, const memory_order mo=memory_order_seq_cst) volatile noexcept
volatile版本的简化版弱比较交换操作
T exchange(T value, const memory_order mo=memory_order_seq_cst) volatile noexcept
volatile版本的原子交换操作
bool compare_exchange_strong(T &expected, T desired, const memory_order success, const memory_order failure) volatile noexcept
volatile版本的强比较交换操作
bool is_lock_free() const volatile noexcept
volatile版本的检查是否支持无锁操作
T load(const memory_order mo=memory_order_seq_cst) const volatile noexcept
volatile版本的原子加载操作
bool compare_exchange_weak(T &expected, T desired, const memory_order success, const memory_order failure) noexcept
弱比较交换操作
bool compare_exchange_strong(T &expected, T value, const memory_order mo=memory_order_seq_cst) volatile noexcept
volatile版本的简化版强比较交换操作