<div dir="ltr">Ok thanks for the confirmations!<div><br></div><div>If I'm understanding correctly, in the future the way to guarantee once-only initialization of a ClassValue would be to have it produce a StableValue, of which only one will be installed, and which will then be guaranteed by the JVM to have once-only initialization and a foldable, stable result. Until that is generally available, I should use a factory object with double-checked locking to ensure once-only initialization (which is essentially what my own ClassValue did, but built as a wrapper around java.lang.ClassValue).</div></div><br><div class="gmail_quote gmail_quote_container"><div dir="ltr" class="gmail_attr">On Wed, May 21, 2025 at 1:59 PM Remi Forax <<a href="mailto:forax@univ-mlv.fr">forax@univ-mlv.fr</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div><div style="font-family:arial,helvetica,sans-serif;font-size:12pt;color:rgb(0,0,0)"><div><br></div><div><br></div><hr id="m_3148345915004059102zwchr"><div><blockquote style="border-left:2px solid rgb(16,16,255);margin-left:5px;padding-left:5px;color:rgb(0,0,0);font-weight:normal;font-style:normal;text-decoration:none;font-family:Helvetica,Arial,sans-serif;font-size:12pt"><b>From: </b>"Chen Liang" <<a href="mailto:chen.l.liang@oracle.com" target="_blank">chen.l.liang@oracle.com</a>><br><b>To: </b>"Da Vinci Machine Project" <<a href="mailto:mlvm-dev@openjdk.org" target="_blank">mlvm-dev@openjdk.org</a>>, "Charles Oliver Nutter" <<a href="mailto:headius@headius.com" target="_blank">headius@headius.com</a>><br><b>Sent: </b>Wednesday, May 21, 2025 8:47:24 PM<br><b>Subject: </b>Re: Years later, finally integrating ClassValue... but...<br></blockquote></div><div></div><div><blockquote style="border-left:2px solid rgb(16,16,255);margin-left:5px;padding-left:5px;color:rgb(0,0,0);font-weight:normal;font-style:normal;text-decoration:none;font-family:Helvetica,Arial,sans-serif;font-size:12pt">
<div style="font-family:"Calibri Light","Helvetica Light",sans-serif;font-size:12pt;color:rgb(0,0,0)">
Hi Charles,</div>
<div style="font-family:"Calibri Light","Helvetica Light",sans-serif;font-size:12pt;color:rgb(0,0,0)">
Indeed, the docs for "computeValue" was wrong. The doc for "get" was right. The upcoming JDK 25 will provide a spec update that simplifies the mental model of a ClassValue and correct these mistakes in the docs.</div>
<div style="font-family:"Calibri Light","Helvetica Light",sans-serif;font-size:12pt;color:rgb(0,0,0)">
<br>
</div>
<div style="font-family:"Calibri Light","Helvetica Light",sans-serif;font-size:12pt;color:rgb(0,0,0)">
ClassValue always have been utilizing racy computation, that multiple threads can compute a value, so there is minimal locking - the only locking would be at the time when a thread tries to associate a value. This association is essentially a CAS, where only
one thread's computeValue result is ever installed. There is no synchronization lock on computeValue, and there is no way for a synchronized computeValue to check whether a value is already associated. The only threading guarantee provided by ClassValue is
that the associated value's identity/value is unique and its computation result is published to all threads that saw the value via a get.</div>
<div style="font-family:"Calibri Light","Helvetica Light",sans-serif;font-size:12pt;color:rgb(0,0,0)">
<br>
</div>
<div style="font-family:"Calibri Light","Helvetica Light",sans-serif;font-size:12pt;color:rgb(0,0,0)">
If you wish for synchronous initialization, I think you can store a container, such as a memoized function (such as StableValue::supplier), in a ClassValue, and then call the function (or a synchronized initializer) on all accessing threads - this ensures the
is exactly one memoized function object, and that object is initialized exactly once.</div></blockquote><div><br></div><div>yes, a double check locking (or a stable value but it's in preview in 25) solve the issue.<br></div><div><br></div><blockquote style="border-left:2px solid rgb(16,16,255);margin-left:5px;padding-left:5px;color:rgb(0,0,0);font-weight:normal;font-style:normal;text-decoration:none;font-family:Helvetica,Arial,sans-serif;font-size:12pt">
<div style="font-family:"Calibri Light","Helvetica Light",sans-serif;font-size:12pt;color:rgb(0,0,0)">
<br>
</div>
<div style="font-family:"Calibri Light","Helvetica Light",sans-serif;font-size:12pt;color:rgb(0,0,0)">
Regards,</div>
<div style="font-family:"Calibri Light","Helvetica Light",sans-serif;font-size:12pt;color:rgb(0,0,0)">
Chen Liang</div></blockquote><div><br></div><div>Rémi</div><div><br></div><blockquote style="border-left:2px solid rgb(16,16,255);margin-left:5px;padding-left:5px;color:rgb(0,0,0);font-weight:normal;font-style:normal;text-decoration:none;font-family:Helvetica,Arial,sans-serif;font-size:12pt">
<hr style="display:inline-block;width:98%">
<div id="m_3148345915004059102divRplyFwdMsg" dir="ltr"><font face="Calibri, sans-serif" style="font-size:11pt" color="#000000"><b>From:</b> mlvm-dev <<a href="mailto:mlvm-dev-retn@openjdk.org" target="_blank">mlvm-dev-retn@openjdk.org</a>> on behalf of Charles Oliver Nutter <<a href="mailto:headius@headius.com" target="_blank">headius@headius.com</a>><br><b>Sent:</b> Wednesday, May 21, 2025 12:42 PM<br><b>To:</b> Da Vinci Machine Project <<a href="mailto:mlvm-dev@openjdk.java.net" target="_blank">mlvm-dev@openjdk.java.net</a>><br><b>Subject:</b> Years later, finally integrating ClassValue... but...</font>
<div> </div>
</div>
<div>
<div dir="ltr">
<div>Due to issues for years trying to understand the lifecycle of class/value pairs stored in ClassValue, I am only now integrating the JDK-provided version of ClassValue into JRuby's logic to store method tables for Java classes. And I have run into a new
inconsistency I want to clarify.</div>
<div><br>
</div>
<div>The docs for ClassValue state this for the "computeValue" method:</div>
<div><br>
</div>
<div>"This method will be invoked within the first thread that accesses the value with the get method.</div>
Normally, this method is invoked at most once per class, but it may be invoked again if there has been a call to remove."
<div><br>
</div>
<div>To me, that means computeValue will be invoked *exactly once* per class (ignoring removals), regardless of how many threads attempt to compute it at the same time. Otherwise, what's the point of saying it will be invoked by the "first thread"?</div>
<div><br>
</div>
<div>But then the docs for "get" say something different:</div>
<div><br>
</div>
<div>"Returns the value for the given class. If no value has yet been computed, it is obtained by an invocation of the computeValue method.<br>
The actual installation of the value on the class is performed atomically. At that point, if several racing threads have computed values, one is chosen, and returned to all the racing threads."</div>
<div><br>
</div>
<div>Ok hold up... so now it's possible for multiple threads to independently computeValue? These two statements don't appear to mesh... I'm looking for the behavior computeValue describes: basically computeIfAbsent. But in practice (and from what I have read
of the current implementation), multiple threads might call computeValue for a given class.</div>
<div><br>
</div>
<div>In JRuby, where the computation of this value also sets up global namespace tables, it results in warnings that the namespace entry has been initialized multiple times.</div>
<div><br>
</div>
<div>Adding synchronized to my implementation of computeValue does not help; it just means two threads don't compute at the same time. They will block until the first thread finishes its computeValue, and while that first thread is initializing the ClassValue,
they'll proceed to computeValue several more times.</div>
<div><br>
</div>
<div>This was not a problem with my home-grown ClassValue implementation because I double-check the cache before proceeding into synchronized code that makes the computeValue call (computeIfAbsent behavior).</div>
<div><br>
</div>
<div>I can work around this by also overriding ClassValue.get to be synchronized, but the existing behavior does not seem right to me. It works this way on 1.8, 21, and 24, so nothing has changed. Either the docs are wrong or the implementation is wrong.</div>
<div><br>
<div>
<div dir="ltr">
<div dir="ltr"><b>Charles Oliver Nutter</b>
<div><i>Architect and Technologist</i></div>
<div>Headius Enterprises</div>
<a href="https://www.headius.com" target="_blank">https://www.headius.com</a>
<div>
<div><a href="mailto:headius@headius.com" target="_blank">headius@headius.com</a><br></div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<br>_______________________________________________<br>mlvm-dev mailing list<br><a href="mailto:mlvm-dev@openjdk.org" target="_blank">mlvm-dev@openjdk.org</a><br><a href="https://mail.openjdk.org/mailman/listinfo/mlvm-dev" target="_blank">https://mail.openjdk.org/mailman/listinfo/mlvm-dev</a><br></blockquote></div></div></div></blockquote></div>