so#ware transac-onal memory · – stm a == a computaon producing a value with type a that does...

65
So#ware Transac-onal Memory COS 326 David Walker Princeton University

Upload: others

Post on 30-Dec-2020

3 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: So#ware Transac-onal Memory · – STM a == a computaon producing a value with type a that does transac-onal memory book keeping on the side – Haskell type system ensures TVars

So#wareTransac-onalMemory

COS326DavidWalker

PrincetonUniversity

Page 2: So#ware Transac-onal Memory · – STM a == a computaon producing a value with type a that does transac-onal memory book keeping on the side – Haskell type system ensures TVars

LearnYouaHaskell!

hFp://learnyouahaskell.com/

Page 3: So#ware Transac-onal Memory · – STM a == a computaon producing a value with type a that does transac-onal memory book keeping on the side – Haskell type system ensures TVars

Hardware

Concurrency primitives

Library Library Library

Library

LibraryLibrary

Library

Libraries build layered concurrency abstractions

Whatwewant

Page 4: So#ware Transac-onal Memory · – STM a == a computaon producing a value with type a that does transac-onal memory book keeping on the side – Haskell type system ensures TVars

Immutable Data

FirstIdea:Don’tUseMutableData/Effects

Library Library

Library

Hardware

Library Library

Library

Good:•  no race conditions, •  no deadlock•  interleavings don’t matter•  deterministic•  equivalent to sequential code it looks pretty to boot

Bad:•  Can’t interact with the

world.•  The world changes.•  Threads can’t talk back and

forth.

Page 5: So#ware Transac-onal Memory · – STM a == a computaon producing a value with type a that does transac-onal memory book keeping on the side – Haskell type system ensures TVars

SecondOp-on:Locks

let deposit (a:account) (amount:int) : unit = with_lock a.lock (fun () -> if a.bal + amount < max_balance then a.bal <- a.bal + amount))

let with_lock l f = Mutex.lock l; let res = try f () with exn -> (Mutex.unlock l; raise exn) in Mutex.unlock l; res

Associatealockwitheachmutableobject.Acquire&releasesurroundingeachaccessReasonasifasequenceofinstruc-onsoccurssequen-ally.

Page 6: So#ware Transac-onal Memory · – STM a == a computaon producing a value with type a that does transac-onal memory book keeping on the side – Haskell type system ensures TVars

But....Managingmul-plemutableobjectsgotreallyhardbecauseyoucan'teasilyputtwogoodprogramcomponentstogethertomakeanothergoodprogramcomponent:

let withdraw (a:account) = ... let deposit (a:account) = ... let transfer (a:account)(b:account)(amt:float) = withdraw a amt; deposit b amt

otherthreadscanseeabadbalancevalueinbetweenwithdrawalanddeposit

hugeproblem:programmerss-llhavetothinkaboutallpossibleinterleavings

Page 7: So#ware Transac-onal Memory · – STM a == a computaon producing a value with type a that does transac-onal memory book keeping on the side – Haskell type system ensures TVars

And....Managingmul-plemutableobjectsgotreallyhardbecauseprogramsthatuselocksjustdon'tcomposeverywell:

let pop_two (s1:‘a stack) (s2:‘a stack) : (‘a * ‘a) option = with_lock s1.lock (fun _ -> with_lock s2.lock (fun _ -> match no_lock_pop s1, no_lock_pop s2 with | Some x, Some y -> Some (x,y) | Some x, None -> no_lock_push s1 x ; None | None, Some y -> no_lock_push s2 y ; None))

AndwehadtoworryaboutforgeZngtoacquirelocksorcrea-ngdeadlocks...

Page 8: So#ware Transac-onal Memory · – STM a == a computaon producing a value with type a that does transac-onal memory book keeping on the side – Haskell type system ensures TVars

SecondIdea:UseLocksorMPI

Hardware

Library Library

LibraryLibrar

y

Locks

