Lecture Notes for Concurrent Programming

15 July 2003 - Synchronization Utilities


Synchronization Utilities


Multiple-Unit Resource Management


Counting Semaphores


Counting Semaphore Implementation

 
 class CountingSemaphore

   private Mutex mtx(false), waitq(false)
   private int count, waiters = 0

   Semaphore(int count) { this.count = count }

   void down() throws InterruptedException
     mtx.lock()

     while count < 1
       waiters++
       mtx.unlock()

       try { waitq.lock() }
       catch (InterruptedException ie)
	 synchronized (this) { waiters-- } ; throw ie 

       try mtx.lock()
       catch (InterruptedException ie)
	 synchronized (this) { waiters-- } ; throw ie 
       waiters--

     count--
     mtx.unlock()

   void up()
     try mtx.lock()
     catch (InterruptedException ie) return
     count++
     if (waiters > 0) waitq.unlock()
     mtx.unlock()
 


Boss-Worker Synchronization


Latches


Latch Implementation

 class Latch

   // Invariant:  ready -> waiters = 0

   private boolean ready = false

   synchronized void block() 
   throws InterruptedException
     try { while (!ready) wait() }
     catch (InterruptedException ie) 
       throw ie

   void unblock()
     if !ready
       ready = true
       notifyAll()


Double Buffering


Exchangers


Exchanger Implementation


 class Exchanger

   private Object fst, snd
   private boolean interrupted = false

   synchronized Object exchange(Object o) 
   throws InterruptedException

     if interrupted
       interrupted = false
       throw new InterruptedException()

     if fst != null
       snd = o
       notify()
       return fst

     fst = o
     try { while (snd == null) wait() }
     catch (InterruptedException ie)
       fst = snd = null
       interrupted = true
       throw ie

     return snd


Example Exchanger Use


class Producer
extends Runnable

  private Exchanger exch
  private Buffer b

  public void run() 
    while true
      fill_buffer(b)
      b = exch.exchange(b)

class Consumer
extends Runnable

  private Exchanger exch
  private Buffer b

  public void run() 
    while true
      b = exch.exchange(b)
      empty_buffer(b)

Class ProducerConsumer

  public static void main() 
    Exchanger exch = new Exchanger
    Producer p = new Producer(exch)
    Consumer p = new Consumer(exch)


Single Wait Sets


Condition Variables


Condition Variable Example


Condition Variable Implementation

 public class ConditionVariable

   public void pause() throws InterruptedException

     if Thread.interrupted() 
       throw new InterruptedException()

     try
       synchronized this
	 mutex.unlock()  // It's locked coming in.
	 try wait() 
	 catch (InterruptedException ex)
	   notify()
	   throw ex
     finally
       boolean interrupted = false
       while true
	 try
	   mutex.lock()
	   break
	 catch (InterruptedException ex)
	   interrupted = true
       if interrupted
	 Thread.currentThread().interrupt()

   public synchronized void proceed()
     notify()


Points to Remember


This page last modified on 16 July 2003.