259
Concurrent Linked Lists Acknowledgement: Slides adopted from the companion slides for the book "The Art of Mul<processor Programming" by Maurice Herlihy and Nir Shavit

ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul

  • Upload
    others

  • View
    0

  • Download
    0

Embed Size (px)

Citation preview

Page 1: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul

Concurrent  Linked  Lists  

Acknowledgement:    Slides  adopted  from  the  companion  slides  for  the  book    "The  Art  of  Mul<processor  Programming"    by  Maurice  Herlihy  and  Nir  Shavit  

Page 2: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul

What  We'll  Cover  Today  Chapter  9  of:  

Digital  copy  can  be  obtained  via  WUSTL  library:  hHp://catalog.wustl.edu/search/    

Page 3: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul

Today:  Concurrent  Objects  

•  Adding  threads  should  not  lower  throughput  –  ContenAon  effects  – Mostly  fixed  by  Queue  locks  

•  Should  increase  throughput  – Not  possible  if  inherently  sequenAal  –  Surprising  things  are  parallelizable  

Page 4: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul

Coarse-­‐Grained  SynchronizaAon:  the  Good  

•  Each  method  locks  the  object  – Avoid  contenAon  using  queue  locks    –  Easy  to  reason  about  

•  In  simple  cases  

Page 5: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul

Art of Multiprocessor Programming 5

•  SequenAal  boQleneck  –  Threads  “stand  in  line”  

•  Adding  more  threads  – Does  not  improve  throughput  –  Struggle  to  keep  it  from  geUng  worse  

Coarse-­‐Grained  SynchronizaAon:  the  Bad  

Page 6: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul

Art of Multiprocessor Programming 6

This  Lecture  

•  Introduce  four  “paQerns”  –  Bag  of  tricks  …  – Methods  that  work  more  than  once  …  

•  For  highly-­‐concurrent  objects  –  Concurrent  access  – More  threads,  more  throughput  

Page 7: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul

This  Lecture  

•  Coarse-­‐grained  locking  •  Fine-­‐grained  locking  •  OpAmisAc  synchronizaAon  •  Lazy  synchronizaAon  •  Lock-­‐free  synchronizaAon  

Page 8: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul

First:  Fine-­‐Grained  SynchronizaAon  

•  Instead  of  using  a  single  lock  …  •  Split  object  into  

–  Independently-­‐synchronized  components  

• Methods  conflict  when  they  access  –  The  same  component  …  – At  the  same  Ame  

Page 9: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul

Second:  OpAmisAc  SynchronizaAon  

•  Search  without  locking  …  •  If  you  find  it,  lock  and  check  …  

– OK:  we  are  done  – Oops:  start  over  

•  EvaluaAon  – Usually  cheaper  than  locking,  but  – Mistakes  are  expensive  

Page 10: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul

Third:  Lazy  SynchronizaAon  

•  Postpone  hard  work  •  Removing  components  is  tricky  

–  Logical  removal  • Mark  component  to  be  deleted  

–  Physical  removal  • Do  what  needs  to  be  done  

Page 11: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul

Fourth:  Lock-­‐Free  SynchronizaAon  

•  Don’t  use  locks  at  all  – Use  compareAndSet()  &  relaAves  …  

•  Advantages  – No  Scheduler  AssumpAons/Support  

•  Disadvantages  –  Complex  –  SomeAmes  high  overhead  

Page 12: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul

Linked  List  

•  Illustrate  these  paQerns  …  •  Using  a  list-­‐based  Set  

–  Common  applicaAon  –  Building  block  for  other  apps  

Page 13: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul

Set  Interface  

•  Unordered  collecAon  of  items  •  No  duplicates  • Methods  

– add(x)  put  x  in  set  – remove(x)  take  x  out  of  set  – contains(x)  tests  if  x  in  set  

Page 14: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul

List-­‐Based  Sets  

public interface Set<T> { public boolean add(T x); public boolean remove(T x); public boolean contains(T x); }

Page 15: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul

List-­‐Based  Sets  

public interface Set<T> { public boolean add(T x); public boolean remove(T x); public boolean contains(T x); }

Add item to set

Page 16: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul

List-­‐Based  Sets  

public interface Set<T> { public boolean add(T x); public boolean remove(T x); public boolean contains(Tt x); }

Remove item from set

Page 17: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul

List-­‐Based  Sets  

public interface Set<T> { public boolean add(T x); public boolean remove(T x); public boolean contains(T x); }

Is item in set?

Page 18: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul

List  Node  

public class Node { public T item; public int key; public volatile Node next; }

Page 19: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul

List  Node  

public class Node { public T item; public int key; public volatile Node next; }

item of interest

Page 20: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul

List  Node  

public class Node { public T item; public int key; public volatile Node next; }

Usually hash code

Page 21: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul

List  Node  

public class Node { public T item; public int key; public Node next; }

Reference to next node

Page 22: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul

The  List-­‐Based  Set  

a b c

Sorted  with  SenAnel  nodes  (min  &  max  possible  keys)  

-∞

+∞

Once  you  find  a  key  larger  than  the  key  you  are  searching  for,  you  are  done.    

Page 23: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul

Reasoning  about  Concurrent  Objects  

•  Invariant  –  Property  that  always  holds  

•  Established  because  –  True  when  object  is  created  –  Truth  preserved  by  each  method  

• Each  step  of  each  method  

Page 24: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul

Specifically  …  

•  Invariants  preserved  by  – add() – remove() – contains()

• Most  steps  are  trivial  – Usually  one  step  tricky  – Oeen  linearizaAon  point  

Page 25: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul

Interference  •  Invariants  make  sense  only  if  we  assume  freedom  from  interference:  methods  considered  are  the  only  modifiers  to  the  data  structure.  

•  Language  encapsulaAon  helps  –  List  nodes  not  visible  outside  class  

•  Freedom  from  interference  needed  even  for  removed  nodes  –  Some  algorithms  traverse  removed  nodes  –  Careful  with  malloc()&  free()!  

•  We  rely  on  garbage  collecAon  

Page 26: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul

Abstract  Data  Types  

•  Concrete  representaAon:  

•  Abstract  Type:    {a,  b}  

a b

Page 27: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul

• Meaning  of  representaAon  given  by  abstrac'on  map,  carrying  lists  that  saAsfy  representaAon  invariant  to  set.  

     S(                                                                                )  =  {a,b}  

Abstract  Data  Types  

a b

Page 28: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul

RepresentaAon  Invariant  

• Which  concrete  values  meaningful?  –  Sorted?  – Duplicates?  

•  Rep  invariant  –  Characterizes  legal  concrete  reps  –  Preserved  by  methods  –  Relied  on  by  methods  

Page 29: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul

RepresentaAon  Invariant  

•  SenAnel  nodes  –  tail  reachable  from  head    

•  Sorted  •  No  duplicates  

Page 30: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul

AbstracAon  Map  

• S(head)  =  {  x  |  there  exists  a  such  that  

• a  reachable  from  head  and  • a.item    =  x  

}  

