Sleep/Wakeup and Condition Variables. Example: Await/Awake Consider a very simple use of sleep/wakeup to implement two new primitives: currentThread->Await()

Slides:



Advertisements
Similar presentations
Operating Systems Semaphores II
Advertisements

Operating Systems: Monitors 1 Monitors (C.A.R. Hoare) higher level construct than semaphores a package of grouped procedures, variables and data i.e. object.
Ch 7 B.
Chapter 6: Process Synchronization
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 6: Process Synchronization.
Process Synchronization. Module 6: Process Synchronization Background The Critical-Section Problem Peterson’s Solution Synchronization Hardware Semaphores.
CS 5704 Fall 00 1 Monitors in Java Model and Examples.
Concurrency (3) CSE 132. iClicker/WUTexter Question The following four numbers are in 8-bit 2’s complement form: Which.
Monitors and Semaphores. Annotated Condition Variable Example Condition *cv; Lock* cvMx; int waiter = 0; void await() { cvMx->Lock(); waiter = waiter.
1 Semaphores and Monitors: High-level Synchronization Constructs.
Operating Systems ECE344 Ding Yuan Synchronization (I) -- Critical region and lock Lecture 5: Synchronization (I) -- Critical region and lock.
1 CS318 Project #3 Preemptive Kernel. 2 Continuing from Project 2 Project 2 involved: Context Switch Stack Manipulation Saving State Moving between threads,
5.6 Semaphores Semaphores –Software construct that can be used to enforce mutual exclusion –Contains a protected variable Can be accessed only via wait.
5.6.2 Thread Synchronization with Semaphores Semaphores can be used to notify other threads that events have occurred –Producer-consumer relationship Producer.
Monitors CSCI 444/544 Operating Systems Fall 2008.
02/23/2004CSCI 315 Operating Systems Design1 Process Synchronization Notice: The slides for this lecture have been largely based on those accompanying.
CS533 Concepts of Operating Systems Class 3 Monitors.
02/17/2010CSCI 315 Operating Systems Design1 Process Synchronization Notice: The slides for this lecture have been largely based on those accompanying.
U NIVERSITY OF M ASSACHUSETTS, A MHERST Department of Computer Science Emery Berger University of Massachusetts, Amherst Operating Systems CMPSCI 377 Lecture.
Synchronization CSCI 444/544 Operating Systems Fall 2008.
02/19/2007CSCI 315 Operating Systems Design1 Process Synchronization Notice: The slides for this lecture have been largely based on those accompanying.
Discussion Week 3 TA: Kyle Dewey. Overview Concurrency overview Synchronization primitives Semaphores Locks Conditions Project #1.
CS510 Concurrent Systems Introduction to Concurrency.
Experience with Processes and Monitors in Mesa
© 2004, D. J. Foreman 1 High Level Synchronization and Inter-Process Communication.
Implementing Synchronization. Synchronization 101 Synchronization constrains the set of possible interleavings: Threads “agree” to stay out of each other’s.
The Synchronization Toolbox. Mutual Exclusion Race conditions can be avoiding by ensuring mutual exclusion in critical sections. Critical sections are.
Operating Systems ECE344 Ashvin Goel ECE University of Toronto Mutual Exclusion.
4061 Session 21 (4/3). Today Thread Synchronization –Condition Variables –Monitors –Read-Write Locks.
CSC321 Concurrent Programming: §5 Monitors 1 Section 5 Monitors.
Chapter 6 – Process Synchronisation (Pgs 225 – 267)
Java Thread and Memory Model
D u k e S y s t e m s More Synchronization featuring Producer/Consumer with Semaphores Jeff Chase Duke University.
SPL/2010 Guarded Methods and Waiting 1. SPL/2010 Reminder! ● Concurrency problem: asynchronous modifications to object states lead to failure of thread.
CS399 New Beginnings Jonathan Walpole. 2 Concurrent Programming & Synchronization Primitives.
13/03/07Week 21 CENG334 Introduction to Operating Systems Erol Sahin Dept of Computer Eng. Middle East Technical University Ankara, TURKEY URL:
Problems with Semaphores Used for 2 independent purposes –Mutual exclusion –Condition synchronization Hard to get right –Small mistake easily leads to.
CS533 – Spring Jeanie M. Schwenk Experiences and Processes and Monitors with Mesa What is Mesa? “Mesa is a strongly typed, block structured programming.
U NIVERSITY OF M ASSACHUSETTS A MHERST Department of Computer Science Computer Systems Principles Synchronization Emery Berger and Mark Corner University.
Administrivia Nachos guide and Lab #1 are on the web. Form project teams of 2-3. Lab #1 due February 5. Synchronization.
1 Previous Lecture Overview  semaphores provide the first high-level synchronization abstraction that is possible to implement efficiently in OS. This.
CSE 153 Design of Operating Systems Winter 2015 Lecture 5: Synchronization.
Lecture 6 Page 1 CS 111 Summer 2013 Concurrency Solutions and Deadlock CS 111 Operating Systems Peter Reiher.
1 Lecture 6 “Nachos” n nachos overview n directory structure n nachos emulated machine n nachos OS n nachos scheduler n nachos threads.
Implementing Lock. From the Previous Lecture  The “too much milk” example shows that writing concurrent programs directly with load and store instructions.
CS 153 Design of Operating Systems Winter 2016 Lecture 7: Synchronization.
CS533 Concepts of Operating Systems Class 2a Monitors.
CS510 Concurrent Systems Jonathan Walpole. Introduction to Concurrency.
© 2004, D. J. Foreman 1 Monitors and Inter-Process Communication.
© 2004, D. J. Foreman 1 Monitors and Inter-Process Communication.
Implementing Mutual Exclusion Andy Wang Operating Systems COP 4610 / CGS 5765.
Mutual Exclusion -- Addendum. Mutual Exclusion in Critical Sections.
CS162 Section 2. True/False A thread needs to own a semaphore, meaning the thread has called semaphore.P(), before it can call semaphore.V() False: Any.
CSE 120 Principles of Operating
CSE 120 Principles of Operating
Background on the need for Synchronization
Semaphores and Condition Variables
CSE 120 Principles of Operating
Threads and Synchronization
CSCI 511 Operating Systems Chapter 5 (Part C) Monitor
Midterm review: closed book multiple choice chapters 1 to 9
CSE 451: Operating Systems Autumn 2004 Module 6 Synchronization
CSE 451: Operating Systems Autumn 2003 Lecture 7 Synchronization
CSE 451: Operating Systems Autumn 2005 Lecture 7 Synchronization
CSE 451: Operating Systems Winter 2004 Module 6 Synchronization
CSE 451: Operating Systems Winter 2003 Lecture 7 Synchronization
CSE 153 Design of Operating Systems Winter 19
CS333 Intro to Operating Systems
Monitors and Inter-Process Communication
Don Porter Portions courtesy Emmett Witchel
Presentation transcript:

Sleep/Wakeup and Condition Variables

Example: Await/Awake Consider a very simple use of sleep/wakeup to implement two new primitives: currentThread->Await() Block the calling thread. Thread::Awake() If the target thread is sleeping in a previous Await(), wake it up. Else leave it alone.

Await/Awake with Sleep/Wakeup (first cut) void Thread::Await() { ASSERT(this == currentThread); ASSERT(!awaiting); awaiting = TRUE;/* “I’m sleeping” */ Sleep();/* sleep */ } void Thread::Awake() { ASSERT(this != currentThread); if (awaiting) /* wakeup */ scheduler->ReadyToRun(this); awaiting = FALSE;/* “you’re awake” */ }

The Trouble with Sleep/Wakeup void Thread::Await() { ASSERT(this == currentThread); ASSERT(!awaiting); awaiting = TRUE;/* “I’m sleeping” */ Sleep();/* sleep */ } void Thread::Awake() { ASSERT(this != currentThread); if (awaiting) /* wakeup */ scheduler->ReadyToRun(this); awaiting = FALSE;/* “you’re awake” */ } These sleep/wakeup races are sometimes referred to as the wake-up waiter problem. missed wakeup double wakeup

Using Sleep/Wakeup Safely Thread* waiter = 0; void await() { disable interrupts waiter = currentThread;/* “I’m sleeping” */ currentThread->Sleep();/* sleep */ enable interrupts } void awake() { disable interrupts if (waiter)/* wakeup */ scheduler->ReadyToRun(waiter); waiter = (Thread*)0;/* “you’re awake” */ enable interrupts } Disabling interrupts prevents a context switch between “I’m sleeping” and “sleep”. Disabling interrupts prevents a context switch between “wakeup” and “you’re awake”. Will this work on a multiprocessor? Nachos Thread::Sleep requires disabling interrupts (???).

What to Know about Sleep/Wakeup 1. Sleep/wakeup primitives are the fundamental basis for all blocking synchronization. 2. All use of sleep/wakeup requires some additional low-level mechanism to avoid missed and double wakeups. disabling interrupts, and/or constraints on preemption, and/or (Unix kernels use this instead of disabling interrupts) spin-waiting, e.g., with TSL(on a multiprocessor) 3. These low-level mechanisms are tricky and error-prone. 4. High-level synchronization primitives take care of the details of using sleep/wakeup, hiding them from the caller. semaphores, mutexes, condition variables

Digression: Sleep and Yield in Nachos disable interrupts enable interrupts Yield() { IntStatus old = SetLevel(IntOff); next = scheduler->FindNextToRun(); if (next != NULL) { scheduler->ReadyToRun(this); scheduler->Run(next); } interrupt->SetLevel(old); } Sleep() { ASSERT(getLevel = IntOff); this->status = BLOCKED; next = scheduler->FindNextToRun(); while(next = NULL) { /* idle */ next = scheduler->FindNextToRun(); } scheduler->Run(next); } Disable interrupts on the call to Sleep or Yield, and rely on the “other side” to re-enable on return from its own Sleep or Yield. Context switch itself is a critical section, which we enter only via Sleep or Yield.

A New Synchronization Problem: Ping-Pong void PingPong() { while(not done) { if (blue) switch to purple; if (purple) switch to blue; } How to do this correctly using sleep/wakeup? How to do it without using sleep/wakeup?

Ping-Pong with Sleep/Wakeup? void PingPong() { while(not done) { blue->Sleep(); purple->Wakeup(); } void PingPong() { while(not done) { blue->Wakeup(); purple->Sleep(); }

Ping-Pong with Mutexes? void PingPong() { while(not done) { Mx->Acquire(); Mx->Release(); }

Mutexes Don’t Work for Ping-Pong

Condition Variables Condition variables allow explicit event notification. much like a souped-up sleep/wakeup associated with a mutex to avoid sleep/wakeup races Condition::Wait(Lock*) Called with lock held: sleep, atomically releasing lock. Atomically reacquire lock before returning. Condition:: Signal(Lock*) Wake up one waiter, if any. Condition::Broadcast(Lock*) Wake up all waiters, if any.

Ping-Pong Using Condition Variables void PingPong() { mx->Acquire(); while(not done) { cv->Signal(); cv->Wait(); } mx->Release(); } See how the associated mutex avoids sleep/wakeup races? Will your Lab #2 condition variables execute this example correctly?

Using Condition Variables Condition *cv; Lock* cvMx; int waiter = 0; void await() { cvMx->Lock(); waiter = waiter + 1;/* “I’m sleeping” */ cv->Wait(cvMx);/* sleep */ cvMx->Unlock(); } void wakeup() { cvMx->Lock(); if (waiter) cv->Signal(cvMx); waiter = waiter - 1; CvMx->Unlock(); } Must hold lock when calling Wait. Wait atomically reacquires lock before returning. Wait atomically releases lock and sleeps until next Signal. Association with mutex allows threads to safely manage state related to the sleep/wakeup coordination (e.g., waiters count).

The Roots of Condition Variables: Monitors A monitor is a module (a collection of procedures) in which execution is serialized. P1() P2() P3() P4() state ready to enter blocked wait() At most one thread may be active in the monitor at a time. (exit) (enter) A thread may wait in the monitor, allowing another thread to enter. A thread in the monitor may signal a waiting thread, causing it to return from its wait and reenter the monitor. signal() CVs are easier to understand if we think about them in terms of the original monitor formulation. [Brinch Hansen 1973, C.A.R. Hoare 1974]

Hoare Semantics P1() P2() P3() P4() state ready to enter waiting wait() (exit) (enter) Hoare semantics: the signaled thread immediately takes over the monitor, and the signaler is suspended. signal() (Hoare) Hoare semantics allow the signaled thread to assume that the state has not changed since the signal that woke it up. Suppose purple signals blue in the previous example. suspended signal() (Hoare) The signaler does not continue in the monitor until the signaled thread exits or waits again.

Mesa Semantics P1() P2() P3() P4() state ready to (re)enter waiting wait() (exit) (enter) Mesa semantics: the signaled thread transitions back to the ready state. signal() (Mesa) BUT: the signaled thread must examine the monitor state again after the wait, as the state may have changed since the signal. Suppose again that purple signals blue in the original example. There is no suspended state: the signaler continues until it exits the monitor or waits. The signaled thread contends with other ready threads to (re)enter the monitor and return from wait. Mesa semantics are easier to understand and implement... Loop before you leap!

From Monitors to Mx/Cv Pairs Mutexes and condition variables (as in Nachos) are based on monitors, but they are more flexible. A monitor is “just like” a module whose state includes a mutex and a condition variable. It’s “just as if” the module’s methods Acquire the mutex on entry and Release the mutex before returning. But with mutexes, the critical regions within the methods can be defined at a finer grain, to allow more concurrency. With condition variables, the module methods may wait and signal on multiple independent conditions. Nachos (and Topaz and Java) use Mesa semantics for their condition variables: loop before you leap!

Mutual Exclusion in Java Mutexes and condition variables are built in to every Java object. no explicit classes for mutuxes and condition variables Every object is/has a “monitor”. At most one thread may “own” any given object’s monitor. A thread becomes the owner of an object’s monitor by executing a method declared as synchronized some methods may choose not to enforce mutual exclusion (unsynchronized) by executing the body of a synchronized statement supports finer-grained locking than “pure monitors” allow exactly identical to the Modula-2 “LOCK(m) DO” construct in Birrell

Wait/Notify in Java public class Object { void notify(); /* signal */ void notifyAll(); /* broadcast */ void wait(); void wait(long timeout); } public class PingPong (extends Object) { public synchronized void PingPong() { while(true) { notify(); wait(); } Every Java object may be treated as a condition variable for threads using its monitor. A thread must own an object’s monitor to call wait/notify, else the method raises an IllegalMonitorStateException. Wait(*) waits until the timeout elapses or another thread notifies, then it waits some more until it can re-obtain ownership of the monitor: Mesa semantics. Loop before you leap!

Spin-Yield: Just Say No void Thread::Await() { awaiting = TRUE; while(awaiting) Yield(); } void Thread::Awake() { if (awaiting) awaiting = FALSE; }