zio.stm

package zio.stm

Members list

Concise view

Type members

Classlikes

object STM

Attributes

Graph
Supertypes
class Object
trait Matchable
class Any
Self type
STM.type
final class TArray[A] extends AnyVal

Wraps array of TRef and adds methods for convenience.

Wraps array of TRef and adds methods for convenience.

Attributes

Companion:
object
Graph
Supertypes
class AnyVal
trait Matchable
class Any
object TArray

Attributes

Companion:
class
Graph
Supertypes
class Object
trait Matchable
class Any
Self type
TArray.type
trait TDequeue[+A] extends Serializable

A transactional queue that can only be dequeued.

A transactional queue that can only be dequeued.

Attributes

Graph
Supertypes
class Object
trait Matchable
class Any
Known subtypes
trait TQueue[A]
trait TEnqueue[-A] extends Serializable

A transactional queue that can only be enqueued.

A transactional queue that can only be enqueued.

Attributes

Graph
Supertypes
class Object
trait Matchable
class Any
Known subtypes
class THub[A]
trait TQueue[A]
abstract class THub[A] extends TEnqueue[A]

A THub is a transactional message hub. Publishers can publish messages to the hub and subscribers can subscribe to take messages from the hub.

A THub is a transactional message hub. Publishers can publish messages to the hub and subscribers can subscribe to take messages from the hub.

Attributes

Companion:
object
Graph
Supertypes
trait TEnqueue[A]
class Object
trait Matchable
class Any
object THub

Attributes

Companion:
class
Graph
Supertypes
class Object
trait Matchable
class Any
Self type
THub.type
final class TMap[K, V]

Transactional map implemented on top of TRef and TArray. Resolves conflicts via chaining.

Transactional map implemented on top of TRef and TArray. Resolves conflicts via chaining.

Attributes

Companion:
object
Graph
Supertypes
class Object
trait Matchable
class Any
object TMap

Attributes

Companion:
class
Graph
Supertypes
class Object
trait Matchable
class Any
Self type
TMap.type
final class TPriorityQueue[A] extends AnyVal

A TPriorityQueue contains values of type A that an Ordering is defined on. Unlike a TQueue, take returns the highest priority value (the value that is first in the specified ordering) as opposed to the first value offered to the queue. The ordering that elements with the same priority will be taken from the queue is not guaranteed.

A TPriorityQueue contains values of type A that an Ordering is defined on. Unlike a TQueue, take returns the highest priority value (the value that is first in the specified ordering) as opposed to the first value offered to the queue. The ordering that elements with the same priority will be taken from the queue is not guaranteed.

Attributes

Companion:
object
Graph
Supertypes
class AnyVal
trait Matchable
class Any

Attributes

Companion:
class
Graph
Supertypes
class Object
trait Matchable
class Any
Self type
final class TPromise[E, A] extends AnyVal

Attributes

Companion:
object
Graph
Supertypes
class AnyVal
trait Matchable
class Any
object TPromise

Attributes

Companion:
class
Graph
Supertypes
class Object
trait Matchable
class Any
Self type
trait TQueue[A] extends TDequeue[A] with TEnqueue[A]

A TQueue is a transactional queue. Offerors can offer values to the queue and takers can take values from the queue.

A TQueue is a transactional queue. Offerors can offer values to the queue and takers can take values from the queue.

Attributes

Companion:
object
Graph
Supertypes
trait TEnqueue[A]
trait TDequeue[A]
class Object
trait Matchable
class Any
object TQueue

Attributes

Companion:
trait
Graph
Supertypes
class Object
trait Matchable
class Any
Self type
TQueue.type
trait TRandom

Attributes

Companion:
object
Graph
Supertypes
class Object
trait Matchable
class Any
object TRandom extends Serializable

Attributes

Companion:
trait
Graph
Supertypes
class Object
trait Matchable
class Any
Self type
TRandom.type
final class TReentrantLock

A TReentrantLock is a reentrant read/write lock. Multiple readers may all concurrently acquire read locks. Only one writer is allowed to acquire a write lock at any given time. Read locks may be upgraded into write locks. A fiber that has a write lock may acquire other write locks or read locks.

A TReentrantLock is a reentrant read/write lock. Multiple readers may all concurrently acquire read locks. Only one writer is allowed to acquire a write lock at any given time. Read locks may be upgraded into write locks. A fiber that has a write lock may acquire other write locks or read locks.

The two primary methods of this structure are readLock, which acquires a read lock in a scoped context, and writeLock, which acquires a write lock in a scoped context.

Although located in the STM package, there is no need for locks within STM transactions. However, this lock can be quite useful in effectful code, to provide consistent read/write access to mutable state; and being in STM allows this structure to be composed into more complicated concurrent structures that are consumed from effectful code.

Attributes

Companion:
object
Graph
Supertypes
class Object
trait Matchable
class Any

Attributes

Companion:
class
Graph
Supertypes
class Object
trait Matchable
class Any
Self type
final class TRef[A] extends Serializable

A TRef is a purely functional description of a mutable reference that can be modified as part of a transactional effect. The fundamental operations of a TRef are set and get. set transactionally sets the reference to a new value. get gets the current value of the reference.