Page 31: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul

SequenAal  List  Based  Set    

a c d

a b c

add()

remove()

Page 32: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul

SequenAal  List  Based  Set    

a c d

b

a b c

add()

remove()

Page 33: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul

Coarse-­‐Grained  Locking  

•  Easy,  same  as  synchronized  methods  –  "One  lock  to  rule  them  all  …  "  

•  Simple,  clearly  correct  – Deserves  respect!  

• Works  poorly  with  contenAon  – Queue  locks  help  –  But  boQleneck  sAll  an  issue  

Page 34: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul

Fine-­‐grained  Locking  

•  Requires  careful  thought  –  “Do  not  meddle  in  the  affairs  of  wizards,  for  they  are  subtle  and  quick  to  anger”  

Page 35: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul

Art of Multiprocessor Programming 35

Fine-­‐grained  Locking  

•  Requires  careful  thought  –  “Do  not  meddle  in  the  affairs  of  wizards,  for  they  are  subtle  and  quick  to  anger”  

•  Split  object  into  pieces  –  Each  piece  has  own  lock  – Methods  that  work  on  disjoint  pieces  need  not  exclude  each  other  

Page 36: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul

Hand-­‐over-­‐Hand  locking  

a b c

Page 37: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul

Hand-­‐over-­‐Hand  locking  

a b c

Page 38: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul

Hand-­‐over-­‐Hand  locking  

a b c

Page 39: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul

Hand-­‐over-­‐Hand  locking  

a b c

Page 40: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul

Hand-­‐over-­‐Hand  locking  

a b c

Page 41: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul

Removing  a  Node  

a b c d

remove(b)

Page 42: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul

Removing  a  Node  

a b c d

remove(b)

Page 43: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul

Removing  a  Node  

a b c d

remove(b)

Page 44: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul

Art of Multiprocessor Programming 44

Removing  a  Node  

a b c d

remove(b)

Page 45: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul

Removing  a  Node  

a b c d

remove(b)

Page 46: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul

Removing  a  Node  

a c d

remove(b) Why lock victim node?

Page 47: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul

Concurrent  Removes  

a b c d

remove(c) remove(b)

Page 48: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul

Concurrent  Removes  

a b c d

remove(b) remove(c)

Page 49: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul

Concurrent  Removes  

a b c d

remove(b) remove(c)

Page 50: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul

Concurrent  Removes  

a b c d

remove(b) remove(c)

Page 51: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul

Concurrent  Removes  

a b c d

remove(b) remove(c)

Page 52: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul

Concurrent  Removes  

a b c d

remove(b) remove(c)

Page 53: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul

Concurrent  Removes  

a b c d

remove(b) remove(c)

Page 54: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul

Concurrent  Removes  

a b c d

remove(b) remove(c)

Page 55: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul

Art of Multiprocessor Programming 55

Concurrent  Removes  

a b c d

remove(b) remove(c)

Page 56: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul

Art of Multiprocessor Programming 56

Concurrent  Removes  

a b c d

remove(b) remove(c)

Page 57: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul

Uh,  Oh  

a c d

remove(b) remove(c)

Page 58: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul

Uh,  Oh  

a c d

Bad news, c not removed

remove(b) remove(c)

Page 59: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul

Problem  

