Concurrent ¡Revisions ¡
A ¡novel ¡determinis2c ¡concurrency ¡model ¡
Daan ¡Leijen ¡
Microso9 ¡Research ¡
Concurrent Revisions A novel determinis2c concurrency model - - PowerPoint PPT Presentation
Concurrent Revisions A novel determinis2c concurrency model Daan Leijen Microso9 Research Side effects and TPL TPL is great for introducing parallelism
Microso9 ¡Research ¡
int ¡size; ¡ int[,] ¡result,m1,m2; ¡ Parallel.For( ¡0, ¡size, ¡delegate(int ¡i) ¡{ ¡ ¡ ¡for ¡(int ¡j ¡= ¡0; ¡j ¡< ¡size; ¡j++) ¡{ ¡ ¡ ¡ ¡ ¡result[i, ¡j] ¡= ¡0; ¡ ¡ ¡ ¡ ¡for ¡(int ¡k ¡= ¡0; ¡k ¡< ¡size; ¡k++) ¡{ ¡ ¡ ¡ ¡ ¡ ¡ ¡result[i, ¡j] ¡+= ¡m1[i, ¡k] ¡* ¡m2[k, ¡j]; ¡ ¡ ¡ ¡ ¡} ¡ ¡ ¡} ¡ }); ¡
int ¡x,y; ¡ int ¡sum ¡= ¡0; ¡ Task ¡t ¡= ¡fork ¡{ ¡ ¡ ¡ ¡sum ¡+= ¡x; ¡ } ¡ sum ¡+= ¡y; ¡ join ¡t; ¡
int ¡x ¡= ¡0; ¡ int ¡y ¡= ¡0; ¡ Task ¡t ¡= ¡fork ¡{ ¡ ¡ ¡ ¡if ¡(x==0) ¡y++; ¡ } ¡ if ¡(y==0) ¡x++; ¡ join ¡t; ¡
int ¡x ¡= ¡0; ¡ int ¡y ¡= ¡0; ¡ Task ¡t ¡= ¡fork ¡{ ¡ ¡ ¡ ¡if ¡(x==0) ¡y++; ¡ ¡(1) ¡ } ¡ if ¡(y==0) ¡x++; ¡ ¡ ¡ ¡ ¡(2) ¡ join ¡t; ¡
x ¡= ¡0; ¡ y ¡= ¡0; ¡ task ¡t ¡= ¡fork ¡{ ¡ ¡ ¡atomic ¡{ ¡if ¡(x==0) ¡y++; ¡} ¡ } ¡ atomic ¡{ ¡if ¡(y==0) ¡x++; ¡} ¡ join ¡t; ¡
x ¡= ¡0; ¡ y ¡= ¡0; ¡ task ¡t ¡= ¡fork ¡{ ¡ ¡ ¡atomic ¡{ ¡if ¡(x==0) ¡y++; ¡} ¡ } ¡ atomic ¡{ ¡if ¡(y==0) ¡x++; ¡} ¡ join ¡t; ¡
Autosave ¡ Net-‑ ¡ work ¡ Render ¡ Control ¡ Parallel ¡ Physics ¡ Sound ¡
Paginate ¡ Compute ¡ Formulas ¡ User ¡Edit ¡ Display ¡ Parallel ¡ Spellcheck ¡ Save ¡
Render ¡ Java-‑ ¡ script ¡ Anima2on ¡ Save ¡ Parallel ¡ Layout ¡ User ¡
Autosave ¡ Net-‑ ¡ work ¡ Render ¡ Control ¡ Parallel ¡ Physics ¡ Sound ¡
Reads ¡and ¡writes ¡all ¡posi2ons ¡ Reads ¡all ¡posi2ons ¡ Writes ¡some ¡posi2ons ¡
Reads ¡all, ¡Writes ¡some ¡posi2ons ¡ Reads ¡all ¡posi2ons ¡
Main ¡Revision ¡ Revision ¡b ¡ a ¡= ¡fork ¡{…}; ¡ join ¡a; ¡ Revision ¡a ¡ join ¡b; ¡ b ¡= ¡fork ¡{…}; ¡
int ¡x ¡= ¡0; ¡ Task ¡t ¡= ¡fork ¡{ ¡ ¡ ¡ ¡x ¡= ¡1; ¡ } ¡ assert(x==0 ¡|| ¡x==1); ¡ join ¡t; ¡ assert(x==1); ¡ Versioned<int> ¡x ¡= ¡0; ¡ Revision ¡r ¡= ¡rfork ¡{ ¡ ¡ ¡ ¡x ¡= ¡1; ¡ } ¡ assert(x==0); ¡ join ¡r; ¡ assert(x==1); ¡ Tradi2onal ¡Task ¡ Concurrent ¡Revisions ¡ fork ¡revision: ¡ forks ¡off ¡a ¡private ¡copy ¡of ¡ the ¡shared ¡state ¡ ¡ join ¡revision: ¡ waits ¡for ¡the ¡revision ¡to ¡ terminate ¡and ¡writes ¡back ¡ changes ¡into ¡the ¡main ¡revision ¡ isola?on: ¡ Concurrent ¡modifica2ons ¡ are ¡not ¡seen ¡by ¡others ¡
int ¡x ¡= ¡0; ¡ int ¡y ¡= ¡0; ¡ Task ¡t ¡= ¡fork ¡{ ¡ ¡ ¡ ¡if ¡(x==0) ¡y++; ¡ } ¡ if ¡(y==0) ¡x++; ¡ join ¡t; ¡ int ¡x ¡= ¡0; ¡ int ¡y ¡= ¡0; ¡ Task ¡t ¡= ¡fork ¡{ ¡ ¡ ¡atomic ¡{ ¡if ¡(x==0) ¡y++; ¡} ¡ } ¡ atomic ¡{ ¡if ¡(y==0) ¡x++; ¡} ¡ join ¡t; ¡ Versioned<int> ¡x ¡= ¡0; ¡ Versioned<int> ¡y ¡= ¡0; ¡ Revision ¡r ¡= ¡rfork ¡{ ¡ ¡ ¡ ¡if ¡(x==0) ¡y++; ¡ } ¡ if ¡(y==0) ¡x++; ¡ join ¡r; ¡ ¡ ¡ ¡ ¡ ¡ Sequen2al ¡Consistency ¡ Transac2onal ¡Memory ¡ assert( ¡ ¡ ¡(x==0 ¡&& ¡y==1) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡|| ¡(x==1 ¡&& ¡y==0) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡|| ¡(x==1 ¡&& ¡y==1)); ¡ assert( ¡ ¡ ¡(x==0 ¡&& ¡y==1) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡|| ¡(x==1 ¡&& ¡y==0)); ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡
Versioned<int> ¡x ¡= ¡0; ¡ Versioned<int> ¡y ¡= ¡0; ¡ Revision ¡r ¡= ¡rfork ¡{ ¡ ¡ ¡ ¡if ¡(x==0) ¡y++; ¡ } ¡ if ¡(y==0) ¡x++; ¡ join ¡r; ¡ ¡ ¡ ¡ ¡ ¡
Main ¡Revision ¡ Revision ¡c ¡ a ¡= ¡fork ¡{…}; ¡ join ¡a; ¡ b ¡= ¡fork ¡{…}; ¡ Revision ¡a ¡ join ¡b; ¡ ¡ ¡NOT ¡POSSIBLE! ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡(since ¡b ¡is ¡not ¡visible) ¡ Revision ¡b ¡ c ¡= ¡fork ¡{…}; ¡ Main ¡Revision ¡ a ¡= ¡fork ¡{…}; ¡ join ¡a; ¡ b ¡= ¡fork ¡{…}; ¡ Revision ¡a ¡ join ¡b; ¡ Revision ¡b ¡ c ¡= ¡fork ¡{…}; ¡
Cumula2veInt ¡x ¡= ¡0; ¡
x+=3 ¡ x+=1 ¡ x+=2 ¡ x==5 ¡ x==4 ¡ x==4 ¡ x==0 ¡ x==6 ¡
Versioned<int> ¡x ¡= ¡0; ¡
x=1 ¡ x=2 ¡ x==2 ¡ x==1 ¡ x==1 ¡ x==0 ¡ x==2 ¡
Cumula?veInt ¡x ¡= ¡0; ¡
x+=3 ¡ x+=1 ¡ x+=2 ¡ x==5 ¡ x==4 ¡ x==4 ¡ x==0 ¡ x==6 ¡ x==3 ¡
merge(0,4,0) = ¡0+(4-‑0) ¡ merge(4,5,3) = ¡4+(5-‑3) ¡
merge(main,join,orig) ¡ ¡ ¡ ¡= ¡main ¡+ ¡(join ¡– ¡orig) ¡
Reads ¡and ¡writes ¡all ¡posi2ons ¡ Reads ¡all ¡posi2ons ¡ Writes ¡some ¡posi2ons ¡
Reads ¡all, ¡Writes ¡some ¡posi2ons ¡ Reads ¡all ¡posi2ons ¡
Render ¡ Physics ¡ network ¡ autosave ¡ ¡ (long ¡running) ¡ Collision ¡Detec?on ¡ part ¡4 ¡ part ¡3 ¡ part ¡2 ¡ part ¡1 ¡
Render ¡ Physics ¡ network ¡ autosave ¡ ¡ (long ¡running) ¡ Collision ¡Detec?on ¡ part ¡4 ¡ part ¡3 ¡ part ¡2 ¡ part ¡1 ¡
Render ¡ Physics ¡ network ¡ autosave ¡ ¡ (long ¡running) ¡ Collision ¡Detec?on ¡ part ¡4 ¡ part ¡3 ¡ part ¡2 ¡ part ¡1 ¡
Revision ¡ Value ¡ 1 ¡ 0 ¡ 40 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Console.WriteLine("i ¡= ¡" ¡+ ¡i); ¡ ¡ ¡} ¡ } ¡
class ¡Sandbox ¡ { ¡ ¡ ¡[Versioned] ¡ ¡ ¡int ¡i ¡= ¡0; ¡ ¡ ¡public ¡void ¡Run() ¡ ¡ ¡{ ¡ ¡ ¡ ¡ ¡var ¡r ¡= ¡CurrentRevision.Branch("FlakyCode"); ¡ ¡ ¡ ¡ ¡try ¡{ ¡ ¡ ¡ ¡ ¡ ¡ ¡r.Run(() ¡=> ¡ ¡ ¡ ¡ ¡ ¡ ¡{ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡i ¡= ¡1; ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡throw ¡new ¡Exception("Oops"); ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡}); ¡ ¡ ¡ ¡ ¡ ¡ ¡CurrentRevision.Merge(r); ¡ ¡ ¡ ¡ ¡} ¡ ¡ ¡ ¡ ¡catch ¡{ ¡ ¡ ¡ ¡ ¡ ¡ ¡CurrentRevision.Abandon(r); ¡ ¡ ¡ ¡ ¡} ¡ ¡ ¡ ¡ ¡Console.WriteLine("\n ¡i ¡= ¡" ¡+ ¡i); ¡ ¡ ¡} ¡ } ¡
Fork ¡a ¡revision ¡without ¡forking ¡ an ¡associated ¡task/thread Run ¡code ¡in ¡a ¡certain ¡revision Merge ¡changes ¡in ¡a ¡ revision ¡into ¡the ¡main ¡one Abandon ¡a ¡revision ¡and ¡don’t ¡ merge ¡its ¡changes.
By ¡construc2on, ¡there ¡is ¡no ¡ ‘global’ ¡state: ¡just ¡local ¡state ¡for ¡ each ¡revision State ¡is ¡simply ¡a ¡(par2al) ¡ func2on ¡from ¡a ¡loca2on ¡to ¡a ¡ value ¡
For ¡some ¡revision ¡r, ¡with ¡snapshot ¡ ¡ and ¡local ¡modifica2ons ¡ ¡and ¡an ¡ expression ¡context ¡with ¡hole ¡(x.e) v the ¡state ¡is ¡a ¡composi2on ¡
and ¡local ¡modifica2ons ¡ On ¡a ¡fork, ¡the ¡ snapshot ¡of ¡the ¡new ¡ revision ¡r’ ¡is ¡the ¡ current ¡state: ¡ ¡:: On ¡a ¡join, ¡the ¡writes ¡of ¡the ¡ joinee ¡r’ ¡take ¡priority ¡over ¡ the ¡writes ¡of ¡ ¡the ¡current ¡ revision: ¡ ¡::’