Search documentation of erts

# View Source atomics (erts v15.0.1)

Atomic Functions

This module provides a set of functions to do atomic operations towards mutable atomic variables. The implementation utilizes only atomic hardware instructions without any software level locking, which makes it very efficient for concurrent access. The atomics are organized into arrays with the following semantics:

- Atomics are 64 bit integers.
- Atomics can be represented as either signed or unsigned.
- Atomics wrap around at overflow and underflow operations.
- All operations guarantee atomicity. No intermediate results can be seen. The result of one mutation can only be the input to one following mutation.
- All atomic operations are mutually ordered. If atomic B is updated after atomic A, then that is how it will appear to any concurrent readers. No one can read the new value of B and then read the old value of A.
- Indexes into atomic arrays are one-based. An atomic array of arity N contains N atomics with index from 1 to N.

# Summary

## Types

atomics_ref()

Identifies an atomic array returned from `new/2`.

## Functions

add(Ref, Ix, Incr)

Add `Incr` to atomic.

add_get(Ref, Ix, Incr)

Atomically add `Incr` to atomic and return the result.

compare_exchange(Ref, Ix, Expected, Desired)

Atomically compare the atomic with `Expected`, and if those are equal, set atomic to `Desired`.

exchange(Ref, Ix, Desired)

Atomically replace the value of the atomic with `Desired` and return the previous value.

get(Ref, Ix)

Read atomic value.

info(Ref)

Return information about an atomic array in a map.

new(Arity, Opts)

Create a new array of `Arity` number of atomics. All atomics in the array are initially set to zero.

put(Ref, Ix, Value)

Set atomic to `Value`.

sub(Ref, Ix, Decr)

Subtract `Decr` from atomic.

sub_get(Ref, Ix, Decr)

Atomically subtract `Decr` from atomic and return the result.

# Types

Link to this opaque

# atomics_ref()

View Source (since OTP 21.2)

    -opaque atomics_ref()

Identifies an atomic array returned from `new/2`.

# Functions

Link to this function

# add(Ref, Ix, Incr)

View Source (since OTP 21.2)

    -spec add(Ref, Ix, Incr) -> ok when Ref :: atomics_ref(), Ix :: integer(), Incr :: integer().

Add `Incr` to atomic.

Link to this function

# add_get(Ref, Ix, Incr)

View Source (since OTP 21.2)

    -spec add_get(Ref, Ix, Incr) -> integer() when Ref :: atomics_ref(), Ix :: integer(), Incr :: integer().

Atomically add `Incr` to atomic and return the result.

Link to this function

# compare_exchange(Ref, Ix, Expected, Desired)

View Source (since OTP 21.2)

    -spec compare_exchange(Ref, Ix, Expected, Desired) -> ok | integer()
                              when
                                  Ref :: atomics_ref(),
                                  Ix :: integer(),
                                  Expected :: integer(),
                                  Desired :: integer().

Atomically compare the atomic with `Expected`, and if those are equal, set atomic to `Desired`.

Return `ok` if `Desired` was written. Return the actual atomic value if not equal to `Expected`.

Link to this function

# exchange(Ref, Ix, Desired)

View Source (since OTP 21.2)

    -spec exchange(Ref, Ix, Desired) -> integer()
                      when Ref :: atomics_ref(), Ix :: integer(), Desired :: integer().

Atomically replace the value of the atomic with `Desired` and return the previous value.

Link to this function

# get(Ref, Ix)

View Source (since OTP 21.2)

    -spec get(Ref, Ix) -> integer() when Ref :: atomics_ref(), Ix :: integer().

Read atomic value.

Link to this function

# info(Ref)

View Source (since OTP 21.2)

    -spec info(Ref) -> Info
                  when
                      Ref :: atomics_ref(),
                      Info :: #{size := Size, max := Max, min := Min, memory := Memory},
                      Size :: non_neg_integer(),
                      Max :: integer(),
                      Min :: integer(),
                      Memory :: non_neg_integer().

Return information about an atomic array in a map.

The map has the following keys:

- `size` - The number of atomics in the array.
- `max` - The highest possible value an atomic in this array can hold.
- `min` - The lowest possible value an atomic in this array can hold.
- `memory` - Approximate memory consumption for the array in bytes.

Link to this function

# new(Arity, Opts)

View Source (since OTP 21.2)

    -spec new(Arity, Opts) -> atomics_ref()
                 when Arity :: pos_integer(), Opts :: [Opt], Opt :: {signed, boolean()}.

Create a new array of `Arity` number of atomics. All atomics in the array are initially set to zero.

Argument `Opts` is a list of the following possible options:

- `{signed, boolean()}` - Indicate if the elements of the array will be treated as signed or unsigned integers. Default is `true` (signed).

  The integer interval for signed atomics are from `-(1 bsl 63)` to `(1 bsl 63)-1` and for unsigned atomics from `0` to `(1 bsl 64)-1`.

Atomics are not tied to the current process and are automatically garbage collected when they are no longer referenced.

Link to this function

# put(Ref, Ix, Value)

View Source (since OTP 21.2)

    -spec put(Ref, Ix, Value) -> ok when Ref :: atomics_ref(), Ix :: integer(), Value :: integer().

Set atomic to `Value`.

Link to this function

# sub(Ref, Ix, Decr)

View Source (since OTP 21.2)

    -spec sub(Ref, Ix, Decr) -> ok when Ref :: atomics_ref(), Ix :: integer(), Decr :: integer().

Subtract `Decr` from atomic.

Link to this function

# sub_get(Ref, Ix, Decr)

View Source (since OTP 21.2)

    -spec sub_get(Ref, Ix, Decr) -> integer() when Ref :: atomics_ref(), Ix :: integer(), Decr :: integer().

Atomically subtract `Decr` from atomic and return the result.