•  To  delete  node  c  –  Swing  node  b’s  next  field  to  d  (c's  next  field)  

•  Problem  is,  –  Someone  deleAng  b  concurrently  could          direct  a  pointer  to  c        (reading  b's  next  field)  

b a c

b a c

Page 60: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul

Insight  

•  If  a  node  is  locked  – No  one  can  change  node’s  successor  

•  If  a  thread  locks  – Node  to  be  deleted  (so  its  successor  don't  change)  

– And  its  predecessor  (so  you  are  the  only  one  changing  its  successor)  

–  Then  it  works  

Page 61: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul

Removing  a  Node  

a b c d

remove(b) remove(c)

Page 62: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul

Removing  a  Node  

a b c d

remove(b) remove(c)

Page 63: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul

Removing  a  Node  

a b c d

remove(b) remove(c)

Page 64: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul

Removing  a  Node  

a b c d

remove(b) remove(c)

Page 65: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul

Removing  a  Node  

a b c d

remove(b) remove(c)

Page 66: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul

Removing  a  Node  

a b c d

remove(b) remove(c)

Page 67: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul

Removing  a  Node  

a b c d

remove(b) remove(c)

Page 68: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul

Removing  a  Node  

a b c d

remove(b) remove(c)

Page 69: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul

Removing  a  Node  

a b c d

Must acquire Lock for

b

remove(c)

Page 70: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul

Removing  a  Node  

a b c d

Waiting to acquire

lock for b

remove(c)

Page 71: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul

Removing  a  Node  

a b c d

Wait! remove(c)

Page 72: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul

Removing  a  Node  

a b d

Proceed to

remove(b)

Page 73: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul

Removing  a  Node  

a b d

remove(b)

Page 74: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul

Removing  a  Node  

a b d

remove(b)

Page 75: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul

Removing  a  Node  

a d

remove(b)

Page 76: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul

Remove  method  public boolean remove(T item) { int key = item.hashCode(); Node pred, curr; pred = head; pred.lock(); curr = pred.next; curr.lock(); try { … } finally { curr.unlock(); pred.unlock(); }}

Page 77: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul

Remove  method  public boolean remove(T item) { int key = item.hashCode(); Node pred, curr; pred = head; pred.lock(); curr = pred.next; curr.lock(); try { … } finally { curr.unlock(); pred.unlock(); }}

Key used to order node

Page 78: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul

Remove  method  public boolean remove(T item) { int key = item.hashCode(); Node pred, curr; pred = head; pred.lock(); curr = pred.next; curr.lock(); try { … } finally { curr.unlock(); pred.unlock(); }}

Predecessor and current nodes

Page 79: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul

Remove  method  public boolean remove(T item) { int key = item.hashCode(); Node pred, curr; pred = head; pred.lock(); curr = pred.next; curr.lock(); try { … } finally { curr.unlock(); pred.unlock(); }}

lock pred == head before accessing its

next field

Page 80: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul

Remove  method  public boolean remove(T item) { int key = item.hashCode(); Node pred, curr; pred = head; pred.lock(); curr = pred.next; curr.lock(); try { … } finally { curr.unlock(); pred.unlock(); }}

lock the node after head

Page 81: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul

Remove  method  public boolean remove(T item) { int key = item.hashCode(); Node pred, curr; pred = head; pred.lock(); curr = pred.next; curr.lock(); try { … } finally { curr.unlock(); pred.unlock(); }}

When enter try, we hold locks on

pred and curr

Page 82: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul

Remove  method  public boolean remove(T item) { int key = item.hashCode(); Node pred, curr; pred = head; pred.lock(); curr = pred.next; curr.lock(); try { … } finally { curr.unlock(); pred.unlock(); }}

Traverse the rest of the list

Page 83: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul

Remove  method  public boolean remove(T item) { int key = item.hashCode(); Node pred, curr; pred = head; pred.lock(); curr = pred.next; curr.lock(); try { … } finally { curr.unlock(); pred.unlock(); }}

Make sure locks released

Page 84: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul

while (curr.key <= key) { if (item == curr.item) { pred.next = curr.next; return true; } pred.unlock(); pred = curr; curr = curr.next; curr.lock(); } return false;

Remove:  searching  (Inside  the  Try  Block)  

Page 85: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul

while (curr.key <= key) { if (item == curr.item) { pred.next = curr.next; return true; } pred.unlock(); pred = curr; curr = curr.next; curr.lock(); } return false;

Remove:  searching  (Inside  the  Try  Block)  

Search key range

Page 86: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul

while (curr.key <= key) { if (item == curr.item) { pred.next = curr.next; return true; } pred.unlock(); pred = curr; curr = curr.next; curr.lock(); } return false;

Remove:  searching  (Inside  the  Try  Block)  

At start of each loop: curr and pred locked

Page 87: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul

while (curr.key <= key) { if (item == curr.item) { pred.next = curr.next; return true; } pred.unlock(); pred = curr; curr = curr.next; curr.lock(); } return false;

Remove:  searching  (Inside  the  Try  Block)  

If item found, remove node

Page 88: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul

while (curr.key <= key) { if (item == curr.item) { pred.next = curr.next; return true; } pred.unlock(); pred = curr; curr = curr.next; curr.lock(); } return false;

Remove:  searching  (Inside  the  Try  Block)  

Hand-over-hand locking again

otherwise

Page 89: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul

while (curr.key <= key) { if (item == curr.item) { pred.next = curr.next; return true; } pred.unlock(); pred = curr; curr = curr.next; curr.lock(); } return false;

Remove:  searching  (Inside  the  Try  Block)  

Lock invariant restored

Page 90: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul

while (curr.key <= key) { if (item == curr.item) { pred.next = curr.next; return true; } pred.unlock(); pred = curr; curr = curr.next; curr.lock(); } return false;

Remove:  searching  (Inside  the  Try  Block)  

Otherwise, not present

Page 91: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul

Aside:  Next  Field  Must  be  VolaAle!  

public class Node { public T item; public int key; public volatile Node next; }

Since  we  are  no  longer  holding  a  lock  when  we  read  the  "next"  field,  it  needs  to  be  volaAle  to  avoid  race  condiAons  

(more  on  that  in  future  lecture).  

Page 92: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul

while (curr.key <= key) { if (item == curr.item) { pred.next = curr.next; return true; } pred.unlock(); pred = curr; curr = curr.next; curr.lock(); } return false;

Why  remove()  is  linearizable  

• pred reachable from head • curr is pred.next • So curr.item is in the set

Page 93: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul

while (curr.key <= key) { if (item == curr.item) { pred.next = curr.next; return true; } pred.unlock(); pred = curr; curr = curr.next; curr.lock(); } return false;

Why  remove()  is  linearizable  

Linearization point if item is present

Page 94: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul

while (curr.key <= key) { if (item == curr.item) { pred.next = curr.next; return true; } pred.unlock(); pred = curr; curr = curr.next; curr.lock(); } return false;

Why  remove()  is  linearizable  

Node locked, so no other thread can remove it ….

Page 95: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul

while (curr.key <= key) { if (item == curr.item) { pred.next = curr.next; return true; } pred.unlock(); pred = curr; curr = curr.next; curr.lock(); } return false;

Why  remove()  is  linearizable  

Item not present

Page 96: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul

while (curr.key <= key) { if (item == curr.item) { pred.next = curr.next; return true; } pred.unlock(); pred = curr; curr = curr.next; curr.lock(); } return false;

Why  remove()  is  linearizable  

• pred reachable from head • curr is pred.next • pred.key < key • key < curr.key

Page 97: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul

while (curr.key <= key) { if (item == curr.item) { pred.next = curr.next; return true; } pred.unlock(); pred = curr; curr = curr.next; curr.lock(); } return false;

Why  remove()  is  linearizable  

Linearization point: the most recent

read before return

Page 98: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul

Adding  Nodes  

•  To  add  node  e  – Must  lock  predecessor  – Must  lock  successor  

•  Neither  can  be  deleted  –  (Is  successor  lock  actually  required?)  

Page 99: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul

AbstracAon  Map  

• S(head)  =  {  x  |  there  exists  a  such  that  

• a  reachable  from  head  and  • a.item    =  x  

}  

Page 100: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul

RepresentaAon  Invariant  

•  Easy  to  check  that  –  tail  always  reachable  from  head  – Nodes  sorted,  no  duplicates  

Page 101: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul

Drawbacks  

•  BeQer  than  coarse-­‐grained  lock  –  Threads  can  traverse  in  parallel  

•  SAll  not  ideal  –  Long  chain  of  acquire/release  –  Inefficient  

Page 102: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul

OpAmisAc  SynchronizaAon  

•  Find  nodes  without  locking  •  Lock  nodes  •  Check  that  everything  is  OK  

Page 103: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul

OpAmisAc:  Traverse  without  Locking  

b d e a

add(c) Aha!

Page 104: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul

OpAmisAc:  Lock  and  Load  

b d e a

add(c)

Page 105: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul

Art of Multiprocessor Programming 105

OpAmisAc:  Lock  and  Load  

b d e a

add(c)

c

Page 106: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul

What  could  go  wrong?  

b d e a

add(c) Aha!

Page 107: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul

Art of Multiprocessor Programming 107

What  could  go  wrong?  

b d e a

add(c)

Page 108: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul

Art of Multiprocessor Programming 108

What  could  go  wrong?  

b d e a

remove(b)

Page 109: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul

Art of Multiprocessor Programming 109

What  could  go  wrong?  

b d e a

remove(b)

Page 110: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul

Art of Multiprocessor Programming 110

What  could  go  wrong?  

b d e a

add(c)

Page 111: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul

Art of Multiprocessor Programming 111

What  could  go  wrong?  

b d e a

add(c)

c

Page 112: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul

Art of Multiprocessor Programming 112

What  could  go  wrong?  

d e a

add(c) Uh-oh

Page 113: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul

Validate  –  Part  1  

b d e a

add(c) Yes, b still reachable from head

Page 114: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul

Art of Multiprocessor Programming 114

What  Else  Could  Go  Wrong?  

b d e a

add(c) Aha!

Page 115: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul

What  Else  Coould  Go  Wrong?  

b d e a

add(c) add(b’)

Page 116: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul

Art of Multiprocessor Programming 116

What  Else  Coould  Go  Wrong?  

b d e a

add(c) add(b’) b’

Page 117: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul

Art of Multiprocessor Programming 117

What  Else  Could  Go  Wrong?  

b d e a

add(c)

b’

Page 118: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul

Art of Multiprocessor Programming 118

What  Else  Could  Go  Wrong?  

b d e a

add(c)

c

Page 119: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul

Validate  Part  2  (while  holding  locks)  

b d e a

add(c) Yes, b still points to d

Page 120: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul

OpAmisAc:  LinearizaAon  Point  

b d e a

add(c) c

Page 121: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul

Same  AbstracAon  Map  

• S(head)  =  {  x  |  there  exists  a  such  that  

• a  reachable  from  head  and  • a.item    =  x  

}  

Page 122: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul

Invariants  

•  Careful:  we  may  traverse  deleted  nodes  •  But  we  establish  properAes  by  

– ValidaAon  – Aeer  we  lock  target  nodes  

Page 123: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul

Correctness  

•  If  – Nodes  b  and  c  both  locked  – Node  b  sAll  accessible  – Node  c  sAll  successor  to  b  

•  Then  – Neither  will  be  deleted  – OK  to  delete  and  return  true  

Page 124: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul

Unsuccessful  Remove  

a b d e

remove(c) Aha!

Page 125: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul

Validate  (1)  

a b d e

Yes, b still reachable from head

remove(c)

Page 126: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul

Validate  (2)  

a b d e

remove(c) Yes, b still points to d

Page 127: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul

OK  Computer  

a b d e

remove(c) return false

Page 128: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul

Correctness  

•  If  – Nodes  b  and  d  both  locked  – Node  b  sAll  accessible  – Node  d  sAll  successor  to  b  

•  Then  – Neither  will  be  deleted  – No  thread  can  add  c  aeer  b  – OK  to  return  false  

Page 129: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul

ValidaAon  

private boolean validate(Node pred, Node curry) { Node node = head; while (node.key <= pred.key) { if (node == pred) return pred.next == curr; node = node.next; } return false; }

Page 130: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul

private boolean validate(Node pred, Node curr) { Node node = head; while (node.key <= pred.key) { if (node == pred) return pred.next == curr; node = node.next; } return false; }

ValidaAon  

Predecessor & current nodes

Page 131: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul

private boolean validate(Node pred, Node curr) { Node node = head; while (node.key <= pred.key) { if (node == pred) return pred.next == curr; node = node.next; } return false; }

ValidaAon  

Begin at the beginning

Page 132: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul

private boolean validate(Node pred, Node curr) { Node node = head; while (node.key <= pred.key) { if (node == pred) return pred.next == curr; node = node.next; } return false; }

ValidaAon  

Search range of keys

Page 133: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul

private boolean validate(Node pred, Node curr) { Node node = head; while (node.key <= pred.key) { if (node == pred) return pred.next == curr; node = node.next; } return false; }

ValidaAon  

Predecessor reachable

Page 134: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul

private boolean validate(Node pred, Node curr) { Node node = head; while (node.key <= pred.key) { if (node == pred) return pred.next == curr; node = node.next; } return false; }

ValidaAon  

Is current node next?

Page 135: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul

private boolean validate(Node pred, Node curr) { Node node = head; while (node.key <= pred.key) { if (node == pred) return pred.next == curr; node = node.next; } return false; }

ValidaAon  

Otherwise move on

Page 136: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul

private boolean validate(Node pred, Node curr) { Node node = head; while (node.key <= pred.key) { if (node == pred) return pred.next == curr; node = node.next; } return false; }

ValidaAon  

Predecessor not reachable

Page 137: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul

Remove:  the  Traversal  

public boolean remove(T item) { int key = item.hashCode(); retry: while (true) { Node pred = head; Node curr = pred.next; while (curr.key <= key) { if (item == curr.item) break; pred = curr; curr = curr.next; } …

Page 138: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul

public boolean remove(T item) { int key = item.hashCode(); retry: while (true) { Node pred = head; Node curr = pred.next; while (curr.key <= key) { if (item == curr.item) break; pred = curr; curr = curr.next; } …

Remove:  the  Traversal  

Search key

Page 139: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul

public boolean remove(T item) { int key = item.hashCode(); retry: while (true) { Node pred = head; Node curr = pred.next; while (curr.key <= key) { if (item == curr.item) break; pred = curr; curr = curr.next; } …

Remove:  the  Traversal  

Retry on synchronization conflict (If validation fails, we come back here.)

Page 140: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul

public boolean remove(T item) { int key = item.hashCode(); retry: while (true) { Node pred = head; Node curr = pred.next; while (curr.key <= key) { if (item == curr.item) break; pred = curr; curr = curr.next; } …

Remove:  the  Traversal  

Examine predecessor and current nodes

Page 141: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul

public boolean remove(T item) { int key = item.hashCode(); retry: while (true) { Node pred = head; Node curr = pred.next; while (curr.key <= key) { if (item == curr.item) break; pred = curr; curr = curr.next; } …

Remove:  the  Traversal  

Search by key

Page 142: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul

public boolean remove(T item) { int key = item.hashCode(); retry: while (true) { Node pred = head; Node curr = pred.next; while (curr.key <= key) { if (item == curr.item) break; pred = curr; curr = curr.next; } …

Remove:  the  Traversal  

Stop if we find item

Page 143: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul

public boolean remove(T item) { int key = item.hashCode(); retry: while (true) { Node pred = head; Node curr = pred.next; while (curr.key <= key) { if (item == curr.item) break; pred = curr; curr = curr.next; } …

Remove:  the  Traversal  

Move along

Page 144: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul

On  Exit  from  Inner  Loop  

•  If  item  is  present  –  curr  holds  item  –  pred  just  before  curr  

•  If  item  is  absent  –  curr  has  first  higher  key  –  pred  just  before  curr  

•  Assuming  no  synchronizaAon  problems  

Page 145: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul

Remove  ConAnue:  the  DeleAon  (Aeer  ExisAng  Inner  Loop)  

pred.lock(); curr.lock(); try { if (validate(pred,curr) { if (curr.item == item) { pred.next = curr.next; return true; } else { return false; }}} finally {

pred.unlock(); curr.unlock();

}}}

Page 146: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul

pred.lock(); curr.lock(); try { if (validate(pred,curr) { if (curr.item == item) { pred.next = curr.next; return true; } else { return false; }}} finally {

pred.unlock(); curr.unlock();

}}}

Always unlock

Remove  ConAnue:  the  DeleAon  (Aeer  ExisAng  Inner  Loop)  

Page 147: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul

pred.lock(); curr.lock(); try { if (validate(pred,curr) { if (curr.item == item) { pred.next = curr.next; return true; } else { return false; }}} finally {

pred.unlock(); curr.unlock();

}}}

Remove  ConAnue:  the  DeleAon  (Aeer  ExisAng  Inner  Loop)  

Check for synchronization

conflicts

Page 148: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul

pred.lock(); curr.lock(); try { if (validate(pred,curr) { if (curr.item == item) { pred.next = curr.next; return true; } else { return false; }}} finally {

pred.unlock(); curr.unlock();

}}}

Remove  ConAnue:  the  DeleAon  (Aeer  ExisAng  Inner  Loop)  

target found, remove node

Page 149: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul

pred.lock(); curr.lock(); try { if (validate(pred,curr) { if (curr.item == item) { pred.next = curr.next; return true; } else { return false; }}} finally {

pred.unlock(); curr.unlock();

}}}

Remove  ConAnue:  the  DeleAon  (Aeer  ExisAng  Inner  Loop)  

target not found

Page 150: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul

OpAmisAc  List  

•  Limited  hot-­‐spots  – Holding  locks  only  on  the  targets  of  add(),  remove(),  contains()  

– No  contenAon  on  traversals  –  Traversals  are  "wait-­‐free"    (What's  wait  free?)  

Page 151: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul

Progress  CondiAons      

•  Deadlock-­‐free:  some  thread  trying  to  acquire  the  lock  eventually  succeeds.  

•  Starva'on-­‐free:  every  thread  trying  to  acquire  the  lock  eventually  succeeds.  

•  Lock-­‐free:  some  thread  calling  a  method  eventually  returns.  

•  Wait-­‐free:  every  thread  calling  a  method  eventually  returns.  

 

Page 152: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul

Progress  CondiAons      

Wait-free

Lock-free

Starvation-free

Deadlock-free

Everyone makes

progress

Non-Blocking Blocking

Someone makes

progress

Page 153: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul

So  Far,  So  Good  

• Much  less  lock  acquisiAon/release  –  Performance  –  Concurrency  

•  Problems  – Need  to  traverse  list  twice  – contains()  method  acquires  locks  

Page 154: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul

EvaluaAon  

•  OpAmisAc  is  effecAve  if  –  cost  of  scanning  twice  without  locks  

is  less  than  –  cost  of  scanning  once  with  locks  

•  Drawback  – contains()  acquires  locks  –  90%  of  calls  in  many  apps  

Page 155: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul

Lazy  List  

•  Like  opAmisAc,  except  –  Scan  once  – contains(x)  never  locks  …  

•  Key  insight  –  Removing  nodes  causes  trouble  – Do  it  “lazily”  

Page 156: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul

Lazy  List  

• remove() –  Scans  list  (as  before)  –  Locks  predecessor  &  current  (as  before)  

•  Logical  delete  – Marks  current  node  as  removed  (new!)  

•  Physical  delete  –  Redirects  predecessor’s  next  (as  before)  

Page 157: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul

Lazy  Removal  

a a b c d

Page 158: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul

c

Lazy  Removal  

a a b d

Present in list

Page 159: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul

c

Art of Multiprocessor Programming 159

Lazy  Removal  

a a b d

Logically deleted

Page 160: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul

Lazy  Removal  

a a b c d

Physically deleted

Page 161: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul

Lazy  Removal  

a a b d

Physically deleted

Page 162: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul

Lazy  List  

•  All  Methods  –  Scan  through  locked  and  marked  nodes  –  add  and  remove  sAll  locks  pred  and  curr,  but  not  contain  

– Adding  /  removing  a  node  doesn’t  slow  down  contain()  …  

Page 163: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul

Lazy  List  ValidaAon  

•  No  need  to  rescan  list!  •  Check  that  pred  is  not  marked  •  Check  that  curr  is  not  marked  •  Check  that  pred  points  to  curr  

Page 164: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul

New  AbstracAon  Map  

• S(head)  =  {  x  |  there  exists  node  a  such  that  

• a  reachable  from  head  and  • a.item    =  x  and  • a  is  unmarked  

}  

Page 165: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul

Invariant  

•  If  an  item  is  not  marked,  it  is  reachable  from  head  and  sAll  in  the  set.  

•  Any  unmarked  reachable  node  remains  reachable  even  if  its  predecessor  is  logically  or  physically  removed  

Page 166: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul

ValidaAon  

private boolean validate(Node pred, Node curr) { return !pred.marked && !curr.marked && pred.next == curr); }

Page 167: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul

private boolean validate(Node pred, Node curr) { return !pred.marked && !curr.marked && pred.next == curr); }

List  Validate  Method  

Predecessor not Logically removed

Page 168: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul

private boolean validate(Node pred, Node curr) { return !pred.marked && !curr.marked && pred.next == curr); }

List  Validate  Method  

Current not Logically removed

Page 169: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul

private boolean validate(Node pred, Node curr) { return !pred.marked && !curr.marked && pred.next == curr); }

List  Validate  Method  

Predecessor still Points to current

Page 170: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul

ValidaAon  

private boolean validate(Node pred, Node curr) { return !pred.marked && !curr.marked && pred.next == curr); }

Both  the  next  and  marked  fields  need  to  be  vola<le!  

Page 171: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul

Remove:  the  DeleAon  ... // the traversal pred.lock(); curr.lock(); try { if (validate(pred,curr) { if (curr.key == key) { curr.marked = true; pred.next = curr.next; return true; } else { return false; }}} finally {

pred.unlock(); curr.unlock();

}}}

Page 172: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul

Remove:  the  DeleAon  ... // the traversal pred.lock(); curr.lock(); try { if (validate(pred,curr) { if (curr.key == key) { curr.marked = true; pred.next = curr.next; return true; } else { return false; }}} finally {

pred.unlock(); curr.unlock();

}}}

Validate as before

Page 173: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul

Remove:  the  DeleAon  ... // the traversal pred.lock(); curr.lock(); try { if (validate(pred,curr) { if (curr.key == key) { curr.marked = true; pred.next = curr.next; return true; } else { return false; }}} finally {

pred.unlock(); curr.unlock();

}}}

