Project

General

Profile

Actions

Feature #214

open

Atomic types and operations

Feature #214: Atomic types and operations

Added by Eugen Wissner 5 days ago.

Status:
New
Priority:
Normal
Assignee:
-
Category:
Language
Start date:
Due date:
% Done:

0%

Estimated time:

Description

Every other synchronization primitive is constructed over atomics. A language without atomic operations cannot express a correct concurrent program at all, not merely an inconvenient one, and no library can fill the gap.

Type system

Mixing atomic and non-atomic access to one object is always a data race, and only a distinct type can make the plain access unwriteable. The type also carries the alignment the instruction requires, and confines the set to widths the machine can do without a hidden lock.

  • Word*
  • Int*
  • Bool
  • Aotmic[T] for pointers

Conversion between atomic and not atomic types is explicit, but the values should be initializable with integer or word literals.

Operations

The machine operation is not a binary operator: fetch_add takes a location, mutates it, and returns the previous value. Memory ordering has no operand slot, so an operator must default to sequential consistency, the most expensive ordering. A contended read-modify-write costs tens of cycles and should not look like +.

  • load, store
  • exchange, compare_exchange
  • fetch_add, fetch_sub, fetch_and, fetch_or, fetch_xor, fetch_nand
  • thread_fence, signal_fence

The first 3 categories have _1, _2, _4 and _8 variants to specify the integer size. The compiler can define them as built-in generic functions, that have special typechecking rules allowing any integral type.

Memory orders are:

  • memory_order_relaxed. Atomicity only, no ordering.
  • memory_order_consume. Orders only data-dependent reads.
  • memory_order_acquire. Nothing after may move before.
  • memory_order_release. Nothing before may move after.
  • memory_order_acq_rel. Both halves of an RMW.
  • memory_order_seq_cst. acq_rel plus one global total order.

Signatures

  • Binary operation: __atomic_fetch_add_4(object: Pointer; value: Word; order: AtomicOrder) -> Word.
  • __atomic_load_4(object: const Pointer; order: AtomicOrder) -> Word.
  • __atomic_store_4(object: Pointer; value: Word; order: AtomicOrder).
  • __atomic_compare_exchange_4(object, expected: Pointer; desired: Word; weak: Bool, success_order, failure_order: AtomicOrder) -> Bool.
  • __atomic_thread_fence(Int) and __atomic_signal_fence(Int).

No data to display

Actions

Also available in: PDF Atom