package multicore-magic

  1. Overview
  2. Docs

Module Multicore_magic.Transparent_atomicSource

A replacement for Stdlib.Atomic with fixes and performance improvements

Stdlib.Atomic.get is incorrectly subject to CSE optimization in OCaml 5.0.0 and 5.1.0. This can result in code being generated that can produce results that cannot be explained with the OCaml memory model. It can also sometimes result in code being generated where a manual optimization to avoid writing to memory is defeated by the compiler as the compiler eliminates a (repeated) read access. This module implements get such that argument to Stdlib.Atomic.get is passed through Sys.opaque_identity, which prevents the compiler from applying the CSE optimization.

OCaml 5 generates inefficient accesses of 'a Stdlib.Atomic.t arrays assuming that the array might be an array of floating point numbers. That is because the Stdlib.Atomic.t type constructor is opaque, which means that the compiler cannot assume that _ Stdlib.Atomic.t is not the same as float. This module defines the type as private 'a ref, which allows the compiler to know that it cannot be the same as float, which allows the compiler to generate more efficient array accesses. This can both improve performance and reduce size of generated code when using arrays of atomics.

Sourcetype !'a t = private 'a ref
Sourceval make : 'a -> 'a t
Sourceval make_contended : 'a -> 'a t
Sourceval get : 'a t -> 'a
Sourceval fenceless_get : 'a t -> 'a
Sourceval set : 'a t -> 'a -> unit
Sourceval fenceless_set : 'a t -> 'a -> unit
Sourceval exchange : 'a t -> 'a -> 'a
Sourceval compare_and_set : 'a t -> 'a -> 'a -> bool
Sourceval fetch_and_add : int t -> int -> int
Sourceval incr : int t -> unit
Sourceval decr : int t -> unit