Java Threads mailing list archive

Re: It's been quiet for a while...

From: P.H.Welch@ukc.ac.uk
Date: Wed, 21 Oct 1998 19:31:48 +0100

> ...so here is an item for discussion.

The code looks dodgy to me?  For one thing, they break one of Doug Lea's
golden rules never to `wait' inside an `if' but always in a `while':

  public syncronized void readLock() ...
    ...
      if ( allowReadLock() == false) {
        wait ();
      }
      // Bingo! Switch from a waiting lock to an active lock
      activeReadLocks++;
    ...
  }

Alas, because of the semantics of Java's `notify', we can't assume that
`allowReadLock' will now be true simply because some notifying thread
made it so and notified us ... it has to be done in a `while' loop.

The JavaPP channel algorithms also break Doug's rule ... but we know it's
safe for us ;-)

But I haven't got to grips with this code yet!  As we know with this problem,
four interdependent monitor methods are horrible to try and understand ...
so maybe it's OK ...

But it sure looks like there's a hole here ... everyone gets notified in
`writeUnlock' evry time ... not only when `activeWriteLocks' reaches zero
... and what about `waitingWriteLocks' ... surely that `if' has to be
a `while' ... ?!!

Anyway, notifying everyone every time a write goes away is awfully
inefficient ...

>From the documentation, waiting writers have precedence over readers ...
OK ... but it looks like a couple of slow writers could forever starve
readers from getting the lock ... as the writers alternately wait for
each other to finish writing ... always having found something else
to write before the other writer finishes.

Hoare's algorithm from his 1975 CACM paper and the one I presented in
the spare slot at WoTUG-21 guarantee fair service to readers and writers
... with neither being able to block the other indefinitely.

Enclosed is a uuencoded gzipped tar file containing:

  CrewServer.java:  a (JCSP) process with a channel interface implementing
		    the WoTUG-21 algorithm (which is trivial to prove correct)
  
  TestCrew.java:    a demonstration program using a CrewServer.

  Crew.java:        an ADT wrapper for CrewServer that has the same API as
		    Hoare's original code (and the JServLock - apart from
		    the timeouts).

  TestCrew2.java:   the same demonstration program as before but using 
		    a Crew object rather than the CrewServer process.

Feedback welcome ... you have to have the `jcspclasses.jar' file to use
the above ...

Cheers,

Peter.

(cut here)
-----------------------------------------------------------------------------
begin 644 crew.gz
M'XL("`@G+C8``V-R97<`[1M=<]LV,L_^%;B\1*IE69)E9R:Z/*1.9BYW3=-Q
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