Next: , Previous: Special Variables, Up: Threading


12.3 Atomic Operations

SBCL provides a few special purpose atomic operations, particularly useful for implementing lockless algorithms.

— Macro: sb-ext:atomic-decf place &optional diff

Atomically decrements place by diff, and returns the value of place before the increment.

The decrementation is done using word-size modular arithmetic: on 32 bit platforms atomic-decf of #x0 by one results in #xFFFFFFFF being stored in place.

place must be an accessor form whose car is the name of a defstruct accessor whose declared type is (UNSIGNED-BYTE 32) on 32 bit platforms, and (UNSIGNED-BYTE 64) on 64 bit platforms.

diff defaults to 1, and must be a (SIGNED-BYTE 32) on 32 bit platforms, and (SIGNED-BYTE 64) on 64 bit platforms.

experimental: Interface subject to change.

— Macro: sb-ext:atomic-incf place &optional diff

Atomically increments place by diff, and returns the value of place before the increment.

The incrementation is done using word-size modular arithmetic: on 32 bit platforms atomic-incf of #xFFFFFFFF by one results in #x0 being stored in place.

place must be an accessor form whose car is the name of a defstruct accessor whose declared type is (UNSIGNED-BYTE 32) on 32 bit platforms, and (UNSIGNED-BYTE 64) on 64 bit platforms.

diff defaults to 1, and must be a (SIGNED-BYTE 32) on 32 bit platforms, and (SIGNED-BYTE 64) on 64 bit platforms.

experimental: Interface subject to change.

— Macro: sb-ext:compare-and-swap place old new env

Atomically stores new in place if old matches the current value of place. Two values are considered to match if they are eq. Returns the previous value of place: if the returned value is eq to old, the swap was carried out.

place must be an accessor form whose car is one of the following:

car, cdr, first, rest, symbol-plist, symbol-value, svref

or the name of a defstruct created accessor for a slot whose declared type is either fixnum or t. Results are unspecified if the slot has a declared type other then fixnum or t.

experimental: Interface subject to change.