//@author: j.n.magee 11/11/96


/********************************************************/
//
// The Read Write Monitor Class Version 2
//
class ReadWriteVs2 implements ReadWrite{

    private protected int readers =0;
    private protected boolean writing = false;
    private protected int waitingW = 0; // no of waiting Writers.

    synchronized public void acquireRead() {
        while (writing || waitingW>0) {
            try{ wait(); } catch(InterruptedException e){}
        }
        ++readers;
      }

    synchronized public void releaseRead() {
        --readers;
        if(readers==0) notify();
    }

    synchronized public void acquireWrite() {
        while (readers>0 || writing) {
            ++waitingW;
            try{ wait(); } catch(InterruptedException e){}
            --waitingW;
        }
        writing = true;
      }

    synchronized public void releaseWrite() {
        writing = false;
        notifyAll();
    }

 }
