8. Condition Objects Prof. O. Nierstrasz Selected material © 2005 Bowbeer, Goetz, Holmes, Lea and Peierls.

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.
– R 7 :: 1 – 0024 Spring 2010 Parallel Programming 0024 Recitation Week 7 Spring Semester 2010.
Practice Session 7 Synchronization Liveness Deadlock Starvation Livelock Guarded Methods Model Thread Timing Busy Wait Sleep and Check Wait and Notify.
Ch 7 B.
Ch. 7 Process Synchronization (1/2) I Background F Producer - Consumer process :  Compiler, Assembler, Loader, · · · · · · F Bounded buffer.
Chapter 6 Process Synchronization Bernard Chen Spring 2007.
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.
5. Liveness and Guarded Methods Prof. O. Nierstrasz Selected material © Magee and Kramer.
Introduction to Software Engineering 7. Modeling Behaviour.
CP — Concurrent Programming 9. Condition Objects Prof. O. Nierstrasz Wintersemester 2005 / 2006.
CP — Concurrent Programming 8. Liveness and Asynchrony Prof. O. Nierstrasz Wintersemester 2005 / 2006.
Concurrency (3) CSE 132. iClicker/WUTexter Question The following four numbers are in 8-bit 2’s complement form: Which.
Monitors Chapter 7. The semaphore is a low-level primitive because it is unstructured. If we were to build a large system using semaphores alone, the.
CS 11 java track: lecture 7 This week: Web tutorial:
10. Petri Nets Prof. O. Nierstrasz. Roadmap  Definition: —places, transitions, inputs, outputs —firing enabled transitions  Modelling: —concurrency.
Shared Counters and Parallelism Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.
ESE Einführung in Software Engineering N. XXX Prof. O. Nierstrasz Fall Semester 2009.
5.6 Semaphores Semaphores –Software construct that can be used to enforce mutual exclusion –Contains a protected variable Can be accessed only via wait.
ESE Einführung in Software Engineering X. CHAPTER Prof. O. Nierstrasz Wintersemester 2005 / 2006.
CP — Concurrent Programming 12. Petri Nets Prof. O. Nierstrasz Wintersemester 2005 / 2006.
Metamodeling Seminar X. CHAPTER Prof. O. Nierstrasz Spring Semester 2008.
8. Condition Objects Prof. O. Nierstrasz Selected material © 2005 Bowbeer, Goetz, Holmes, Lea and Peierls.
5.6.2 Thread Synchronization with Semaphores Semaphores can be used to notify other threads that events have occurred –Producer-consumer relationship Producer.
Concurrency Monitors and Condition Synchronization Claus Brabrand
ESE Einführung in Software Engineering X. CHAPTER Prof. O. Nierstrasz Wintersemester 2005 / 2006.
N. XXX Prof. O. Nierstrasz Thanks to Jens Palsberg and Tony Hosking for their kind permission to reuse and adapt the CS132 and CS502 lecture notes.
Synchronization in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
7. Fixed Points. © O. Nierstrasz PS — Fixed Points 7.2 Roadmap  Representing Numbers  Recursion and the Fixed-Point Combinator  The typed lambda calculus.
CP — Concurrent Programming 3. Safety and Synchronization Prof. O. Nierstrasz Wintersemester 2005 / 2006.
7. Liveness and Asynchrony Prof. O. Nierstrasz. Roadmap  Asynchronous invocations  Simple Relays —Direct invocations —Thread-based messages —Command-based.
OORPT Object-Oriented Reengineering Patterns and Techniques X. CHAPTER Prof. O. Nierstrasz.
CP — Concurrent Programming X. CHAPTER Prof. O. Nierstrasz Wintersemester 2005 / 2006.
12. eToys. © O. Nierstrasz PS — eToys 12.2 Denotational Semantics Overview:  … References:  …
CS Introduction to Operating Systems
1 CSCI 6900: Design, Implementation, and Verification of Concurrent Software Eileen Kraemer August 19 th, 2010 The University of Georgia.
Parallel Processing (CS526) Spring 2012(Week 8).  Thread Status.  Synchronization in Shared Memory Programming(Java threads ) ◦ Locks ◦ Barriars.
Semaphores, Locks and Monitors By Samah Ibrahim And Dena Missak.
4061 Session 21 (4/3). Today Thread Synchronization –Condition Variables –Monitors –Read-Write Locks.
CSC321 Concurrent Programming: §5 Monitors 1 Section 5 Monitors.
In Java processes are called threads. Additional threads are associated with objects. An application is associated with an initial thread via a static.
Advanced Concurrency Topics Nelson Padua-Perez Bill Pugh Department of Computer Science University of Maryland, College Park.
SPL/2010 Synchronization 1. SPL/2010 Overview ● synchronization mechanisms in modern RTEs ● concurrency issues ● places where synchronization is needed.
TANNENBAUM SECTION 2.3 INTERPROCESS COMMUNICATION3 OPERATING SYSTEMS.
CS399 New Beginnings Jonathan Walpole. 2 Concurrent Programming & Synchronization Primitives.
Monitors and Blocking Synchronization Dalia Cohn Alperovich Based on “The Art of Multiprocessor Programming” by Herlihy & Shavit, chapter 8.
1 Condition Variables CS 241 Prof. Brighten Godfrey March 16, 2012 University of Illinois.
13/03/07Week 21 CENG334 Introduction to Operating Systems Erol Sahin Dept of Computer Eng. Middle East Technical University Ankara, TURKEY URL:
CS533 – Spring Jeanie M. Schwenk Experiences and Processes and Monitors with Mesa What is Mesa? “Mesa is a strongly typed, block structured programming.
CP — Concurrent Programming 6. Liveness and Guarded Methods Prof. O. Nierstrasz Wintersemester 2005 / 2006.
Comunication&Synchronization threads 1 Programación Concurrente Benemérita Universidad Autónoma de Puebla Facultad de Ciencias de la Computación Comunicación.
Dining Philosophers & Monitors Questions answered in this lecture: How to synchronize dining philosophers? What are monitors and condition variables? What.
1 G53SRP: Java Concurrency Control (2) – wait/notify Chris Greenhalgh School of Computer Science.
Background on the need for Synchronization
Condition Synchronization
Lecture 25 More Synchronized Data and Producer/Consumer Relationship
Threads Chate Patanothai.
Monitors Chapter 7.
Condition Variables and Producer/Consumer
Condition Variables and Producer/Consumer
Multithreading.
Monitors Chapter 7.
Monitors Chapter 7.
CSE 153 Design of Operating Systems Winter 19
CS333 Intro to Operating Systems
Software Engineering and Architecture
CSE 542: Operating Systems
CSE 542: Operating Systems
Presentation transcript:

