Docsity

Preventing Concurrent Access: Techniques for Mutual Exclusion, Slides of Computer Numerical Control

Toggle sidebar
bg1
1
Implementing Mutual Exclusion
Arvind Krishnamurthy
Spring 2004
Disable Interrupts
nUniprocessor only: an operation will be atomic as long as a
context switch does not occur
nTwo ways for dispatcher to get control:
nInternal events –thread does something to relinquish the CPU
nExternal events –interrupts cause dispatcher to take CPU away
nNeed to prevent both internal and external events
nPreventing internal events is easy
nPreventing external events require disabling interrupts
nHardware delays handling of external events
pf3
pf4
pf5
pf8
pf9

Partial preview of the text

Download Preventing Concurrent Access: Techniques for Mutual Exclusion and more Slides Computer Numerical Control in PDF only on Docsity!

Implementing Mutual Exclusion

Arvind Krishnamurthy

Spring 2004

Disable Interrupts

n Uniprocessor only: an operation will be atomic as long as a

context switch does not occur

n Two ways for dispatcher to get control:

n Internal events – thread does something to relinquish the CPU n External events – interrupts cause dispatcher to take CPU away

n Need to prevent both internal and external events

n Preventing internal events is easy n Preventing external events require disabling interrupts n Hardware delays handling of external events

Using interrupts (1)

n Flawed but simple:

Lock::Acquire() { disable interrupts; }

Lock::Release() { enable interrupts; }

n Problems:

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

Using interrupts (2)

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; }

Interrupt disable/enable pattern

Time

Thread A . . Disable interrupt Sleep

Sleep return Enable interrupts . .

Thread B

Sleep return Enable interrupts . . Disable interrupts Sleep

switch

switch

Atomic read-modify-write

n On a multiprocessor, interrupt disable does not provide

atomicity

n other CPUs could still enter the critical section n disabling interrupts on all CPUs would be expensive

n Solution: HW provides some special instructions

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.

Locks using test&set (1)

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

Locks using test&set (2)

Key idea : only busy-wait to atomically check lock value --- if

lock is busy, give up CPU. Use a guard on the lock itself.

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; }

Array Locks

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;

Locks Summary

n Supports two operations: acquire and release

n Is easy to write programs with mutual exclusion n Make accesses to shared data be guarded by a lock

n Implementation of locks:

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

Busy-waiting

n Busy-waiting wastes processor cycles

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 Non-busy-waiting solutions are better:

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

Priorities, Locks, Scheduling

n Even without spin-locks, there are subtle interactions

between priorities and scheduling and holding locks

n Mars Pathfinder:

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