Ref
An asynchronous, concurrent mutable reference.
Provides safe concurrent access and modification of its content, but no
functionality for synchronisation, which is instead handled by Deferred.
For this reason, a
functionality for synchronisation, which is instead handled by Deferred.
For this reason, a
Ref
is always initialised to a value.The default implementation is nonblocking and lightweight, consisting essentially
of a purely functional wrapper over an
of a purely functional wrapper over an
AtomicReference
.- Companion
- object
class Object
trait Matchable
class Any
Value members
Methods
Obtains the current value.
Since
completes immediately after being bound.
Ref
is always guaranteed to have a value, the returned actioncompletes immediately after being bound.
Sets the current value to
a
.The returned action completes after the reference has been successfully set.
Satisfies:
r.set(fa) *> r.get == fa
Updates the current value using
f
and returns the previous value.In case of retries caused by concurrent modifications,
the returned value will be the last one before a successful update.
the returned value will be the last one before a successful update.
Obtains a snapshot of the current value, and a setter for updating it.
The setter may noop (in which case
call to
The setter may noop (in which case
false
is returned) if another concurrentcall to
access
uses its setter first.Once it has noop'd or been used once, a setter never succeeds again.
Satisfies:
r.access.map(_._1) == r.get
r.access.flatMap { case (v, setter) => setter(f(v)) } == r.tryUpdate(f).map(_.isDefined)
Attempts to modify the current value once, returning
concurrent modification completes between the time the variable is
read and the time it is set.
false
if anotherconcurrent modification completes between the time the variable is
read and the time it is set.
Like
type
successfully and
tryUpdate
but allows the update function to return an output value oftype
B
. The returned action completes with None
if the value is not updatedsuccessfully and
Some(b)
otherwise.Modifies the current value using the supplied update function. If another modification
occurs between the time the current value is read and subsequently updated, the modification
is retried using the new value. Hence,
occurs between the time the current value is read and subsequently updated, the modification
is retried using the new value. Hence,
f
may be invoked multiple times.Satisfies:
r.update(_ => a) == r.set(a)
Update the value of this ref with a state computation.
The current value of this ref is used as the initial state and the computed output state
is stored in this ref after computation completes. If a concurrent modification occurs,
is stored in this ref after computation completes. If a concurrent modification occurs,
None
is returned.