A TRef is a purely functional description of a mutable reference that can be modified as part of a transactional effect. The fundamental operations of a TRef are set and get. set transactionally sets the reference to a new value. get gets the current value of the reference.

NOTE: While TRef provides the transactional equivalent of a mutable reference, the value inside the TRef should be immutable. For performance reasons TRef is implemented in terms of compare and swap operations rather than synchronization. These operations are not safe for mutable values that do not support concurrent access.

Attributes

Companion:
object
Graph
Supertypes
class Object
trait Matchable
class Any
Self type
TRef[A]
object TRef

Attributes

Companion:
class
Graph
Supertypes
class Object
trait Matchable
class Any
Self type
TRef.type
final class TSemaphore extends Serializable

A TSemaphore is a semaphore that can be composed transactionally. Because of the extremely high performance of ZIO's implementation of software transactional memory TSemaphore can support both controlling access to some resource on a standalone basis as well as composing with other STM data structures to solve more advanced concurrency problems.

A TSemaphore is a semaphore that can be composed transactionally. Because of the extremely high performance of ZIO's implementation of software transactional memory TSemaphore can support both controlling access to some resource on a standalone basis as well as composing with other STM data structures to solve more advanced concurrency problems.

For basic use cases, the most idiomatic way to work with a semaphore is to use the withPermit operator, which acquires a permit before executing some ZIO effect and release the permit immediately afterward. The permit is guaranteed to be released immediately after the effect completes execution, whether by success, failure, or interruption. Attempting to acquire a permit when a sufficient number of permits are not available will semantically block until permits become available without blocking any underlying operating system threads. If you want to acquire more than one permit at a time you can use withPermits, which allows specifying a number of permits to acquire. You can also use withPermitScoped or withPermitsScoped to acquire and release permits within the context of a scoped effect for composing with other resources.

For more advanced concurrency problems you can use the acquire and release operators directly, or their variants acquireN and releaseN, all of which return STM transactions. Thus, they can be composed to form larger STM transactions, for example acquiring permits from two different semaphores transactionally and later releasing them transactionally to safely synchronize on access to two different mutable variables.

Attributes

Companion:
object
Graph
Supertypes
class Object
trait Matchable
class Any
object TSemaphore

Attributes

Companion:
class
Graph
Supertypes
class Object
trait Matchable
class Any
Self type
final class TSet[A] extends AnyVal

Transactional set implemented on top of TMap.

Transactional set implemented on top of TMap.

Attributes

Companion:
object
Graph
Supertypes
class AnyVal
trait Matchable
class Any
object TSet

Attributes

Companion:
class
Graph
Supertypes
class Object
trait Matchable
class Any
Self type
TSet.type
sealed trait ZSTM[-R, +E, +A] extends Serializable

STM[E, A] represents an effect that can be performed transactionally, resulting in a failure E or a value A.

STM[E, A] represents an effect that can be performed transactionally, resulting in a failure E or a value A.

def transfer(receiver: TRef[Int],
             sender: TRef[Int], much: Int): UIO[Int] =
 STM.atomically {
   for {
     balance <- sender.get
     _       <- STM.check(balance >= much)
     _       <- receiver.update(_ + much)
     _       <- sender.update(_ - much)
     newAmnt <- receiver.get
   } yield newAmnt
 }

 val action: UIO[Int] =
   for {
     t <- STM.atomically(TRef.make(0).zip(TRef.make(20000)))
     (receiver, sender) = t
     balance <- transfer(receiver, sender, 1000)
   } yield balance

Software Transactional Memory is a technique which allows composition of arbitrary atomic operations. It is the software analog of transactions in database systems.

The API is lifted directly from the Haskell package Control.Concurrent.STM although the implementation does not resemble the Haskell one at all. http://hackage.haskell.org/package/stm-2.5.0.0/docs/Control-Concurrent-STM.html

STM in Haskell was introduced in: Composable memory transactions, by Tim Harris, Simon Marlow, Simon Peyton Jones, and Maurice Herlihy, in ACM Conference on Principles and Practice of Parallel Programming 2005. https://www.microsoft.com/en-us/research/publication/composable-memory-transactions/

See also: Lock Free Data Structures using STMs in Haskell, by Anthony Discolo, Tim Harris, Simon Marlow, Simon Peyton Jones, Satnam Singh) FLOPS 2006: Eighth International Symposium on Functional and Logic Programming, Fuji Susono, JAPAN, April 2006 https://www.microsoft.com/en-us/research/publication/lock-free-data-structures-using-stms-in-haskell/

Attributes

Companion:
object
Graph
Supertypes
class Object
trait Matchable
class Any
Self type
ZSTM[R, E, A]
object ZSTM

Attributes

Companion:
trait
Graph
Supertypes
trait Sum
trait Mirror
class Object
trait Matchable
class Any
Self type
ZSTM.type

Types

type RSTM[-R, +A] = ZSTM[R, Throwable, A]
type STM[+E, +A] = ZSTM[Any, E, A]
type TaskSTM[+A] = ZSTM[Any, Throwable, A]
type URSTM[-R, +A] = ZSTM[R, Nothing, A]
type USTM[+A] = ZSTM[Any, Nothing, A]