Locks/MPI �(a) are hard to reason about(c) have lots of error modes

“Buildingcomplexparallelprogramsislikebuildingaskyscraperoutofbananas.”--SimonPeytonJones

Page 9: So#ware Transac-onal Memory · – STM a == a computaon producing a value with type a that does transac-onal memory book keeping on the side – Haskell type system ensures TVars

TheProblem

LocksareanindirectwayatgeZngtowhatwereallywant:

Howmightwedesignalanguageprimi-vethatencapsulatesthisno-on?

a; b; c; d;

executetheseinstruc-onsinorderwithoutinterrup-onbyotherthreads

Page 10: So#ware Transac-onal Memory · – STM a == a computaon producing a value with type a that does transac-onal memory book keeping on the side – Haskell type system ensures TVars

ThirdIdea:AtomicBlocks

Atomic blocks

Library Library Library

Library

LibraryLibrary

Library

Hardware

Atomic blocks are much easier to use, and do compose

Atomic blocks are pieces of code that you can count on to operate exactly like sequential programs

Tricky gaps, so a little harder than immutable data but you can do more stuff

Page 11: So#ware Transac-onal Memory · – STM a == a computaon producing a value with type a that does transac-onal memory book keeping on the side – Haskell type system ensures TVars

AtomicBlocksCutDownNondeterminism

ac-on1: ac-on2:

read x write x read x write x

read x write x read x write x

Page 12: So#ware Transac-onal Memory · – STM a == a computaon producing a value with type a that does transac-onal memory book keeping on the side – Haskell type system ensures TVars

read x write x read x write x

read x write x read x write x

ac-on1: ac-on2:

withtransac-ons:

orread x write x read x write x

read x write x read x write x

read x write x read x write x

read x write x read x write x

withoutatomictransac-ons:vastlymorepossibleinterleavings

read x

write x

read x

write x

read x

write x

read x

write x

AtomicBlocksCutDownNondeterminism

Page 13: So#ware Transac-onal Memory · – STM a == a computaon producing a value with type a that does transac-onal memory book keeping on the side – Haskell type system ensures TVars

STMINHASKELL

Page 14: So#ware Transac-onal Memory · – STM a == a computaon producing a value with type a that does transac-onal memory book keeping on the side – Haskell type system ensures TVars

ConcurrentThreadsinHaskell

main = do

id <- fork action1 action2 ...

fork :: IO a -> IO ThreadId

ac-on1andac-on2inparallel

Page 15: So#ware Transac-onal Memory · – STM a == a computaon producing a value with type a that does transac-onal memory book keeping on the side – Haskell type system ensures TVars

AtomicBlocksinHaskell

main = do id <- fork (atomic action1) atomic action2 ...

Idea: add a function atomic that guarantees atomic execution of a suspended (effectful) computation

ac-on1andac-on2atomicandparallel

Page 16: So#ware Transac-onal Memory · – STM a == a computaon producing a value with type a that does transac-onal memory book keeping on the side – Haskell type system ensures TVars

read x write x read x write x

read x write x read x write x

ac-on1: ac-on2:

withtransac-ons:

orread x write x read x write x

read x write x read x write x

read x write x read x write x

read x write x read x write x

main = do id <- fork (atomic action1) atomic action2 ...

AtomicBlocksinHaskell

Page 17: So#ware Transac-onal Memory · – STM a == a computaon producing a value with type a that does transac-onal memory book keeping on the side – Haskell type system ensures TVars

RecallMonadsKeyidea:

–  Monadictypingconstrainstheuseofeffec5ulopera6ons

•  int->int:–  cannotaccessareference–  cannotdooIOsoyouknowthatthatfunc-onispure

•  int->IOint:–  returnsasuspendedcomputa-onthatdoesaccessreferences–  IOintcomputa-oncanbecomposedwithothercomputa-ons

Wewilldothesamethingtoimplementtransac-ons.Newkindofreferencethatcanonlybeaccessedinsideanatomicblock

