Docsity

Lock Implementation: Atomicity and Synchronization Techniques - Prof. Samuel T. King, Study notes of Operating Systems

Partial preview of the text

Download Lock Implementation: Atomicity and Synchronization Techniques - Prof. Samuel T. King and more Study notes Operating Systems in PDF only on Docsity!

CS 423: Lock implementation

Sam King / Klara Nahrstedt

Implementing locks

• Concurrent programs use high-level

synchronization operations

  • Used my multiple threads so they need to worry

about atomicity (e.g., they use data structures)

  • Can’t use the high-level synchronization

operations themselves

Concurrent programs

High-level synchronization

provided by software

Low-level atomic operations

provided by hardware

Interrupt enable/disable for atomicity

  • With interrupt enable/disable, why do we need locks? - User program could call interrupt disable before

entering critical section and call interrupt enable

after leaving

Lock impl. #1 (disable interrupts w/ busy waiting) Lock() { disable inerrupts while(value != FREE){ enable interrupts disable interrupts } value = BUSY enable interrupts } Unlock() { disable interrupts value = FREE enable interrupts }

  • Why does lock() disable interrupts in the beginning

of the function?

Read-modify-write instructions

• Interrupt disable works on a

uniprocessor

– Does not work on multiprocessor

• Could use atomic load / atomic store

instructions (Too Much Milk #3)

• Modern processors provide an easier

way with atomic read-modify-write

instructions

– Atomically {read value from mem into reg,

then write new value to mem location}

Test and set

• Atomically writes 1 to a memory

location (set) and returns the value that

used to be there (test)

Test_and_set(x) { tmp = X X = 1 return(tmp) }

• Note that only 1 process can see a

transition from 0 -> 1

Busy waiting

• Problem with lock impl. #1 and

  • Waiting thread uses lots of Cpu time waiting for

lock to free

  • Better for thread to go to sleep and let other

threads run

  • Strategy for reducing busy-waiting: integrate the

lock implementation with the thread dispatcher

data structures and have the lock code manipulate

thread queues

Lock impl. #3 (int. disable, no busy wait) Lock() { disable interrupts if(value == FREE) { value = BUSY } else { add thread to wait queue for this lock switch to next thread } enable interrupts } Unlock() { disable interrupts value = FREE if(any thread waiting for this lock) { move wait thread to ready queue value = BUSY } enable interrupts }

Interrupt disable / enable

  • When should lock() re-enable interrupts?
  • Enable before adding thread to the wait queue? Lock() { disable interrupts … if(lock is busy) { enable interrupts add thread to lock wait queue switch to next runnable thread }
  • When could this fail?

Lock() {

disable interrupts

if(lock is busy) {

add thread to lock wait queue

enable interrupts

switch to next runnable thread

  • But this fails if interrupt happens after thread enables interrupts - Lock() adds thread to wait queue - Lock() enables interrupts - Interrupt causes preemption - Now thread is on two queues!
  • Invariant
    • All thread promise to have interrupts disabled whtn they call switch
    • All threads promise to re-enable interrupts after they get returned to from switch

Thread a enable interrupts } Lock() { disable interrupts … switch back from switch enable interrupts } Thread b Yield() { disable interrupts switch back from switch enable interrupts } Unlock() // move a to rq Yield() { disable interrupts switch

Lock() { while(test&set(guard)) { } if(value == FREE) { value = BUSY } else { add thread to wait q switch to next run t } guard = 0 } Unlock() { while(test&set(guard)) { } value = FREE if(thread waiting for lock) { move thread to ready queue value = BUSY } guard = 0 }