Key found

Page 174: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul

Remove:  the  DeleAon  ... // the traversal pred.lock(); curr.lock(); try { if (validate(pred,curr) { if (curr.key == key) { curr.marked = true; pred.next = curr.next; return true; } else { return false; }}} finally {

pred.unlock(); curr.unlock();

}}}

Logical remove

Page 175: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul

Remove:  the  DeleAon  ... // the traversal pred.lock(); curr.lock(); try { if (validate(pred,curr) { if (curr.key == key) { curr.marked = true; pred.next = curr.next; return true; } else { return false; }}} finally {

pred.unlock(); curr.unlock();

}}}

physical remove

Page 176: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul

Contains  

public boolean contains(T item) { int key = item.hashCode(); Node curr = head; while (curr.key < key) { curr = curr.next; } return curr.key == key && !curr.marked; }

Page 177: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul

public boolean contains(T item) { int key = item.hashCode(); Node curr = head; while (curr.key < key) { curr = curr.next; } return curr.key == key && !curr.marked; }

Contains  

Start at the head

Page 178: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul

public boolean contains(T item) { int key = item.hashCode(); Node curr = head; while (curr.key < key) { curr = curr.next; } return curr.key == key && !curr.marked; }

Contains  

Search key range

Page 179: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul

