Fuzion Logo
fuzion-lang.dev — The Fuzion Language Portal
JavaScript seems to be disabled. Functionality is limited.

atomic_mutate

concur.atomic_mutate

atomic_mutate -- a multi-thread variant of mutate using atomic operations

This mutate ensures exclusive access to shared memory via lock-free atomic
operations like compare-and-swap.

In case the type of a mutable field does not permit lock-free atomic accesses,
an internal global lock is used instead to ensure these accesses are
atomic.

Read accesses using `get` are protected from reusing outdated values
by a `read_fence` while Write accesses using `put` include a `write_fence`
after writing the value to ensure the write is not delayed after any
subsequent writes.

Constructors

(T 
type
)
:
Buffer T, mutate.this,mutable_element
 is
[Inherited from  mutate]
[Private constructor]
create a mutable array.
(T 
type
)
:
Mutable_Array2 T, mutate.this,mutable_element
 is
[Inherited from  mutate]
[Private constructor]
create a mutable array with two dimensions.
(T 
type
)
:
Mutable_Array3 T, mutate.this,mutable_element
 is
[Inherited from  mutate]
[Private constructor]
create a mutable array with three dimensions.
:
Any
 is
[Inherited from  mutate]
[Private constructor]
common type for mutable data
(T 
type
, mutable_value T)
:
mutable_element,auto_unwrap T, mutate
 is
[Inherited from  mutate]
create a var mutable value with the given initial value and update the
'mutate' effect in the current environment

Functions

 => 
bool
[Inherited from  effect]
does this effect support abort?

Helps detecting unexpected calls to
`abort` at runtime and ensure that the static analysis finds that the
code executed with this effect will always return normally and produce
a result. This is used, e.g, in `mutate` to avoid static analysis
reporting `panic` as an effect of the use of a local mutate instance.
(R 
type
, E 
type
:
effect, e Lazy E)
 => 
Functional R
[Inherited from  effect]
fluent interface for using multiple effects together
(R 
type
)
 => 
Functional R
[Inherited from  effect]
this effect reinterpreted as a Functional
which allows composition of effects
 => 
String
[Inherited from  Any]
create a String from this instance. Unless redefined, `a.as_string` will
create `"instance[T]"` where `T` is the dynamic type of `a`
(T 
type
)
 => 
bool
[Inherited from  mutate]
Does the current system permit atomic accesses to values of type T?

This is typically true for ref values and for small primitive types
such as `i32`, `f64`, etc. while this is typically not the case for
more complex types such as `point(x,y i64)`.

For mutable fields created by heirs of `concur.atomic_mutate`, it will
not be possible to use `get_racy` or `put_racy` unless they are
performed on a type where these accesses are atomic.
(R 
type
, F 
type
: Typed_Function R, f F)
 => 
R
[Inherited from  Any]
dynamic_apply -- apply `f.call` to `Any.this`'s dynamic type and value

This can be used to perform operation on values depending on their dynamic
type.

Here is an example that takes a `Sequence Any` that may contain boxed values
of types `i32` and `f64`. We can now write a feature `get_f64` that extracts
these values converted to `f64` and build a function `sum` that sums them up
as follows:


 => 
Type
[Inherited from  Any]
Get the dynamic type of this instance. For value instances `x`, this is
equal to `type_of x`, but for `x` with a `ref` type `x.dynamic_type` gives
the actual runtime type, while `type_of x` results in the static
compile-time type.

There is no dynamic type of a type instance since this would result in an
endless hierarchy of types. So for Type values, dynamic_type is redefined
to just return Type.type.
(R 
type
, F 
type
: Function R, code F)
 => 
R
[Redefinition of  mutate.exclusive]
perform given code with exclusive access to the mutable values created with
this instance of `mutate`.

redefines:

 => 
bool
[Inherited from  mutate]
is instance `mutate.this` instated for type `mutate.this` and, if `mutate.this` does
not support use in several threads, check we are running in the thread `mutate.this`
was created in.

This is used a pre-condition for `exclusive` and `exclusive_when`.

Note that this is redefined in `blocking_mutate_using_clock` since it does not
require exclusivity there, but it is sufficient if that is instated.
(R 
type
, C 
type
:
Function, F 
type
: Function R, condition C, code F)
 => 
R
[Inherited from  mutate]
exclusively wait for the given condition to hold and then perform given code with
exclusive access to the mutable values created with this instance of `mutate`.
 => 
unit
[Inherited from  effect]
feature that will be called on the final instance of this effect
after it has been de-instated.

This happens either on a normal return form the code passed to
`instate`, on an abort performed on this effect or on an abort
performed on an effect instated prior to this effect's instation.

This can be used to perform actions when leaving an effect such
a cleanup up resources that were created or opened by operations
of this effect.
(R 
type
, code Function R)
 => 
R
[Inherited from  effect]
infix variant instate_self

use it like this:

(R 
type
, code Function R)
 => 
R
[Inherited from  effect]
convenience version of `instate` for effect values whose type is
exactly the effect type (i.e., the value type does not inherit
from the effect type).

Execute 'code' in a context where this effect instance has been
instated for effect type `effect.this`.

In case `f` returns normally, this will return `f`'s result.

In case `f` aborts this effect, this will panic.
 => 
bool
[Inherited from  mutate]
check if the current mutate is a child of 'conur.atomic_mutate'
 => 
bool
[Inherited from  mutate]
check if the current thread is permitted to access mutable state managed by mutate.this
exclusively.

For a single-thread mutate, this is usually the case for the same thread this mutate was
created in. Furthermore, multi-thread mutate implementations may ensure exclusive
access to other threads via redefining the `exclusive` features.
(T 
type
, I 
type
:
integer, length I, init Unary T I)
 => 