Page 18: So#ware Transac-onal Memory · – STM a == a computaon producing a value with type a that does transac-onal memory book keeping on the side – Haskell type system ensures TVars

AtomicDetails

Introduceatypeforimpera-vetransac-onvariables(TVar)andanewMonad(STM)totracktransac-ons.–  STMa==acomputa-onproducingavaluewithtypeathatdoes

transac-onalmemorybookkeepingontheside–  HaskelltypesystemensuresTVarscanonlybemodifiedintransac-ons

withtypeSTMa•  justlikeHaskellrefscanonlybeusedinsidecomputa-onswithtypeIOa

atomic :: STM a -> IO a new :: a -> STM (TVar a) read :: TVar a -> STM a write :: TVar a -> a -> STM ()

TVar a == ‘a ref

Haskell OCaml

Page 19: So#ware Transac-onal Memory · – STM a == a computaon producing a value with type a that does transac-onal memory book keeping on the side – Haskell type system ensures TVars

AtomicExample

-- inc adds 1 to the mutable reference r

inc :: TVar Int -> STM ()

inc r = do

v <- read r

write r (v+1)

main = do

r <- atomic (new 0) fork (atomic (inc r)) atomic (inc r);

Page 20: So#ware Transac-onal Memory · – STM a == a computaon producing a value with type a that does transac-onal memory book keeping on the side – Haskell type system ensures TVars

AtomicExample

-- inc adds 1 to the mutable reference r

inc :: TVar Int -> STM ()

inc r = do

v <- read r

write r (v+1)

main = do

r <- atomic (new 0) fork (atomic (inc r)) atomic (inc r);

Haskellislazysothesecomputa-onsaresuspendedandexecutedwithintheatomicblock

Page 21: So#ware Transac-onal Memory · – STM a == a computaon producing a value with type a that does transac-onal memory book keeping on the side – Haskell type system ensures TVars

STMinHaskell

TheSTMmonadincludesaspecificsetofopera-ons:•  Can’tuseTVarsoutsideatomicblock•  Can’tdoIOinsideatomicblock:

•  atomicisafunc-on,notasyntac-cconstruct–  calledatomicallyintheactualimplementa-on

•  ...and,bestofall...

atomic :: STM a -> IO a new :: a -> STM (TVar a) read :: TVar a -> STM a write :: TVar a -> a -> STM()

atomic (if x<y then launchMissiles)

Page 22: So#ware Transac-onal Memory · – STM a == a computaon producing a value with type a that does transac-onal memory book keeping on the side – Haskell type system ensures TVars

STMComputa-onsCompose

ThetypeguaranteesthatanSTMcomputa-onisalwaysexecutedatomically.–  GluemanySTM

computa-onstogetherinsidea“do”block

–  ThenwrapwithatomictoproduceanIOac-on.

inc :: TVar Int -> STM ()

inc r = do

v <- read r write r (v+1)

inc :: TVar Int -> STM () inc2 r = do

inc r

inc r

inc :: TVar Int -> STM () foo = atomic (inc2 r)

Composi6onisTHEwaytobuildbigprogramsthatwork

Page 23: So#ware Transac-onal Memory · – STM a == a computaon producing a value with type a that does transac-onal memory book keeping on the side – Haskell type system ensures TVars

Excep-ons

let with_lock l f = Mutex.lock l; let res = try f () with exn -> (Mutex.unlock l; raise exn) in Mutex.unlock l; res

whenexcep-onsgetthrown,weareo#eninthemidstofsomecomplexac-on.here,wemustunlockthelocktogetbacktotheini-alstatemoregenerally,wemighthavemutatedmanypiecesofstateandmust"undo"ourchanges

Page 24: So#ware Transac-onal Memory · – STM a == a computaon producing a value with type a that does transac-onal memory book keeping on the side – Haskell type system ensures TVars

TheSTMmonadsupportsexcep-ons:

