Class Atomic
Exposes atomic operations for thread-safe scenarios.
Inherited Members
Namespace: DotNext.Threading
Assembly: DotNext.dll
Syntax
public static class Atomic
Methods
View SourceAccumulateAndGet<T>(ref T, T, Func<T, T, T>)
Atomically updates the current value with the results of applying the given function to the current and given values, returning the updated value.
Declaration
[MemoryOrdering(MemoryOrdering.SequentiallyConsistent)]
public static T AccumulateAndGet<T>(ref T location, T x, Func<T, T, T> accumulator)
Parameters
| Type | Name | Description |
|---|---|---|
| T | location | Reference to a value to be modified. |
| T | x | Accumulator operand. |
| Func<T, T, T> | accumulator | A side-effect-free function of two arguments. |
Returns
| Type | Description |
|---|---|
| T | The updated value. |
Type Parameters
| Name | Description |
|---|---|
| T |
Remarks
The function is applied with the current value as its first argument, and the given update as the second argument.
AccumulateAndGet<T, TAccumulator>(ref T, T, TAccumulator)
Atomically updates the current value with the results of applying the given function to the current and given values, returning the updated value.
Declaration
[MemoryOrdering(MemoryOrdering.SequentiallyConsistent)]
public static T AccumulateAndGet<T, TAccumulator>(ref T location, T x, TAccumulator accumulator) where TAccumulator : ISupplier<T, T, T>, allows ref struct
Parameters
| Type | Name | Description |
|---|---|---|
| T | location | Reference to a value to be modified. |
| T | x | Accumulator operand. |
| TAccumulator | accumulator | A side-effect-free function of two arguments. |
Returns
| Type | Description |
|---|---|
| T | The updated value. |
Type Parameters
| Name | Description |
|---|---|
| T | |
| TAccumulator | The type implementing accumulator. |
Remarks
The function is applied with the current value as its first argument, and the given update as the second argument.
FalseToTrue(ref bool)
Declaration
[MemoryOrdering(MemoryOrdering.SequentiallyConsistent)]
public static bool FalseToTrue(ref bool location)
Parameters
| Type | Name | Description |
|---|---|---|
| bool | location | The destination, whose value is compared with comparand and possibly replaced. |
Returns
| Type | Description |
|---|---|
| bool | true if current value is modified successfully; otherwise, false. |
GetAndAccumulate<T>(ref T, T, Func<T, T, T>)
Atomically updates the current value with the results of applying the given function to the current and given values, returning the original value.
Declaration
[MemoryOrdering(MemoryOrdering.SequentiallyConsistent)]
public static T GetAndAccumulate<T>(ref T location, T x, Func<T, T, T> accumulator)
Parameters
| Type | Name | Description |
|---|---|---|
| T | location | Reference to a value to be modified. |
| T | x | Accumulator operand. |
| Func<T, T, T> | accumulator | A side-effect-free function of two arguments. |
Returns
| Type | Description |
|---|---|
| T | The original value. |
Type Parameters
| Name | Description |
|---|---|
| T |
Remarks
The function is applied with the current value as its first argument, and the given update as the second argument.
GetAndAccumulate<T, TAccumulator>(ref T, T, TAccumulator)
Atomically updates the current value with the results of applying the given function to the current and given values, returning the original value.
Declaration
[MemoryOrdering(MemoryOrdering.SequentiallyConsistent)]
public static T GetAndAccumulate<T, TAccumulator>(ref T location, T x, TAccumulator accumulator) where TAccumulator : ISupplier<T, T, T>, allows ref struct
Parameters
| Type | Name | Description |
|---|---|---|
| T | location | Reference to a value to be modified. |
| T | x | Accumulator operand. |
| TAccumulator | accumulator | A side-effect-free function of two arguments. |
Returns
| Type | Description |
|---|---|
| T | The original value. |
Type Parameters
| Name | Description |
|---|---|
| T | |
| TAccumulator | The type implementing accumulator. |
Remarks
The function is applied with the current value as its first argument, and the given update as the second argument.
GetAndNegate(ref bool)
Negates currently stored value atomically.
Declaration
[MemoryOrdering(MemoryOrdering.SequentiallyConsistent)]
public static bool GetAndNegate(ref bool location)
Parameters
| Type | Name | Description |
|---|---|---|
| bool | location | Reference to a value to be modified. |
Returns
| Type | Description |
|---|---|
| bool | The original value before negation. |
GetAndUpdate<T>(ref T, Func<T, T>)
Atomically updates the stored value with the results of applying the given function, returning the original value.
Declaration
[MemoryOrdering(MemoryOrdering.SequentiallyConsistent)]
public static T GetAndUpdate<T>(ref T location, Func<T, T> updater)
Parameters
| Type | Name | Description |
|---|---|---|
| T | location | Reference to a value to be modified. |
| Func<T, T> | updater | A side-effect-free function. |
Returns
| Type | Description |
|---|---|
| T | The original value. |
Type Parameters
| Name | Description |
|---|---|
| T |
GetAndUpdate<T, TUpdater>(ref T, TUpdater)
Atomically updates the stored value with the results of applying the given function, returning the original value.
Declaration
[MemoryOrdering(MemoryOrdering.SequentiallyConsistent)]
public static T GetAndUpdate<T, TUpdater>(ref T location, TUpdater updater) where TUpdater : ISupplier<T, T>, allows ref struct
Parameters
| Type | Name | Description |
|---|---|---|
| T | location | Reference to a value to be modified. |
| TUpdater | updater | A side-effect-free function. |
Returns
| Type | Description |
|---|---|
| T | The original value. |
Type Parameters
| Name | Description |
|---|---|
| T | |
| TUpdater | The type implementing updater. |
GetOrSet<T>(ref Atomic<Optional<T>>, T, out bool)
Returns the value if it is defined; or sets the supplied value.
Declaration
[MemoryOrdering(MemoryOrdering.Acquire)]
public static T GetOrSet<T>(this ref Atomic<Optional<T>> atomic, T value, out bool isSet)
Parameters
| Type | Name | Description |
|---|---|---|
| Atomic<Optional<T>> | atomic | |
| T | value | The value to set if the receiver is undefined. |
| bool | isSet | true if |
Returns
| Type | Description |
|---|---|
| T | The existing value contained in the receiver; or |
Type Parameters
| Name | Description |
|---|---|
| T |
IsAtomic<T>()
Determines that write to the location in the memory of
type T is atomic.
Declaration
public static bool IsAtomic<T>()
Returns
| Type | Description |
|---|---|
| bool |
Type Parameters
| Name | Description |
|---|---|
| T |
NegateAndGet(ref bool)
Negates currently stored value atomically.
Declaration
[MemoryOrdering(MemoryOrdering.SequentiallyConsistent)]
public static bool NegateAndGet(ref bool location)
Parameters
| Type | Name | Description |
|---|---|---|
| bool | location | Reference to a value to be modified. |
Returns
| Type | Description |
|---|---|
| bool | Negation result. |
Read(ref readonly double)
Reads atomically the value from the specified location in the memory.
Declaration
[CLSCompliant(false)]
[MemoryOrdering(MemoryOrdering.Acquire)]
public static double Read(ref readonly double location)
Parameters
| Type | Name | Description |
|---|---|---|
| double | location | The location of the value. |
Returns
| Type | Description |
|---|---|
| double | The value at the specified location. |
Remarks
This method works correctly on 32-bit and 64-bit architectures.
Read(ref readonly long)
Reads atomically the value from the specified location in the memory.
Declaration
[MemoryOrdering(MemoryOrdering.Acquire)]
public static long Read(ref readonly long location)
Parameters
| Type | Name | Description |
|---|---|---|
| long | location | The location of the value. |
Returns
| Type | Description |
|---|---|
| long | The value at the specified location. |
Remarks
This method works correctly on 32-bit and 64-bit architectures.
Read(ref readonly ulong)
Reads atomically the value from the specified location in the memory.
Declaration
[CLSCompliant(false)]
[MemoryOrdering(MemoryOrdering.Acquire)]
public static ulong Read(ref readonly ulong location)
Parameters
| Type | Name | Description |
|---|---|---|
| ulong | location | The location of the value. |
Returns
| Type | Description |
|---|---|
| ulong | The value at the specified location. |
Remarks
This method works correctly on 32-bit and 64-bit architectures.
Read<T>(T**)
Reads the pointer from the specified location. On systems that require it, inserts a memory barrier that prevents the processor from reordering memory operations as follows: if a read or write appears after this method in the code, the processor cannot move it before this method.
Declaration
[CLSCompliant(false)]
[MemoryOrdering(MemoryOrdering.Acquire)]
public static T* Read<T>(T** location) where T : unmanaged
Parameters
| Type | Name | Description |
|---|---|---|
| T** | location | The location of the pointer to read. |
Returns
| Type | Description |
|---|---|
| T* | The pointer that was read. |
Type Parameters
| Name | Description |
|---|---|
| T | The type of the value referenced by the pointer. |
Remarks
Unlike volatile field modifier, this method can be used for the location obtained through the pointer.
Read<T>(ref readonly T*)
Reads the pointer from the specified location. On systems that require it, inserts a memory barrier that prevents the processor from reordering memory operations as follows: if a read or write appears after this method in the code, the processor cannot move it before this method.
Declaration
[CLSCompliant(false)]
[MemoryOrdering(MemoryOrdering.Acquire)]
public static T* Read<T>(ref readonly T* location) where T : unmanaged
Parameters
| Type | Name | Description |
|---|---|---|
| T* | location | The location of the pointer to read. |
Returns
| Type | Description |
|---|---|
| T* | The pointer that was read. |
Type Parameters
| Name | Description |
|---|---|
| T | The type of the value referenced by the pointer. |
Remarks
Unlike volatile field modifier, this method can be used for any location passed by reference, such as an array element or a field of the struct accessed by reference.
TrueToFalse(ref bool)
Declaration
[MemoryOrdering(MemoryOrdering.SequentiallyConsistent)]
public static bool TrueToFalse(ref bool location)
Parameters
| Type | Name | Description |
|---|---|---|
| bool | location | The destination, whose value is compared with comparand and possibly replaced. |
Returns
| Type | Description |
|---|---|
| bool | true if current value is modified successfully; otherwise, false. |
TrySet<T>(ref Atomic<Optional<T>>, T)
Sets the value to if it's not defined in the receiver.
Declaration
[MemoryOrdering(MemoryOrdering.Acquire)]
public static bool TrySet<T>(this ref Atomic<Optional<T>> atomic, T value)
Parameters
| Type | Name | Description |
|---|---|---|
| Atomic<Optional<T>> | atomic | |
| T | value | The value to set. |
Returns
| Type | Description |
|---|---|
| bool | true if |
Type Parameters
| Name | Description |
|---|---|
| T |
UpdateAndGet<T>(ref T, Func<T, T>)
Atomically updates the stored value with the results of applying the given function, returning the updated value.
Declaration
[MemoryOrdering(MemoryOrdering.SequentiallyConsistent)]
public static T UpdateAndGet<T>(ref T location, Func<T, T> updater)
Parameters
| Type | Name | Description |
|---|---|---|
| T | location | Reference to a value to be modified. |
| Func<T, T> | updater | A side-effect-free function. |
Returns
| Type | Description |
|---|---|
| T | The updated value. |
Type Parameters
| Name | Description |
|---|---|
| T |
UpdateAndGet<T, TUpdater>(ref T, TUpdater)
Atomically updates the stored value with the results of applying the given function, returning the updated value.
Declaration
[MemoryOrdering(MemoryOrdering.SequentiallyConsistent)]
public static T UpdateAndGet<T, TUpdater>(ref T location, TUpdater updater) where TUpdater : ISupplier<T, T>, allows ref struct
Parameters
| Type | Name | Description |
|---|---|---|
| T | location | Reference to a value to be modified. |
| TUpdater | updater | A side-effect-free function. |
Returns
| Type | Description |
|---|---|
| T | The updated value. |
Type Parameters
| Name | Description |
|---|---|
| T | |
| TUpdater | The type implementing updater. |
Write(ref double, double)
Writes atomically the value at the specified location in the memory.
Declaration
[CLSCompliant(false)]
[MemoryOrdering(MemoryOrdering.Release)]
public static void Write(ref double location, double value)
Parameters
| Type | Name | Description |
|---|---|---|
| double | location | The location of the value. |
| double | value | The desired value at the specified location. |
Remarks
This method works correctly on 32-bit and 64-bit architectures.
Write(ref long, long)
Writes atomically the value at the specified location in the memory.
Declaration
[MemoryOrdering(MemoryOrdering.Release)]
public static void Write(ref long location, long value)
Parameters
| Type | Name | Description |
|---|---|---|
| long | location | The location of the value. |
| long | value | The desired value at the specified location. |
Remarks
This method works correctly on 32-bit and 64-bit architectures.
Write(ref ulong, ulong)
Writes atomically the value at the specified location in the memory.
Declaration
[CLSCompliant(false)]
[MemoryOrdering(MemoryOrdering.Release)]
public static void Write(ref ulong location, ulong value)
Parameters
| Type | Name | Description |
|---|---|---|
| ulong | location | The location of the value. |
| ulong | value | The desired value at the specified location. |
Remarks
This method works correctly on 32-bit and 64-bit architectures.
Write<T>(T**, T*)
Writes the pointer to the specified location. On systems that require it, inserts a memory barrier that prevents the processor from reordering memory operations as follows: if a read or write appears before this method in the code, the processor cannot move it after this method.
Declaration
[CLSCompliant(false)]
[MemoryOrdering(MemoryOrdering.Release)]
public static void Write<T>(T** location, T* value) where T : unmanaged
Parameters
| Type | Name | Description |
|---|---|---|
| T** | location | The location of the pointer to write. |
| T* | value | The pointer to write. |
Type Parameters
| Name | Description |
|---|---|
| T | The type of the value referenced by the pointer. |
Remarks
Unlike volatile field modifier, this method can be used for the location obtained through the pointer.
Write<T>(ref T*, T*)
Writes the pointer to the specified location. On systems that require it, inserts a memory barrier that prevents the processor from reordering memory operations as follows: if a read or write appears before this method in the code, the processor cannot move it after this method.
Declaration
[CLSCompliant(false)]
[MemoryOrdering(MemoryOrdering.Release)]
public static void Write<T>(ref T* location, T* value) where T : unmanaged
Parameters
| Type | Name | Description |
|---|---|---|
| T* | location | The location of the pointer to write. |
| T* | value | The pointer to write. |
Type Parameters
| Name | Description |
|---|---|
| T | The type of the value referenced by the pointer. |
Remarks
Unlike volatile field modifier, this method can be used for any location passed by reference, such as an array element or a field of the struct accessed by reference.
get_IsUndefined<T>(ref readonly Atomic<Optional<T>>)
Indicates that the value withing the receiver is not set.
Declaration
[MemoryOrdering(MemoryOrdering.Acquire)]
public static bool get_IsUndefined<T>(ref readonly Atomic<Optional<T>> atomic)
Parameters
| Type | Name | Description |
|---|---|---|
| Atomic<Optional<T>> | atomic |
Returns
| Type | Description |
|---|---|
| bool |
Type Parameters
| Name | Description |
|---|---|
| T |