RFR: 8344706: Compiler Implementation of Compact Source Files and Instance Main Methods
Stuart Marks
smarks at openjdk.org
Mon Apr 7 17:28:52 UTC 2025
On Mon, 7 Apr 2025 07:01:56 GMT, Jan Lahoda <jlahoda at openjdk.org> wrote:
>> src/java.base/share/classes/java/lang/IO.java line 183:
>>
>>> 181: * @return the internal BufferedReader instance
>>> 182: */
>>> 183: static synchronized BufferedReader reader() {
>>
>> Is the lock only inteneded for initialization, i.e. should it apply for simply getting the reader?
>
> I believe there needs to be some barriers (at least `volatile`) even for getting the already initialized reader. And then, given the code here, it is probably fine and less error prone to simply use `synchronized` that something more complex.
>
> But, given we'll hopefully have `@StableValue` soon, maybe we could use that? What do you think @stuart-marks?
>
> Thanks!
It's even more complex than `volatile`. If a thread comes along and reads a null here, it still needs to take a lock (or something) to prevent more than one thread from creating the BufferedReader and storing it back into the field. This leads down the path of double-checked locking, initialize-on-demand holder, etc.
It's not clear to me this is in such a critical path that it warrants adding any of this stuff. StableValue probably has the right semantics. However, it's not even in the JDK yet (though it is Targeted to 25 as of this moment). But I don't think we want to add a dependency on a Preview API.
-------------
PR Review Comment: https://git.openjdk.org/jdk/pull/24438#discussion_r2031689379
More information about the compiler-dev
mailing list