





Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!






n Internal events – thread does something to relinquish the CPU n External events – interrupts cause dispatcher to take CPU away
n Preventing internal events is easy n Preventing external events require disabling interrupts n Hardware delays handling of external events
Lock::Acquire() { disable interrupts; }
Lock::Release() { enable interrupts; }
n the above may make some user thread never give back CPU
n critical sections can be arbitrarily long --- it may take too long to respond to an interrupt --- real-time system won’t be happy
n this won’t work for other higher-level primitives such as semaphores and condition variables
Key idea : maintain a lock variable and impose mutual exclusion only on the operations of testing and setting that variable
class Lock { int value = FREE; }
Lock::Acquire() { Disable interrupts; while (value != FREE) { Enable interrupts; Disable interrupts; } value = BUSY; Enable interrupts; }
Lock::Release() { Disable interrupts ; value = FREE; Enable interrupts; }
Time
Thread A . . Disable interrupt Sleep
Sleep return Enable interrupts . .
Thread B
Sleep return Enable interrupts . . Disable interrupts Sleep
switch
switch
n other CPUs could still enter the critical section n disabling interrupts on all CPUs would be expensive
n test&set (most arch) --- read value, write 1 back to memory n exchange (x86) --- swaps value between register and memory n compare&swap (68000) --- read value; if value matches register, do exchange n load linked and conditional store (MIPS R4000, Alpha) n read value in one instruction, do some operations, when store occurs, check if value has been modified in the meantime. If not, ok; otherwise, abort, and jump back to start.
n Flawed but simple:
lock value = 0;
Lock::Acquire() { while (test&set(value) == 1); }
Lock::Release() { value = 0;}
n Problems:
n busy-waiting --- thread consumes CPU while it is waiting n also known as “Spin” lock n could cause problems if threads have different priorities
Lock::Acquire() { while (test&set(guard)) // short wait time ;
if (value == BUSY) { Put on queue of threads waiting for lock; Go to sleep and set guard to 0 } else { value = BUSY; guard = 0; } }
Lock::Release() { while (test&set(guard)) ; if anyone on wait queue { Take a waiting thread off wait queue and put it at the front of the ready queue; } else { value = FREE; } guard = 0; }
n Problem with ticket locks: everyone is polling the same location
n Distribute the shared value, and do directed “unlocks”
Initialization: int slots[numProcs] = {has_lock, must_wait, must_wait, … }; next_slot = 0;
Lock: my_slot = fetch_and_increment(next_slot); my_slot = my_slot % numProcs; while (slots[my_slot] == must_wait); slots[my_slot] = must_wait;
Unlock: slots[(my_slot + 1) % numProcs] = has_lock;
n Is easy to write programs with mutual exclusion n Make accesses to shared data be guarded by a lock
n Uniprocessor: can use interrupt-enable/disable n Solution should not require busy-waiting (or spin-locks) n Typically, requires a queue of threads waiting for each lock n Multiprocessors: n Requires special hardware instructions such as test-and-set n Also need to avoid busy-waiting n Interesting variants exist that try to minimize bus traffic generated by locks
n Might prevent the thread that has the lock from running n Scenario: n Thread A has lock but gets context-switched out n Thread B starts running, decides to acquire lock n Spins trying to acquire the lock; prevents thread A from making forward progress (and thus delaying the lock release) n If threads package does not use preemption (or if thread B has higher priority), could result in deadlocks
n Thread B just goes to sleep if lock is not available n Thread A executes, makes progress, releases lock, and thread B is woken up
n Success story for the first few days n Landed with fancy airbags, released a “rover”, shot some spectacular photos of the Mars landscape n A few days later after it started collecting meteorological data, system started resetting itself periodically