package stm
- Alphabetic
- By Inheritance
- stm
- AnyRef
- Any
- Hide All
- Show All
- Public
- All
Type Members
-
trait
CommitBarrier
extends AnyRef
A
CommitBarrier
allows multiple transactions on separate threads to perform a single atomic commit.A
CommitBarrier
allows multiple transactions on separate threads to perform a single atomic commit. All of the actions performed by all of the atomic blocks executed by members of the barrier will appear to occur as a single atomic action, even though they are spread across multiple threads.Commit barriers can be used to implement transactors, where actions taken by multiple actors should be atomic as a single unit.
Because there is no ordering possible between the atomic blocks that make up a commit barrier, if those transactions conflict then the only way to avoid deadlock is to roll back all of the barrier's members. If you observe a cancel cause of
CommitBarrier.MemberCycle
then this has happened to you, and you need to run more of the logic on a single thread inside a single transaction.This abstraction is based on Multiverse's
CountDownCommitBarrier
, by Peter Veentjer. -
trait
InTxn
extends InTxnEnd
The presence of an implicit
InTxn
instance grants the caller permission to perform transactional reads and writes onRef
instances, as well as permission to callobject Txn
methods that require anInTxnEnd
.The presence of an implicit
InTxn
instance grants the caller permission to perform transactional reads and writes onRef
instances, as well as permission to callobject Txn
methods that require anInTxnEnd
.InTxn
instances themselves might be reused by the STM, useNestingLevel.current
orNestingLevel.root
to get aNestingLevel
if you need to track an individual execution attempt. -
trait
InTxnEnd
extends MaybeTxn
The presence of an implicit
InTxnEnd
instance inside a transaction life-cycle handler grants permission to call methods inobject Txn
that locate nesting levels or register additional handlers.The presence of an implicit
InTxnEnd
instance inside a transaction life-cycle handler grants permission to call methods inobject Txn
that locate nesting levels or register additional handlers. This functionality is separated from that granted byInTxn
becauseRef
operations are not allowed from handlers after commit has begun. -
trait
MaybeTxn
extends AnyRef
MaybeTxn
allows lookup of the implicitInTxn
instance without failing if theInTxn
is not known at compile time.MaybeTxn
allows lookup of the implicitInTxn
instance without failing if theInTxn
is not known at compile time.implicitly[MaybeTxn]
will bind to an implicitInTxn
if one is available, otherwise it will bind to the objectTxnUnkown
. AMaybeTxn
ofTxnUnknown
should trigger a dynamically-scopedInTxn
search usingTxn.findCurrent
. -
trait
NestingLevel
extends AnyRef
A
NestingLevel
instance describes a single attempt to execute an atomic block inside a transaction.A
NestingLevel
instance describes a single attempt to execute an atomic block inside a transaction. Reads and writes performed by a transaction will only be made visible to other threads after (if) the root nesting level commits.Methods on this class may be called from any thread, and may be called after the corresponding execution attempt has been completed.
-
class
PendingAtomicBlock
[A] extends AnyRef
Instances of
PendingAtomicBlock
defer the execution of an atomic block until all of the alternatives can be gathered from the user.Instances of
PendingAtomicBlock
defer the execution of an atomic block until all of the alternatives can be gathered from the user. There is an implicit conversion in thestm
package object from any typeA
to aPendingAtomicBlock[A]
, which will kick in if there is an attempt to call.orAtomic
on a value. -
trait
Ref
[A] extends RefLike[A, InTxn] with Source[A] with Sink[A]
Provides access to a single element of type A.
Provides access to a single element of type A. Accesses are performed as part of a memory transaction that comprises all of the operations of an atomic block and any nested blocks. Single-operation memory transactions may be performed without an explicit atomic block using the
Ref.View
returned fromsingle
. The software transactional memory performs concurrency control to make sure that all committed transactions are linearizable. Reads and writes performed by a successful transaction return the same values as if they were executed instantaneously at the transaction's commit (linearization) point.The static scope of an atomic block is defined by access to an implicit
InTxn
passed to the block by the STM. Atomic blocks nest, so to participate in an atomic block for which aInTxn
is not conveniently available, just create a new atomic block usingatomic { implicit t => // the body }
In the static scope of an atomic block reads and writes of a
Ref
are performed byx.get
andx.set(v)
, or more concisely byx()
andx() = v
.x.single
returns aRef.View
that will dynamically resolve the current scope during each method call, automatically creating a single-operation atomic block if no transaction is active.It is possible for separate
Ref
instances to refer to the same element; in this case they will compare equal. (As an example, a transactional array class might store elements in an array and createRef
s on demand.)Ref
s may be provided for computed values, such as the emptiness of a queue, to allow conditional retry and waiting on semantic properties.To perform an access outside a transaction, use the view returned by
single
. Each access through the returned view will act as if it was performed in its own single-operation transaction, dynamically nesting into an active atomic block as appropriate.Ref
's companion object contains factory methods that createRef
instances paired with a single STM-managed memory location. - trait RefCompanion extends AnyRef
-
trait
RefLike
[A, Context] extends SourceLike[A, Context] with SinkLike[A, Context]
Provides all of the operations of a
Ref[A]
, without the ability to get aRef.View
. -
trait
Sink
[-A] extends SinkLike[A, InTxn]
Sink[+A]
consists of the contra-variant write-only operations ofRef[A]
. -
trait
SinkLike
[-A, Context] extends AnyRef
Provides all of the operations of a
Sink[A]
, without the ability to get aSink.View
. -
trait
Source
[+A] extends SourceLike[A, InTxn] with TxnDebuggable
Source[+A]
consists of the covariant read-only operations ofRef[A]
. -
trait
SourceLike
[+A, Context] extends AnyRef
Provides all of the operations of a
Source[A]
, without the ability to get aSource.View
. -
trait
TArray
[A] extends TxnDebuggable
Bulk transactional storage, roughly equivalent to
Array[Ref[T]]
but potentially much more space efficient.Bulk transactional storage, roughly equivalent to
Array[Ref[T]]
but potentially much more space efficient. Elements can be read and written directly, or therefs
method can be used to obtain transientRef
instances backed by the elements of theTArray
. -
trait
TMap
[A, B] extends TxnDebuggable
A transactional map implementation that requires that all of its map-like operations be called from inside an atomic block.
A transactional map implementation that requires that all of its map-like operations be called from inside an atomic block. Rather than extending
Map
, an implicit conversion is provided fromTMap
toMap
if the current scope is part of an atomic block (seeTMap.asMap
).The keys (with type
A
) must be immutable, or at least not modified while they are in the map. TheTMap
implementation assumes that it can safely perform key equality and hash checks outside a transaction without affecting atomicity. -
trait
TSet
[A] extends TxnDebuggable
A transactional set implementation that requires that all of its set-like operations be called from inside an atomic block.
A transactional set implementation that requires that all of its set-like operations be called from inside an atomic block. Rather than extending
Set
, an implicit conversion is provided fromTSet
toSet
if the current scope is part of an atomic block (seeTSet.asSet
).The elements (with type
A
) must be immutable, or at least not modified while they are in the set. TheTSet
implementation assumes that it can safely perform equality and hash checks outside a transaction without affecting atomicity. -
trait
TxnDebuggable
extends AnyRef
This trait implements methods that can be used to examine the content of transactional data structures in a debugger with minimal modification to the behavior of the program.
This trait implements methods that can be used to examine the content of transactional data structures in a debugger with minimal modification to the behavior of the program. Normal transactional reads would add to an atomic block's read set, which could reduce the number of valid program execution orders.
dbgStr
anddbgValue
perform transactional reads, but then erase them from the enclosing transaction (if any).You can use these methods from an IDE debugger manually, by watching
x.dbgStr
orx.dbgValue
rather thanx
.If you use Eclipse, you can make this method the default view by going to Window->Preferences->Java[+]->Debug[+]->Detail Formatters and entering the code snippet
dbgStr()
(ordbgValue()
) for instances ofscala.concurrent.stm.TxnDebuggable
.If you use IntelliJ IDEA, go to File->Settings...->Debugger->Data Type Renderers and create a new renderer for
scala.concurrent.stm.TxnDebuggable
that usesdbgStr()
for rendering anddbgValue()
for node expansion. -
trait
TxnExecutor
extends AnyRef
A
TxnExecutor
is responsible for executing atomic blocks transactionally using a set of configuration parameters.A
TxnExecutor
is responsible for executing atomic blocks transactionally using a set of configuration parameters. Configuration changes are made by constructing a newTxnExecutor
usingwithConfig
orwithHint
. The new executor may be used immediately, saved and used multiple times, or registered as the new system-wide default usingTxnExecutor.transformDefault
. -
trait
TxnLocal
[A] extends RefLike[A, InTxnEnd]
TxnLocal[A]
holds an instance ofA
that is local to an atomic block.TxnLocal[A]
holds an instance ofA
that is local to an atomic block. See the factory method in the companion object for information about the life-cycle.
Value Members
-
def
atomic: TxnExecutor
Atomically executes atomic blocks using the default
TxnExecutor
.Atomically executes atomic blocks using the default
TxnExecutor
. SeeTxnExecutor.apply
. -
def
retry(implicit txn: InTxn): Nothing
Equivalent to
Txn.retry
. -
def
retryFor(timeout: Long, unit: TimeUnit = TimeUnit.MILLISECONDS)(implicit txn: InTxn): Unit
Equivalent to
Txn.retryFor(timeout, unit)
. -
implicit
def
wrapChainedAtomic[A](lhs: ⇒ A): PendingAtomicBlock[A]
This is the first half of the machinery for implementing
orAtomic
. - object CommitBarrier
- object MaybeTxn
- object NestingLevel
-
object
Ref
extends RefCompanion
object Ref
contains factory methods that allocate an STM-managed memory location and return aRef
instance that provides access to that location. - object Sink
- object Source
- object TArray
- object TMap
- object TSet
-
object
Txn
The
Txn
object provides methods that operate on the current transaction context.The
Txn
object provides methods that operate on the current transaction context. These methods are only valid within an atomic block or a transaction life-cycle handler, which is checked at compile time by requiring that an implicitInTxn
orInTxnEnd
be available. -
object
TxnExecutor
object TxnExecutor
manages the system-wide defaultTxnExecutor
. - object TxnLocal
-
object
TxnUnknown
extends MaybeTxn
An object that represents the absence of a statically-bound current transaction.
An object that represents the absence of a statically-bound current transaction.
- See also
scala.concurrent.stm.MaybeTxn