Re: JavaMemoryModel: Class initialization safety is hard

From: Paul Haahr (haahr@jivetech.com)
Date: Fri Jul 09 1999 - 18:15:35 EDT


Bill Pugh wrote, replying to Joshua Bloch:
> I disagree with your analysis above. I believe the appropriate
> section is 12.4.1: [...]
>
> Section 12.4.2 would prevent another thread from running the clinit
> method of a class if another thread had already started it. But it
> doesn't guarantee class initialization safety.
>
> The problem is the word _first_ in:
>
> >A class or interface type T will be initialized at its first active
> >use, which occurs if:
>
> What does "first" mean in a multi-processor system? This is worse
> than general relativity: it isn't just that different yardsticks are
> used to measure time on different processors. Time doesn't even have
> the same linear ordering on different processors.

I fear it's more ambiguous than that. Going back to section 12.4.2,
there's this text:

  [the virtual machine] assumes that the Class object has already been
  verified and prepared, and that the Class object contains state that
  indicates one of four situations:

    * This Class object is verified and prepared but not initialized.
    * This Class object is being initialized by some particular thread T.
    * This Class object is fully initialized and ready for use.
    * This Class object is in an erroneous state, perhaps because the
      verification or preparation step failed, or because initialization
      was attempted and failed.

My interpretation of this section is that the VM must discriminate among
these four situations in a manner that is safe for its implementation.
That is, detecting that a class is ``fully initialized'' must be a
multiprocessor-safe operation in an MP implementation.

On the other hand, I suspect it's possible to determine that the Class
object is fully initialized without doing any memory barriers. (We can
all imagine variants of the double-check idiom which get there.)

--p
-------------------------------
This is the JavaMemoryModel mailing list, managed by Majordomo 1.94.4.

To send a message to the list, email JavaMemoryModel@cs.umd.edu
To send a request to the list, email majordomo@cs.umd.edu and put
your request in the body of the message (use the request "help" for help).
For more information, visit http://www.cs.umd.edu/~pugh/java/memoryModel



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