public boolean contains(T item) { int key = item.hashCode(); Node curr = head; while (curr.key < key) { curr = curr.next; } return curr.key == key && !curr.marked; }

Contains  

Traverse without locking (nodes may have been removed)

Page 180: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul

public boolean contains(T item) { int key = item.hashCode(); Node curr = head; while (curr.key < key) { curr = curr.next; } return curr.key == key && !curr.marked; }

Contains  

Present and undeleted?

Page 181: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul

Summary:  Wait-­‐free  Contains  

a 0 0 a b c e d

Use Mark bit + list ordering 1.  Not marked à in the set 2.  Marked or missing à not in the set 3.  Traverse the list only once!

Page 182: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul

EvaluaAon  

•  Good:  – contains()  doesn’t  lock  –  In  fact,  its  wait-­‐free!    – Good  because  typically  high  %  contains()  – Uncontended  calls  to  add  and  remove  don’t  re-­‐traverse  

•  Bad  –  Contended  add()  and  remove()  calls  must  re-­‐traverse  

–  Traffic  jam  if  one  thread  delays  

Page 183: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul

Traffic  Jam  

•  Any  concurrent  data  structure  based  on  mutual  exclusion  has  a  weakness  

•  If  one  thread  –  Enters  criAcal  secAon  – And  “eats  the  big  muffin”  

