On Thu, 18 Jun 2026 14:11:51 GMT, Chen Liang <[email protected]> wrote:

>> src/java.base/share/classes/java/lang/invoke/X-VarHandle.java.template line 
>> 619:
>> 
>>> 617:         @ForceInline
>>> 618:         static boolean compareAndSet(VarHandle ob, $type$ expected, 
>>> $type$ value) {
>>> 619:             FieldStaticReadWrite handle = (FieldStaticReadWrite) 
>>> ob.onStaticFieldAccess(true, true);
>> 
>> CompareAndSet do both a read and write, so here you pass `true, true`. This 
>> will end up calling `notifyStrictStaticAccess` which then clears the 'unset' 
>> bit of the field for writes. However, a CAS is not guaranteed to do a write. 
>> Isn't that problematic? The VM would consider the field initialized after a 
>> failed CAS.
>> 
>> Would be nice if you could add a test for that.
>
> I can degrade the calls to be reporting only read for cas: after the first 
> write, we don't really care about future writes.

Right, but shouldn't we be able to read after a _successful_ CAS? Reporting the 
write seems needed as well.

I just had a look with a debugger, and it seems the ordering of the read check 
before the write check in `initialize` saves us. The read check will throw an 
exception, so the write check (which clears the bit) never actually happens.

I think it would still be good to add a test case for CAS, and maybe a comment 
that the read check should be ordered before the write check in `initialize`.

-------------

PR Review Comment: 
https://git.openjdk.org/valhalla/pull/2550#discussion_r3436492893

Reply via email to