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
- Self type
-
Members list
Value members
Concrete methods
A symbolic alias for orDie
.
A symbolic alias for orDie
.
Attributes
Sequentially zips this value with the specified one, discarding the first element of the tuple.
Sequentially zips this value with the specified one, discarding the first element of the tuple.
Attributes
Sequentially zips this value with the specified one, discarding the second element of the tuple.
Sequentially zips this value with the specified one, discarding the second element of the tuple.
Attributes
Sequentially zips this value with the specified one.
Sequentially zips this value with the specified one.
Attributes
A symbolic alias for orElseEither
.
A symbolic alias for orElseEither
.
Attributes
Tries this effect first, and if it fails or retries, tries the other effect.
Tries this effect first, and if it fails or retries, tries the other effect.
Attributes
Tries this effect first, and if it enters retry, then it tries the other effect. This is an equivalent of haskell's orElse.
Tries this effect first, and if it enters retry, then it tries the other effect. This is an equivalent of haskell's orElse.
Attributes
Returns an effect that submerges the error case of an Either
into the STM
. The inverse operation of STM.either
.
Returns an effect that submerges the error case of an Either
into the STM
. The inverse operation of STM.either
.
Attributes
Maps the success value of this effect to the specified constant value.
Maps the success value of this effect to the specified constant value.
Attributes
Maps the success value of this effect to an optional value.
Maps the success value of this effect to an optional value.
Attributes
Maps the error value of this effect to an optional value.
Maps the error value of this effect to an optional value.
Attributes
Recovers from all errors.
Recovers from all errors.
Attributes
Recovers from some or all of the error cases.
Recovers from some or all of the error cases.
Attributes
Simultaneously filters and maps the value produced by this effect.
Simultaneously filters and maps the value produced by this effect.
Attributes
Simultaneously filters and flatMaps the value produced by this effect. Continues on the effect returned from pf.
Simultaneously filters and flatMaps the value produced by this effect. Continues on the effect returned from pf.
Attributes
Commits this transaction atomically.
Commits this transaction atomically.
Attributes
Commits this transaction atomically, regardless of whether the transaction is a success or a failure.
Commits this transaction atomically, regardless of whether the transaction is a success or a failure.
Attributes
Converts the failure channel into an Either
.
Converts the failure channel into an Either
.
Attributes
Executes the specified finalization transaction whether or not this effect succeeds. Note that as with all STM transactions, if the full transaction fails, everything will be rolled back.
Executes the specified finalization transaction whether or not this effect succeeds. Note that as with all STM transactions, if the full transaction fails, everything will be rolled back.
Attributes
Returns an effect that ignores errors and runs repeatedly until it eventually succeeds.
Returns an effect that ignores errors and runs repeatedly until it eventually succeeds.
Attributes
Dies with specified Throwable
if the predicate fails.
Dies with specified Throwable
if the predicate fails.
Attributes
Dies with a java.lang.RuntimeException having the specified text message if the predicate fails.
Dies with a java.lang.RuntimeException having the specified text message if the predicate fails.
Attributes
Supplies zstm
if the predicate fails.
Supplies zstm
if the predicate fails.
Attributes
Applies f
if the predicate fails.
Applies f
if the predicate fails.
Attributes
Fails with e
if the predicate fails.
Fails with e
if the predicate fails.
Attributes
Feeds the value produced by this effect to the specified function, and then runs the returned effect as well to produce its results.
Feeds the value produced by this effect to the specified function, and then runs the returned effect as well to produce its results.
Attributes
Creates a composite effect that represents this effect followed by another one that may depend on the error produced by this one.
Creates a composite effect that represents this effect followed by another one that may depend on the error produced by this one.
Attributes
Flattens out a nested STM
effect.
Flattens out a nested STM
effect.
Attributes
Unwraps the optional error, defaulting to the provided value.
Unwraps the optional error, defaulting to the provided value.
Attributes
Flips the success and failure channels of this transactional effect. This allows you to use all methods on the error channel, possibly before flipping back.
Flips the success and failure channels of this transactional effect. This allows you to use all methods on the error channel, possibly before flipping back.
Attributes
Swaps the error/value parameters, applies the function f
and flips the parameters back
Swaps the error/value parameters, applies the function f
and flips the parameters back
Attributes
Folds over the STM
effect, handling both failure and success, but not retry.
Folds over the STM
effect, handling both failure and success, but not retry.
Attributes
Effectfully folds over the STM
effect, handling both failure and success.
Effectfully folds over the STM
effect, handling both failure and success.
Attributes
Returns a successful effect with the head of the list if the list is non-empty or fails with the error None
if the list is empty.
Returns a successful effect with the head of the list if the list is non-empty or fails with the error None
if the list is empty.
Attributes
Returns a new effect that ignores the success or failure of this effect.
Returns a new effect that ignores the success or failure of this effect.
Attributes
Returns whether this transactional effect is a failure.
Returns whether this transactional effect is a failure.
Attributes
Returns whether this transactional effect is a success.
Returns whether this transactional effect is a success.
Attributes
"Zooms in" on the value in the Left
side of an Either
, moving the possibility that the value is a Right
to the error channel.
"Zooms in" on the value in the Left
side of an Either
, moving the possibility that the value is a Right
to the error channel.
Attributes
Maps the value produced by the effect.
Maps the value produced by the effect.
Attributes
Maps the value produced by the effect with the specified function that may throw exceptions but is otherwise pure, translating any thrown exceptions into typed failed effects.
Maps the value produced by the effect with the specified function that may throw exceptions but is otherwise pure, translating any thrown exceptions into typed failed effects.
Attributes
Returns an STM
effect whose failure and success channels have been mapped by the specified pair of functions, f
and g
.
Returns an STM
effect whose failure and success channels have been mapped by the specified pair of functions, f
and g
.
Attributes
Maps from one error type to another.
Maps from one error type to another.
Attributes
Returns a new effect where the error channel has been merged into the success channel to their common combined type.
Returns a new effect where the error channel has been merged into the success channel to their common combined type.
Attributes
Requires the option produced by this value to be None
.
Requires the option produced by this value to be None
.
Attributes
Converts the failure channel into an Option
.
Converts the failure channel into an Option
.
Attributes
Translates STM
effect failure into death of the fiber, making all failures unchecked and not a part of the type of the effect.
Translates STM
effect failure into death of the fiber, making all failures unchecked and not a part of the type of the effect.
Attributes
Keeps none of the errors, and terminates the fiber running the STM
effect with them, using the specified function to convert the E
into a Throwable
.
Keeps none of the errors, and terminates the fiber running the STM
effect with them, using the specified function to convert the E
into a Throwable
.
Attributes
Named alias for <>
.
Named alias for <>
.
Attributes
Returns a transactional effect that will produce the value of this effect in left side, unless it fails or retries, in which case, it will produce the value of the specified effect in right side.
Returns a transactional effect that will produce the value of this effect in left side, unless it fails or retries, in which case, it will produce the value of the specified effect in right side.
Attributes
Tries this effect first, and if it fails or retries, fails with the specified error.
Tries this effect first, and if it fails or retries, fails with the specified error.
Attributes
Returns an effect that will produce the value of this effect, unless it fails with the None
value, in which case it will produce the value of the specified effect.
Returns an effect that will produce the value of this effect, unless it fails with the None
value, in which case it will produce the value of the specified effect.
Attributes
Tries this effect first, and if it fails or retries, succeeds with the specified value.
Tries this effect first, and if it fails or retries, succeeds with the specified value.
Attributes
Named alias for <|>
.
Named alias for <|>
.
Attributes
Provides the transaction its required environment, which eliminates its dependency on R
.
Provides the transaction its required environment, which eliminates its dependency on R
.
Attributes
Transforms the environment being provided to this effect with the specified function.
Transforms the environment being provided to this effect with the specified function.
Attributes
Keeps some of the errors, and terminates the fiber with the rest.
Keeps some of the errors, and terminates the fiber with the rest.
Attributes
Keeps some of the errors, and terminates the fiber with the rest, using the specified function to convert the E
into a Throwable
.
Keeps some of the errors, and terminates the fiber with the rest, using the specified function to convert the E
into a Throwable
.
Attributes
Fail with the returned value if the PartialFunction
matches, otherwise continue with our held value.
Fail with the returned value if the PartialFunction
matches, otherwise continue with our held value.
Attributes
Continue with the returned computation if the PartialFunction
matches, translating the successful match into a failure, otherwise continue with our held value.
Continue with the returned computation if the PartialFunction
matches, translating the successful match into a failure, otherwise continue with our held value.
Attributes
Repeats this STM
effect until its result satisfies the specified predicate. '''WARNING''': repeatUntil
uses a busy loop to repeat the effect and will consume a thread until it completes (it cannot yield). This is because STM describes a single atomic transaction which must either complete, retry or fail a transaction before yielding back to the ZIO Runtime.
Repeats this STM
effect until its result satisfies the specified predicate. '''WARNING''': repeatUntil
uses a busy loop to repeat the effect and will consume a thread until it completes (it cannot yield). This is because STM describes a single atomic transaction which must either complete, retry or fail a transaction before yielding back to the ZIO Runtime.
- Use retryUntil instead if you don't need to maintain transaction state for repeats.
- Ensure repeating the STM effect will eventually satisfy the predicate.
- Consider using the Blocking thread pool for execution of the transaction.
Attributes
Repeats this STM
effect while its result satisfies the specified predicate. '''WARNING''': repeatWhile
uses a busy loop to repeat the effect and will consume a thread until it completes (it cannot yield). This is because STM describes a single atomic transaction which must either complete, retry or fail a transaction before yielding back to the ZIO Runtime.
Repeats this STM
effect while its result satisfies the specified predicate. '''WARNING''': repeatWhile
uses a busy loop to repeat the effect and will consume a thread until it completes (it cannot yield). This is because STM describes a single atomic transaction which must either complete, retry or fail a transaction before yielding back to the ZIO Runtime.
- Use retryWhile instead if you don't need to maintain transaction state for repeats.
- Ensure repeating the STM effect will eventually not satisfy the predicate.
- Consider using the Blocking thread pool for execution of the transaction.
Attributes
Filters the value produced by this effect, retrying the transaction until the predicate returns true for the value.
Filters the value produced by this effect, retrying the transaction until the predicate returns true for the value.
Attributes
Filters the value produced by this effect, retrying the transaction while the predicate returns true for the value.
Filters the value produced by this effect, retrying the transaction while the predicate returns true for the value.
Attributes
"Zooms in" on the value in the Right
side of an Either
, moving the possibility that the value is a Left
to the error channel.
"Zooms in" on the value in the Right
side of an Either
, moving the possibility that the value is a Left
to the error channel.
Attributes
Converts an option on values into an option on errors.
Converts an option on values into an option on errors.
Attributes
Extracts the optional value, or returns the given 'default'.
Extracts the optional value, or returns the given 'default'.
Attributes
Extracts the optional value, or executes the effect 'default'.
Extracts the optional value, or executes the effect 'default'.
Attributes
Extracts the optional value, or fails with the given error 'e'.
Extracts the optional value, or fails with the given error 'e'.
Attributes
Extracts the optional value, or fails with a java.util.NoSuchElementException
Summarizes a STM
effect by computing a provided value before and after execution, and then combining the values to produce a summary, together with the result of execution.
Summarizes a STM
effect by computing a provided value before and after execution, and then combining the values to produce a summary, together with the result of execution.
Attributes
"Peeks" at the success of transactional effect.
"Peeks" at the success of transactional effect.
Attributes
"Peeks" at both sides of an transactional effect.
"Peeks" at both sides of an transactional effect.
Attributes
"Peeks" at the error of the transactional effect.
"Peeks" at the error of the transactional effect.
Attributes
Maps the success value of this effect to unit.
Maps the success value of this effect to unit.
Attributes
Converts a ZSTM[R, Either[E, B], A]
into a ZSTM[R, E, Either[A, B]]
. The inverse of left
.
Converts a ZSTM[R, Either[E, B], A]
into a ZSTM[R, E, Either[A, B]]
. The inverse of left
.
Attributes
The moral equivalent of if (!p) exp
The moral equivalent of if (!p) exp
Attributes
The moral equivalent of if (!p) exp
when p
has side-effects
The moral equivalent of if (!p) exp
when p
has side-effects
Attributes
Converts a ZSTM[R, Either[B, E], A]
into a ZSTM[R, E, Either[B, A]]
. The inverse of right
.
Converts a ZSTM[R, Either[B, E], A]
into a ZSTM[R, E, Either[B, A]]
. The inverse of right
.
Attributes
Converts an option on errors into an option on values.
Converts an option on errors into an option on values.
Attributes
Updates a service in the environment of this effect.
Updates a service in the environment of this effect.
Attributes
Updates a service at the specified key in the environment of this effect.
Updates a service at the specified key in the environment of this effect.
Attributes
The moral equivalent of if (p) exp
The moral equivalent of if (p) exp
Attributes
The moral equivalent of if (p) exp
when p
has side-effects
The moral equivalent of if (p) exp
when p
has side-effects
Attributes
Same as retryUntil.
Named alias for <*>
.
Named alias for <*>
.
Attributes
Named alias for <*
.
Named alias for <*
.
Attributes
Named alias for *>
.
Named alias for *>
.
Attributes
Sequentially zips this value with the specified one, combining the values using the specified combiner function.
Sequentially zips this value with the specified one, combining the values using the specified combiner function.