View

trait View[A] extends View[A] with View[A]

Ref.View provides access to the contents of a Ref without requiring that an implicit InTxn be available. When called from within the dynamic scope of a transaction, View's methods operate as part of that transaction. When there is no transaction active View's methods are still atomic, but only for the duration of the method call.

Ref.View provides access to the contents of a Ref without requiring that an implicit InTxn be available. When called from within the dynamic scope of a transaction, View's methods operate as part of that transaction. When there is no transaction active View's methods are still atomic, but only for the duration of the method call.

A mental model of View is that view.foo(args) acts like atomic { implicit t => view.ref.foo(args) }.

trait View[A]
trait View[A]
class Object
trait Matchable
class Any

Value members

Abstract methods

def compareAndSet(before: A, after: A): Boolean

Equivalent to atomically executing (if (before == get) { set(after); true } else false), but may be more efficient, especially if there is no enclosing atomic block.

Equivalent to atomically executing (if (before == get) { set(after); true } else false), but may be more efficient, especially if there is no enclosing atomic block.

Value Params
after

a value to store if before was equal to the previous contents.

before

a value to compare against the ref's contents using the value's == method.

Returns

true if before was equal to the previous value of the viewed Ref, false otherwise.

def compareAndSetIdentity[B <: A & AnyRef](before: B, after: A): Boolean

Equivalent to atomically executing (if (before eq get) { set(after); true } else false), but may be more efficient, especially if there is no enclosing atomic block.

Equivalent to atomically executing (if (before eq get) { set(after); true } else false), but may be more efficient, especially if there is no enclosing atomic block.

Value Params
after

a value to store if before was eq to the previous contents.

before

a value to compare against the ref's contents using reference identity equality (eq).

Returns

true if before was eq to the previous value of the viewed Ref, false otherwise.

def getAndTransform(f: A => A): A

Atomically replaces the value ''v'' stored in the Ref with f(''v''), returning the old value. transform should be preferred if the return value is not needed, since it gives the STM more flexibility to avoid transaction conflicts.

Atomically replaces the value ''v'' stored in the Ref with f(''v''), returning the old value. transform should be preferred if the return value is not needed, since it gives the STM more flexibility to avoid transaction conflicts.

Value Params
f

a function that is safe to call multiple times, and safe to call later during any enclosing atomic block.

Returns

the previous value of the viewed Ref.

override def ref: Ref[A]

Returns a Ref that accesses the same memory location as this view. The returned Ref might be the original reference that was used to construct this view, or it might be a Ref that is equivalent (and ==) to the original.

Returns a Ref that accesses the same memory location as this view. The returned Ref might be the original reference that was used to construct this view, or it might be a Ref that is equivalent (and ==) to the original.

Returns

a Ref that accesses the same memory location as this view.

Definition Classes
def swap(v: A): A

Works like set(v), but returns the old value. This is an atomic swap, equivalent to atomically performing a get followed by set(v).

Works like set(v), but returns the old value. This is an atomic swap, equivalent to atomically performing a get followed by set(v).

Returns

the previous value held by ref.

def transform(f: A => A): Unit

Atomically replaces the value ''v'' stored in the Ref with f(''v''). Some Ref implementations may defer execution of f or call f multiple times to avoid transaction conflicts.

Atomically replaces the value ''v'' stored in the Ref with f(''v''). Some Ref implementations may defer execution of f or call f multiple times to avoid transaction conflicts.

Value Params
f

a function that is safe to call multiple times, and safe to call later during the enclosing atomic block, if any.

def transformAndGet(f: A => A): A

Atomically replaces the value ''v'' stored in the Ref with f(''v''), returning the new value. transform should be preferred if the return value is not needed, since it gives the STM more flexibility to avoid transaction conflicts.

Atomically replaces the value ''v'' stored in the Ref with f(''v''), returning the new value. transform should be preferred if the return value is not needed, since it gives the STM more flexibility to avoid transaction conflicts.

Value Params
f

a function that is safe to call multiple times, and safe to call later during any enclosing atomic block.

Returns

the new value of the viewed Ref.

def transformIfDefined(pf: PartialFunction[A, A]): Boolean

Atomically replaces the value ''v'' stored in the Ref with pf(''v'') if pf.isDefinedAt(''v''), returning true, otherwise leaves the element unchanged and returns false. pf.apply and pf.isDefinedAt might be invoked multiple times by the STM, and might be called later in any enclosing atomic block.

