trait View[A] extends Source.View[A] with Sink.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.
A mental model of View
is that view.foo(args)
acts like
atomic { implicit t => view.ref.foo(args) }
.
- Alphabetic
- By Inheritance
- View
- View
- View
- TxnDebuggable
- AnyRef
- Any
- Hide All
- Show All
- Public
- Protected
Abstract Value Members
- abstract def await(f: (A) => Boolean): Unit
Blocks until
f(get)
is true, in a manner consistent with the current context.Blocks until
f(get)
is true, in a manner consistent with the current context. Requires that the predicate be safe to reevaluate, and thatf(x) == f(y)
ifx == y
.v.await(f)
is equivalent toatomic { implicit t => if (!f(v.get)) retry }
If you want to wait for a predicate that involves more than one
Ref
then useretry
directly.- f
a predicate that is safe to evaluate multiple times.
- Definition Classes
- View
- abstract 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.- before
a value to compare against the
ref
's contents using the value's==
method.- after
a value to store if
before
was equal to the previous contents.- returns
true if
before
was equal to the previous value of the viewedRef
, false otherwise.
- abstract def compareAndSetIdentity[B <: A](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.- before
a value to compare against the
ref
's contents using reference identity equality (eq
).- after
a value to store if
before
waseq
to the previous contents.- returns
true if
before
waseq
to the previous value of the viewedRef
, false otherwise.
- abstract 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.
- Definition Classes
- View
- abstract def getAndTransform(f: (A) => A): A
Atomically replaces the value v stored in the
Ref
withf
(v), returning the old value.Atomically replaces the value v stored in the
Ref
withf
(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.- 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
.
- abstract def getWith[Z](f: (A) => Z): Z
Acts like
ref.getWith(f)
if there is an active transaction, otherwise just returnsf(get)
.Acts like
ref.getWith(f)
if there is an active transaction, otherwise just returnsf(get)
.- f
an idempotent function.
- returns
the result of applying
f
to the value contained inref
.
- Definition Classes
- View
- abstract def ref: Ref[A]
Returns a
Ref
that accesses the same memory location as this view.Returns a
Ref
that accesses the same memory location as this view. The returnedRef
might be the original reference that was used to construct this view, or it might be aRef
that is equivalent (and==
) to the original.- returns
a
Ref
that accesses the same memory location as this view.
- abstract def relaxedGet(equiv: (A, A) => Boolean): A
Acts like
ref.relaxedGet(equiv)
if there is an active transaction, otherwise just returnsget
.Acts like
ref.relaxedGet(equiv)
if there is an active transaction, otherwise just returnsget
.- 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.
- Definition Classes
- View
- abstract def set(v: A): Unit
Performs an atomic write; equivalent to
update(v)
.Performs an atomic write; equivalent to
update(v)
.- Definition Classes
- View
- abstract def swap(v: A): A
Works like
set(v)
, but returns the old value.Works like
set(v)
, but returns the old value. This is an atomic swap, equivalent to atomically performing aget
followed byset(v)
.- returns
the previous value held by
ref
.
- abstract def transform(f: (A) => A): Unit
Atomically replaces the value v stored in the
Ref
withf
(v).Atomically replaces the value v stored in the
Ref
withf
(v). SomeRef
implementations may defer execution off
or callf
multiple times to avoid transaction conflicts.- f
a function that is safe to call multiple times, and safe to call later during the enclosing atomic block, if any.
- abstract def transformAndGet(f: (A) => A): A
Atomically replaces the value v stored in the
Ref
withf
(v), returning the new value.Atomically replaces the value v stored in the
Ref
withf
(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.- 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
.
- abstract def transformIfDefined(pf: PartialFunction[A, A]): Boolean
Atomically replaces the value v stored in the
Ref
withpf
(v) ifpf.isDefinedAt
(v), returning true, otherwise leaves the element unchanged and returns false.Atomically replaces the value v stored in the
Ref
withpf
(v) ifpf.isDefinedAt
(v), returning true, otherwise leaves the element unchanged and returns false.pf.apply
andpf.isDefinedAt
might be invoked multiple times by the STM, and might be called later in any enclosing atomic block.- 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 thisRef
on entry.
- abstract def tryAwait(timeout: Long, unit: TimeUnit = TimeUnit.MILLISECONDS)(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 toatomic { implicit t => f(v.get) || { retryFor(timeout) ; false } }
- timeout
the maximum amount of time to wait, in units of
unit
.- unit
the units in which the timeout is measured, defaulting to milliseconds.
- f
a predicate that is safe to evaluate multiple times.
- returns
true if the predicate was satisfied, false if the wait timed out.
- Definition Classes
- View
- abstract def trySet(v: A): Boolean
Performs an atomic write and returns true, or returns false.
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.
- Definition Classes
- View
Concrete Value Members
- final def !=(arg0: Any): Boolean
- Definition Classes
- AnyRef → Any
- final def ##: Int
- Definition Classes
- AnyRef → Any
- def *=(rhs: A)(implicit 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 ifA
is a primitive type.- rhs
the quantity by which to multiple the value of
ref
.
- def +=(rhs: A)(implicit 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 ifA
is a primitive type.- rhs
the quantity by which to increment the value of
ref
.
- def -=(rhs: A)(implicit 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 ifA
is a primitive type.- rhs
the quantity by which to decrement the value of
ref
.
- def /=(rhs: A)(implicit 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 typeA
.Transforms the value stored in
ref
by performing a division on it, throwing away the remainder if division is not exact for instances of typeA
. The careful reader will note that division is actually provided byFractional[A]
orIntegral[A]
, it is not defined onNumeric[A]
. To avoid compile-time ambiguity this method accepts aNumeric[A]
and assumes that it can be converted at runtime into either aFractional[A]
or anIntegral[A]
.Note: Implementations may choose to ignore the provided
Numeric[A]
instance ifA
is a primitive type.- rhs
the quantity by which to divide the value of
ref
.
- final def ==(arg0: Any): Boolean
- Definition Classes
- AnyRef → Any
- def apply(): A
Performs an atomic read of the value in
ref
.Performs an atomic read of the value in
ref
. If an atomic block is active (seeTxn.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 toget
.- returns
the value of the
Ref
as observed by the current context.
- Definition Classes
- View
- final def asInstanceOf[T0]: T0
- Definition Classes
- Any
- def clone(): AnyRef
- Attributes
- protected[lang]
- Definition Classes
- AnyRef
- Annotations
- @throws(classOf[java.lang.CloneNotSupportedException]) @native() @HotSpotIntrinsicCandidate()
- def dbgStr: String
Returns a string representation of the transactional value in this instance for debugging convenience.
Returns a string representation of the transactional value in this instance for debugging convenience. The
Ref
reads (and writes) performed while constructing the result will be discarded before returning. This method works fine outside a transaction.If this method is called from within a transaction that is already doomed (status
Txn.Rolledback
), a string describing the reason for the outer transaction's rollback will be returned.- Definition Classes
- View → TxnDebuggable
- def dbgValue: Any
Returns some value that is suitable for examination in a debugger, or returns a
Txn.RollbackCause
if called from inside a doomed atomic block.Returns some value that is suitable for examination in a debugger, or returns a
Txn.RollbackCause
if called from inside a doomed atomic block.- Definition Classes
- View → TxnDebuggable
- final def eq(arg0: AnyRef): Boolean
- Definition Classes
- AnyRef
- def equals(arg0: AnyRef): Boolean
- Definition Classes
- AnyRef → Any
- final def getClass(): Class[_ <: AnyRef]
- Definition Classes
- AnyRef → Any
- Annotations
- @native() @HotSpotIntrinsicCandidate()
- def hashCode(): Int
- Definition Classes
- AnyRef → Any
- Annotations
- @native() @HotSpotIntrinsicCandidate()
- final def isInstanceOf[T0]: Boolean
- Definition Classes
- Any
- final def ne(arg0: AnyRef): Boolean
- Definition Classes
- AnyRef
- final def notify(): Unit
- Definition Classes
- AnyRef
- Annotations
- @native() @HotSpotIntrinsicCandidate()
- final def notifyAll(): Unit
- Definition Classes
- AnyRef
- Annotations
- @native() @HotSpotIntrinsicCandidate()
- final def synchronized[T0](arg0: => T0): T0
- Definition Classes
- AnyRef
- def toString(): String
- Definition Classes
- AnyRef → Any
- 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 byf
(v), returning the second element.Atomically replaces the value v stored in the
Ref
with the first element of the 2-tuple returned byf
(v), returning the second element.- f
a function that is safe to call multiple times.
- returns
the second element of the tuple returned by
f
.
- def update(v: A): Unit
Performs an atomic write of the value in
ref
.Performs an atomic write of the value in
ref
. If an atomic block is active (seeTxn.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 toset(v)
.- Definition Classes
- View
- final def wait(arg0: Long, arg1: Int): Unit
- Definition Classes
- AnyRef
- Annotations
- @throws(classOf[java.lang.InterruptedException])
- final def wait(arg0: Long): Unit
- Definition Classes
- AnyRef
- Annotations
- @throws(classOf[java.lang.InterruptedException]) @native()
- final def wait(): Unit
- Definition Classes
- AnyRef
- Annotations
- @throws(classOf[java.lang.InterruptedException])
Deprecated Value Members
- def finalize(): Unit
- Attributes
- protected[lang]
- Definition Classes
- AnyRef
- Annotations
- @throws(classOf[java.lang.Throwable]) @Deprecated
- Deprecated