Atomic hardware operations perform the whole operation, without any other task seeing an intermediate state. These operations can be used to synchronize tasks without using slow OS primitives, but compared to the non-atomic sequences of operations they tend to be slow. Atomic operations only work correctly on aligned addresses, even on hardware that otherwise does not require alignment.
atomic!@
( w1 a-addr – w2 ) gforth-experimental “atomic-store-fetch”
Fetch w2 from a_addr, then store w1 there, combined into an atomic operation.
atomic+!@
( u1 a-addr – u2 ) gforth-experimental “atomic-plus-store-fetch”
Fetch w2 from a_addr, then increment this location by u1. This atomic operation is commonly known as fetch-and-add.
atomic?!@
( unew uold a-addr – uprev ) gforth-experimental “atomic-question-store-fetch”
Fetch uprev from a_addr, compare it to uold, and if equal, store unew there. This atomic operation is commonly known as compare-and-swap.
There are also the non-atomic !@
and +!@
(otherwise the
same behaviour, see Memory Access).
Another hardware operation is the memory barrier. Unfortunately modern hardware often can reorder memory operations relative to other memory operations (as seen by a different core), and the memory barrier suppresses this reordering for one point in the execution of the task.
barrier
( – ) gforth-experimental “barrier”
All memory operations before the barrier are performed before any memory operation after the barrier.