8. Condition Objects Prof. O. Nierstrasz Selected material © 2005 Bowbeer, Goetz, Holmes, Lea and Peierls

© Oscar Nierstrasz 2 Roadmap  Condition Objects —Simple Condition Objects —The “Nested Monitor Problem” —Permits and Semaphores Condition Objects

© Oscar Nierstrasz 3 Roadmap  Condition Objects —Simple Condition Objects —The “Nested Monitor Problem” —Permits and Semaphores Condition Objects

Pattern: Condition Objects Intent: Condition objects encapsulate the waits and notifications used in guarded methods. Applicability  To simplify class design by off-loading waiting and notification mechanics. —Because of the limitations surrounding the use of condition objects in Java, in some cases the use of condition objects will increase rather than decrease design complexity!  … © Oscar Nierstrasz Condition Objects 4

Pattern: Condition Objects Applicability …  As an efficiency manoeuvre. —By isolating conditions, you can often avoid notifying waiting threads that could not possibly proceed given a particular state change.  As a means of encapsulating special scheduling policies surrounding notifications, for example to impose fairness or prioritization policies.  In the particular cases where conditions take the form of “permits” or “latches”. © Oscar Nierstrasz Condition Objects 5

© Oscar Nierstrasz Condition Objects 6 A client that awaits a condition blocks until another object signals that the condition now may hold. public interface Condition { public void await ();// wait for some condition public void signal ();// signal that condition } Cf. java.util.concurrent.locks.Condition Counter

© Oscar Nierstrasz Condition Objects 7 A Simple Condition Object We can encapsulate guard conditions with this class: public class SimpleConditionObject implements Condition { public synchronized void await () { try { wait(); } catch (InterruptedException ex) {} } public synchronized void signal () { notifyAll (); } NB: Careless use can lead to the “Nested Monitor Problem”

© Oscar Nierstrasz 8 Roadmap  Condition Objects —Simple Condition Objects —The “Nested Monitor Problem” —Permits and Semaphores Condition Objects

© Oscar Nierstrasz Condition Objects 9 The Nested Monitor problem We want to avoid waking up the wrong threads by separately notifying the conditions notMin and notMax: public class BoundedCounterNestedMonitorBAD extends BoundedCounterAbstract { protected Condition notMin = new SimpleConditionObject(); protected Condition notMax = new SimpleConditionObject(); public synchronized long value() { return count; }... Counter

© Oscar Nierstrasz Condition Objects 10 public synchronized void dec() { while (count == MIN) notMin.await();// wait till count not MIN if (count-- == MAX) notMax.signal(); } public synchronized void inc() {// can’t get in! while (count == MAX) notMax.await(); if (count++ == MIN) notMin.signal();// we never get here! }

© Oscar Nierstrasz Condition Objects 11 The Nested Monitor problem... Nested monitor lockouts occur whenever a blocked thread holds the lock for an object containing the method that would otherwise provide a notification to unblock the wait.

© Oscar Nierstrasz Condition Objects 12 2 nd example — Nested Monitors in FSP Nested Monitors typically arise when one synchronized object is implemented using another. Recall our one Slot buffer in FSP: Suppose we try to implement a call/reply protocol using a private instance of Slot: const N = 2 Slot = (put[v:0..N] -> get[v] -> Slot). ReplySlot=( put[v:0..N] -> my.put[v] -> ack -> ReplySlot | get -> my.get[v:0..N] -> ret[v] -> ReplySlot ). 01

© Oscar Nierstrasz Condition Objects 13 Nested Monitors in FSP... Our producer/consumer chain obeys the new protocol: 01 Producer = (put[0] -> ack ->put[1] -> ack ->put[2] -> ack -> Producer ). Consumer = ( get-> ret[x:0..N]->Consumer ). ||Chain = (Producer||ReplySlot||my:Slot||Consumer).

© Oscar Nierstrasz Condition Objects 14 Nested Monitors in FSP... But now the chain may deadlock: Progress violation for actions: {{ack, get}, my.{get, put}[0..2], {put, ret}[0..2]} Trace to terminal set of states: get Actions in terminal set: {} 01

© Oscar Nierstrasz Condition Objects 15 Solving the Nested Monitors problem You must ensure that:  Waits do not occur while synchronization is held on the host object. —Leads to a guard loop that reverses the faulty synchronization  Notifications are never missed. —The entire guard wait loop should be enclosed within synchronized blocks on the condition object.  Notifications do not deadlock. —All notifications should be performed only upon release of all synchronization (except for the notified condition object).  Helper and host state must be consistent. —If the helper object maintains any state, it must always be consistent with that of the host —If it shares any state with the host, access must be synchronized.

© Oscar Nierstrasz Condition Objects 16 Example solution public class BoundedCounterCondition extends BoundedCounterAbstract {public void dec() {// not synched! boolean wasMax = false;// record notification condition synchronized(notMin) {// synch on condition object while (true) {// new guard loop synchronized(this) { if (count > MIN) {// check and act wasMax = (count == MAX); count--; break; } notMin.await();// release host synch before wait } if (wasMax) notMax.signal();// release all syncs! }... }

Other solutions …  Be sure to lock just a single object —i.e., either the host or the condition object  Remove host synchronization (if safe or immutable) © Oscar Nierstrasz Condition Objects 17

2 nd example — removing synchronization © Oscar Nierstrasz Condition Objects 18 ||ReplySlot = (Putter||Getter). Putter = ( put[v:0..N] -> my.put[v] -> ack -> Putter ). Getter = ( get -> my.get[v:0..N] -> ret[v] -> Getter ). The ReplySlot wrapper has no state of its own, so we can simply remove the synchronization! Progress Check States: 54 Transitions: 90 Memory used: 6612K No progress violations detected. Progress Check in: 1ms

© Oscar Nierstrasz 19 Roadmap  Condition Objects —Simple Condition Objects —The “Nested Monitor Problem” —Permits and Semaphores Condition Objects

© Oscar Nierstrasz Condition Objects 20 Pattern: Permits and Semaphores Intent: Bundle synchronization in a condition object when synchronization depends on the value of a counter. Applicability  When any given await may proceed only if there have been more signals than awaits. —I.e., when await decrements and signal increments the number of available “permits”.  You need to guarantee the absence of missed signals. —Unlike simple condition objects, semaphores work even if one thread enters its await after another thread has signalled that it may proceed (!)  The host classes can arrange to invoke Condition methods outside of synchronized code.

Permits and Semaphores — design steps  Define a class implementing Condition that maintains a permit count, and immediately releases await if there are already enough permits. —e.g., BoundedCounter © Oscar Nierstrasz Condition Objects 21

Example © Oscar Nierstrasz Condition Objects 22 public class Permit implements Condition { private int count; Permit(int init) { count = init; } public synchronized void await() { while (count == 0) { try { wait(); } catch(InterruptedException ex) { }; } count --; } public synchronized void signal() { count ++; notifyAll(); } Counter

© Oscar Nierstrasz Condition Objects 23 Design steps...  As with all kinds of condition objects, their clients must avoid invoking await inside of synchronized code. —You can use a before/after design of the form: class Host { Condition aCondition;... public method m1() { aCondition.await();// not synced doM1();// synced for each Condition c enabled by m1() c.signal();// not synced } protected synchronized doM1() {... } }

Using permits © Oscar Nierstrasz Condition Objects 24 public class Building{ Permit permit; Building(int n) { permit = new Permit(n); } void enter(String person) { // NB: unsynchronized permit.await(); System.out.println(person + " has entered the building"); } void leave(String person) { System.out.println(person + " has left the building"); permit.signal(); } Counter

Using permits © Oscar Nierstrasz Condition Objects 25 public static void main(String[] args) { Building building = new Building(3); enterAndLeave(building, "bob"); enterAndLeave(building, "carol");... } private static void enterAndLeave(final Building building, final String person) { new Thread() { public void run() { building.enter(person); pause(); building.leave(person); } }.start(); } bob has entered the building carol has entered the building ted has entered the building bob has left the building alice has entered the building ted has left the building carol has left the building elvis has entered the building alice has left the building elvis has left the building

© Oscar Nierstrasz Condition Objects 26 Variants Permit Counters: (Counting Semaphores)  Just keep track of the number of “permits”  Can use notify instead of notifyAll if class is final Fair Semaphores:  Maintain FIFO queue of threads waiting on a SimpleCondition Locks and Latches:  Locks can be acquired and released in separate methods  Keep track of thread holding the lock so locks can be reentrant!  A latch is set to true by signal, and always stays true (eg a future)

© Oscar Nierstrasz Condition Objects 27 Semaphores in Java public class Semaphore {// simple version private int value; public Semaphore (int initial) { value = initial; } synchronized public void up() {// AKA V ++value; notify();// wake up just one thread! } synchronized public void down() {// AKA P while (value== 0) { try { wait(); } catch(InterruptedException ex) { }; } --value; } See also. java.util.concurrent.Semaphore Counter

© Oscar Nierstrasz Condition Objects 28 Using Semaphores public class BoundedCounterSem extends BoundedCounterAbstract { … protected Semaphore mutex, full, empty; BoundedCounterVSem() { mutex = new Semaphore(1); full = new Semaphore(0);// number of counters empty = new Semaphore(MAX-MIN); // number of empty slots } public long value() { mutex.down(); // grab the resource long val = count; mutex.up(); // release it return val; } public void inc() { empty.down(); // grab a slot mutex.down(); count ++; mutex.up(); full.up(); // release a counter } … Counter

© Oscar Nierstrasz Condition Objects 29 Using Semaphores... These would cause a nested monitor problem!... public void BADinc() { mutex.down(); empty.down(); // locks out BADdec! count ++; full.up(); mutex.up(); } public void BADdec() { mutex.down(); full.down(); // locks out BADinc! count --; empty.up(); mutex.up(); }

The JUC version © Oscar Nierstrasz Condition Objects 30 import java.util.concurrent.Semaphore; public class BoundedCounterJUCSem extends BoundedCounterAbstract { protected Semaphore mutex; protected Semaphore full; protected Semaphore empty; BoundedCounterJUCSem() { mutex = new Semaphore(1);// one permit for critical section full = new Semaphore(0); // number of counters empty = new Semaphore((int)(MAX-MIN)); // number of empty slots }... public void inc() { try { empty.acquire();// grab a slot mutex.acquire(); } catch (InterruptedException e) { } count ++; mutex.release(); full.release();// release a counter checkInvariant(); }... }

© Oscar Nierstrasz Condition Objects CP 9.31 What you should know!  What are “condition objects”? How can they make your life easier? Harder?  What is the “nested monitor problem”?  How can you avoid nested monitor problems?  What are “permits” and “latches”? When is it natural to use them?  How does a semaphore differ from a simple condition object?  Why (when) can semaphores use notify() instead of notifyAll()?

© Oscar Nierstrasz Condition Objects CP 9.32 Can you answer these questions?  Why doesn’t SimpleConditionObject need any instance variables?  What is the easiest way to avoid the nested monitor problem?  What assumptions do nested monitors violate?  How can the obvious implementation of semaphores (in Java) violate fairness?  How would you implement fair semaphores?

License © Oscar Nierstrasz ESE — Introduction Attribution-ShareAlike 3.0 Unported You are free: to Share — to copy, distribute and transmit the work to Remix — to adapt the work Under the following conditions: Attribution. You must attribute the work in the manner specified by the author or licensor (but not in any way that suggests that they endorse you or your use of the work). Share Alike. If you alter, transform, or build upon this work, you may distribute the resulting work only under the same, similar or a compatible license. For any reuse or distribution, you must make clear to others the license terms of this work. The best way to do this is with a link to this web page. Any of the above conditions can be waived if you get permission from the copyright holder. Nothing in this license impairs or restricts the author's moral rights.