Skip to main content

Bool

This class represents an atomic Boolean. It stores the boolean value as an unsigned 32-bit integer internally to leverage atomic functions available in Go.

Methods


Load()

@classmethod
def Load() - > bool

Fetches the current boolean value stored in the atomic boolean. Callers use this to retrieve the current state without modification.

Returns

TypeDescription
boolThe current boolean value.

CompareAndSwap()

@classmethod
def CompareAndSwap(
old: bool,
new: bool
) - > bool

Atomically compares the current boolean value with 'old' and, if they are equal, replaces the current value with 'new'. This is used for conditional updates, ensuring the value is changed only if it matches an expected state.

Parameters

NameTypeDescription
oldboolThe expected current boolean value to compare against.
newboolThe new boolean value to set if the comparison succeeds.

Returns

TypeDescription
boolTrue if the swap occurred (i.e., the old value matched the current value), false otherwise.

Store()

@classmethod
def Store(
new: bool
)

Atomically sets the boolean to the 'new' value. Callers use this to unconditionally update the boolean's state.

Parameters

NameTypeDescription
newboolThe boolean value to store.

Swap()

@classmethod
def Swap(
new: bool
) - > bool

Atomically sets the boolean to the 'new' value and returns the old value. Callers use this to update the state and retrieve the previous state in a single atomic operation.

Parameters

NameTypeDescription
newboolThe new boolean value to set.

Returns

TypeDescription
boolThe boolean value that was previously stored before the swap.

Toggle()

@classmethod
def Toggle() - > bool

Atomically inverts the current boolean value. Callers use this to flip the state (true to false, or false to true) in a thread-safe manner.

Returns

TypeDescription
boolThe new boolean value after the toggle operation.