• Cache  miss,  page  fault,  descheduled  …  

–  Everyone  else  using  that  lock  is  stuck!  – Need  to  trust  the  scheduler….  

Page 184: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul

Lock-­‐Free  Data  Structures  

•  No  maQer  what  …  – Guarantees  minimal  progress  in  any  execuAon  –  i.e.  Some  thread  will  always  complete  a  method  call  

–  Even  if  others  halt  at  malicious  Ames  –  Implies  that  implementaAon  can’t  use  locks  

Page 185: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul

Lock-­‐free  Lists  

•  Next  logical  step  – Wait-­‐free  contains() –  lock-­‐free  add()  and  remove()

•  Use  only  compareAndSet() – What  could  go  wrong?  

Page 186: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul

public abstract class CASObject { private int value; public boolean synchronized compareAndSet(int expected, int update) { if (value==expected) { value = update; return true; } return false; } … }

compareAndSet  

Art of Multiprocessor Programming

Page 187: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul

public abstract class CASObject { private int value; public boolean synchronized compareAndSet(int expected, int update) { if (value==expected) { value = update; return true; } return false; } … }

compareAndSet  

If value is as expected, …

Page 188: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul

public abstract class CASOBJECT{ private int value; public boolean synchronized compareAndSet(int expected, int update) { if (value==expected) { value = update; return true; } return false; } … }

compareAndSet  

… replace it

Page 189: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul

public abstract class RMWRegister { private int value; public boolean synchronized compareAndSet(int expected, int update) { if (value==expected) { value = update; return true; } return false; } … }

compareAndSet  

Report success

Page 190: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul

public abstract class RMWRegister { private int value; public boolean synchronized compareAndSet(int expected, int update) { if (value==expected) { value = update; return true; } return false; } … }

compareAndSet  

Otherwise report failure

Page 191: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul

a 0 0 0 a b c 0 e 1 c

Logical Removal

Physical Removal Use CAS to verify pointer is correct

Not enough!

Lock-­‐free  Lists  

Page 192: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul

Problem…  

a 0 0 0 a b c 0 e 1 c

T1: Logical Removal

T1: Physical Removal 0 d

T2: Node added

Lost  Update!  

Page 193: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul

The  SoluAon:  Combine  Bit  and  Pointer  

a 0 0 0 a b c 0 e 1 c

Logical Removal = Set Mark Bit

Physical Removal CAS

0 d

Mark-Bit and Pointer are CASed together (AtomicMarkableReference)

Fail CAS: Node not added after logical Removal

Page 194: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul

Marking  a  Node  

•  AtomicMarkableReference  class  –  Java.util.concurrent.atomic  package  

address F

mark bit

Reference

Page 195: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul

ExtracAng  Reference  &  Mark  

public Object get(boolean[] marked);

Page 196: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul

ExtracAng  Reference  &  Mark  

public Object get(boolean[] marked);

Returns reference

Returns mark at array index 0!

Page 197: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul

ExtracAng  Mark  Only  

public boolean isMarked();

Value of mark

Page 198: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul

Changing  State  

public boolean compareAndSet( Object expectedRef, Object updateRef, boolean expectedMark, boolean updateMark);

Page 199: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul

Changing  State  

public boolean compareAndSet( Object expectedRef, Object updateRef, boolean expectedMark, boolean updateMark);

If this is the current reference …

And this is the current mark …

Page 200: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul

Changing  State  

public boolean compareAndSet( Object expectedRef, Object updateRef, boolean expectedMark, boolean updateMark);

…then change to this new reference …

… and this new mark

Page 201: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul

Changing  State  

public boolean attemptMark( Object expectedRef, boolean updateMark);

Page 202: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul

Changing  State  

public boolean attemptMark( Object expectedRef, boolean updateMark);

If this is the current reference …

Page 203: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul

Changing  State  

public boolean attemptMark( Object expectedRef, boolean updateMark);

.. then change to this new mark.

Page 204: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul

b CAS

Removing  a  Node  

a c d

remove(c)

Page 205: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul

Removing  a  Node  

a b d

remove(b)

c

failed

CAS CAS

remove(c)

Page 206: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul

Traversing  the  List  

•  Q:  what  do  you  do  when  you  find  a  “logically”  deleted  node  in  your  path?  

•  A:  finish  the  job.  –  CAS  the  predecessor’s  next  field  –  Proceed  (repeat  as  needed)  

Page 207: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul

Lock-­‐Free  Traversal  (only  Add  and  Remove)  

a b c d CAS

Uh-oh

pred curr pred curr

remove(c)

Page 208: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul

The  Window  Class  

class Window { public Node pred; public Node curr; Window(Node pred, Node curr) { pred = pred; curr = curr; } }

Page 209: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul

The  Window  Class  

class Window { public Node pred; public Node curr; Window(Node pred, Node curr) { pred = pred; curr = curr; } }

A container for pred and current values

Page 210: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul

Using  the  Find  Method  

Window window = find(head, key); Node pred = window.pred; curr = window.curr;

Page 211: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul

Using  the  Find  Method  

Window window = find(head, key); Node pred = window.pred; curr = window.curr;

Find returns window

Page 212: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul

Using  the  Find  Method  

Window window = find(head, key); Node pred = window.pred; curr = window.curr;

Extract pred and curr

Page 213: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul

The  Find  Method  

Window window = find(item);

At some instant,

pred curr succ

item or …

Page 214: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul

The  Find  Method  

Window window = find(item);

At some instant,

pred curr= null

succ

item not in list

Page 215: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul

Remove  

public boolean remove(T item) { Boolean snip; while (true) { Window window = find(head, key); Node pred = window.pred, curr = window.curr; if (curr.key != key) { return false; } else { Node succ = curr.next.getReference(); snip = curr.next.compareAndSet(succ, succ, false true); if (!snip) continue; pred.next.compareAndSet(curr, succ, false, false); return true; }}}

