review linearizability defini3on
play

Review: Linearizability Defini3on Given some component C - PowerPoint PPT Presentation

Review: Linearizability Defini3on Given some component C (say, a class) And some opera3ons O1, O2, .. (say, methods) An opera)on is linearizable


  1. Review: ¡Linearizability ¡

  2. Defini3on ¡ • Given ¡some ¡component ¡C ¡(say, ¡a ¡class) ¡ • And ¡some ¡opera3ons ¡O1, ¡O2, ¡.. ¡(say, ¡methods) ¡ • An ¡opera)on ¡is ¡linearizable ¡if ¡it ¡always ¡appears ¡ to ¡take ¡effect ¡at ¡a ¡single ¡instant ¡of ¡)me ¡(called ¡ the ¡commit ¡point) ¡which ¡happens ¡some)me ¡ a<er ¡the ¡opera)on ¡is ¡called ¡and ¡before ¡it ¡ returns. ¡ • Linearizable ¡opera3ons ¡are ¡some3mes ¡called ¡ atomic, ¡ but ¡that ¡term ¡is ¡overused. ¡

  3. Example ¡1: ¡Stack ¡ • The ¡following ¡history ¡is ¡linearizable. ¡ TIME ¡ Push(10) ¡ Push(20) ¡ Ok ¡ Ok ¡ Thread ¡1 ¡ TryPop ¡() ¡ 10 ¡ Thread ¡2 ¡ TryPop() ¡ Fail ¡ Thread ¡2 ¡

  4. Example ¡1: ¡Stack ¡ • The ¡following ¡history ¡is ¡linearizable. ¡ TIME ¡ Push(10) ¡ Push(20) ¡ Ok ¡ Ok ¡ Thread ¡1 ¡ TryPop ¡() ¡ 10 ¡ Thread ¡2 ¡ TryPop() ¡ Fail ¡ Thread ¡2 ¡

  5. Example ¡2: ¡Stack ¡ • The ¡following ¡history ¡is ¡not ¡linearizable. ¡ TIME ¡ Push(10) ¡ TryPop() ¡ Ok ¡ 10 ¡ Thread ¡1 ¡ Push(10) ¡ Ok ¡ TryPop ¡() ¡ Fail ¡ Thread ¡2 ¡

  6. Example ¡2: ¡Stack ¡ • The ¡following ¡history ¡is ¡not ¡linearizable. ¡ TIME ¡ Push(10) ¡ TryPop() ¡ Ok ¡ 10 ¡ Thread ¡1 ¡ Push(10) ¡ Ok ¡ TryPop ¡() ¡ Fail ¡ Thread ¡2 ¡ At ¡this ¡point, ¡both ¡pushes ¡have ¡ ¡ taken ¡effect. ¡So ¡there ¡can ¡not ¡be ¡less ¡ Than ¡1 ¡element ¡in ¡the ¡stack. ¡

  7. Quick ¡Ques3on ¡ • Q: ¡What ¡is ¡the ¡most ¡frequently ¡used ¡ linearizable ¡data ¡type? ¡

  8. Quick ¡Ques3on ¡ • Q: ¡What ¡is ¡the ¡most ¡frequently ¡used ¡ ¡ linearizable ¡data ¡type? ¡ • A: ¡an ¡atomic ¡register ¡(historic ¡name) ¡ set ¡ increment ¡ Example: ¡ get ¡ C ¡= ¡integer ¡ O ¡= ¡{ ¡int ¡get(), ¡ void ¡set(int ¡val), ¡ ¡ integer ¡ int ¡increment() ¡} ¡ variable ¡ Plain ¡fields ¡and ¡variables ¡are ¡ ¡ not ¡linearizable ¡by ¡default! ¡

  9. Atomic ¡Registers ¡in ¡C# ¡ • Use ¡vola3le ¡declara3on, ¡e.g. ¡ ¡ ¡ ¡ ¡ ¡ volatile ¡int ¡x; ¡ – Lets ¡compiler ¡know ¡that ¡you ¡would ¡like ¡to ¡read ¡& ¡ write ¡this ¡field ¡atomically. ¡Important ¡to ¡avoid ¡memory ¡ model ¡issues. ¡ – Does ¡not ¡work ¡with ¡longs, ¡structs ¡ • Use ¡“Interlocked” ¡opera3ons ¡if ¡you ¡need ¡an ¡ atomic ¡modifica3on ¡ – Interlocked.Increment, ¡Interlocked.Decrement, ¡ Interlocked.Add ¡ – Interlocked.CompareExchange, ¡Interlocked.Exchange ¡ – Interlocked.Read ¡(for ¡reading ¡64-­‑bit ¡longs) ¡

  10. Example: ¡Vola3le/Interlockeds ¡Can ¡ Replace ¡Locks ¡ class ¡MyCounter() ¡ { ¡ class ¡MyCounter() ¡ ¡ ¡ ¡ ¡Object ¡ mylock ¡ = ¡ ¡new ¡Object(); ¡ { ¡ ¡ ¡ ¡ ¡int ¡balance; ¡ ¡ ¡ ¡ ¡ vola,le ¡int ¡balance; ¡ ¡ ¡ ¡ ¡public ¡void ¡Deposit(int ¡what) ¡ ¡ ¡ ¡ ¡ ¡{ ¡ ¡ ¡ ¡ ¡public ¡void ¡Deposit(int ¡what) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ lock(mylock) ¡ ¡ ¡ ¡ ¡{ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡balance ¡= ¡balance ¡+ ¡what; ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡Interlocked.Add (ref ¡balance, ¡what) ¡ ¡ ¡ ¡ ¡ ¡} ¡ ¡ ¡ ¡ ¡} ¡ ¡ ¡ ¡ ¡ ¡public ¡int ¡GetBalance() ¡ ¡ ¡ ¡ ¡ ¡public ¡int ¡GetBalance() ¡ ¡ ¡ ¡ ¡ ¡{ ¡ ¡ ¡ ¡ ¡ ¡{ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡lock(mylock) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡return ¡balance; ¡/* ¡vola3le ¡read ¡*/ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡return ¡balance; ¡ ¡ ¡ ¡ ¡ ¡} ¡ ¡ ¡ ¡ ¡ ¡} ¡ ¡ ¡ ¡ ¡ ¡public ¡int ¡GetBalance(int ¡val) ¡ ¡ ¡ ¡ ¡ ¡public ¡void ¡SetBalance(int ¡val) ¡ ¡ ¡ ¡ ¡ ¡{ ¡ ¡ ¡ ¡ ¡ ¡{ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡balance ¡= ¡val; ¡/* ¡vola3le ¡write ¡*/ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡lock(mylock) ¡ ¡ ¡ ¡ ¡ ¡} ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡balance ¡= ¡val; ¡ ¡ ¡ ¡ ¡ ¡} ¡ } ¡

  11. The ¡Composi3on ¡Problem ¡ • Atomic ¡Registers ¡& ¡Linearizable ¡Objects ¡are ¡ great ¡if ¡you ¡do ¡1 ¡thing ¡at ¡a ¡3me. ¡ ¡ • What ¡if ¡you ¡need ¡to ¡do ¡more ¡than ¡one ¡thing ¡at ¡ a ¡3me? ¡

  12. Stack ¡Example ¡ class ¡SpecialStack ¡ { ¡ ¡ ¡LinearizableStack<Elt> ¡stack; ¡ ¡ ¡ ¡volatile ¡int ¡count; ¡ ¡// ¡counts ¡number ¡of ¡important ¡elts ¡in ¡stack ¡ } ¡ Linearizable? ¡ bool ¡Clear(x) ¡ { ¡ ¡ ¡if ¡(count ¡== ¡0) ¡ void ¡Insert(x) ¡ ¡ ¡{ ¡ ¡ ¡ ¡ ¡ ¡ ¡stack.Clear(); ¡ { ¡ ¡ ¡ ¡ ¡ ¡return ¡true; ¡ ¡ ¡stack.Push(x); ¡ ¡ ¡if(x.Important) ¡ ¡ ¡} ¡ ¡ ¡ ¡ ¡Interlocked.Increment(ref ¡count); ¡ ¡ ¡else ¡ ¡ ¡{ ¡ } ¡ ¡ ¡ ¡ ¡ ¡return ¡false; ¡ ¡ ¡} ¡ } ¡

  13. Not ¡linearizable. ¡ Insert(x) ¡ Ok ¡ Thread ¡1 ¡ Interlocked.Increment ¡ Stack.Push ¡(x) ¡ Clear(10) ¡ Ok ¡ Thread ¡2 ¡ get ¡count ¡ stack.Clear() ¡ bool ¡Clear(x) ¡ Final ¡state: ¡stack ¡empty, ¡count=1 ¡ { ¡ ¡ ¡if ¡(count ¡== ¡0) ¡ ¡ ¡{ ¡ ¡ ¡ ¡ ¡ ¡ ¡stack.Clear(); ¡ void ¡Insert(x) ¡ ¡ ¡ ¡ ¡ ¡return ¡true; ¡ { ¡ ¡ ¡} ¡ ¡ ¡stack.Push(x); ¡ ¡ ¡else ¡ ¡ ¡if(x.Important) ¡ ¡ ¡{ ¡ ¡ ¡ ¡ ¡Interlocked.Increment(ref ¡count); ¡ ¡ ¡ ¡ ¡ ¡return ¡false; ¡ } ¡ ¡ ¡} ¡ } ¡

  14. Why ¡so ¡complicated? ¡Just ¡use ¡a ¡lock. ¡ Linearizability ¡Restored. ¡ class ¡SpecialStack ¡ bool ¡Clear(x) ¡ { ¡ ¡ ¡Stack<Elt> ¡stack; ¡ ¡ { ¡ ¡ ¡int ¡count; ¡ ¡ ¡lock(this) ¡ } ¡ ¡ ¡{ ¡ ¡ ¡ ¡ ¡if ¡(count ¡== ¡0) ¡ ¡ ¡ ¡ ¡{ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡stack.Clear(); ¡ void ¡Insert(x) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡return ¡true; ¡ { ¡ ¡ ¡ ¡ ¡} ¡ ¡ ¡ ¡lock(this) ¡ ¡ ¡ ¡ ¡else ¡ ¡ ¡ ¡{ ¡ ¡ ¡ ¡ ¡{ ¡ ¡ ¡ ¡ ¡ ¡if(x.Important) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡return ¡false; ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡count++; ¡ ¡ ¡ ¡ ¡} ¡ ¡ ¡ ¡ ¡ ¡stack.Push(x); ¡ ¡ ¡} ¡ ¡ ¡ ¡} ¡ } ¡ } ¡

  15. Transac3ons ¡& ¡ ¡ Concurrency ¡Control ¡ Unit ¡8.c ¡ Prac3cal ¡Parallel ¡and ¡Concurrent ¡ 8/17/2010 ¡ Programming ¡DRAFT: ¡comments ¡to ¡ 1 ¡ msrpcpcp@microson.com ¡ ¡ ¡

  16. Acknowledgments ¡ • Authored ¡by ¡ – ¡Sebas3an ¡Burckhardt, ¡MSR ¡Redmond ¡ Prac3cal ¡Parallel ¡and ¡Concurrent ¡ Programming ¡DRAFT: ¡comments ¡to ¡ 5/3/11 ¡ 16 ¡ msrpcpcp@microson.com ¡ ¡

  17. Transac3ons ¡ • Clients ¡vs. ¡Data ¡ – Clients ¡are ¡concurrent ¡ (e.g. ¡threads, ¡processes, ¡computers) ¡ – Data ¡may ¡be ¡spread ¡out ¡ (e.g. ¡across ¡processes, ¡files, ¡servers) ¡ • Clients ¡perform ¡transac3ons ¡ – bounded ¡sequence ¡of ¡opera3ons ¡ READ(location), ¡WRITE(location, ¡value) ¡ – May ¡include ¡data-­‑dependent ¡branching ¡or ¡ looping ¡ Data ¡2 ¡ – May ¡have ¡real-­‑world ¡significance, ¡e.g. ¡ Data ¡1 ¡ represent ¡a ¡purchase ¡or ¡a ¡reserva3on ¡ – What ¡could ¡possibly ¡go ¡wrong? ¡

  18. Example ¡1: ¡Bank ¡Accounts ¡ Transfer ¡100 ¡from ¡ ¡ Balance ¡Inquiry ¡ account1 ¡to ¡account2 ¡ BEGIN_TRANSACTION ¡ BEGIN_TRANSACTION ¡ int ¡x ¡= ¡READ(account1); ¡ int ¡x ¡= ¡READ(account1); ¡ int ¡y ¡= ¡READ(account2); ¡ if ¡(x ¡>= ¡100) ¡ Print(“total=“, ¡x+y); ¡ { ¡ COMMIT ¡ ¡ ¡ ¡ ¡WRITE(account1, ¡x-­‑100); ¡ ¡ ¡ ¡ ¡int ¡y ¡= ¡READ(account2); ¡ ¡ ¡ ¡ ¡WRITE(account2, ¡y+100); ¡ } ¡ COMMIT ¡ • If ¡interleaved, ¡may ¡present ¡incorrect ¡total ¡balance. ¡

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend