Download - Software Transactional Memory (STM) in Frege
![Page 1: Software Transactional Memory (STM) in Frege](https://reader031.vdocument.in/reader031/viewer/2022021500/58ecdec91a28ab4d4b8b46a1/html5/thumbnails/1.jpg)
STMSoftware Transactional Memory
in Frege - a purely functional JVM language
Parallel 2016, Heidelberg
![Page 2: Software Transactional Memory (STM) in Frege](https://reader031.vdocument.in/reader031/viewer/2022021500/58ecdec91a28ab4d4b8b46a1/html5/thumbnails/2.jpg)
Dierk König Canoo
mittie
![Page 3: Software Transactional Memory (STM) in Frege](https://reader031.vdocument.in/reader031/viewer/2022021500/58ecdec91a28ab4d4b8b46a1/html5/thumbnails/3.jpg)
Goal: make you curiousHow to program with STM
How STM works in principle
The benefits of STM
How Frege keeps you safe
![Page 4: Software Transactional Memory (STM) in Frege](https://reader031.vdocument.in/reader031/viewer/2022021500/58ecdec91a28ab4d4b8b46a1/html5/thumbnails/4.jpg)
Silly Clock
10010 every ms
on overflow
every so often
![Page 5: Software Transactional Memory (STM) in Frege](https://reader031.vdocument.in/reader031/viewer/2022021500/58ecdec91a28ab4d4b8b46a1/html5/thumbnails/5.jpg)
Transactional Variables
10010
CounterCounter
millissecs
TVar Int
![Page 6: Software Transactional Memory (STM) in Frege](https://reader031.vdocument.in/reader031/viewer/2022021500/58ecdec91a28ab4d4b8b46a1/html5/thumbnails/6.jpg)
Create new TVartype Counter = TVar Int
newCounter :: STM CounternewCounter = TVar.new 0
STM action type
![Page 7: Software Transactional Memory (STM) in Frege](https://reader031.vdocument.in/reader031/viewer/2022021500/58ecdec91a28ab4d4b8b46a1/html5/thumbnails/7.jpg)
Read & Write TVartick :: Counter -> STM ()tick counter = do value <- counter.read counter.write (value + 1)
STM action (no IO)
![Page 8: Software Transactional Memory (STM) in Frege](https://reader031.vdocument.in/reader031/viewer/2022021500/58ecdec91a28ab4d4b8b46a1/html5/thumbnails/8.jpg)
Check transaction invariantmaxTick :: Counter -> Int -> STM ()maxTick counter max = do tick counter value <- counter.read check (value <= max)
Composition !
![Page 9: Software Transactional Memory (STM) in Frege](https://reader031.vdocument.in/reader031/viewer/2022021500/58ecdec91a28ab4d4b8b46a1/html5/thumbnails/9.jpg)
Consistent updateonOverflow :: Counter->Counter->Int->STM ()onOverflow counter overflowCounter max = do value <- counter.read check (value == max) tick overflowCounter reset counter
Composition !
![Page 10: Software Transactional Memory (STM) in Frege](https://reader031.vdocument.in/reader031/viewer/2022021500/58ecdec91a28ab4d4b8b46a1/html5/thumbnails/10.jpg)
Type of reset enforcedreset :: Counter -> STM ()reset counter = counter.write 0
must be STM action
![Page 11: Software Transactional Memory (STM) in Frege](https://reader031.vdocument.in/reader031/viewer/2022021500/58ecdec91a28ab4d4b8b46a1/html5/thumbnails/11.jpg)
Atomicallyreport :: Counter -> Counter -> IO ()report millis secs = do (millisValue, secsValue) <- atomically $ do a <- millis.read b <- secs.read return (a, b) println $ show secsValue ++ " " ++ show millisValue
Transaction demarcation calls STM action inside IO action
![Page 12: Software Transactional Memory (STM) in Frege](https://reader031.vdocument.in/reader031/viewer/2022021500/58ecdec91a28ab4d4b8b46a1/html5/thumbnails/12.jpg)
Starting the threadsmain _ = do millis <- atomically newCounter secs <- atomically newCounter milliOverflow = 1000 runTicker = maxTick millis milliOverflow runSec = onOverflow millis secs milliOverflow
forkOS $ forever (atomically runTicker >> Thread.sleep 1) forkOS $ forever (atomically runSec ) forever (report millis secs >> Thread.sleep 100)
![Page 13: Software Transactional Memory (STM) in Frege](https://reader031.vdocument.in/reader031/viewer/2022021500/58ecdec91a28ab4d4b8b46a1/html5/thumbnails/13.jpg)
STMWorks like compare and swap but for all TVars inside an atomically functionplus invariants (check)plus alternatives (orElse)plus proper exception handling
No user-managed locks -> No deadlocks
![Page 14: Software Transactional Memory (STM) in Frege](https://reader031.vdocument.in/reader031/viewer/2022021500/58ecdec91a28ab4d4b8b46a1/html5/thumbnails/14.jpg)
First one wins
isolated work commit
failretry
![Page 15: Software Transactional Memory (STM) in Frege](https://reader031.vdocument.in/reader031/viewer/2022021500/58ecdec91a28ab4d4b8b46a1/html5/thumbnails/15.jpg)
Problem with locksIssue Risk
Too few locks Race conditions
Wrong lock order Deadlock
Too many locks Less throughput
Lock leakage Breaks modularity
![Page 16: Software Transactional Memory (STM) in Frege](https://reader031.vdocument.in/reader031/viewer/2022021500/58ecdec91a28ab4d4b8b46a1/html5/thumbnails/16.jpg)
Less concurreny errorsSTM replaces all your locks just like GC replaces manual memory management
Makes compositional and modular code
Is optimistic and thus works best in low contention scenarios
Is not a panacea (sorry).
![Page 17: Software Transactional Memory (STM) in Frege](https://reader031.vdocument.in/reader031/viewer/2022021500/58ecdec91a28ab4d4b8b46a1/html5/thumbnails/17.jpg)
You must notUse TVars outside a transaction
Do side effects inside a transaction no REST calls, no DB access, no file system access, no I/0 at all (time, threads, random), no object mutation, not even a „harmless“ println!
![Page 18: Software Transactional Memory (STM) in Frege](https://reader031.vdocument.in/reader031/viewer/2022021500/58ecdec91a28ab4d4b8b46a1/html5/thumbnails/18.jpg)
Developer Discipline
Pure Functional Language
code inspection type system
![Page 19: Software Transactional Memory (STM) in Frege](https://reader031.vdocument.in/reader031/viewer/2022021500/58ecdec91a28ab4d4b8b46a1/html5/thumbnails/19.jpg)
Pure Transactions
![Page 20: Software Transactional Memory (STM) in Frege](https://reader031.vdocument.in/reader031/viewer/2022021500/58ecdec91a28ab4d4b8b46a1/html5/thumbnails/20.jpg)
Type inference FTW
![Page 21: Software Transactional Memory (STM) in Frege](https://reader031.vdocument.in/reader031/viewer/2022021500/58ecdec91a28ab4d4b8b46a1/html5/thumbnails/21.jpg)
Classic: Account transferdeposit :: Account -> Int -> STM ()deposit account amount = do balance <- account.read account.write (balance + amount)
withdraw :: Account -> Int -> STM ()withdraw account amount = deposit account (- amount) -- composed
limitedWithdraw :: Account -> Int -> STM ()limitedWithdraw account amount = do withdraw account amount -- composed balance <- account.read check (balance >= 0)
transfer :: Account -> Account -> Int -> STM ()transfer from to amount = do limitedWithdraw from amount -- composed deposit to amount -- composed
![Page 22: Software Transactional Memory (STM) in Frege](https://reader031.vdocument.in/reader031/viewer/2022021500/58ecdec91a28ab4d4b8b46a1/html5/thumbnails/22.jpg)
Classic: Ant colony
AntsFood Pheromones Evaporation Reporter
![Page 23: Software Transactional Memory (STM) in Frege](https://reader031.vdocument.in/reader031/viewer/2022021500/58ecdec91a28ab4d4b8b46a1/html5/thumbnails/23.jpg)
![Page 24: Software Transactional Memory (STM) in Frege](https://reader031.vdocument.in/reader031/viewer/2022021500/58ecdec91a28ab4d4b8b46a1/html5/thumbnails/24.jpg)
Frege STM summaryNo access to TVars outside transactionsNo side effects inside transactions
TYPE SYSTEM (pure FP)Developer discipline (everybody else)
www.frege-lang.org is the only option on the JVM
![Page 25: Software Transactional Memory (STM) in Frege](https://reader031.vdocument.in/reader031/viewer/2022021500/58ecdec91a28ab4d4b8b46a1/html5/thumbnails/25.jpg)
Dierk König canoo
mittie
Please give feedback!
Credits Volker Steiss master thesis Simon Peyton-Jones Beautiful concurrency