software transactional memory (stm) in frege

25
STM Software Transactional Memory in Frege - a purely functional JVM language Parallel 2016, Heidelberg

Upload: dierk-koenig

Post on 11-Apr-2017

585 views

Category:

Technology


4 download

TRANSCRIPT

Page 1: Software Transactional Memory (STM) in Frege

STMSoftware Transactional Memory

in Frege - a purely functional JVM language

Parallel 2016, Heidelberg

Page 2: Software Transactional Memory (STM) in Frege

Dierk König Canoo

mittie

Page 3: Software Transactional Memory (STM) in Frege

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

Silly Clock

10010 every ms

on overflow

every so often

Page 5: Software Transactional Memory (STM) in Frege

Transactional Variables

10010

CounterCounter

millissecs

TVar Int

Page 6: Software Transactional Memory (STM) in Frege

Create new TVartype Counter = TVar Int

newCounter :: STM CounternewCounter = TVar.new 0

STM action type

Page 7: Software Transactional Memory (STM) in Frege

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

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

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

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

must be STM action

Page 11: Software Transactional Memory (STM) 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 12: Software Transactional Memory (STM) 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 13: Software Transactional Memory (STM) 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 user-managed locks -> No deadlocks

Page 14: Software Transactional Memory (STM) in Frege

First one wins

isolated work commit

failretry

Page 15: Software Transactional Memory (STM) in Frege

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

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

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

Developer Discipline

Pure Functional Language

code inspection type system

Page 19: Software Transactional Memory (STM) in Frege

Pure Transactions

Page 20: Software Transactional Memory (STM) in Frege

Type inference FTW

Page 21: Software Transactional Memory (STM) 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 22: Software Transactional Memory (STM) in Frege

Classic: Ant colony

AntsFood Pheromones Evaporation Reporter

Page 23: Software Transactional Memory (STM) in Frege
Page 24: Software Transactional Memory (STM) in Frege

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

Dierk König canoo

mittie

Please give feedback!

Credits Volker Steiss master thesis Simon Peyton-Jones Beautiful concurrency