





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!






Mutual exclusion Synchronize entry into critical sections Normally done with locks Point-to-point synchronization Tell a set of processors (normally set cardinality is one) that they can proceed Normally done with flags Global synchronization Bring every processor to sync Wait at a point until everyone is there Normally done with barriers
Normally a two-part process: acquire and release; acquire can be broken into two parts: intent and wait Intent: express intent to synchronize (i.e. contend for the lock, arrive at a barrier) Wait: wait for your turn to synchronization (i.e. wait until you get the lock) Release: proceed past synchronization and enable other contenders to synchronize Waiting algorithms do not depend on the type of synchronization
Not popular today Less flexible Cannot support large number of locks Possible designs Dedicated lock line in bus so that the lock holder keeps it asserted and waiters snoop the lock line in hardware Set of lock registers shared among processors and lock holder gets a lock register (Cray Xmp )
Bakery algorithm
Shared: choosing[P] = FALSE, ticket[P] = 0;
Acquire : choosing[ i ] = TRUE; ticket[ i ] = max(ticket[0],…,ticket[P-1]) + 1;
choosing[ i ] = FALSE; for j = 0 to P-
while (choosing[j]); while (ticket[j] && ((ticket[j], j) < (ticket[ i ], i )));
endfor
Release : ticket[ i ] = 0;
Does it work for multiprocessors? Assume sequential consistency Performance issues related to coherence? Too much overhead: need faster and simpler lock algorithms Need some hardware support
Start with a simple software lock
Shared: lock = 0; Acquire : while (lock); lock = 1; Release or Unlock : lock = 0;
Assembly translation
Lock: lw register, lock_addr /* register is any processor register */ bnez register, Lock addi register, register, 0x sw register, lock_addr Unlock: xor register, register, register sw register, lock_addr
Does it work?
What went wrong? We wanted the read-modify-write sequence to be atomic
We can fix this if we have an atomic exchange instruction
Lock: xchg register, lock_addr /* An atomic load and store */
bnez register, Lock Unlock remains unchanged
Various processors support this type of instruction Intel x86 has xchg , Sun UltraSPARC has ldstub (load-store-unsigned byte), UltraSPARC also has swap Normally easy to implement for bus-based systems: whoever wins the bus for xchg can lock the bus Difficult to support in distributed memory systems
addi reg1, r0, 0x0 /* reg1 has 0x0 / addi reg2, r0, 0x1 / reg2 has 0x1 */ Lock: compare & swap reg1, reg2, lock_addr bnez reg2, Lock
In some machines (e.g., SGI Origin 2000) uncached fetch & op is supported Every such instruction will generate a transaction (may be good or bad depending on the support in memory controller; will discuss later) Let us assume that the lock location is cacheable and is kept coherent Every invocation of test & set must generate a bus transaction; Why? What is the transaction? What are the possible states of the cache line holding lock_addr? Therefore all lock contenders repeatedly generate bus transactions even if someone is still in the critical section and is holding the lock Can we improve this? Test & set with backoff
Instead of retrying immediately wait for a while
How long to wait? Waiting for too long may lead to long latency and lost opportunity Constant and variable backoff Special kind of variable backoff : exponential backoff (after the i th attempt the delay is k* c i where k and c are constants) Test & set with exponential backoff works pretty well
delay = k Lock: ts register, lock_addr bez register, Enter_CS pause (delay) /* Can be simulated as a timed loop / delay = delayc j Lock
Reduce traffic further
Before trying test & set make sure that the lock is free
Lock: ts register, lock_addr bez register, Enter_CS Test: lw register, lock_addr bnez register, Test j Lock
How good is it? In a cacheable lock environment the Test loop will execute from cache until it receives an invalidation (due to store in unlock); at this point the load may return a zero value after fetching the cache line If the location is zero then only everyone will try test & set