Inthecall(atomics),ifsthrowsanexcep-on,thetransac6onisabortedwithnoeffectandtheexcep-onispropagatedtotheenclosingcode.Noneedtorestoreinvariants,orreleaselocks!(yous-llneedtodealwiththeexcep-on...)

throw :: Exception -> STM a catch :: STM a ->(Exception -> STM a)-> STM a

Excep-ons

Page 25: So#ware Transac-onal Memory · – STM a == a computaon producing a value with type a that does transac-onal memory book keeping on the side – Haskell type system ensures TVars

Starva-on

Worry:Couldthesystem“thrash”bycon-nuallycollidingandre-execu-ng?No:Atransac-oncanbeforcedtore-executeonlyifanothersucceedsincommiZng.Thatgivesastrongprogressguarantee.But:Apar-cularthreadcouldstarve:

Thread 1Thread 2Thread 3

Page 26: So#ware Transac-onal Memory · – STM a == a computaon producing a value with type a that does transac-onal memory book keeping on the side – Haskell type system ensures TVars

THREEMOREIDEAS:RETRY,ORELSE,ALWAYS

Page 27: So#ware Transac-onal Memory · – STM a == a computaon producing a value with type a that does transac-onal memory book keeping on the side – Haskell type system ensures TVars

Idea1:Composi-onalBlocking

•  retrymeans“abortthecurrenttransac-onandre-executeitfromthebeginning”.

•  Implementa-onavoidsearlyretryusingreadsinthetransac-onlog(i.e.acc)towaitonallreadvariables.–  ie:retryonlyhappenswhenoneofthevariablesreadonthepathto

theretrychanges

withdraw :: TVar Int -> Int -> STM () withdraw acc n =

do bal <- readTVar acc if bal < n then retry writeTVar acc (bal-n)

retry :: STM ()

Page 28: So#ware Transac-onal Memory · – STM a == a computaon producing a value with type a that does transac-onal memory book keeping on the side – Haskell type system ensures TVars

Composi-onalBlocking

•  Retryingthreadiswokenupautoma-callywhenacciswriFen,sothereisnodangerofforgoFenno-fies.

•  NodangerofforgeZngtotestcondi-onsagainwhenwokenupbecausethetransac-onrunsfromthebeginning.

•  Correct-by-construc6ondesign!

withdraw :: TVar Int -> Int -> STM () withdraw acc n =

do { bal <- readTVar acc; if bal < n then retry; writeTVar acc (bal-n) }

Page 29: So#ware Transac-onal Memory · – STM a == a computaon producing a value with type a that does transac-onal memory book keeping on the side – Haskell type system ensures TVars

WhatmakesRetryComposi-onal?

retrycanappearanywhereinsideanatomicblock,includingnesteddeepwithinacall.Forexample,

waitsfor:•  a1balance>3•  anda2balance>7•  withoutanychangetowithdrawfunc6on.

atomic (do { withdraw a1 3; withdraw a2 7 })

Page 30: So#ware Transac-onal Memory · – STM a == a computaon producing a value with type a that does transac-onal memory book keeping on the side – Haskell type system ensures TVars

Idea2:Choice

Supposewewanttotransfer3dollarsfromeitheraccounta1ora2intoaccountb.

orElse :: STM a -> STM a -> STM a

atomic (

do (withdraw a1 3) `orElse` (withdraw a2 3)

deposit b 3

)

Try this ...and if it retries, try this

then afterward, do this

Page 31: So#ware Transac-onal Memory · – STM a == a computaon producing a value with type a that does transac-onal memory book keeping on the side – Haskell type system ensures TVars

Choiceiscomposable,too!

transfer :: TVar Int -> TVar Int -> TVar Int -> STM ()

transfer a1 a2 b = do withdraw a1 3 `orElse` withdraw a2 3 deposit b 3

atomic ( transfer a1 a2 b

`orElse` transfer a3 a4 b )

