condition synchronization · 2006-10-06 · 9 synchronization constructs synchronization...

7
1 Condition Synchronization 2 Beyond Locks Locks ensure mutual exclusion Is this sufficient? What if you want to synchronize on a condition? Example: Producer-consumer problem Class BoundedBuffer{ Lock lock; int count = 0; } BoundedBuffer::Deposit(c){ lockacquire(); while (count == n); Add c to the buffer; count++; lockrelease(); } BoundedBuffer::Remove(c){ lockacquire(); while (count == 0); Remove c from buffer; count--; lockrelease(); } What is wrong with this? 3 Introducing Condition Variables Correctness requirements for bounded buffer producer- consumer problem Only one thread manipulates the buffer at any time (mutual exclusion) Consumer must wait for producer when the buffer is empty (scheduling/synchronization constraint) Producer must wait for the consumer when the buffer is full (scheduling/synchronization constraint) Solution: condition variables An abstraction that supports conditional synchronization Key idea: Enable threads to wait inside a critical section by atomically releasing lock at the same time 4 Condition Variables: Operations Three operations Wait() Release lock Go to sleep Reacquire lock upon return Signal() Wake up a waiter, if any Broadcast() Wake up all the waiters Implementation Requires a per-condition variable queue to be maintained Threads waiting for the condition wait for a signal() Wait() usually specifies a lock to be released as a parameter

Upload: others

Post on 10-Jul-2020

1 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Condition Synchronization · 2006-10-06 · 9 Synchronization Constructs Synchronization Coordinating execution of multiple threads that share data structures Past few lectures: Locks:

1

Condition Synchronization

2

Beyond Locks

Locks ensure mutual exclusion

Is this sufficient? What if you want to synchronize on a condition? Example: Producer-consumer problem

Class BoundedBuffer{ … Lock lock; int count = 0;}

BoundedBuffer::Deposit(c){ lockacquire(); while (count == n); Add c to the buffer; count++; lockrelease();}

BoundedBuffer::Remove(c){ lockacquire(); while (count == 0); Remove c from buffer; count--; lockrelease();}

What is wrong with this?

3

Introducing Condition Variables

Correctness requirements for bounded buffer producer-consumer problem Only one thread manipulates the buffer at any time (mutual

exclusion) Consumer must wait for producer when the buffer is empty

(scheduling/synchronization constraint) Producer must wait for the consumer when the buffer is full

(scheduling/synchronization constraint)

Solution: condition variables An abstraction that supports conditional synchronization Key idea:

Enable threads to wait inside a critical section by atomicallyreleasing lock at the same time

4

Condition Variables: Operations

Three operations Wait()

Release lock Go to sleep Reacquire lock upon return

Signal() Wake up a waiter, if any

Broadcast() Wake up all the waiters

Implementation Requires a per-condition variable queue to be maintained Threads waiting for the condition wait for a signal()

Wait() usually specifies a lock to be released as a parameter

Page 2: Condition Synchronization · 2006-10-06 · 9 Synchronization Constructs Synchronization Coordinating execution of multiple threads that share data structures Past few lectures: Locks:

5

Implementing Wait() and Signal()

Condition::Wait(lock){ numWaiting++; lockrelease(); Put TCB on the waiting queue for the CV; switch(); lockacquire();}

Condition::Signal(){ if (numWaiting > 0) {

Move a TCB from waiting queue to ready queue;numWaiting--;

}}

Does this work?

Condition::Wait(lock){ numWaiting++; Put TCB on the waiting queue for the CV; lockrelease(); switch(); lockacquire();}

6

Using Condition Variables: An Example

Coke machine as a shared buffer

Two types of users Producer: Restocks the coke machine Consumer: Removes coke from the machine

Requirements Only a single person can access the machine at any time If the machine is out of coke, wait until coke is restocked If machine is full, wait for consumers to drink coke prior to restocking

How will we implement this? What is the class definition? How many lock and condition variables do we need?

7

Coke Machine Example

Class CokeMachine{ … Lock lock; int count = 0; Condition notFull, notEmpty;}

CokeMachine::Deposit(){ lockacquire(); while (count == n) {

notFull.wait(&lock); } Add coke to the machine; count++; notEmpty.signal(); lockrelease();}

CokeMachine::Remove(){ lockacquire(); while (count == 0) {

notEmpty.wait(&lock); } Remove coke from to the machine; count--; notFull.signal(); lockrelease();}

8

Semaphores and Monitors: High-level Synchronization Constructs

A Historical Perspective

