28
STM Software Transactional Memory in Frege - a purely functional JVM language Quickie OOP 2016

Quick into to Software Transactional Memory in Frege

Embed Size (px)

Citation preview

Page 1: Quick into to Software Transactional Memory in Frege

STMSoftware Transactional Memory

in Frege - a purely functional JVM language Quickie

OOP 2016

Page 2: Quick into to Software Transactional Memory in Frege

Dierk König Canoo

mittie

Page 3: Quick into to Software Transactional Memory in Frege

Dierk König Canoo

mittie

Page 4: Quick into to Software Transactional Memory in Frege

Silly Clock

10010

Page 5: Quick into to Software Transactional Memory in Frege

Silly Clock

10010 every ms

Page 6: Quick into to Software Transactional Memory in Frege

Silly Clock

10010 every ms

on overflow

Page 7: Quick into to Software Transactional Memory in Frege

Silly Clock

10010 every ms

on overflow

every so often

Page 8: Quick into to Software Transactional Memory in Frege

Transactional Variables

10010

CounterCounter

millissecs

TVar Int

Page 9: Quick into to Software Transactional Memory in Frege

Create new TVartype Counter = TVar Int

newCounter :: STM CounternewCounter = TVar.new 0

STM action type

Page 10: Quick into to Software Transactional Memory in Frege

Read & Write TVartick :: Counter -> STM ()tick counter = do value <- counter.read counter.write (value + 1)

STM action (no IO)

Page 11: Quick into to Software Transactional Memory in Frege

Check transaction invariantmaxTick :: Counter -> Int -> STM ()maxTick counter max = do tick counter value <- counter.read check (value <= max)

Page 12: Quick into to Software Transactional Memory in Frege

Check transaction invariantmaxTick :: Counter -> Int -> STM ()maxTick counter max = do tick counter value <- counter.read check (value <= max)

Composition !

Page 13: Quick into to Software Transactional Memory in Frege

Consistent updateonOverflow :: Counter->Counter->Int->STM ()onOverflow counter overflowCounter max = do value <- counter.read check (value == max) tick overflowCounter reset counter

Composition !

Page 14: Quick into to Software Transactional Memory in Frege

Type of reset enforcedreset :: Counter -> STM ()reset counter = counter.write 0

must be STM action

Page 15: Quick into to Software Transactional Memory in Frege

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 16: Quick into to Software Transactional Memory in Frege

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 17: Quick into to Software Transactional Memory in Frege

STMWorks like compare and swap but for all TVars inside an atomically functionplus invariants (check)plus alternatives (orElse)plus proper exception handling

No locks -> No deadlocks

Page 18: Quick into to Software Transactional Memory in Frege

First one wins

isolated work commit

failretry

Page 19: Quick into to Software Transactional Memory in Frege

STMReplaces 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 20: Quick into to Software Transactional Memory in Frege

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 21: Quick into to Software Transactional Memory in Frege

Classic: Ant colony

AntsFood Pheromones Evaporation Reporter

Page 22: Quick into to Software Transactional Memory in Frege
Page 23: Quick into to Software Transactional Memory in Frege

STM relies upon

Page 24: Quick into to Software Transactional Memory in Frege

STM relies uponNo access to TVars outside transactionsNo side effects inside transactions

Page 25: Quick into to Software Transactional Memory in Frege

STM relies uponNo access to TVars outside transactionsNo side effects inside transactions

TYPE SYSTEM (pure FP)Developer discipline (everybody else)

Page 26: Quick into to Software Transactional Memory in Frege

STM relies uponNo 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 27: Quick into to Software Transactional Memory in Frege

Dierk König canoo

mittie

Credits Volker Steiss master thesis Simon Peyton-Jones Beautiful concurrency

Page 28: Quick into to Software Transactional Memory in Frege

Dierk König canoo

mittie

Please give feedback!

Credits Volker Steiss master thesis Simon Peyton-Jones Beautiful concurrency