Page 216: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul

Remove  

public boolean remove(T item) { Boolean snip; while (true) { Window window = find(head, key); Node pred = window.pred, curr = window.curr; if (curr.key != key) { return false; } else { Node succ = curr.next.getReference(); snip = curr.next.compareAndSet (succ, succ, false, true); if (!snip) continue; pred.next.compareAndSet(curr, succ, false, false); return true; }}}

Keep trying

Page 217: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul

Remove  

public boolean remove(T item) { Boolean snip; while (true) { Window window = find(head, key); Node pred = window.pred, curr = window.curr; if (curr.key != key) { return false; } else { Node succ = curr.next.getReference(); snip = curr.next.compareAndSet (succ, succ, false, true); if (!snip) continue; pred.next.compareAndSet(curr, succ, false, false); return true; }}} Find neighbors

Page 218: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul

Remove  

public boolean remove(T item) { Boolean snip; while (true) { Window window = find(head, key); Node pred = window.pred, curr = window.curr; if (curr.key != key) { return false; } else { Node succ = curr.next.getReference(); snip = curr.next.compareAndSet(succ, succ, false, true); if (!snip) continue; pred.next.compareAndSet(curr, succ, false, false); return true; }}} Not there …

Page 219: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul

Remove  

public boolean remove(T item) { Boolean snip; while (true) { Window window = find(head, key); Node pred = window.pred, curr = window.curr; if (curr.key != key) { return false; } else { Node succ = curr.next.getReference(); snip = curr.next.compareAndSet(succ, succ, false, true); if (!snip) continue; pred.next.compareAndSet(curr, succ, false, false); return true; }}}

Try to mark node as deleted

Page 220: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul

Remove  

public boolean remove(T item) { Boolean snip; while (true) { Window window = find(head, key); Node pred = window.pred, curr = window.curr; if (curr.key != key) { return false; } else { Node succ = curr.next.getReference(); snip = curr.next.compareAndSet(succ, succ, false, true); if (!snip) continue; pred.next.compareAndSet(curr, succ, false, false); return true; }}}

If it doesn’t work, just retry, if it

does, job essentially done

Page 221: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul

Remove  

public boolean remove(T item) { Boolean snip; while (true) { Window window = find(head, key); Node pred = window.pred, curr = window.curr; if (curr.key != key) { return false; } else { Node succ = curr.next.getReference(); snip = curr.next.compareAndSet(succ, succ, false, true); if (!snip) continue; pred.next.compareAndSet(curr, succ, false, false); return true; }}}

Try to advance reference (if we don’t succeed, someone else did or will).

a

Page 222: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul

Remove  

public boolean remove(T item) { Boolean snip; while (true) { Window window = find(head, key); Node pred = window.pred, curr = window.curr; if (curr.key != key) { return false; } else { Node succ = curr.next.getReference(); snip = curr.next.compareAndSet(succ, succ, false true); if (!snip) continue; pred.next.compareAndSet(curr, succ, false, false); return true; }}}

Linearization point if removal is successful

Page 223: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul

Remove  

public boolean remove(T item) { Boolean snip; while (true) { Window window = find(head, key); Node pred = window.pred, curr = window.curr; if (curr.key != key) { return false; } else { Node succ = curr.next.getReference(); snip = curr.next.compareAndSet(succ, succ, false true); if (!snip) continue; pred.next.compareAndSet(curr, succ, false, false); return true; }}}

Linearization point is when we found this node (in Find()) if removal returns false.

Page 224: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul

Add  

public boolean add(T item) { boolean splice; while (true) { Window window = find(head, key); Node pred = window.pred, curr = window.curr; if (curr.key == key) { return false; } else { Node node = new Node(item); node.next = new AtomicMarkableRef(curr, false); if (pred.next.compareAndSet(curr, node, false, false)) {return true;} }}}

Page 225: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul

Add  

public boolean add(T item) { boolean splice; while (true) { Window window = find(head, key); Node pred = window.pred, curr = window.curr; if (curr.key == key) { return false; } else { Node node = new Node(item); node.next = new AtomicMarkableRef(curr, false); if (pred.next.compareAndSet(curr, node, false, false)) {return true;} }}} Item already there

Page 226: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul

Add  

public boolean add(T item) { boolean splice; while (true) { Window window = find(head, key); Node pred = window.pred, curr = window.curr; if (curr.key == key) { return false; } else { Node node = new Node(item); node.next = new AtomicMarkableRef(curr, false); if (pred.next.compareAndSet(curr, node, false, false)) {return true;} }}}

create new node

Page 227: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul

Add  

public boolean add(T item) { boolean splice; while (true) { Window window = find(head, key); Node pred = window.pred, curr = window.curr; if (curr.key == key) { return false; } else { Node node = new Node(item); node.next = new AtomicMarkableRef(curr, false); if (pred.next.compareAndSet(curr, node, false, false)) {return true;} }}}

Install new node, else retry loop

Page 228: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul

Add  

public boolean add(T item) { boolean splice; while (true) { Window window = find(head, key); Node pred = window.pred, curr = window.curr; if (curr.key == key) { return false; } else { Node node = new Node(item); node.next = new AtomicMarkableRef(curr, false); if (pred.next.compareAndSet(curr, node, false, false)) {return true;} }}}

Linearization point if add is successful

Page 229: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul

Add  

public boolean add(T item) { boolean splice; while (true) { Window window = find(head, key); Node pred = window.pred, curr = window.curr; if (curr.key == key) { return false; } else { Node node = new Node(item); node.next = new AtomicMarkableRef(curr, false); if (pred.next.compareAndSet(curr, node, false, false)) {return true;} }}}

Linearization point is when we found this node (in Find()) if removal returns false.

Page 230: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul

Wait-­‐free  Contains  

public boolean contains(T item) { boolean marked; int key = item.hashCode(); Node curr = head; while (curr.key < key) curr = curr.next; Node succ = curr.next.get(marked); return (curr.key == key && !marked[0]) }

Page 231: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul

Wait-­‐free  Contains  

public boolean contains(T item) { boolean marked; int key = item.hashCode(); Node curr = head; while (curr.key < key) curr = curr.next; Node succ = curr.next.get(marked); return (curr.key == key && !marked[0]) }

Only difference is that we get and check marked

Page 232: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul

Wait-­‐free  Contains  

public boolean contains(T item) { boolean marked; int key = item.hashCode(); Node curr = head; while (curr.key < key) curr = curr.next; Node succ = curr.next.get(marked); return (curr.key == key && !marked[0]) }

Page 233: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul

Lock-­‐free  Find  public Window find(Node head, int key) { Node pred = null, curr = null, succ = null; boolean[] marked = {false}; boolean snip; retry: while (true) { pred = head; curr = pred.next.getReference(); while (true) { succ = curr.next.get(marked); while (marked[0]) { … } if (curr.key >= key)

return new Window(pred, curr); pred = curr; curr = succ; } }}