Thefunc-ontransfercallsorElse,butcallstotransfercans-llbecomposedwithorElse.

Page 32: So#ware Transac-onal Memory · – STM a == a computaon producing a value with type a that does transac-onal memory book keeping on the side – Haskell type system ensures TVars

ComposingTransac-ons

•  Atransac-onisavalueoftypeSTMa.•  Transac-onsarefirst-classvalues.•  Buildabigtransac-onbycomposingliFletransac-ons:insequence,usingorElseandretry,insideprocedures....

•  Finallysealupthetransac-onwithatomic::STMa->IOa

Page 33: So#ware Transac-onal Memory · – STM a == a computaon producing a value with type a that does transac-onal memory book keeping on the side – Haskell type system ensures TVars

Equa-onalReasoning

STM supports nice equations for reasoning:

a `orElse` (b `orElse` c) == (a `orElse` b) `orElse` sretry `orElse` s == ss `orElse` retry == s�

These equations make STM an instance of a structure known as a MonadPlus -- a Monad with some extra operations and properties.

Page 34: So#ware Transac-onal Memory · – STM a == a computaon producing a value with type a that does transac-onal memory book keeping on the side – Haskell type system ensures TVars

Idea3:Invariants

Theroutetosanityistoestablishinvariantsthatareassumedonentry,andguaranteedonexit,byeveryatomicblock.–  muchlikeinamodulewithrepresenta6oninvariants–  thisgivesyoulocalreasoningaboutyourcode

•  Wewanttochecktheseguarantees.Butwedon’twanttotesteveryinvarianta#ereveryatomicblock.

•  Hmm....Onlytestwhensomethingreadbytheinvarianthaschanged....ratherlikeretry.

Page 35: So#ware Transac-onal Memory · – STM a == a computaon producing a value with type a that does transac-onal memory book keeping on the side – Haskell type system ensures TVars

Invariants:OneNewPrimi-ve

always :: STM Bool -> STM ()

newAccount :: STM (TVar Int)

newAccount = do { r <- new 0; always (accountInv r); return v }

accountInv r = do { x <- read r; return (x >= 0)};

An arbitrary boolean valued STM computation

Any transaction that modifies the account will check the invariant (no forgotten checks). If the check fails, the transaction restarts. A persistent assert!!

Page 36: So#ware Transac-onal Memory · – STM a == a computaon producing a value with type a that does transac-onal memory book keeping on the side – Haskell type system ensures TVars

Whatalwaysdoes

•  Thefunc-onalwaysaddsanewinvarianttoaglobalpoolofinvariants.

•  Conceptually,everyinvariantischeckedaseverytransac-oncommits.

•  Buttheimplementa-onchecksonlyinvariantsthatreadTVarsthathavebeenwriFenbythetransac-on

•  ...andgarbagecollectsinvariantsthatarecheckingdeadTvars.

always :: STM Bool -> STM ()

Page 37: So#ware Transac-onal Memory · – STM a == a computaon producing a value with type a that does transac-onal memory book keeping on the side – Haskell type system ensures TVars

Whatdoesitallmean?•  Everythingsofarisintui-veandarm-wavey.•  Butwhathappensifit’sraining,andyouareinsideanorElse

andyouthrowanexcep-onthatcontainsavaluethatmen-ons...?

•  Weneedaprecisespecifica-on!

Page 38: So#ware Transac-onal Memory · – STM a == a computaon producing a value with type a that does transac-onal memory book keeping on the side – Haskell type system ensures TVars

No way to wait for complex conditions

One exists

See “Composable Memory Transactions” for details.

Take COS 510 to understand what it means!

Page 39: So#ware Transac-onal Memory · – STM a == a computaon producing a value with type a that does transac-onal memory book keeping on the side – Haskell type system ensures TVars

HASKELLIMPLEMENTATION

Page 40: So#ware Transac-onal Memory · – STM a == a computaon producing a value with type a that does transac-onal memory book keeping on the side – Haskell type system ensures TVars

