Avoid (one) spinlock deadlock
Posted Dec 8, 2005 16:05 UTC (Thu) by
gottlieb (subscriber, #2240)
Parent article:
FOSS.IN: A report
Thanks for posting the slides, which I very much enjoyed. Yet another reason to be pleased with my membership. You give the following code for spinlocks
To lock a spinlock:
Decrement it by one
If resulting value is zero
it's yours
else
increment value
go try again (spin)
When many are contending for the lock, this code can livelock with the value always negative.
To avoid this livelock use a pretest
while value of spinlock is negative
go try again (i.e., this is empty while loop)
Decrement it by one
If resulting value is zero
it's yours
else
increment value
go try again (spin)
Both “go try again”s go to the beginning (the while)
To optimize for non-contending locks, start with the decrement part
(i.e., imagine a jump to the decrement part at the begining; but
when you try again you jump to the while)
allan gottlieb, new york university
(
Log in to post comments)