11 Lock Freedom
Transcript of 11 Lock Freedom
![Page 1: 11 Lock Freedom](https://reader033.fdocuments.in/reader033/viewer/2022052915/577ccd251a28ab9e788ba005/html5/thumbnails/1.jpg)
Obstruction Freedom Categories Guaranteeing Memory Ordering Lock-Free Algorithms
Lock Freedom
David Chisnall
March 8, 2011 (Pancake Day!)
![Page 2: 11 Lock Freedom](https://reader033.fdocuments.in/reader033/viewer/2022052915/577ccd251a28ab9e788ba005/html5/thumbnails/2.jpg)
Obstruction Freedom Categories Guaranteeing Memory Ordering Lock-Free Algorithms
Locks are Slow!
• Cost to acquire and release
• System calls often required
• Can cause n threads to block (wait) if a lock is accessible byn + 1 threads
• Possibility of deadlock
• Not ideal for high-performance computing!
![Page 3: 11 Lock Freedom](https://reader033.fdocuments.in/reader033/viewer/2022052915/577ccd251a28ab9e788ba005/html5/thumbnails/3.jpg)
Obstruction Freedom Categories Guaranteeing Memory Ordering Lock-Free Algorithms
Wait Freedom
Every operation is bounded on the number of steps beforecompletion.
(Never happens, back in the real world)
![Page 4: 11 Lock Freedom](https://reader033.fdocuments.in/reader033/viewer/2022052915/577ccd251a28ab9e788ba005/html5/thumbnails/4.jpg)
Obstruction Freedom Categories Guaranteeing Memory Ordering Lock-Free Algorithms
Lock Freedom
• At least one thread must be able to make progress at anygiven time
• Eventually, all threads must make progress
• Given infinite time, infinitely many threads will progress
![Page 5: 11 Lock Freedom](https://reader033.fdocuments.in/reader033/viewer/2022052915/577ccd251a28ab9e788ba005/html5/thumbnails/5.jpg)
Obstruction Freedom Categories Guaranteeing Memory Ordering Lock-Free Algorithms
Obstruction Freedom
A single thread, with all other threads paused, may complete itswork.
![Page 6: 11 Lock Freedom](https://reader033.fdocuments.in/reader033/viewer/2022052915/577ccd251a28ab9e788ba005/html5/thumbnails/6.jpg)
Obstruction Freedom Categories Guaranteeing Memory Ordering Lock-Free Algorithms
Implementing Obstruction Free Algorithms
• Requires strong guarantees on memory ordering
• Needs lots of thought!
![Page 7: 11 Lock Freedom](https://reader033.fdocuments.in/reader033/viewer/2022052915/577ccd251a28ab9e788ba005/html5/thumbnails/7.jpg)
Obstruction Freedom Categories Guaranteeing Memory Ordering Lock-Free Algorithms
Problem 1: Compiler Reorders Memory Access
�a = b;
b = c; � �• Two store operations
• No dependencies
• Compiler is free to issue them in any order
• May also remove load operations if the value is already in aregister!
![Page 8: 11 Lock Freedom](https://reader033.fdocuments.in/reader033/viewer/2022052915/577ccd251a28ab9e788ba005/html5/thumbnails/8.jpg)
Obstruction Freedom Categories Guaranteeing Memory Ordering Lock-Free Algorithms
The volatile Keyword
�volatile int a; � �
• The compiler must issue a memory read for every access to a
• The compiler must issue a memory write for every assignmentto a
• The compiler may not re-order accesses and assignments to a
• The compiler is free to rearrange accesses to a relative toother memory access
• The compiler makes no guarantees about multithreaded access
![Page 9: 11 Lock Freedom](https://reader033.fdocuments.in/reader033/viewer/2022052915/577ccd251a28ab9e788ba005/html5/thumbnails/9.jpg)
Obstruction Freedom Categories Guaranteeing Memory Ordering Lock-Free Algorithms
Problem 2: CPU Reorders Memory Access
• Most modern chips issue operations out of order
• Memory reads and writes may be reordered
• The processor will ensure that the current thread doesn’t seethe reordering...
• ...but other threads still can
![Page 10: 11 Lock Freedom](https://reader033.fdocuments.in/reader033/viewer/2022052915/577ccd251a28ab9e788ba005/html5/thumbnails/10.jpg)
Obstruction Freedom Categories Guaranteeing Memory Ordering Lock-Free Algorithms
Memory Barriers
�// GCC extension , full memory barrier:
__sync_synchronize (); � �• Provides a line in the instruction stream
• Memory accesses may not be reordered across the line
• Some architectures provide various forms of relaxed barriers(e.g. only writes may not be reordered)
![Page 11: 11 Lock Freedom](https://reader033.fdocuments.in/reader033/viewer/2022052915/577ccd251a28ab9e788ba005/html5/thumbnails/11.jpg)
Obstruction Freedom Categories Guaranteeing Memory Ordering Lock-Free Algorithms
Example: Xen Time Source
• Hypervisor must provide guest VMs with current time
• Desire to avoid expensive calls from guest to hypervisor
• Lock-free mechanism for updating time
![Page 12: 11 Lock Freedom](https://reader033.fdocuments.in/reader033/viewer/2022052915/577ccd251a28ab9e788ba005/html5/thumbnails/12.jpg)
Obstruction Freedom Categories Guaranteeing Memory Ordering Lock-Free Algorithms
Time in Xen
• Hypervisor provides coarse-grained time and time-stampcounter (TSC) value when it was accurate
• Generating the current time requires reading several valuesfrom memory
• What happens if your read overlaps with an update?
![Page 13: 11 Lock Freedom](https://reader033.fdocuments.in/reader033/viewer/2022052915/577ccd251a28ab9e788ba005/html5/thumbnails/13.jpg)
Obstruction Freedom Categories Guaranteeing Memory Ordering Lock-Free Algorithms
Solution: Versioned Reads�struct shared_info
{
int version , nanosecs , seconds , tscs;
};
struct shared_info atomic_read(volatile struct
shared_info *info)
{
struct ret;
while ((ret ->version = info ->version) & 1) ;
ret ->nanosecs = info ->nanosecs;
ret ->seconds = info ->seconds;
ret ->tscs = info ->seconds;
if (ref ->version == info ->version)
return ret;
return atomic_read(info);
} � �
![Page 14: 11 Lock Freedom](https://reader033.fdocuments.in/reader033/viewer/2022052915/577ccd251a28ab9e788ba005/html5/thumbnails/14.jpg)
Obstruction Freedom Categories Guaranteeing Memory Ordering Lock-Free Algorithms
Write Algorithm
�info ->version ++;
__sync_synchronize ();
info ->nanosecs = nanosecs;
info ->seconds = seconds;
info ->tscs = seconds;
__sync_synchronize ();
info ->version ++; � �
![Page 15: 11 Lock Freedom](https://reader033.fdocuments.in/reader033/viewer/2022052915/577ccd251a28ab9e788ba005/html5/thumbnails/15.jpg)
Obstruction Freedom Categories Guaranteeing Memory Ordering Lock-Free Algorithms
Performance
Reader:
• No atomic operations required
• Common case just requires 5 reads
• Very fast!
• May need to retry if concurrent with write
• Unbounded worst-case time
Writer:
• Needs two barriers or atomic increments
• Similar cost to acquiring and releasing a mutex
• But never blocks - hard realtime guarantee for the writer!
![Page 16: 11 Lock Freedom](https://reader033.fdocuments.in/reader033/viewer/2022052915/577ccd251a28ab9e788ba005/html5/thumbnails/16.jpg)
Obstruction Freedom Categories Guaranteeing Memory Ordering Lock-Free Algorithms
Example: Lockless Ring Buffer
• Producer-consumer problem
• Solution without locks
• Producer and consumer can both access queue concurrently!
![Page 17: 11 Lock Freedom](https://reader033.fdocuments.in/reader033/viewer/2022052915/577ccd251a28ab9e788ba005/html5/thumbnails/17.jpg)
Obstruction Freedom Categories Guaranteeing Memory Ordering Lock-Free Algorithms
Simple Ring Buffer
1. Acquire lock
2. Insert object
3. Release lock
1. Acquire lock
2. Collect object
3. Release lock
How do we make this lock free?
![Page 18: 11 Lock Freedom](https://reader033.fdocuments.in/reader033/viewer/2022052915/577ccd251a28ab9e788ba005/html5/thumbnails/18.jpg)
Obstruction Freedom Categories Guaranteeing Memory Ordering Lock-Free Algorithms
Potential Concurrency Problems
• Producer must find free space
• Consumer must find next item
• Producer must be able to tell if the buffer is full
• Consumer must be able to tell if the buffer is empty
![Page 19: 11 Lock Freedom](https://reader033.fdocuments.in/reader033/viewer/2022052915/577ccd251a28ab9e788ba005/html5/thumbnails/19.jpg)
Obstruction Freedom Categories Guaranteeing Memory Ordering Lock-Free Algorithms
Solution: Free-running Counters
�volatile uint32_t producer;
volatile uint32_t consumer;
int shift = 8;
// Must be power of two!
const bufferSize = 1<<shift;
const bufferMask = bufferSize - 1;
void *buffer[bufferSize ]; � �
![Page 20: 11 Lock Freedom](https://reader033.fdocuments.in/reader033/viewer/2022052915/577ccd251a28ab9e788ba005/html5/thumbnails/20.jpg)
Obstruction Freedom Categories Guaranteeing Memory Ordering Lock-Free Algorithms
Inserting into the Ring
�void insert(void *v)
{
while (producer - consumer > bufferSize);
buffer[producer & bufferMask] = v;
producer ++;
} � �
![Page 21: 11 Lock Freedom](https://reader033.fdocuments.in/reader033/viewer/2022052915/577ccd251a28ab9e788ba005/html5/thumbnails/21.jpg)
Obstruction Freedom Categories Guaranteeing Memory Ordering Lock-Free Algorithms
Retrieving the Next Value
�void* fetch(void)
{
while (producer == consumer);
void *v = buffer[consumer & bufferMask ];
consumer ++;
return v;
} � �
![Page 22: 11 Lock Freedom](https://reader033.fdocuments.in/reader033/viewer/2022052915/577ccd251a28ab9e788ba005/html5/thumbnails/22.jpg)
Obstruction Freedom Categories Guaranteeing Memory Ordering Lock-Free Algorithms
Why it Works
• The counters are only ever read from one thread, written fromthe other
• Each update only increments a single shared variable
• Wrap-around is handled by overflow
![Page 23: 11 Lock Freedom](https://reader033.fdocuments.in/reader033/viewer/2022052915/577ccd251a28ab9e788ba005/html5/thumbnails/23.jpg)
Obstruction Freedom Categories Guaranteeing Memory Ordering Lock-Free Algorithms
Better than Condition Variables?
• Very fast when the consumer and producer are both running
• Causes producer to spin when queue is full
• Causes consumer to spin when queue is empty
• Solution: Hybrid - use a condition variable only on boundaryconditions (i.e. when transitioning to and from full / emptystates)
![Page 24: 11 Lock Freedom](https://reader033.fdocuments.in/reader033/viewer/2022052915/577ccd251a28ab9e788ba005/html5/thumbnails/24.jpg)
Obstruction Freedom Categories Guaranteeing Memory Ordering Lock-Free Algorithms
Questions?