Implementa-on

Anaiveimplementa-on(c.f.databases):– Everyloadandstoreinstruc-onlogsinforma-onintoathread-locallog.

– Astoreinstruc-onwritesthelogonly.– Aloadinstruc-onconsultsthelogfirst.– Validatethelogattheendoftheblock.

•  Ifsucceeds,atomicallycommittosharedmemory.•  Iffails,restartthetransac-on.

Page 41: So#ware Transac-onal Memory · – STM a == a computaon producing a value with type a that does transac-onal memory book keeping on the side – Haskell type system ensures TVars

StateoftheArtCirca2003No

rmal

ised

exec

ution

time

Sequential baseline (1.00x)

Coarse-grained locking (1.13x)

Fine-grained locking (2.57x)

Traditional STM (5.69x)

Workload: operations on a red-black tree, 1 thread, 6:1:1

lookup:insert:delete mix with keys 0..65535

See “Optimizing Memory Transactions” for more information.

Page 42: So#ware Transac-onal Memory · – STM a == a computaon producing a value with type a that does transac-onal memory book keeping on the side – Haskell type system ensures TVars

NewImplementa-onTechniquesDirect-updateSTM

–  Allowstransac-onstomakeupdatesinplaceintheheap–  Avoidsreadsneedingtosearchthelogtoseeearlierwritesthatthe

transac-onhasmade–  Makessuccessfulcommitopera-onsfasteratthecostofextraworkon

conten-onorwhenatransac-onaborts

Compilerintegra-on–  Decomposetransac-onalmemoryopera-onsintoprimi-ves–  Exposetheseprimi-vestocompilerop-miza-on(e.g.tohoist

concurrencycontrolopera-onsoutofaloop)

Run-mesystemintegra-on–  Integratestransac-onswiththegarbagecollectortoscaletoatomic

blockscontaining100Mmemoryaccesses

Page 43: So#ware Transac-onal Memory · – STM a == a computaon producing a value with type a that does transac-onal memory book keeping on the side – Haskell type system ensures TVars

Results:ConcurrencyControlOverheadNo

rmal

ised

exec

ution

time

Sequential baseline (1.00x)

Coarse-grained locking (1.13x)

Fine-grained locking (2.57x)

Direct-update STM (2.04x)

Direct-update STM + compiler integration

(1.46x)

Traditional STM (5.69x)

Scalable to multicore

Workload: operations on a red-black tree, 1 thread, 6:1:1

lookup:insert:delete mix with keys 0..65535

Page 44: So#ware Transac-onal Memory · – STM a == a computaon producing a value with type a that does transac-onal memory book keeping on the side – Haskell type system ensures TVars

Results:Scalability(forsomebenchmark;yourexperiencemayvary)

#threads

Fine-grained locking

Direct-update STM + compiler integration

Traditional STM

Coarse-grained locking

Micro

seco

nds

per

oper

ation

Page 45: So#ware Transac-onal Memory · – STM a == a computaon producing a value with type a that does transac-onal memory book keeping on the side – Haskell type system ensures TVars

Performance,SummaryNaïveSTMimplementa-onishopelesslyinefficient.Thereisalotofresearchgoingoninthecompilerandarchitecturecommuni-estoop-mizeSTM.

–  hardware-supportedSTM

Page 46: So#ware Transac-onal Memory · – STM a == a computaon producing a value with type a that does transac-onal memory book keeping on the side – Haskell type system ensures TVars

STMWRAPUP

Page 47: So#ware Transac-onal Memory · – STM a == a computaon producing a value with type a that does transac-onal memory book keeping on the side – Haskell type system ensures TVars

STMinMainstreamLanguages

TherearesimilarproposalsforaddingSTMtoJavaandothermainstreamlanguages.

