<div dir="ltr"><div dir="ltr">Well, I didn't mean to pin the virtual thread to the platform thread. I mean, the virtual thread tries to steal the value from the current platform thread when calling the pin method.<div>After that, it can be freely switched to other platform threads, but still has the value, and will not be returned to the platform thread until unpin.<br></div><div><br></div><div>However, after rethinking the problem, I feel that such a new abstraction is really not needed.<br></div><div>We can combine ThreadLocal and ConcurrentLinkedQueue, continue to use ThreadLocal for platform threads, and use ConcurrentLinkedQueue for virtual threads for caching, which should be enough.<br></div><div><br></div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Tue, Nov 1, 2022 at 7:15 PM Duncan MacGregor <<a href="mailto:duncan.macgregor@servicenow.com" target="_blank">duncan.macgregor@servicenow.com</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 lang="EN-GB">
<div>
<p class="MsoNormal"><span style="font-size:11pt">Pinning virtual threads to only be scheduled on a particular OS thread greatly complicates the scheduling of them (when they are pinned due to monitors or native frames they prevent
other virtual threads from running on that carrier, and this is normally a situation you want to avoid) and would raise a whole set of API questions around multiple virtual threads attempting to pin a platform thread local (do they block until it is unpinned
from the current carrier thread, block until they are scheduled on a different carrier thread where the pinning attempt can be made again?). I would think there has to be a very good <a href="http://reason.to" target="_blank">reason.to</a> justify not using an object pool and a queue in this sort of situation.<u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-size:11pt"><u></u> <u></u></span></p>
<p class="MsoNormal"><span style="font-size:11pt">Duncan.
<u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-size:11pt"><u></u> <u></u></span></p>
<div style="border-right:none;border-bottom:none;border-left:none;border-top:1pt solid rgb(181,196,223);padding:3pt 0cm 0cm">
<p class="MsoNormal" style="margin-bottom:12pt"><b><span style="font-size:12pt;color:black">From:
</span></b><span style="font-size:12pt;color:black">loom-dev <<a href="mailto:loom-dev-retn@openjdk.org" target="_blank">loom-dev-retn@openjdk.org</a>> on behalf of Glavo <<a href="mailto:zjx001202@gmail.com" target="_blank">zjx001202@gmail.com</a>><br>
<b>Date: </b>Tuesday, 1 November 2022 at 04:58<br>
<b>To: </b>Volkan Yazıcı <<a href="mailto:volkan@yazi.ci" target="_blank">volkan@yazi.ci</a>><br>
<b>Cc: </b><a href="mailto:loom-dev@openjdk.org" target="_blank">loom-dev@openjdk.org</a> <<a href="mailto:loom-dev@openjdk.org" target="_blank">loom-dev@openjdk.org</a>><br>
<b>Subject: </b>Re: Thread-local successor for object pooling<u></u><u></u></span></p>
</div>
<div>
<div>
<div>
<h3><strong><span style="font-family:Helvetica;color:black;background:yellow">[External Email]</span></strong><span style="font-family:Helvetica;color:black"><u></u><u></u></span></h3>
<p class="MsoNormal"><span style="font-size:9pt;font-family:Helvetica;color:black"><u></u> <u></u></span></p>
<div class="MsoNormal" align="center" style="text-align:center"><span style="font-size:11pt">
<hr size="1" width="100%" align="center">
</span></div>
</div>
<p class="MsoNormal"><span style="font-size:11pt">I'm wondering if we can create a new ThreadLocal replacement for virtual threads?<u></u><u></u></span></p>
<div>
<p class="MsoNormal"><span style="font-size:11pt"><u></u> <u></u></span></p>
</div>
<div>
<p class="MsoNormal"><span style="font-size:11pt">I call this alternative PlatformThreadLocal. It differs from ThreadLocal in that it is based on platform threads and can be shared among multiple virtual threads.<u></u><u></u></span></p>
</div>
<div>
<p class="MsoNormal"><span style="font-size:11pt"><u></u> <u></u></span></p>
</div>
<div>
<p class="MsoNormal"><span style="font-size:11pt">If we don't want to share it between virtual threads, we need to call a method called 'pin' to get the value and call the 'unpin' method when we are done using it (or use the try-with-resources statement):<u></u><u></u></span></p>
</div>
<div>
<p class="MsoNormal"><span style="font-size:11pt"><u></u> <u></u></span></p>
</div>
<blockquote style="margin:5pt 0cm 5pt 30pt">
<div>
<p class="MsoNormal"><span style="font-size:11pt">static PlatformThreadLocal<SomeValue> cache = PlatformThreadLocal.withInitial(/* heavy initialization */);<u></u><u></u></span></p>
</div>
<div>
<p class="MsoNormal"><span style="font-size:11pt"><u></u> <u></u></span></p>
</div>
<div>
<p class="MsoNormal"><span style="font-size:11pt">void foo() {<u></u><u></u></span></p>
</div>
<div>
<p class="MsoNormal"><span style="font-size:11pt"> try (ThreadLocal<SomeValue> pinnedCache = cache.pin()) {<u></u><u></u></span></p>
</div>
<div>
<p class="MsoNormal"><span style="font-size:11pt"> SomeValue value = pinnedCache.get();<u></u><u></u></span></p>
</div>
<div>
<p class="MsoNormal"><span style="font-size:11pt"> // use the value<u></u><u></u></span></p>
</div>
<div>
<p class="MsoNormal"><span style="font-size:11pt"> }<u></u><u></u></span></p>
</div>
<div>
<p class="MsoNormal"><span style="font-size:11pt">}<u></u><u></u></span></p>
</div>
</blockquote>
<p class="MsoNormal"><span style="font-size:11pt"><u></u> <u></u></span></p>
<div>
<p class="MsoNormal"><span style="font-size:11pt">For platform threads, the 'pin' method should return the 'cache' itself;<u></u><u></u></span></p>
</div>
<div>
<p class="MsoNormal"><span style="font-size:11pt"><u></u> <u></u></span></p>
</div>
<div>
<p class="MsoNormal"><span style="font-size:11pt">For virtual threads, the 'pin' method will pin the value of the platform thread on the current virtual thread, until the 'unpin' method is called.<u></u><u></u></span></p>
</div>
<div>
<p class="MsoNormal"><span style="font-size:11pt">During this time, if another virtual thread running on the same platform thread tries to pin the value, it will get a new ThreadLocal value associated with the current virtual thread.<u></u><u></u></span></p>
</div>
<div>
<p class="MsoNormal"><span style="font-size:11pt"><u></u> <u></u></span></p>
</div>
<div>
<p class="MsoNormal"><span style="font-size:11pt"><u></u> <u></u></span></p>
</div>
</div>
<p class="MsoNormal"><span style="font-size:11pt"><u></u> <u></u></span></p>
<div>
<div>
<p class="MsoNormal"><span style="font-size:11pt">On Mon, Oct 31, 2022 at 6:21 PM Volkan Yazıcı <</span><a href="mailto:volkan@yazi.ci" target="_blank"><span style="font-size:11pt">volkan@yazi.ci</span></a><span style="font-size:11pt">> wrote:<u></u><u></u></span></p>
</div>
<blockquote style="border-top:none;border-right:none;border-bottom:none;border-left:1pt solid rgb(204,204,204);padding:0cm 0cm 0cm 6pt;margin:5pt 0cm 5pt 4.8pt">
<div>
<div>
<p class="MsoNormal"><span style="font-size:11pt">Hello,<u></u><u></u></span></p>
</div>
<div>
<p class="MsoNormal"><span style="font-size:11pt"><u></u> <u></u></span></p>
</div>
<div>
<p class="MsoNormal"><span style="font-size:11pt">Log4j heavily relies on thread-locals (TLs) for object pooling. This becomes of particular importance for caching and garbage-free logging purposes. With JEP 425 (Virtual Threads), this becomes problematic
due to two main issues:<u></u><u></u></span></p>
</div>
<div>
<p class="MsoNormal"><span style="font-size:11pt"><u></u> <u></u></span></p>
</div>
<div>
<p class="MsoNormal"><span style="font-size:11pt">1. TLs on vthreads yield no allocation benefits since vhtreads are short-lived. On the contrary, it becomes a redundant memory cost due to the excessive vthread count. In this case, Log4j still works, though
slower and with increased memory footprint.<u></u><u></u></span></p>
</div>
<div>
<p class="MsoNormal"><span style="font-size:11pt"><u></u> <u></u></span></p>
</div>
<div>
<p class="MsoNormal"><span style="font-size:11pt">2. TL mutation can be disabled for vthreads. In this case, Log4j doesn't work, since TL setters throw UnsupportedOperationException.<u></u><u></u></span></p>
</div>
<div>
<p class="MsoNormal"><span style="font-size:11pt"><u></u> <u></u></span></p>
</div>
<div>
<p class="MsoNormal"><span style="font-size:11pt">Users experimenting with Java 19 have already started reporting these issues[1]. JEP 429 (Scoped values) might be the answer to our questions, yet I have the impression that there will be a time gap between
the GA releases of vthreads and SVs. Hence I would like to know what is the best angle to address this problem using the tooling that will be provided by JEP 425.<u></u><u></u></span></p>
</div>
<div>
<p class="MsoNormal"><span style="font-size:11pt"><u></u> <u></u></span></p>
</div>
<div>
<p class="MsoNormal"><span style="font-size:11pt">I believe our use cases apply to many other major libraries in the wild. I would appreciate it if OpenJDK developers can help us with determining a way forward.<u></u><u></u></span></p>
</div>
<p class="MsoNormal"><span style="font-size:11pt"><u></u> <u></u></span></p>
<div>
<p class="MsoNormal"><span style="font-size:11pt">Kind regards.<u></u><u></u></span></p>
</div>
<div>
<p class="MsoNormal"><span style="font-size:11pt"><u></u> <u></u></span></p>
</div>
<div>
<p class="MsoNormal"><span style="font-size:11pt">[1] </span><a href="https://issues.apache.org/jira/browse/LOG4J2-3622" target="_blank"><span style="font-size:11pt">https://issues.apache.org/jira/browse/LOG4J2-3622</span></a><span style="font-size:11pt"><u></u><u></u></span></p>
</div>
</div>
</blockquote>
</div>
</div>
</div>
</div>
</div></blockquote></div>
</div>