org.eigenbase.runtime
Class Interlock

java.lang.Object
  extended by org.eigenbase.runtime.Interlock
Direct Known Subclasses:
ExclusivePipe

public class Interlock
extends Object

A synchronization primitive which allows a producer and a consumer to use the same resources without treading on each other's feet.

At most one of the producer and consumer has access at a time. The synchronization ensures that the call sequence is as follows:

ExclusivePipe is a simple extension to this class containing a ByteBuffer as the shared resource.

Version:
$Id: //open/dev/farrago/src/org/eigenbase/runtime/Interlock.java#9 $
Author:
jhyde

Field Summary
private  Semaphore empty
          The producer notifies empty every time it finishes writing.
private  Semaphore full
          The consumer notifies full every time it finishes reading.
 
Constructor Summary
Interlock()
           
 
Method Summary
 void beginReading()
          Acquires the buffer, in preparation for reading.
 void beginWriting()
          Acquires the buffer, in preparation for writing.
 void endReading()
          Releases the buffer after reading its contents.
 void endWriting()
          Releases the buffer after writing.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

empty

private final Semaphore empty
The producer notifies empty every time it finishes writing. The consumer waits for it.


full

private final Semaphore full
The consumer notifies full every time it finishes reading. The producer waits for it, then starts work.

Constructor Detail

Interlock

public Interlock()
Method Detail

beginWriting

public void beginWriting()
Acquires the buffer, in preparation for writing.

The producer should call this method. After this call completes, the consumer's call to beginReading() will block until the producer has called endWriting().


endWriting

public void endWriting()
Releases the buffer after writing.

The producer should call this method. After this call completes, the producers's call to beginWriting() will block until the consumer has called beginReading() followed by endReading().


beginReading

public void beginReading()
Acquires the buffer, in preparation for reading.

After this call completes, the producer's call to beginWriting() will block until the consumer has called endReading().


endReading

public void endReading()
Releases the buffer after reading its contents.

The consumer should call this method. After this call completes, the consumer's call to beginReading() will block until the producer has called beginWriting() followed by endWriting().