class Account { float balance; void deposit(float amt) { atomic { balance += amt; } } void withdraw(float amt) { atomic { if(balance < amt) throw new OutOfMoneyError(); balance -= amt; } } void transfer(Acct other, float amt) { atomic { // Can compose withdraw and deposit. other.withdraw(amt); this.deposit(amt); } } }

Page 48: So#ware Transac-onal Memory · – STM a == a computaon producing a value with type a that does transac-onal memory book keeping on the side – Haskell type system ensures TVars

WeakvsStrongAtomicity

•  UnlikeHaskell,typesystemsinmainstreamlanguagesdon’tcontrolwhereeffectsoccur.

•  Whathappensifcodeoutsideatransac-onconflictswithcodeinsideatransac-on?– WeakAtomicity:Non-transac-onalcodecanseeinconsistentmemorystates.Programmershouldavoidsuchsitua-onsbyplacingallaccessestosharedstateintransac-on.

– StrongAtomicity:Non-transac-onalcodeisguaranteedtoseeaconsistentviewofsharedstate.Thisguaranteemaycauseaperformancehit.For more information: “Enforcing Isolation and Ordering in STM”

Page 49: So#ware Transac-onal Memory · – STM a == a computaon producing a value with type a that does transac-onal memory book keeping on the side – Haskell type system ensures TVars

EveninHaskell:Easier,ButNotEasy.

Theessenceofshared-memoryconcurrencyisdecidingwherecri6calsec6onsshouldbeginandend

–  Toosmall:applica-on-specificdataraces(Eg,mayseedepositbutnotwithdrawiftransferisnotatomic).

–  Toolarge:delayprogressbecausedenyotherthreadsaccesstoneededresources.

InHaskell,wecancomposeSTMsubprogramsbutatsome

point,wemustdecidetowrapanSTMin"atomic"

Programscans-llbenon-determinis-candhardtodebug

Page 50: So#ware Transac-onal Memory · – STM a == a computaon producing a value with type a that does transac-onal memory book keeping on the side – Haskell type system ensures TVars

S-llNotEasy,Example

Considerthefollowingprogram:

Successfulcomple-onrequiresA3toruna#erA1butbeforeA2.Sodele-ngacri-calsec-on(byuncommen-ngA0)changesthebehavioroftheprogram(fromnon-termina-ngtotermina-ng).

Thread 1 // atomic { //A0 atomic { x = 1; } //A1 atomic { if (y==0) abort; } //A2 //}

Thread 2 atomic { //A3 if (x==0) abort; y = 1; }

Initially, x = y = 0

Page 51: So#ware Transac-onal Memory · – STM a == a computaon producing a value with type a that does transac-onal memory book keeping on the side – Haskell type system ensures TVars

STMConclusions

Atomicblocks(atomic,retry,orElse)drama-callyraisethelevelofabstrac-onforconcurrentprogramming.

–  Givesprogrammerbacksomecontroloverwhenandwheretheyhavetoworryaboutinterleavings

Itislikeusingahigh-levellanguageinsteadofassemblycode.Wholeclassesoflow-levelerrorsareeliminated.

–  Correct-by-construc-ondesign

Notasilverbullet:–  youcans-llwritebuggyprograms;–  concurrentprogramsares-llharderthansequen-alones–  aimedonlyatsharedmemoryconcurrency,notmessagepassing

Page 52: So#ware Transac-onal Memory · – STM a == a computaon producing a value with type a that does transac-onal memory book keeping on the side – Haskell type system ensures TVars

WHAT'SNEXT?

Page 53: So#ware Transac-onal Memory · – STM a == a computaon producing a value with type a that does transac-onal memory book keeping on the side – Haskell type system ensures TVars

Inthiscourse•  Anintroduc-ontofunc-onalprogramming

–  immutabledata–  func-onsasdata–  coolabstrac-ons:

•  futures,lazycomputa-ons,streams,parallelcollec-ons,atomicblocks,con-nua-ons,modules,functors,monads

•  Inmostcases,Itriedtoexplainhowanabstrac-onhelpedaprogrammerreasonabouthisorherprogram

•  Because,intheend,thatisthemostimportantaspectofagoodprogramminglanguage:itmakesiteasiertoreasonaboutyourprograms

Page 54: So#ware Transac-onal Memory · – STM a == a computaon producing a value with type a that does transac-onal memory book keeping on the side – Haskell type system ensures TVars

COS510(Spring2015)

Anintroduc-ontomechanicalreasoningaboutprogramsandprogramminglanguages.

It'sagradcoursebutundergradsareencouragedtotakeit!Colleen(room510)willfilloutallthesignaturesontheformconcerning

undergraduatestakingagraduatecourse!

Page 55: So#ware Transac-onal Memory · – STM a == a computaon producing a value with type a that does transac-onal memory book keeping on the side – Haskell type system ensures TVars

ProgrammingLanguageTheoryOCaml'stypesystemmakespredic-ons:

e:int->int

"expressione,ifitterminates,willevaluatetoafunc-onvaluefunx->..."

CanweprovethatOCaml'spredic-onsalwayscometrue?

That'sactuallyapreFydifficultthingtoshowingeneral!Thereareinfinitelymanywell-typedprograms!Andthemayexecuteforarbitrarymanysteps!

Page 56: So#ware Transac-onal Memory · – STM a == a computaon producing a value with type a that does transac-onal memory book keeping on the side – Haskell type system ensures TVars

Also,recallthemidterm:

letreciterate(f:intlist->intlist)(x:intlist):intlist=matchxwith[]->[]|hd::tl->iteratef(ftl)letreciterate2(x:intlist)(f:intlist->intlist):intlist=matchxwith[]->[]|hd::tl->iterate2(ftl)fTheorem:forallf:intlist->intlist.forallx:intlist.iteratefx==iterate2xf

casex=hd::tl:iteratef(hd::tl) (LHS)==matchhd::tlwith... (eval)==iteratef(ftl) (eval)==iterate2(ftl)f (byIH)==matchhd::tlwith... (rev.eval)==iterate2(hd::tl)f (rev.eval)

oops!Canwedevisea

programminglanguagethatcancheckthecorrectnessofour

proofs?

Page 57: So#ware Transac-onal Memory · – STM a == a computaon producing a value with type a that does transac-onal memory book keeping on the side – Haskell type system ensures TVars

Yuppers!

Coq:Atheoremprovingenvironment

Page 58: So#ware Transac-onal Memory · – STM a == a computaon producing a value with type a that does transac-onal memory book keeping on the side – Haskell type system ensures TVars

Yuppers!

Page 59: So#ware Transac-onal Memory · – STM a == a computaon producing a value with type a that does transac-onal memory book keeping on the side – Haskell type system ensures TVars

Yuppers!

Page 60: So#ware Transac-onal Memory · – STM a == a computaon producing a value with type a that does transac-onal memory book keeping on the side – Haskell type system ensures TVars

Yuppers!

Page 61: So#ware Transac-onal Memory · – STM a == a computaon producing a value with type a that does transac-onal memory book keeping on the side – Haskell type system ensures TVars

Yuppers!

Page 62: So#ware Transac-onal Memory · – STM a == a computaon producing a value with type a that does transac-onal memory book keeping on the side – Haskell type system ensures TVars

Yuppers!

Page 63: So#ware Transac-onal Memory · – STM a == a computaon producing a value with type a that does transac-onal memory book keeping on the side – Haskell type system ensures TVars

Yuppers!

Page 64: So#ware Transac-onal Memory · – STM a == a computaon producing a value with type a that does transac-onal memory book keeping on the side – Haskell type system ensures TVars

Yuppers!

Page 65: So#ware Transac-onal Memory · – STM a == a computaon producing a value with type a that does transac-onal memory book keeping on the side – Haskell type system ensures TVars

Summary

Ifyouenjoyed326,especiallythetheore-calparts,thereisalotmorefunstufftolearnin510.

HappyHolidays!