Page 3: Condition Synchronization · 2006-10-06 · 9 Synchronization Constructs Synchronization Coordinating execution of multiple threads that share data structures Past few lectures: Locks:

9

Synchronization Constructs

Synchronization Coordinating execution of multiple threads that share data

structures

Past few lectures: Locks: provide mutual exclusion Condition variables: provide conditional synchronization

Today: Historical perspective Semaphores

Introduced by Dijkstra in 1960s Main synchronization primitives in early operating systems

Monitors Alternate high-level language constructs

10

Semaphores

An abstract data type

A non-negative integer variable with two atomic operations

We assume that a semaphore is fair No thread t that is blocked on a P() operation remains blocked if the V()

operation on the semaphore is invoked infinitely often In practice, FIFO is mostly used, transforming the set into a queue.

SemaphoreP() (Passeren; wait)If sem > 0, then decrement sem by 1Otherwise “wait” until sem > 0 and thendecrement

SemaphoreV() (Vrijgeven; signal)Increment sem by 1Wake up a thread waiting in P()

11

Important properties of Semaphores

Semaphores are non-negative integers

The only operations you can use to change the value of asemaphore are P() and V() (except for the initial setup) P() can block, but V() never blocks

Semaphores are used both for Mutual exclusion, and Conditional synchronization

Two types of semaphores Binary semaphores: Can either be 0 or 1 General/Counting semaphores: Can take any non-negative value Binary semaphores are as expressive as general semaphores

(given one can implement the other)

12

Using Semaphores for Mutual Exclusion

Use a binary semaphore for mutual exclusion

Using Semaphores for producer-consumer with boundedbuffer

Semaphore = new Semaphore(1);

SemaphoreP(); Critical Section;SemaphoreV();

Semaphore mutex;Semaphore fullBuffers;Semaphore emptyBuffers;

Use a separatesemaphore foreach constraint

Page 4: Condition Synchronization · 2006-10-06 · 9 Synchronization Constructs Synchronization Coordinating execution of multiple threads that share data structures Past few lectures: Locks:

13

Revisiting Coke Machine Example

Class CokeMachine{ … Semaphore new mutex(1); Semaphores new fullBuffers(0); Semaphores new emptyBuffers(numBuffers);}

CokeMachine::Deposit(){ emptyBuffersP(); mutexP(); Add coke to the machine; mutexV(); fullBuffersV();}

CokeMachine::Remove(){ fullBuffersP(); mutexP(); Remove coke from to the machine; mutexV(); emptyBuffersV();}

14

CokeMachine::Deposit(){ lockacquire(); while (count == n) {

notFull.wait(&lock); } Add coke to the machine; count++; notEmpty.signal(); lockrelease();}

CokeMachine::Deposit(){ emptyBuffersP(); mutexP(); Add coke to the machine; mutexV(); fullBuffersV();}

CokeMachine::Deposit(){ mutexP(); emptyBuffersP(); Add coke to the machine; fullBuffersV(); mutexV();}

Does the order of P matter? V?

15

Implementing Semaphores

