Re: JavaMemoryModel: References

From: Bill Pugh (pugh@cs.umd.edu)
Date: Thu May 20 2004 - 08:11:44 EDT


If people are defining their own subclasses of a Reference class, they
are responsible for proper synchronization, not the VM.

At any rate, so long as people aren't passing pointers to References
around via a data race, we shouldn't have to worry about imposing
a hb-edge from Reference construction to Reference use.

The only cases that we really need to worry about are:
        thread 1 calls clear, thread 2 calls get
        GC clears reference, thread 2 calls get

which are handled by making the referent field volatile.

        Bill

On May 19, 2004, at 7:13 PM, Sylvia Else wrote:

> At 11:51 AM 19/05/2004 -0400, Bill Pugh wrote:
>> OK, we've reviewed the stuff on references.
>>
>> I think most of this stuff can go into the java.lang.ref JavaDoc
>> rather
>> than the jsr133 spec. After all, references are not mentioned in the
>> JLS.
>>
>> Within the Reference class, there should be an hb edge
>> from creation of the Reference or call to clear()
>
> I too wondered about having the hb edge from the creation of the
> Reference, but these classes can be subclassed. It seems to me that
> the reference can be cleared while the subclass constructor is still
> being executed. This would arise where the only strong reference
> remaining was the one passed to the Reference constructor. So putting
> the hb edge from the creation of the reference means one has to pay
> very close attention to exactly what "creating a Reference" actually
> means.
>
> Maybe it's sufficient to clarify it as:
>
> There is an happens-before edge from the completion of the
> WeakReference, SoftReference or PhantomReference constructor....
>
> That said, if subclasses are being used, it seems more than likely
> that fields set by the subclasses will be used by the code that
> retrieves the Reference object once the reference has been cleared. At
> best such code would contain a data race in the absence of
> synchronization, and even with synchronization the code could still
> easily be executed in the wrong order from the programmer's
> perspective.
>
> Suddenly this seems like a minefield.
>
> Sylvia.
>
> -------------------------------
> JavaMemoryModel mailing list -
> http://www.cs.umd.edu/~pugh/java/memoryModel

-------------------------------
JavaMemoryModel mailing list - http://www.cs.umd.edu/~pugh/java/memoryModel



This archive was generated by hypermail 2b29 : Thu Oct 13 2005 - 07:01:07 EDT