mutate.this.array T
[Inherited from  mutate]
convenience feature for creating a new array,
in some cases even with type inference
(T 
type
, length0 i64, length1 i64, init T)
 => 
container.Mutable_Array2 T mutate.this
[Inherited from  mutate]
convenience feature for creating a new array2,
in some cases even with type inference
(T 
type
, length0 i64, length1 i64, length2 i64, init T)
 => 
container.Mutable_Array3 T mutate.this
[Inherited from  mutate]
convenience feature for creating a new array3,
in some cases even with type inference
 => 
String
[Inherited from  Any]
convenience prefix operator to create a string from a value.

This permits usage of `$` as a prefix operator in a similar way both
inside and outside of constant strings: $x and "$x" will produce the
same string.
 => 
unit
[Inherited from  mutate]
Perform a read fence, i.e., enforce ordering between the previous and the next
read operations: make sure that no previous read will be swapped with the any
following read operation.

When no other synchronization mechanism is used, this is required between reads
of fields that are accessed by concurrent threads to ensure that the order seen
by the reading thread corresponds to the syntactical order the reads are
performed in.
 => 
unit
[Inherited from  effect]
replace existing effect of type `effect.this` by the new effect value `effect.this`.

This is a convenience feature for value type effects for which the type of
the effect instance equals the effect type. `ref` type effects typically have
values that may be children of the effect type that are of a different type, so
`effect_type.replace new_value` must be used.

replace may only be called during the execution of an operation of a currently instated
effect of the same effect type.
(F 
type
:
Function, condition F)
 => 
unit
[Inherited from  mutate]
wait for the given condition to become true by a mutation of the underlying data.

In case this mutate is not a multi-thread implementation and `condition()` is `false`,
this will `panic`.
 => 
unit
[Inherited from  mutate]
Perform a write fence, i.e., enforce ordering between the previous and the next
write operations: make sure that no previous write will be swapped with the any
following write operation.

When no other synchronization mechanism is used, this is required between writes
of fields that are accessed by concurrent threads to ensure that the order seen
by the reading thread corresponds to the syntactical order the writes were
performed in.

Type Features

 => 
void
[Inherited from  effect]
Abort code execution for the instated effect.this.env and return to the point
where the effect was instated.
(e effect.this.type)
 => 
void
[Inherited from  effect]
replace existing effect for type `effect.this` by the new effect value `e`
and abort code execution to return to the point where the effect was instated.
 => 
String
[Inherited from  Type]
string representation of this type to be used for debugging.

result has the form "<name>", but this might change in the future

redefines:

If this effect may be instated automatically at application start (during the
effect big bang singularity), this should be redefined to return the
corresponding effect value
 => 
Type
[Inherited from  Type]
There is no dynamic type of a type instance since this would result in an
endless hierarchy of types, so dynamic_type is redefined to just return
Type.type here.

Note: Typechecking is undecidable when 'type' is a type, Mark B. Reinhold, 1989
see: https://dspace.mit.edu/bitstream/handle/1721.1/149366/MIT-LCS-TR-458.pdf?sequence=6

redefines:

 => 
effect.this.type
[Inherited from  effect]
get this effect from env
has an effect of the given type been instated?
(T 
type
)
 => 
bool
[Inherited from  Type]
Is this type assignable to a type parameter with constraint `T`?

The result of this is a compile-time constant that can be used to specialize
code for a particular type.


it is most useful in conjunction with preconditions or `if` statements as in


or

(R 
type
, e effect.this.type, code Function R)
 => 
R
[Inherited from  effect]
execute 'code' in a context where the effect instance `e` has been
installed for effect type `effect.this`.

In case `f` returns normally, this will return `f`'s result.

In case `f` aborts this effect, this will panic.
(R 
type
, e effect.this.type, code Function R, def Unary R effect.this.type)
 => 
R
[Inherited from  effect]
execute 'code' in a context where the effect instance `e` has been
installed for effect type `effect.this`.

In case `f` returns normally, this will return `f`'s result.

In case `f` aborts this effect, return `def()`.
 => 
bool
[Inherited from  effect]
has an effect of this type been instated?
 => 
String
[Inherited from  Type]
name of this type, including type parameters, e.g. 'option (list i32)'.
 => 
String
[Inherited from  Type]
convenience prefix operator to create a string from a value.

This permits usage of `$` as a prefix operator in a similar way both
inside and outside of constant strings: $x and "$x" will produce the
same string.

NYI: Redefinition allows the type feature to be distinguished from its normal counterpart, see #3913

redefines:

(e effect.this.type)
 => 
unit
[Inherited from  effect]
replace existing effect for type `effect.this` by the new effect value `e`.

For effects that model the outside world (e.g., i/o, time, sensors and actuators, etc.),
the effect might be a unit type, so the replace is effectively a no-operation. However,
the call to `replace` is used to model the change of the outside world and must be
included for analysis tools to appreciate this.

replace may only be called during the execution of an operation of a currently instated
effect of the same effect type.

NYI: BUG: It is currently not enforced that replace is only called during the execution
of an operation of a currently instated effect of the same effect type.
 => 
Type
[Inherited from  Any]
Get a type as a value.

This is a feature with the effect equivalent to Fuzion's `expr.type` call tail.
It is recommended to use `expr.type` and not `expr.type_value`.

`type_value` is here to show how this can be implemented and to illustrate the
difference to `dynamic_type`.
0.099dev (GIT hash 1bef408f37c37f3ff3417cdf772c6c40c76064ca)
last changed: 2026-09-17