The stm
module implements a
software transactional memory system.
The functions exported by this module are documented in the following sections.
stm.var(init_val = false, group = false)
Return a new transactional variable initialized to init_val
.
A transactional variable is usually read and written within the context of
a running transaction.
The optional argument group
specifies the transaction group that will be associated with this
variable. A program may choose to group together transaction variables to improve performance.
stm.read(tvar, txn)
Return the value of the transaction variable tvar
as seen in the context of the transaction txn
.
stm.read_enforce(tvar, txn)
Return the value that was last committed to the transaction variable tvar
. This function should be called only if the
transaction txn
wants to make sure tvar
meets some constraints before a new value is written to it.
stm.write(tvar, new_val, txn)
Write the value new_val
to the transaction variable tvar
. Return void
.
This new value will be visible only within the transaction
txn
. The new value will be made visible to the rest of the system only after the transaction has successfully committed
tvar
.
stm.peek(tvar)
Return the value of the transaction variable tvar
that was last committed. This function may return an inconsistent value.
stm.transact(fn)
Call the function fn
with a new transaction as its argument. Return the value of fn
.
let t = stm.var(100)
!stm.transact(^(txn) stm.write(t, stm.read(t, txn) + 10, txn))
task_sleep(2)
stm.peek(t)
// 110
stm.abort(txn, val = false)
Abort the transaction txn
and return val
.
All transactions (at any level of "nesting") must be explicitly aborted.
stm.retry(txn)
Restart the transaction. All changes made to local transaction variables will be reset.
All transactions (at any level of "nesting") must be explicitly restarted.
stm.group(@optional name = false)
Return a new transaction group with the given name.