Professional Documents
Culture Documents
Chapter4 ConcurrentDataStructures
Chapter4 ConcurrentDataStructures
Chapter4 ConcurrentDataStructures
q That you mention their source, after all, I would like people to use my book!
q That you note that they are adapted from (or perhaps identical to)
my slides, and note my copyright of this material.
e s
li ur
o fa
n
deadlock starvation FIFO obstruction non-blocking wait
(livelock) freedom (+ df ) freedom freedom
freedom
Chapter 4 Synchronization Algorithms and Concurrent Programming 7
Gadi Taubenfeld © 2014
Obstruction-freedom
P1 P2 P3 P4
Done
P1 P2 P3 P4
Done
P1 P2 P3 P4
P1 P2 P3 P4
Ø Linearizability
Ø Sequential Consistency
Ø …
q.enq( )
q.deq( )
P1 q.enq q.deq
P2 q.enq q.deq
time
P1 q.enq q.deq
P2 q.enq q.deq
time
P1 q.enq q.deq
P2 q.enq q.deq
time
P1 q.enq q.deq
P2 q.enq q.deq
time
Sequential
specification
concurrent Sequential
executions executions
All executions
Synchronization Algorithms and Concurrent Programming
Chapter 4 22
Gadi Taubenfeld © 2014
Linearizability
for an object
Ø Need to preserve real time order
P1 q.enq q.deq
P2 q.enq q.deq
q.enq
P1 q.enq q.deq
P2 q.enq q.deq
time
P1 q.enq q.deq
P2 q.enq q.deq
time
P1 q.enq q.deq
P2 q.enq q.deq
time
P1 q.enq q.deq
P2 q.enq q.deq
time
Sequential Consistency
Is this execution linearizable? No, BUT …
P1 q.enq q.deq
P2 q.enq q.deq
time
P1 q.enq q.deq
P2 q.enq
time
P1 p.enq
P2 p.enq p.deq
time
P1 p.enq
P2 p.enq p.deq
time
time
time
Proof: ...
enqueue dequeue
tail head
Dummy Node
tail head
tail head
tail head
tail head
tail head
tail head
CAS ( A, B, C )
local:= A
CAS ( A, B, C ) CAS (A, local, 100)
In the following, we will ignore the ABA problem and assume that it
is resolved
Chapter 4 using additional tag fields.
Synchronization Algorithms and Concurrent Programming 50
Gadi Taubenfeld © 2014
Section 4.5
A Non-blocking Queue
repeat
ltail ç tail true
false
lnext ltail done
lnext ç ltail.next
if ltail = tail then
if lnext = NULL then
if CAS (ltail.next, lnext, lnode) then done ç true fi
else CAS (tail, ltail, lnext) fi fi
until done = true try to link lnode to
the end
enqueue failed: tryof
tothe queue
swing tail to
CAS (tail, ltail, lnode) a node
enqueue that
done: trywas inserted
to swing tailby
to some
the inserted node.
other process
repeat
ltail ç tail true
false
lnext ltail done
lnext ç ltail.next
if ltail = tail then
if lnext = NULL then
if CAS (ltail.next, lnext, node) then done ç true fi
else CAS (tail, ltail, lnext) fi fi
until done = true
Question: Would the enqueue
CAS (tail, ltail, lnode) operation be correct if the last
line is omitted?
Chapter 4
Answer: Yes, but…
Synchronization Algorithms and Concurrent Programming
Gadi Taubenfeld © 2014
53
lnode tail head
Enqueue
repeat
ltail ç tail true
false
lnext ltail done
lnext ç ltail.next
Question: Would the enqueue
if ltail = tail then
operation be correct if this line
if lnext = NULL then is omitted?
if CAS (ltail.next, lnext, node) then done ç true fi
else CAS (tail, ltail, lnext) fi fi
until done = true No, when it is assumed that cells can
be released and then reallocated.
CAS (tail, ltail, lnode) Otherwise, yes.
repeat lvalue
lhead ç head
true
false
ltail ç tail
lnext ltail done lhead
lnext ç lhead.next
if lhead = head then
if lhead = ltail then /* empty or tail behind?
if lnext = NULL then return(NULL) fi ; /* empty
CAS (tail, ltail, lnext) /* try to advance tail
else lvalue ç lnext.value /* no need to deal with tail
if CAS (head, lhead, lnext) then done ç true fi fi fi
until done = true
free(lhead) ; return(lvalue)
Chapter 4 Synchronization Algorithms and Concurrent Programming 55
Gadi Taubenfeld © 2014
tail head
Dequeue
repeat lvalue
lhead ç head
true
false
ltail ç tail
lnext ltail done lhead
lnext ç lhead.next
Question: Would the dequeue operation
if lhead = head then be correct if this line is omitted?
if lhead = ltail then /* empty or tail behind?
if lnext = NULL then return(NULL) fi ; /* empty
CAS (tail, ltail, lnext) /* try to advance tail
else lvalue ç lnext.value /* no need to deal with tail
if CAS (head, lhead, lnext) then done ç true fi fi fi
until done = true
free(lhead) ; return(lvalue)
Chapter 4 Synchronization Algorithms and Concurrent Programming 56
Gadi Taubenfeld © 2014
A note about
Memory Barriers
x 0 y 0
Process A Process B
write.x(1) write.y(1)
read.y read.x
x 0 y 0
Process A Process B
write.x(1) write.y(1)
read.y read.x
x 0 y 0
Process A Process B
write.x(1) write.y(1)
read.y read.x
Binary Semaphores
Operations down(S)
m if S > 0, then S = 0 otherwise,
down(S)
1 the process is blocked until the
up(S)
value becomes greater than 0.
m Testing and decrementing the
Types
semaphore are executed
unfair semaphore atomically without interruption.
weak semaphore
strong semaphore up(S)
m S=1
down(S)
critical section 1
up(S)
Weak semaphores
queue
S.0 1
10
S.1 1
Shared bits
queue 0
S.0 10
1 S.1 0
1 empty false
local bits
Weak semaphores
queue
S.0 1
1
S.1 1
Shared bits
queue 0
S.0 0
1 S.1 0 empty false
local bits
empty myqueue
true
false otherqueue
critical
section
Chapter 4 Synchronization Algorithms and Concurrent Programming 65
Gadi Taubenfeld © 2014
Semaphores
Constant Space Starvation-free Algorithm
Weak semaphores
queue
S.0 1
1
S.1 1
Shared bits
queue 0
S.0 1 S.1 0 empty false
local bits
empty myqueue
true
false otherqueue
critical
section
Chapter 4 Synchronization Algorithms and Concurrent Programming 66
Gadi Taubenfeld © 2014
Semaphores
Constant Space Starvation-free Algorithm
Weak semaphores
queue
S.0 1
1
S.1 1
Shared bits
queue 0
S.0 10
1 S.1 0 empty false
local bits
empty myqueue
true
false otherqueue
critical
section
Chapter 4 Synchronization Algorithms and Concurrent Programming 67
Gadi Taubenfeld © 2014
Semaphores
Constant Space Starvation-free Algorithm
Weak semaphores
queue
S.0 1
1
S.1 1
Shared bits
queue 0
S.0 1 S.1 0 empty false
local bits
empty myqueue
true
false otherqueue
critical
section
Chapter 4 Synchronization Algorithms and Concurrent Programming 68
Gadi Taubenfeld © 2014
Semaphores
Constant Space Starvation-free Algorithm
Weak semaphores
queue
S.0 1
1
S.1 1
Shared bits
queue 0
S.0 10
1 S.1 0 empty false
local bits
empty myqueue
true
false otherqueue
critical
section
Chapter 4 Synchronization Algorithms and Concurrent Programming 69
Gadi Taubenfeld © 2014
Semaphores
Constant Space Starvation-free Algorithm
Weak semaphores
queue
S.0 1
1
S.1 1
Shared bits
queue 0
S.0 0
1 S.1 10 empty false
local bits
empty myqueue
true otherqueue
critical
section
Chapter 4 Synchronization Algorithms and Concurrent Programming 70
Gadi Taubenfeld © 2014
Semaphores
Constant Space Starvation-free Algorithm
myeuque = queue Weak semaphores
down (S.myqueue)
if queue = myqueue then S.0 1
otherqueue = 1 – myqueue S.1 1
down(S.otherqueue)
queue = otherqueue Shared bits
repeat
empty = true queue 0
up(S.myqueue) empty false
down(S.myqueue)
until empty local bits
critical section
up(S.otherqueue) myqueue
else empty = false otherqueue
critical section fi
up(S.myqueue)
Chapter 4 Synchronization Algorithms and Concurrent Programming 71
Gadi Taubenfeld © 2014
Semaphores
Constant Space Starvation-free Algorithm
Question: Is it possible
Yes
queue that some process will
be in its CS, while no
10 process is a doorkeeper?
S.0 10
1 S.1 1
empty
true
false
critical
section
Chapter 4 Synchronization Algorithms and Concurrent Programming 72
Gadi Taubenfeld © 2014
Properties of the Algorithm