Semaphore::P() { Disable interrupts; if (value == 0) { Put TCB on wait queue for semaphore; Switch(); // dispatch a ready thread } else {value--;} Enable interrupts;}

Semaphore::V() { Disable interrupts; if wait queue is not empty { Move a waiting thread to ready queue; } else {value++;} Enable interrupts;}

16

Implementing Semaphores

Semaphore::P() { Disable interrupts; while (value == 0) { Put TCB on wait queue for semaphore; Switch(); // dispatch a ready thread } value--; Enable interrupts;}

Semaphore::V() { Disable interrupts; if wait queue is not empty { Move a waiting thread to ready queue; } value++; Enable interrupts;}

Page 5: Condition Synchronization · 2006-10-06 · 9 Synchronization Constructs Synchronization Coordinating execution of multiple threads that share data structures Past few lectures: Locks:

17

The Problem with Semaphores

CokeMachine::Deposit(){ emptyBuffersP(); mutexP(); Add coke to the machine; mutexV(); fullBuffersV();}

CokeMachine::Remove(){ fullBuffersP(); mutexP(); Remove coke from to the machine; mutexV(); emptyBuffersV();}

Semaphores are used for dual purpose Mutual exclusion Conditional synchronization

Difficult to read/develop code

Waiting for condition is independent of mutual exclusion Programmer needs to be clever about using semaphores

18

Separate the concerns of mutual exclusion and conditionalsynchronization

What is a monitor? One lock, and Zero or more condition variables for managing concurrent access to

shared data

General approach: Collect related shared data into an object/module Define methods for accessing the shared data

Monitors were first introduced as a programming language construct Calling a method defined in the monitor automatically acquires the lock Examples: Mesa, Java (synchronized methods)

Monitors also define a programming convention Can be used in any language (C, C++, … )

Introducing Monitors

19

Locks and Condition Variables – Recap

Locks Provide mutual exclusion Support two methods

Lock::Acquire() – wait until lock is free, then grab it Lock::Release() – release the lock, waking up a waiter, if any

Condition variables Support conditional synchronization Three operations

Wait(): Release lock; wait for the condition to become true; reacquire lockupon return

Signal(): Wake up a waiter, if any Broadcast(): Wake up all the waiters

Two semantics for the implementation of wait() and signal() Hoare monitor semantics Hansen monitor semantics

20

Coke Machine Example

Class CokeMachine{ … Lock lock; int count = 0; Condition notFull, notEmpty;}

CokeMachine::Deposit(){ lockacquire(); while (count == n) {

notFull.wait(&lock); } Add coke to the machine; count++; notEmpty.signal(); lockrelease();}

CokeMachine::Remove(){ lockacquire(); while (count == 0) {

notEmpty.wait(&lock); } Remove coke from to the machine; count--; notFull.signal(); lockrelease();}

Page 6: Condition Synchronization · 2006-10-06 · 9 Synchronization Constructs Synchronization Coordinating execution of multiple threads that share data structures Past few lectures: Locks:

21

Hoare Monitors: Semantics

Hoare monitor semantics: Assume thread T1 is waiting on condition x Assume thread T2 is in the monitor Assume thread T2 calls x.signal T2 gives up monitor, T2 blocks! T1 takes over monitor, runs T1 gives up monitor T2 takes over monitor, resumes

Example

fn1(…)…x.wait // T1 blocks

// T1 resumesLockrelease();

fn4(…)…x.signal // T2 blocks

T2 resumes

22

Hansen Monitors: Semantics

Hansen monitor semantics: Assume thread T1 waiting on condition x Assume thread T2 is in the monitor Assume thread T2 calls x.signal; wake up T1 T2 continues, finishes When T1 get a chance to run,T1 takes over monitor, runs T1 finishes, gives up monitor

Example:

fn1(…)…x.wait // T1 blocks

// T1 resumes// T1 finishes

fn4(…)…x.signal // T2 continues// T2 finishes

23

Tradeoff

HoareClaims: Cleaner, good for proofs When a condition variable is

signaled, it does not change Used in most textbooks

…but Inefficient implementation

HansenSignal is only a “hint” that thecondition may be true Need to check condition again

before proceeding Can lead to synchronization bugs

Used by most systems

Benefits: Efficient implementation Condition guaranteed to be true

once you are out of while !

CokeMachine::Deposit(){ lockacquire(); if (count == n) {

notFull.wait(&lock); } Add coke to the machine; count++; notEmpty.signal(); lockrelease();}

CokeMachine::Deposit(){ lockacquire(); while (count == n) {

notFull.wait(&lock); } Add coke to the machine; count++; notEmpty.signal(); lockrelease();}

24

Hansen v. Hoare semanticsThe priority inversion problem

Consider a set of communicating processes with varyingpriority

process P begin : deposit() : end P

High-PriorityProducer

process R begin : remove() : end R

Low-Priority

Consumer

SharedBuffers

signal(notEmpty)

wait(notEmpty)

High-Priority

Consumer

process Q begin : remove() : end Q

With Hoare semantics a low priority process can delay the progressof a high priority process

Page 7: Condition Synchronization · 2006-10-06 · 9 Synchronization Constructs Synchronization Coordinating execution of multiple threads that share data structures Past few lectures: Locks:

25

Summary

Synchronization Coordinating execution of multiple threads that share data

structures

Past lectures: Locks provide mutual exclusion Condition variables provide conditional synchronization

Today: Historical perspective Semaphores

Introduced by Dijkstra in 1960s Two types: binary semaphores and counting semaphores Supports both mutual exclusion and conditional synchronization

Monitors Separate mutual exclusion and conditional synchronization

26

Summary

Non-deterministic order of thread execution concurrencyproblems Multiprocessing

A system may contain multiple processors cooperatingthreads/processes can execute simultaneously

Multi-programming Thread/process execution can be interleaved because of time-

slicing

Goal: Ensure that your concurrent program works under ALL possible

interleaving

Approach: Define synchronization constructs and programming style for

developing concurrent programs Locks provide mutual exclusion Condition variables provide conditional synchronization