Page 234: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul

Lock-­‐free  Find  public Window find(Node head, int key) { Node pred = null, curr = null, succ = null; boolean[] marked = {false}; boolean snip; retry: while (true) { pred = head; curr = pred.next.getReference(); while (true) { succ = curr.next.get(marked); while (marked[0]) { … } if (curr.key >= key) return new Window(pred, curr); pred = curr; curr = succ; } }}

If list changes while traversed,

start over

Page 235: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul

public Window find(Node head, int key) { Node pred = null, curr = null, succ = null; boolean[] marked = {false}; boolean snip; retry: while (true) { pred = head; curr = pred.next.getReference(); while (true) { succ = curr.next.get(marked); while (marked[0]) { … } if (curr.key >= key) return new Window(pred, curr); pred = curr; curr = succ; } }}

Lock-­‐free  Find  Start looking from head

Page 236: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul

public Window find(Node head, int key) { Node pred = null, curr = null, succ = null; boolean[] marked = {false}; boolean snip; retry: while (true) { pred = head; curr = pred.next.getReference(); while (true) { succ = curr.next.get(marked); while (marked[0]) { … } if (curr.key >= key) return new Window(pred, curr); pred = curr; curr = succ; } }}

Lock-­‐free  Find  

Move down the list

Page 237: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul

public Window find(Node head, int key) { Node pred = null, curr = null, succ = null; boolean[] marked = {false}; boolean snip; retry: while (true) { pred = head; curr = pred.next.getReference(); while (true) { succ = curr.next.get(marked); while (marked[0]) { … } if (curr.key >= key) return new Window(pred, curr); pred = curr; curr = succ; } }}

Lock-­‐free  Find  

Get ref to successor and current deleted bit

Page 238: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul

public Window find(Node head, int key) { Node pred = null, curr = null, succ = null; boolean[] marked = {false}; boolean snip; retry: while (true) { pred = head; curr = pred.next.getReference(); while (true) { succ = curr.next.get(marked); while (marked[0]) { … } if (curr.key >= key) return new Window(pred, curr); pred = curr; curr = succ; } }}

Lock-­‐free  Find  

Try to remove deleted nodes in path…code details soon

Page 239: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul

public Window find(Node head, int key) { Node pred = null, curr = null, succ = null; boolean[] marked = {false}; boolean snip; retry: while (true) { pred = head; curr = pred.next.getReference(); while (true) { succ = curr.next.get(marked); while (marked[0]) { … } if (curr.key >= key) return new Window(pred, curr); pred = curr; curr = succ; } }}

Lock-­‐free  Find  

If curr key that is greater or equal, return pred and curr

Page 240: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul

public Window find(Node head, int key) { Node pred = null, curr = null, succ = null; boolean[] marked = {false}; boolean snip; retry: while (true) { pred = head; curr = pred.next.getReference(); while (true) { succ = curr.next.get(marked); while (marked[0]) { … } if (curr.key >= key) return new Window(pred, curr); pred = curr; curr = succ; } }}

Lock-­‐free  Find  

Otherwise advance window and loop again

Page 241: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul

Lock-­‐free  Find  

retry: while (true) { … while (marked[0]) { snip = pred.next.compareAndSet(curr, succ, false, false); if (!snip) continue retry; curr = succ; succ = curr.next.get(marked); } …

Page 242: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul

retry: while (true) { … while (marked[0]) { snip = pred.next.compareAndSet(curr, succ, false, false); if (!snip) continue retry; curr = succ; succ = curr.next.get(marked); } …

Lock-­‐free  Find  Try to snip out node

Page 243: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul

retry: while (true) { … while (marked[0]) { snip = pred.next.compareAndSet(curr, succ, false, false); if (!snip) continue retry; curr = succ; succ = curr.next.get(marked); } …

Lock-­‐free  Find  if predecessor’s next field changed,

retry whole traversal

Page 244: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul

Lock-­‐free  Find  

retry: while (true) { … while (marked[0]) { snip = pred.next.compareAndSet(curr, succ, false, false); if (!snip) continue retry; curr = succ; succ = curr.next.get(marked); } …

Otherwise move on to check if next node deleted

Page 245: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul

Performance    

•  Different  list-­‐based  set  implementaions  •  SunFire  6800  (bus  based  cache  coherence)    •  16-­‐node  machine,  each  1.2  GHz  •  Vary    percentage  of  contains()  calls  

Page 246: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul

High  Contains  RaAo  

Lock-free Lazy list

Coarse Grained Fine Lock-coupling

Ops/sec (90% contain, 9% add, 1% remove)

Page 247: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul

Low  Contains  RaAo      

Lock-free

Lazy list

Coarse Grained Fine Lock-coupling

Ops/sec (50% contain, 45% add, 5% remove)

Page 248: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul

As  Contains  RaAo  Increases      

Lock-free Lazy list

Coarse Grained Fine Lock-coupling

% Contains()

Ops/sec using 32 threads

Page 249: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul

“To  Lock  or  Not  to  Lock”  

•  Locking  vs.  Non-­‐blocking:  –  Depending  on  the  applicaAon  usage  

•  The  answer:  nobler  to  compromise  –  Example:  Lazy  list  combines    blocking  add() and  remove()and  a  wait-­‐free  contains()

–  Remember:  Blocking/non-­‐blocking  is  a  property  of  a  method  

Page 250: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul

This work is licensed under a Creative Commons Attribution-ShareAlike 2.5 License.

•  You are free: –  to Share — to copy, distribute and transmit the work –  to Remix — to adapt the work

•  Under the following conditions: –  Attribution. You must attribute the work to “The Art of

Multiprocessor Programming” (but not in any way that suggests that the authors endorse you or your use of the work).

–  Share Alike. If you alter, transform, or build upon this work, you may distribute the resulting work only under the same, similar or a compatible license.

•  For any reuse or distribution, you must make clear to others the license terms of this work. The best way to do this is with a link to –  http://creativecommons.org/licenses/by-sa/3.0/.

•  Any of the above conditions can be waived if you get permission from the copyright holder.

•  Nothing in this license impairs or restricts the author's moral rights.

Page 251: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul

Business  as  Usual  

a b c

Page 252: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul

Business  as  Usual  

a b c

Page 253: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul

Business  as  Usual  

a b c

Page 254: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul

Business  as  Usual  

a b c

remove(b)

Page 255: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul

Business  as  Usual  

a b c

a not marked

Page 256: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul

Business  as  Usual  

a b c

a still points

to b

Page 257: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul

Business  as  Usual  

a b c

Logical delete

Page 258: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul

Business  as  Usual  

a b c

physical delete

Page 259: ConcurrentLinked)Lists) · 2018-01-17 · ConcurrentLinked)Lists) Acknowledgement:.. Slides.adopted.from.the.companion.slides.for.the.book. "The.Art.of.Mul

Business  as  Usual  

a b c