final class STM[+E, +A] extends AnyVal
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/
- Self Type
- STM[E, A]
- Alphabetic
- By Inheritance
- STM
- AnyVal
- Any
- Hide All
- Show All
- Public
- All
Value Members
-
final
def
!=(arg0: Any): Boolean
- Definition Classes
- Any
-
final
def
##(): Int
- Definition Classes
- Any
-
final
def
*>[E1 >: E, B](that: ⇒ STM[E1, B]): STM[E1, B]
Sequentially zips this value with the specified one, discarding the first element of the tuple.
-
final
def
<*[E1 >: E, B](that: ⇒ STM[E1, B]): STM[E1, A]
Sequentially zips this value with the specified one, discarding the second element of the tuple.
-
final
def
<*>[E1 >: E, B](that: ⇒ STM[E1, B]): STM[E1, (A, B)]
Sequentially zips this value with the specified one.
-
final
def
==(arg0: Any): Boolean
- Definition Classes
- Any
-
final
def
>>=[E1 >: E, B](f: (A) ⇒ STM[E1, B]): STM[E1, B]
Feeds the value produced by this effect to the specified function, and then runs the returned effect as well to produce its results.
-
final
def
asInstanceOf[T0]: T0
- Definition Classes
- Any
-
final
def
collect[B](pf: PartialFunction[A, B]): STM[E, B]
Simultaneously filters and maps the value produced by this effect.
-
final
def
commit: IO[E, A]
Commits this transaction atomically.
-
final
def
const[B](b: ⇒ B): STM[E, B]
Maps the success value of this effect to the specified constant value.
-
final
def
either: STM[Nothing, Either[E, A]]
Converts the failure channel into an
Either
. - val exec: (Journal) ⇒ TRez[E, A]
-
final
def
filter(f: (A) ⇒ Boolean): STM[E, A]
Filters the value produced by this effect, retrying the transaction until the predicate returns true for the value.
-
final
def
flatMap[E1 >: E, B](f: (A) ⇒ STM[E1, B]): STM[E1, B]
Feeds the value produced by this effect to the specified function, and then runs the returned effect as well to produce its results.
-
final
def
flatten[E1 >: E, B](implicit ev: <:<[A, STM[E1, B]]): STM[E1, B]
Flattens out a nested
STM
effect. -
final
def
fold[B](f: (E) ⇒ B, g: (A) ⇒ B): STM[Nothing, B]
Folds over the
STM
effect, handling both failure and success, but not retry. -
final
def
foldM[E1, B](f: (E) ⇒ STM[E1, B], g: (A) ⇒ STM[E1, B]): STM[E1, B]
Effectfully folds over the
STM
effect, handling both failure and success. -
def
getClass(): Class[_ <: AnyVal]
- Definition Classes
- AnyVal → Any
-
final
def
isInstanceOf[T0]: Boolean
- Definition Classes
- Any
-
final
def
map[B](f: (A) ⇒ B): STM[E, B]
Maps the value produced by the effect.
-
final
def
mapError[E1](f: (E) ⇒ E1): STM[E1, A]
Maps from one error type to another.
-
final
def
option: STM[Nothing, Option[A]]
Converts the failure channel into an
Option
. -
final
def
orElse[E1, A1 >: A](that: ⇒ STM[E1, A1]): STM[E1, A1]
Tries this effect first, and if it fails, tries the other effect.
-
final
def
orElseEither[E1 >: E, B](that: ⇒ STM[E1, B]): STM[E1, Either[A, B]]
Returns a transactional effect that will produce the value of this effect in left side, unless it fails, in which case, it will produce the value of the specified effect in right side.
-
def
toString(): String
- Definition Classes
- Any
-
final
def
unit: STM[E, Unit]
Maps the success value of this effect to unit.
-
final
def
withFilter(f: (A) ⇒ Boolean): STM[E, A]
Same as filter
-
final
def
zip[E1 >: E, B](that: ⇒ STM[E1, B]): STM[E1, (A, B)]
Named alias for
<*>
. -
final
def
zipLeft[E1 >: E, B](that: ⇒ STM[E1, B]): STM[E1, A]
Named alias for
<*
. -
final
def
zipRight[E1 >: E, B](that: ⇒ STM[E1, B]): STM[E1, B]
Named alias for
*>
. -
final
def
zipWith[E1 >: E, B, C](that: ⇒ STM[E1, B])(f: (A, B) ⇒ C): STM[E1, C]
Sequentially zips this value with the specified one, combining the values using the specified combiner function.