Read/Write Lock

Origin: Lea 97
Reason: To allow concurrent read access to an object, but exclusive write access.
Synopsis: Use a specialized Scheduler whose method enter is replaced by two methods: readLock and writeLock.
Example: An on-line auction system where remote users get and set bids.
Solution: In a very simple form, a Read/Write Lock is sketched as follow.

Method readLock looks like:

void synchronized readLock () {
    while (pendingWriters + activeWriters > 0) wait ();
    activeReaders++:     // being reading
}
Method writeLock looks like:
void synchronized writeLock () {
    pendingWriters++;    // block readers
    while (activeReaders + activeWriters > 0) wait ();
    pendingWriters--;
    activeWriters++;     // being writing
}
Method done looks like:
void synchronized done () {
    if (activeWriters > 0)  activeWriters--;
    else if (activeReaders > 0)  activeReaders--;
    else error ();
    notifyAll ();
}
Note that activeWriters is either 0 or 1.
See also: Scheduler