RefLike
Provides all of the operations of a Ref[A]
, without the ability to get a
Ref.View
.
Provides all of the operations of a Ref[A]
, without the ability to get a
Ref.View
.
- Authors
Nathan Bronson
Value members
Abstract methods
Works like set(v)
, but returns the old value.
Works like set(v)
, but returns the old value.
- Returns
the previous value of this
Ref
, as observed bytxn
.- Throws
- IllegalStateException
if
txn
is not active.
Transforms the value referenced by this Ref
by applying the function
f
. Acts like ref.set(f(ref.get))
, but the execution of f
may be
deferred or repeated by the STM to reduce transaction conflicts.
Transforms the value referenced by this Ref
by applying the function
f
. Acts like ref.set(f(ref.get))
, but the execution of f
may be
deferred or repeated by the STM to reduce transaction conflicts.
- Value Params
- f
a function that is safe to call multiple times, and safe to call later during the transaction.
- Throws
- IllegalStateException
if
txn
is not active.
Transforms the value ''v'' referenced by this Ref
by to
pf.apply
(''v''), but only if pf.isDefinedAt
(''v''). Returns true if
a transformation was performed, false otherwise. pf.apply
and
pf.isDefinedAt
may be deferred or repeated by the STM to reduce
transaction conflicts.
Transforms the value ''v'' referenced by this Ref
by to
pf.apply
(''v''), but only if pf.isDefinedAt
(''v''). Returns true if
a transformation was performed, false otherwise. pf.apply
and
pf.isDefinedAt
may be deferred or repeated by the STM to reduce
transaction conflicts.
- Value Params
- pf
a partial function that is safe to call multiple times, and safe to call later in the transaction.
- Returns
pf.isDefinedAt(v)
, wherev
was the value of thisRef
before transformation (if any).- Throws
- IllegalStateException
if
txn
is not active.
Concrete methods
Transforms the value stored in the Ref
by multiplying it.
Transforms the value stored in the Ref
by multiplying it.
'''Note: Implementations may choose to ignore the provided Numeric[A]
instance if A
is a primitive type.'''
- Value Params
- rhs
the quantity by which to multiply the value of this
Ref
.
- Throws
- IllegalStateException
if
txn
is not active.
Transforms the value stored in the Ref
by incrementing it.
Transforms the value stored in the Ref
by incrementing it.
'''Note: Implementations may choose to ignore the provided Numeric[A]
instance if A
is a primitive type.'''
- Value Params
- rhs
the quantity by which to increment the value of this
Ref
.
- Throws
- IllegalStateException
if
txn
is not active.
Transforms the value stored in the Ref
by decrementing it.
Transforms the value stored in the Ref
by decrementing it.
'''Note: Implementations may choose to ignore the provided Numeric[A]
instance if A
is a primitive type.'''
- Value Params
- rhs
the quantity by which to decrement the value of this
Ref
.
- Throws
- IllegalStateException
if
txn
is not active.
Transforms the value stored the Ref
by performing a division on it,
throwing away the remainder if division is not exact for instances of
type A
. The careful reader will note that division is actually
provided by Fractional[A]
or Integral[A]
, it is not defined on
Numeric[A]
. To avoid compile-time ambiguity this method accepts a
Numeric[A]
and assumes that it can be converted at runtime into
either a Fractional[A]
or an Integral[A]
.
Transforms the value stored the Ref
by performing a division on it,
throwing away the remainder if division is not exact for instances of
type A
. The careful reader will note that division is actually
provided by Fractional[A]
or Integral[A]
, it is not defined on
Numeric[A]
. To avoid compile-time ambiguity this method accepts a
Numeric[A]
and assumes that it can be converted at runtime into
either a Fractional[A]
or an Integral[A]
.
'''Note: Implementations may choose to ignore the provided Numeric[A]
instance if A
is a primitive type.'''
- Value Params
- rhs
the quantity by which to divide the value of this
Ref
.
- Throws
- IllegalStateException
if
txn
is not active.
Transforms the value referenced by this Ref
by applying the function
f
, and returns the previous value.
Transforms the value referenced by this Ref
by applying the function
f
, and returns the previous value.
- Value Params
- f
a function that is safe to call multiple times.
- Returns
the previous value of this
Ref
(the value passed tof
).- Throws
- IllegalStateException
if
txn
is not active.
Transforms the value referenced by this Ref
from ''v'' to
f
(''v'')._1
, and returns f
(''v'')._2
.
Transforms the value referenced by this Ref
from ''v'' to
f
(''v'')._1
, and returns f
(''v'')._2
.
- Value Params
- f
a function that is safe to call multiple times.
- Returns
the second element of the pair returned by
f
.- Throws
- IllegalStateException
if
txn
is not active.
Transforms the value referenced by this Ref
by applying the function
f
, and returns the new value.
Transforms the value referenced by this Ref
by applying the function
f
, and returns the new value.
- Value Params
- f
a function that is safe to call multiple times.
- Returns
the new value of this
Ref
(the value returned fromf
).- Throws
- IllegalStateException
if
txn
is not active.
Inherited methods
Performs a transactional read and checks that it is consistent with all
reads already made by txn
. Equivalent to get
.
Performs a transactional read and checks that it is consistent with all
reads already made by txn
. Equivalent to get
.
Example:
val x = Ref(0)
atomic { implicit t =>
...
val v = x() // perform a read inside a transaction
...
}
- Value Params
- txn
an active transaction.
- Returns
the value of the
Ref
as observed bytxn
.- Throws
- IllegalStateException
if
txn
is not active.
- Inherited from
- SourceLike
Performs a transactional read and checks that it is consistent with all
reads already made by txn
. Equivalent to apply()
, which is more
concise in many situations.
Performs a transactional read and checks that it is consistent with all
reads already made by txn
. Equivalent to apply()
, which is more
concise in many situations.
- Value Params
- txn
an active transaction.
- Returns
the value of the
Ref
as observed bytxn
.- Throws
- IllegalStateException
if
txn
is not active.
- Inherited from
- SourceLike
Returns f(get)
, possibly reevaluating f
to avoid rollback if a
conflicting change is made but the old and new values are equal after
application of f
. Requires that f(x) == f(y)
if x == y
.
Returns f(get)
, possibly reevaluating f
to avoid rollback if a
conflicting change is made but the old and new values are equal after
application of f
. Requires that f(x) == f(y)
if x == y
.
getWith(f)
is equivalent to f(relaxedGet({ f(_) == f(_) }))
, although
perhaps more efficient.
- Value Params
- f
an idempotent function.
- Returns
the result of applying
f
to the value contained in thisRef
.- Inherited from
- SourceLike
Returns the same value as get
, but allows the caller to determine
whether txn
should be rolled back if another thread changes the value
of this Ref
before txn
is committed. If ref.relaxedGet(equiv)
returns v0
in txn
, another context changes ref
to v1
, and
equiv(v0, v1) == true
, then txn
won't be required to roll back (at
least not due to this read). If additional changes are made to ref
additional calls to the equivalence function will be made, always with
v0
as the first parameter.
Returns the same value as get
, but allows the caller to determine
whether txn
should be rolled back if another thread changes the value
of this Ref
before txn
is committed. If ref.relaxedGet(equiv)
returns v0
in txn
, another context changes ref
to v1
, and
equiv(v0, v1) == true
, then txn
won't be required to roll back (at
least not due to this read). If additional changes are made to ref
additional calls to the equivalence function will be made, always with
v0
as the first parameter.
equiv
will always be invoked on the current thread. Extreme care
should be taken if the equivalence function accesses any Ref
s.
As an example, to perform a read that will not be validated during commit you can use the maximally permissive equivalence function:
val unvalidatedValue = ref.relaxedGet({ (_, _) => true })
To check view serializability rather than conflict serializability for a read:
val viewSerializableValue = ref.relaxedGet({ _ == _ })
The getWith
method provides related functionality.
- Value Params
- equiv
an equivalence function that returns true if a transaction that observed the first argument will still complete correctly, where the second argument is the actual value that should have been observed.
- Returns
a value of the
Ref
, not necessary consistent with the rest of the reads performed bytxn
.- Inherited from
- SourceLike
Performs a transactional write. The new value will not be visible by
any other threads until (and unless) txn
successfully commits.
Equivalent to update(v)
.
Performs a transactional write. The new value will not be visible by
any other threads until (and unless) txn
successfully commits.
Equivalent to update(v)
.
- Value Params
- v
a value to store in the
Ref
.
- Throws
- IllegalStateException
if
txn
is not active.
- Inherited from
- SinkLike
Performs a transactional write and returns true, or returns false. The STM implementation may choose to return false to reduce (not necessarily avoid) blocking. If no other threads are performing any transactional or atomic accesses then this method will succeed.
Performs a transactional write and returns true, or returns false. The STM implementation may choose to return false to reduce (not necessarily avoid) blocking. If no other threads are performing any transactional or atomic accesses then this method will succeed.
- Inherited from
- SinkLike
Performs a transactional write. The new value will not be visible by
any other threads until (and unless) txn
successfully commits.
Equivalent to set(v)
.
Performs a transactional write. The new value will not be visible by
any other threads until (and unless) txn
successfully commits.
Equivalent to set(v)
.
Example:
val x = Ref(0)
atomic { implicit t =>
...
x() = 10 // perform a write inside a transaction
...
}
- Value Params
- v
a value to store in the
Ref
.
- Throws
- IllegalStateException
if
txn
is not active.
- Inherited from
- SinkLike