Spinlocks
Spinlocks
Samy Al Bahra Devon H. O’Dell
Message Systems, Inc.
April 8, 2011
Slide 2
Slide 2 text
Spinlocks
Introduction
Mutexes
A mutex is an object which implements acquire and relinquish
operations such that the execution following an acquire operation
and up to the relinquish operation is executed in a mutually
exclusive manner relative to the object implementing a mutex.
Slide 3
Slide 3 text
Spinlocks
Introduction
Locks
Locks are an implementation of a mutex.
Sleep lock
Any mutex type which deactivates processes that attempt to
acquire a mutex that has already been acquired by another
process until a relinquish operation on the mutex activates one
or more of them.
Spinlock
Any mutex type which forces callers of an acquire operation to
spend an unbounded number of processor cycles re-evaluating
the availability of the mutex until it has been acquired. The
process that invokes acquire is never deactivated before the
completion of the acquire operation.
Spinlocks are preferred to sleep mutexes when the waiting
time for a resource is less than the time for the scheduling
overhead of process activation/deactivation or when
scheduling simply is not possible.
Spinlocks
Non-Arbitrating Spinlocks
Fairness
Non-arbitrating spinlocks do not provide fairness (or
starvation-freedom) guarantees.
Slide 28
Slide 28 text
Spinlocks
Non-Arbitrating Spinlocks
Ticket
0
1
0 2
2
1
3
2
3
Now Serving: 0
Now Serving: 0
Now Serving: 1
Now Serving: 2
Now Serving: 3
LOCK
LOCK, LOCK
UNLOCK
UNLOCK
UNLOCK
UNLOCK, LOCK
4
Now Serving: 4
UNLOCK
Slide 29
Slide 29 text
Spinlocks
Non-Arbitrating Spinlocks
Anderson
U L L L
U L L L
U L L L
L U L L
L U L L
P
P
P
P
P
L L U L
P
L L U L
P
LOCK
LOCK
UNLOCK
LOCK
UNLOCK
LOCK
L L L U
P
U L L L
P
UNLOCK
UNLOCK
Slide 30
Slide 30 text
Spinlocks
Non-Arbitrating Spinlocks
MCS
L
L T0
R
L T0
R
T1
S
L T0
R
T1
S
FAS
L T0
N
T1
S
R = Running
S = Spinning
N = Notify
L T1
R
T0 LOCK
T1 LOCK
T0 UNLOCK
Slide 31
Slide 31 text
Spinlocks
Non-Arbitrating Spinlocks
MCS
L T1
R
T2
S
L T1
S
T2
S
L T1
N
T2
S
L T2
R
T2 LOCK
T1 UNLOCK
Slide 32
Slide 32 text
Spinlocks
Non-Arbitrating Spinlocks
CLH
Stub
Stub T0
Stub T0 T1
T1
LOCK
LOCK
UNLOCK
Stub T0 T1
T0 T1
UNLOCK
R
R
R
S
S
Slide 33
Slide 33 text
Spinlocks
Non-Arbitrating Spinlocks
Fast path latency
Fast path latency
See http://concurrencykit.org/doc/appendixZ.html
Slide 34
Slide 34 text
Spinlocks
Non-Arbitrating Spinlocks
Limitations
Mutexes in general are not composable.
Subtle ordering issues can lead to hard-to-detect deadlock
conditions.
Blocking synchronization is sensitive to preemption.