Module Portable_kernel.Subatomic
A Subatomic.t is a hybrid between ref and Atomic.t. When you have uncontended access to a subatomic, you can do non-atomic reads/writes to it, like a ref. When you have shared access to a subatomic, you can do atomic reads/writes to it, like an Atomic. Nothing can be done to a contended subatomic.
The safety of this interface depends on two differences from each of its counterparts:
- Unlike
ref, you cannot non-atomically read asharedsubatomic - Unlike
Atomic.t, you cannot do anything with acontendedsubatomic, and subatomics consequently do not mode cross on the contention axis
Conceptually, 'a Atomic.t could be implemented as a globally-shared 'a Subatomic.t:
type 'a isolated =
| Isolated : ('a, 'k) Capsule.Data.t * 'k Capsule.Key.t -> 'a isolated
[@@unboxed]
type 'a atomic = 'a subatomic isolatedtype !('a : value_or_null) t = 'a Basement.Subatomic.tinclude sig ... end
val equal :
'a. ('a -> 'a -> bool) ->
'a Portable_kernel.Subatomic.t ->
'a Portable_kernel.Subatomic.t ->
boolval sexp_of_t :
'a. ('a -> Sexplib0.Sexp.t) ->
'a Portable_kernel.Subatomic.t ->
Sexplib0.Sexp.tinclude sig ... end
include sig ... end
val t_of_sexp :
'a. (Sexplib0.Sexp.t -> 'a) ->
Sexplib0.Sexp.t ->
'a Portable_kernel.Subatomic.tval make :
('a : value_or_null). 'a ->
'a Portable_kernel.Subatomic.t @@ portableCreate a subatomic reference; has the same codegen as constructing an Atomic.t or a ref.
val make_alone :
('a : value_or_null). 'a ->
'a Portable_kernel.Subatomic.t @@ portableCreate a subatomic reference that is alone on a cache line. See Atomic.make_alone for details.
val get : ('a : value_or_null). 'a Portable_kernel.Subatomic.t @ local -> 'aRead the current value of a subatomic.
getperforms a non-atomic read on an uncontended subatomic.get [@synchro atomic]andShared.getperform an atomic read on a shared subatomic.
val set : 'a. 'a Portable_kernel.Subatomic.t @ local -> 'a -> unitUpdate the current value of a subatomic.
setperforms a non-atomic write on an uncontended subatomic.set [@synchro atomic]andShared.setperform an atomic write on a shared subatomic.
val exchange : 'a. 'a Portable_kernel.Subatomic.t @ local -> 'a -> 'aUpdate the current value of a subatomic, and return the previous value.
updateperforms a non-atomic read/write on an uncontended subatomic.update [@synchro atomic]andShared.updateperform an atomic read/write on a shared subatomic.
val compare_and_set :
'a. 'a Portable_kernel.Subatomic.t @ local ->
if_phys_equal_to:'a ->
replace_with:'a ->
Portable_kernel.Atomic.Compare_failed_or_set_here.tIf the current value of the subatomic is phys_equal to if_phys_equal, then update it to replace_with and return Set_here; otherwise, return Compare_failed (with the subatomic left unchanged).
compare_and_setperforms a non-atomic read/compare/write on an uncontended subatomic.compare_and_set [@synchro atomic]andShared.compare_and_setperform an atomic read/compare/write on a shared subatomic.
val compare_exchange :
'a. 'a Portable_kernel.Subatomic.t @ local ->
if_phys_equal_to:'a ->
replace_with:'a ->
'aIf the current value of the subatomic is phys_equal to if_phys_equal, then update it to replace_with and return the previous value; otherwise, return current (unchanged) value.
compare_exchangeperforms a non-atomic read/compare/write on an uncontended subatomic.compare_exchange [@synchro atomic]andShared.compare_exchangeperform an atomic read/compare/write on a shared subatomic.
val update :
'a. 'a Portable_kernel.Subatomic.t @ local ->
pure_f:('a -> 'a) @ local ->
unitUpdate t to be the result of pure_f (get t); if t is changed before the whole operation is complete, retry until success. pure_f may be called multiple times, so should be free of side effects.
updateperforms a non-atomic read/compare/write on an uncontended subatomic.pure_fstill may be called multiple times due to nonportable threading or ifpure_fis not actually pure.update [@synchro atomic]andShared.updateperform an atomic read/compare/write on a shared subatomic.
val get_and_update :
'a. 'a Portable_kernel.Subatomic.t @ local ->
pure_f:('a -> 'a) @ local ->
'aUpdate t to be the result of pure_f (get t), and return the old value; if t is changed before the whole operation is complete, retry until success. pure_f may be called multiple times, so should be free of side effects.
get_and_updateperforms a non-atomic read/compare/write on an uncontended subatomic.pure_fstill may be called multiple times due to nonportable threading or ifpure_fis not actually pure.get_and_update [@synchro atomic]andShared.get_and_updateperform an atomic read/compare/write on a shared subatomic.
val fetch_and_add : int Portable_kernel.Subatomic.t @ local -> int -> intIncrements the value of a subatomic by the given value, and return the previous value (before the increment).
fetch_and_addperforms a non-atomic update on an uncontended subatomic.fetch_and_add [@synchro atomic]andShared.fetch_and_addperform an atomic update on a shared subatomic.
val add : int Portable_kernel.Subatomic.t @ local -> int -> unitAdd the given value to the subatomic.
addperforms a non-atomic update on an uncontended subatomic.add [@synchro atomic]andShared.addperform an atomic update on a shared subatomic.
val sub : int Portable_kernel.Subatomic.t @ local -> int -> unitSubtract the given value from the subatomic.
subperforms a non-atomic update on an uncontended subatomic.sub [@synchro atomic]andShared.subperform an atomic update on a shared subatomic.
val logand : int Portable_kernel.Subatomic.t @ local -> int -> unitApply bitwise-and of the given value to the subatomic.
logandperforms a non-atomic update on an uncontended subatomic.logand [@synchro atomic]andShared.logandperform an atomic update on a shared subatomic.
val logor : int Portable_kernel.Subatomic.t @ local -> int -> unitApply bitwise-or of the given value to the subatomic.
logorperforms a non-atomic update on an uncontended subatomic.logor [@synchro atomic]andShared.logorperform an atomic update on a shared subatomic.
val logxor : int Portable_kernel.Subatomic.t @ local -> int -> unitApply bitwise-xor of the given value to the subatomic.
logxorperforms a non-atomic update on an uncontended subatomic.logxor [@synchro atomic]andShared.logxorperform an atomic update on a shared subatomic.
val incr : int Portable_kernel.Subatomic.t @ local -> unitIncrement a subatomic by 1.
incrperforms a non-atomic update on an uncontended subatomic.incr [@synchro atomic]andShared.incrperform an atomic update on a shared subatomic.
val decr : int Portable_kernel.Subatomic.t @ local -> unitDecrement a subatomic by 1.
decrperforms a non-atomic update on an uncontended subatomic.decr [@synchro atomic]andShared.decrperform an atomic update on a shared subatomic.
module Loc : sig ... endA Subatomic.Loc.t is the subatomic analog of Atomic.Loc.t, i.e. a subatomic reference to a field of a record.