On Wed, 5 Aug 2026 10:58:11 GMT, Per Minborg <[email protected]> wrote:

>> This PR proposes to improve the handling of the fields in 
>> `LazyCollections.Mutexes` so that it becomes more evident that there are no 
>> races. It is also proposed to add a defensive check of `mutexes` so that it 
>> is obvious to a reader that the `Unsafe` operation does not operate on 
>> `null`.  Furthermore, it is proposed to remove the extra `AtomicInteger` 
>> object and use an `int` field directly.
>> 
>> Please note that there is no (known) error in how the class `Mutexes` works. 
>> This PR only makes it more explicit and efficient. So, for example, we do 
>> not have to backport this PR.
>> 
>> ---------
>> - [x] I confirm that I make this contribution in accordance with the 
>> [OpenJDK Interim AI Policy](https://openjdk.org/legal/ai).
>
> Per Minborg has updated the pull request incrementally with one additional 
> commit since the last revision:
> 
>   Fix comment

src/java.base/share/classes/java/util/LazyCollections.java line 597:

> 595:         private Mutexes(int length) {
> 596:             this.mutexes = new Object[length];
> 597:             UNSAFE.putIntRelease(this, COUNTER_OFFSET, length);

@minborg You can avoid the putIntRelease if you write counter prior to writing 
mutexes (as the latter is a volatile write) both since you read mutexes before 
reading counter (and since your accesses to counter are all atomic / getAndAdd):

Suggestion:

            this.counter = length; // plain write, will be published by the 
write to mutexes below
            this.mutexes = new Object[length];

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

PR Review Comment: https://git.openjdk.org/jdk/pull/32070#discussion_r3720828731

Reply via email to