Steve Severance Alpha Heavy Industries Motivation Innovations in concurrency has not kept pace Still using locks Immutability has helped in Haskell STM Basics Transactional Concurrency Atomicity Results are visible all at once ID: 216804
Download Presentation The PPT/PDF document "Software Transactional Memory" is the property of its rightful owner. Permission is granted to download and print the materials on this web site for personal, non-commercial use only, and to display it on your personal computer provided you do not modify the materials and that you retain all copyright notices contained in the materials. By downloading content from our website, you accept the terms of this agreement.
Slide1
Software Transactional Memory
Steve Severance – Alpha Heavy IndustriesSlide2
Motivation
Innovations in concurrency has not kept pace
Still using locks
Immutability has helped in HaskellSlide3
STM Basics
Transactional Concurrency
Atomicity – Results are visible all at once
Isolation – Action unaffected by other threads
Everything you need is in the stm-2.4.2 packageSlide4
Haskell and STM
Exploits Three Haskell Features:
Purity (Transactions must be pure)
Monads
Immutability (Values are never modified)
Utilizes the Type System
Anything involved in the transaction is marked (
TVar
,
TChan
, STM, etc…)Slide5
Basic Transaction Pattern
a
tomically $ do …
a
tomically evaluates the transaction
main :: IO ()
m
ain = do
var
<-
newTVarIO
False
atomically $ do
val
<-
readTVar
var
if
val
== False then
writeTVar
var
True else return ()Slide6
Primitives
TVar
(Always has a Value)
TMVar
(Analogous to Regular
MVar
)
TChan
(Multicast Capabilities)
TQueue
(FIFO Queue)
TBQueue
(Bounded Queue)
TSem
(Semaphore)Slide7
Retrying
Use the retry function to retry the transaction
The transaction will only be run when one of the inputs has changed
withdrawFunds
::
TVar
Account -> Money -> STM Money
withdrawFunds
accountVar
amount = do
account <-
readTVar
accountVar
if (
aBalance
account) > amount
then
writeTVar
accountVar
account{
aBalance
= (
aBalance
account) `subtract` amount} >> return amount
else retrySlide8
Choices
orElse
runs a second option if the first
retrys
withdrawEvil
::
TVar
Account ->
TChan
Account -> Money -> STM Money
withdrawEvil
accountVar
nsfChan
amount = do
withdrawFunds
accountVar
amount
`
orElse
`
withdrawNonSufficientFunds
accountVar
nsfChan
amount Slide9
Using Alternative
You always wanted to use those cool operators
Use <|> instead of
orElseSlide10
Dealing with Exceptions
Revisiting the Atomic Guarantee
No
n
eed for rollback unlike
MVar
and Chan
STM can throw and catchSlide11
Bounded Queues
TBQueue
limits its size
Writers will block until space becomes available
Useful for performance and memory managementSlide12
Being Strict
Use
NFData
(
deepseq
)
We internally use special write functions to keep work from leaking across thread boundaries
Transaction Time
writeTVar
’
::
NFData
a =>
TVar
a -> a -> STM ()
{-# INLINE
writeTVar
' #-}
writeTVar
'
var
val
=
rnf
val
`
seq
`
writeTVar
var
valSlide13
Other Languages
Clojure
Scala
C++
C#/
.NetSlide14
References
Composable Memory Transactions
Beautiful Concurrency
STM Retrospective for .Net
Gists
:
Sample 1