Professional Documents
Culture Documents
.. spin
CS
Is item in set?
item of interest
-∞ a b c
+∞
a b
• Abstract Type
– {a, b}
– S( a b ) = {a,b}
Remove()
a b c
b
Remove()
a b c
a b d
a b d
a b d
honk!
c
honk!
a b c
a b c
a b c
a b c
a b c
a b c d
remove(b)
a b c d
remove(b)
a b c d
remove(b)
a b c d
remove(b)
a b c d
remove(b)
a c d
Why do we need
remove(b)
to always hold 2
locks?
a b c d
remove(c)
remove(b)
a b c d
remove(c)
remove(b)
a b c d
remove(c)
remove(b)
a b c d
remove(c)
remove(b)
a b c d
remove(c)
remove(b)
a b c d
remove(c)
remove(b)
a b c d
remove(c)
remove(b)
a b c d
remove(c)
remove(b)
a c d
remove(c)
remove(b)
a c d
remove(c)
remove(b)
a b c d
remove(b)
a b c d
remove(b)
a b c d
remove(b)
a b c d
Found
remove(b) it!
a b c d
Found
remove(b) it!
a c d
remove(b)
a b c d
remove(c)
remove(b)
a b c d
remove(c)
remove(b)
a b c d
remove(c)
remove(b)
a b c d
remove(c)
remove(b)
a b c d
remove(c)
remove(b)
a b c d
remove(c)
remove(b)
a b c d
remove(c)
remove(b)
a b c d
remove(c)
remove(b)
a b c d
remove(c)
Must
acquire
Lock of b
a b c d
remove(c)
Cannot
acquire
lock of b
a b c d
remove(c)
Wait!
a b d
Proceed
to
remove(b)
a b d
remove(b)
a b d
remove(b)
a d
remove(b)
a d
a b d e
add(c) Aha!
a b d e
add(c)
a b d e
remove(b
)
add(c) Aha!
a b d e
Yes, b
add(c) still
reachable
from head
a b d e
add(c)
a b d e
add(b’)
add(c)
a b d e
add(c) Aha!
a b d e
add(c) Yes, b
still points
to d
a b d e
c
add(c)
a b d e
Aha!
remove(c)
a b d e
Yes, b still
remove(c) reachable
from head
a b d e
a b d e
a b c d
a b c d
Present in list
a b c d
Logically deleted
a b c d
Physically deleted
a b d
Physically deleted
a b c
a b c
a b c
a b c
remove(b)
a b c
a not
marked
a b c
a still
points
to b
a b c
Logical
delete
a b c
physical
delete
a b c
Predecessor not
Logically removed
Art of Multiprocessor Programming 180
List Validate Method
private boolean
validate(Node pred, Node curr) {
return
!pred.marked &&
!curr.marked &&
pred.next == curr);
}
Current not
Logically removed
Predecessor still
Points to current
Art of Multiprocessor Programming 182
Remove
try {
pred.lock(); curr.lock();
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();
}}} Art of Multiprocessor Programming 183
Remove
try {
pred.lock(); curr.lock();
if (validate(pred,curr) {
if (curr.key == key) {
curr.marked = true;
pred.next = curr.next;
return true;
} else { Validate as before
return false;
}}} finally {
pred.unlock();
curr.unlock();
}}} Art of Multiprocessor Programming 184
Remove
try {
pred.lock(); curr.lock();
if (validate(pred,curr) {
if (curr.key == key) {
curr.marked = true;
pred.next = curr.next;
return true;
} else {
return false;
}}} finally { Key found
pred.unlock();
curr.unlock();
}}} Art of Multiprocessor Programming 185
Remove
try {
pred.lock(); curr.lock();
if (validate(pred,curr) {
if (curr.key == key) {
curr.marked = true;
pred.next = curr.next;
return true;
} else {
return false;
}}} finally {
pred.unlock(); Logical remove
curr.unlock();
}}} Art of Multiprocessor Programming 186
Remove
try {
pred.lock(); curr.lock();
if (validate(pred,curr) {
if (curr.key == key) {
curr.marked = true;
pred.next = curr.next;
return true;
} else {
return false;
}}} finally {
pred.unlock(); physical remove
curr.unlock();
}}} Art of Multiprocessor Programming 187
Contains
public boolean contains(Item item) {
int key = item.hashCode();
Node curr = this.head;
while (curr.key < key) {
curr = curr.next;
}
return curr.key == key && !curr.marked;
}
a 0 b 0 dc 1
0 e 0
a 0 b 0 dc 1
0 e 0
a 0 b 0 cc 1
0 e 0
a 0 b 0 cc 1
0 e 0
Problem: d 0
Physical
d not added to list… Removal Node added
Must Prevent CAS Before
manipulation of Physical
Removal CAS
removed node’s pointer
Art of Multiprocessor Programming 200
The Solution: Combine Bit and
Pointer
Logical Removal =
Set Mark Bit
a 0 b 0 cc 1
0 e 0
d 0
Physical
Mark-Bit and Pointer Removal Fail CAS: Node not
CAS added after logical
are CASed together Removal
(AtomicMarkableReference)
Reference
address F
mark bit
Art of Multiprocessor Programming 203
Extracting Reference & Mark
Returns mark at
Returns
array index 0!
reference
Value of
mark
.. then change to
this new mark.
a bCAS c d
remov
e c
a CAS bCAS c d
remov
e c
remov
e b
a b c d
remov
e c
remov
e b
a d
remov
e c
remov
e b
Uh-oh
item
At some instant, or …
curr= null
pred succ
Lock-free
Lazy list
Course Grained
Fine Lock-coupling
Lock-free
Lazy list
Course Grained
Fine Lock-coupling
Lock-free
Lazy list
Course Grained
Fine Lock-coupling
% Contains()