Hardware Support Locking
Concept
To evaluate whether a lock works, there are three basic criteria: 1. Mutual exclusion 2. Fairness 3. Performance
Design and Implement Lock
Locks requires OS help. Building spin locks could use:
1. Test-And-Set
.
2. Compare-And-Swap
.
3. Fetch-And-Add
XV6 uses option 1.
TestAndSet
swap flag's value with 1, and return the old value. If result is 1, it was locked before, so we spin and wait. If result is 0, it was unlocked, and we locked succesfully, we can now enter the critical section.
Case Study: Linux-based Futex Locks
How it works
Mutex lock counter: bit 31 clear means unlocked; bit 31 set means locked.
All code that looks at bit 31 first increases the ‘number of interested threads’ usage counter, which is in bits 0-30. All negative mutex values indicate that the mutex is still locked.
Code
This code snippet from lowlevellock.h
in the nptl library (part of the gnu libc library).
Explain
In unlock
, 0x80000000
hex is binary: 10 00000 00000 00000 00000 00000 00000
(31 zeros).
atomic_add_zero
impl is:
exchange_and_add
:Add VALUE to MEM and return the old value of MEM.
What this function does is: Convert hex value to a proper int. Use negative of value. Compare result of exchange_and_add to negative of value, as return result.
The flow of 2 cases:
Case 1: If no one waiting, return T.
Case 2: If someone still waiting, return False.
Last updated