OxL Reference

data TVar

A transactional variable holding a T. A cheap handle: clone() shares the variable; it may live in a static and be used from any task.

Example:

oxl✓ checked in CI
use std.stm { TVar }

fn main() io {
  let from = TVar<i64>.new(100)
  let to = TVar<i64>.new(0)
  atomic {
    from.set(from.get() - 30)
    to.set(to.get() + 30)
  }
  print("{atomic { from.get() }} {atomic { to.get() }}")
}
Output
70 30
@builtin("TVar") pub(api) data TVar<T>

Thread safety. transactional: concurrent `atomic` blocks over the same variables are serialized and retried on conflict.

Since. 1.0.0-beta

Methods

new(value: T) -> Self
A new variable with the initial value value.
get() -> T
The value (a copy) as seen by the running transaction.
set(value: T)
Replaces the value when the transaction commits.
update(f: fn(T) -> T)
set(f(get())).
swap(value: T) -> T
Sets the value and returns the previous one.
clone() -> TVar<T>
Another handle of the same variable.

Defined in: std/stm/stm.oxl:59

See also