zio.stm

package zio.stm

Type members

Classlikes

object STM
final class TArray[A] extends AnyVal

Wraps array of TRef and adds methods for convenience.

Wraps array of TRef and adds methods for convenience.

Companion:
object
object TArray
Companion:
class
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.

Companion:
object
object TMap
Companion:
class
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.

Companion:
object
Companion:
class
final class TPromise[E, A] extends AnyVal
Companion:
object
object TPromise
Companion:
class
final class TQueue[A]
Companion:
object
object TQueue
Companion:
class
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 managed context, and writeLock, which acquires a write lock in a managed 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.

Companion:
object
Companion:
class
object TRef
final class TSemaphore extends AnyVal
Companion:
object
object TSemaphore
Companion:
class
final class TSet[A] extends AnyVal

Transactional set implemented on top of TMap.

Transactional set implemented on top of TMap.

Companion:
object
object TSet
Companion:
class
final class ZSTM[-R, +E, +A] extends AnyVal

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/

Companion:
object
object ZSTM
Companion:
class
sealed abstract class ZTRef[+EA, +EB, -A, +B] extends Serializable

A ZTRef[EA, EB, A, B] is a polymorphic, purely functional description of a mutable reference that can be modified as part of a transactional effect. The fundamental operations of a ZTRef are set and get. set takes a value of type A and transactionally sets the reference to a new value, potentially failing with an error of type EA. get gets the current value of the reference and returns a value of type B, potentially failing with an error of type EB.

A ZTRef[EA, EB, A, B] is a polymorphic, purely functional description of a mutable reference that can be modified as part of a transactional effect. The fundamental operations of a ZTRef are set and get. set takes a value of type A and transactionally sets the reference to a new value, potentially failing with an error of type EA. get gets the current value of the reference and returns a value of type B, potentially failing with an error of type EB.

When the error and value types of the ZTRef are unified, that is, it is a ZTRef[E, E, A, A], the ZTRef also supports atomic modify and update operations. All operations are guaranteed to be executed transactionally.

NOTE: While ZTRef provides the transactional equivalent of a mutable reference, the value inside the ZTRef should be immutable. For performance reasons ZTRef 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.

Companion:
object
object ZTRef
Companion:
class

Types

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