libxr  1.0
Want to be the best embedded framework
Loading...
Searching...
No Matches
LibXR::Flag::Atomic Class Reference

原子标志位 / Atomic flag More...

#include <flag.hpp>

Public Member Functions

 Atomic ()=default
 构造函数,默认未置位 / Constructor, default cleared
 
void Set () noexcept
 置位标志 / Set the flag
 
void Clear () noexcept
 清除标志 / Clear the flag
 
bool IsSet () const noexcept
 判断是否已置位 / Check whether the flag is set
 
bool TestAndSet () noexcept
 测试并置位:置位并返回旧状态 / Test-and-set: set and return previous state
 
bool TestAndClear () noexcept
 测试并清除:清除并返回旧状态 / Test-and-clear: clear and return previous state
 
bool Exchange (bool set_value) noexcept
 交换:写入指定值并返回旧状态 / Exchange: set to desired value and return previous state
 
 Atomic (const Atomic &)=delete
 禁用拷贝构造与拷贝赋值 / Copy construction and copy assignment are disabled
 
Atomicoperator= (const Atomic &)=delete
 

Private Attributes

std::atomic< uint8_t > value_
 标志值(0=clear, 1=set)/ Flag value (0=clear, 1=set)
 

Detailed Description

原子标志位 / Atomic flag

提供 Set/Clear/IsSet 与 TestAndSet/TestAndClear/Exchange,可用于多线程/多核/ISR 共享的状态标记(不提供互斥锁语义)。 Provides Set/Clear/IsSet and TestAndSet/TestAndClear/Exchange for state signaling across threads/cores/ISRs (no mutex semantics).

Definition at line 31 of file flag.hpp.

Member Function Documentation

◆ Clear()

void LibXR::Flag::Atomic::Clear ( )
inlinenoexcept

清除标志 / Clear the flag

Definition at line 47 of file flag.hpp.

47{ value_.store(0u, std::memory_order_release); }
std::atomic< uint8_t > value_
标志值(0=clear, 1=set)/ Flag value (0=clear, 1=set)
Definition flag.hpp:102

◆ Exchange()

bool LibXR::Flag::Atomic::Exchange ( bool set_value)
inlinenodiscardnoexcept

交换:写入指定值并返回旧状态 / Exchange: set to desired value and return previous state

Parameters
set_value期望写入的值 / Desired value to write
Returns
true 旧值为 set / Old value was set
false 旧值为 clear / Old value was clear

Definition at line 90 of file flag.hpp.

91 {
92 return value_.exchange(set_value ? 1u : 0u, std::memory_order_acq_rel) != 0u;
93 }

◆ IsSet()

bool LibXR::Flag::Atomic::IsSet ( ) const
inlinenodiscardnoexcept

判断是否已置位 / Check whether the flag is set

Returns
true 已置位 / Flag is set
false 未置位 / Flag is clear

Definition at line 55 of file flag.hpp.

56 {
57 return value_.load(std::memory_order_acquire) != 0u;
58 }

◆ Set()

void LibXR::Flag::Atomic::Set ( )
inlinenoexcept

置位标志 / Set the flag

Definition at line 42 of file flag.hpp.

42{ value_.store(1u, std::memory_order_release); }

◆ TestAndClear()

bool LibXR::Flag::Atomic::TestAndClear ( )
inlinenodiscardnoexcept

测试并清除:清除并返回旧状态 / Test-and-clear: clear and return previous state

Returns
true 先前已置位 / Was set
false 先前未置位 / Was clear

Definition at line 77 of file flag.hpp.

78 {
79 return value_.exchange(0u, std::memory_order_acq_rel) != 0u;
80 }

◆ TestAndSet()

bool LibXR::Flag::Atomic::TestAndSet ( )
inlinenodiscardnoexcept

测试并置位:置位并返回旧状态 / Test-and-set: set and return previous state

Returns
true 先前已置位 / Was already set
false 先前未置位 / Was clear

Definition at line 66 of file flag.hpp.

67 {
68 return value_.exchange(1u, std::memory_order_acq_rel) != 0u;
69 }

Field Documentation

◆ value_

std::atomic<uint8_t> LibXR::Flag::Atomic::value_
private
Initial value:
{
0u}

标志值(0=clear, 1=set)/ Flag value (0=clear, 1=set)

Definition at line 102 of file flag.hpp.

102 {
103 0u};

The documentation for this class was generated from the following file: