On Tue, 21 Jul 2026 14:32:03 GMT, Jorn Vernee <[email protected]> wrote:

>> src/java.base/share/classes/java/util/LazyCollections.java line 680:
>> 
>>> 678:             // There might be a race where the value is already 
>>> computed and
>>> 679:             // the mutex is cleared, so we need to re-check the value 
>>> again
>>> 680:             final T t = (T) UNSAFE.getReferenceAcquire(array, offset);
>> 
>> It is not obvious to me from the code below that the setting of the value 
>> and the clearing of the "mutex" are guaranteed to be ordered i.e. you might 
>> see a null mutex and also not see the new value.
>
> Right, `acquireMutex` returns `null` when the `mutexes` array is `null`, but 
> it is written and read using plain writes/reads. I think if we want a 
> guarantee that setting the `mutexes` to `null` is ordered after the set of 
> the value, it should use a `putReferenceRelease`, and likewise, reading it in 
> `acquireMutex` should use `getReferenceAcquire`.

The `mutexes` field is accessed using `volatile` semantics, as that field is 
declared as `volatile`. I think this gives us the visibility/ordering we are 
looking for. I.e., if the value is set and then `mutexes` is set to `null` 
then, if we see `mutexes == null` (using `volatile` semantics), we are 
guaranteed to see the value.

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

PR Review Comment: https://git.openjdk.org/jdk/pull/31979#discussion_r3624448755

Reply via email to