Atomically replaces the value ''v'' stored in the Ref with pf(''v'') if pf.isDefinedAt(''v''), returning true, otherwise leaves the element unchanged and returns false. pf.apply and pf.isDefinedAt might be invoked multiple times by the STM, and might be called later in any enclosing atomic block.

Value Params
pf

a partial function that is safe to call multiple times, and safe to call later during any enclosing atomic block.

Returns

pf.isDefinedAt(''v''), where ''v'' is the element held by this Ref on entry.

Concrete methods

def *=(rhs: A)(num: Numeric[A]): Unit

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 multiple the value of ref.

def +=(rhs: A)(num: Numeric[A]): Unit

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 ref.

def -=(rhs: A)(num: Numeric[A]): Unit

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 ref.

def /=(rhs: A)(num: Numeric[A]): Unit

Transforms the value stored in 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 in 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 ref.

def transformAndExtract[B](f: A => (A, B)): B

Atomically replaces the value ''v'' stored in the Ref with the first element of the 2-tuple returned by f(''v''), returning the second element.

Atomically replaces the value ''v'' stored in the Ref with the first element of the 2-tuple returned by f(''v''), returning the second element.

Value Params
f

a function that is safe to call multiple times.

Returns

the second element of the tuple returned by f.

Inherited methods

def apply(): A

Performs an atomic read of the value in ref. If an atomic block is active (see Txn.findCurrent) then the read will be performed as part of the transaction, otherwise it will act as if it was performed inside a new atomic block. Equivalent to get.

Performs an atomic read of the value in ref. If an atomic block is active (see Txn.findCurrent) then the read will be performed as part of the transaction, otherwise it will act as if it was performed inside a new atomic block. Equivalent to get.

Returns

the value of the Ref as observed by the current context.

Inherited from
View
def await(f: A => Boolean): Unit

Blocks until f(get) is true, in a manner consistent with the current context. Requires that the predicate be safe to reevaluate, and that f(x) == f(y) if x == y.

Blocks until f(get) is true, in a manner consistent with the current context. Requires that the predicate be safe to reevaluate, and that f(x) == f(y) if x == y.

v.await(f) is equivalent to

  atomic { implicit t =>
    if (!f(v.get)) retry
  }

If you want to wait for a predicate that involves more than one Ref then use retry directly.

Value Params
f

a predicate that is safe to evaluate multiple times.

Inherited from
View
def dbgStr: String
Inherited from
View
def dbgValue: Any
Inherited from
View
def get: A

Performs an atomic read; equivalent to apply().

Performs an atomic read; equivalent to apply().

Returns

the value of the Ref as observed by the current context.

Inherited from
View
def getWith[Z](f: A => Z): Z

Acts like ref.getWith(f) if there is an active transaction, otherwise just returns f(get).

Acts like ref.getWith(f) if there is an active transaction, otherwise just returns f(get).

Value Params
f

an idempotent function.

Returns

the result of applying f to the value contained in ref.

Inherited from
View
def relaxedGet(equiv: (A, A) => Boolean): A

Acts like ref.relaxedGet(equiv) if there is an active transaction, otherwise just returns get.

Acts like ref.relaxedGet(equiv) if there is an active transaction, otherwise just returns get.

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 by the active transaction, if any.

Inherited from
View
def set(v: A): Unit

Performs an atomic write; equivalent to update(v).

Performs an atomic write; equivalent to update(v).

Inherited from
View
def tryAwait(timeout: Long, unit: TimeUnit)(f: A => Boolean): Boolean

Blocks until f(get) is true and returns true, or returns false if the condition does not become true within within the specified timeout.

Blocks until f(get) is true and returns true, or returns false if the condition does not become true within within the specified timeout.

v.tryAwait(timeout)(f) is equivalent to

  atomic { implicit t =>
    f(v.get) || { retryFor(timeout) ; false }
  }
Value Params
f

a predicate that is safe to evaluate multiple times.

timeout

the maximum amount of time to wait, in units of unit.

unit

the units in which the timeout is measured, defaulting to milliseconds.

Returns

true if the predicate was satisfied, false if the wait timed out.

Inherited from
View
def trySet(v: A): Boolean

Performs an atomic 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 an atomic 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
View
def update(v: A): Unit

Performs an atomic write of the value in ref. If an atomic block is active (see Txn.findCurrent) then the write will be performed as part of the transaction, otherwise it will act as if it was performed inside a new atomic block. Equivalent to set(v).

Performs an atomic write of the value in ref. If an atomic block is active (see Txn.findCurrent) then the write will be performed as part of the transaction, otherwise it will act as if it was performed inside a new atomic block. Equivalent to set(v).

Inherited from
View