<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body style="word-wrap: break-word; -webkit-nbsp-mode: space; line-break: after-white-space;" class="">
That’s correct, and if we build a public construct such as generators on top of Continuation, it will do precisely this kind of thread-confinement checking. However, the Continuation class itself cannot do it, because it is used by constructs that do move it
from thread to thread (namely, virtual threads), and take great care to do it safely, cooperating with the compiler and making sure such transitions only occur inside methods that the compiler knows to treat specially.
<div class=""><br class="">
</div>
<div class="">— Ron<br class="">
<div><br class="">
<blockquote type="cite" class="">
<div class="">On 22 Jul 2022, at 15:33, Pedro Lamarão <<a href="mailto:pedro.lamarao@prodist.com.br" class="">pedro.lamarao@prodist.com.br</a>> wrote:</div>
<br class="Apple-interchange-newline">
<div class="">
<div dir="ltr" class="">
<div dir="ltr" class="">Em sex., 22 de jul. de 2022 às 09:51, Ron Pressler <<a href="mailto:ron.pressler@oracle.com" class="">ron.pressler@oracle.com</a>> escreveu:<br class="">
</div>
<div class="gmail_quote">
<div class=""> <br class="">
</div>
<blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
<div style="overflow-wrap: break-word;" class="">
<div class=""></div>
<div class="">The main issue is that the Continuation class can be used in a way that would cause the current thread (i.e. Thread.currentThread()) to change mid-method. This would not only break a lot of Java code in very surprising ways, but also some assumptions
made by the JIT compilers. Therefore, all safe constructs based on continuations must be confined to a single thread (or implement a thread, as done by virtual threads).</div>
</div>
</blockquote>
<div class=""><br class="">
</div>
<div class=""><br class="">
</div>
<div class="">I have once experimented designing a Generator based on Continuation. </div>
<div class="">Reading your comment above, I fear I may have misunderstood the requirements.</div>
<div class="">I'll try to re-state it, please correct me if I'm wrong.</div>
<div class=""><br class="">
</div>
<div class="">A Continuation is initialized with a scope and a runnable.</div>
<div class="">At some point it will be run for the first time in a certain thread.</div>
<div class="">From this point onwards, this Continuation must continue to run always in the same thread.</div>
<div class="">If the Continuation yields, then it must be restarted in the thread to which ii is "bound", never in some other thread.</div>
<div class="">Is that correct?</div>
<div class=""><br class="">
</div>
<div class="">Let us consider a Generator class defined over a Continuation.</div>
<div class="">The user calls _generate_ for the first time,</div>
<div class="">which calls _run_ on the Continuation for the first time,</div>
<div class="">"binding" the Continuation to the user's thread.</div>
<div class="">The Continuation function produces the value, stores it, and yields;</div>
<div class="">_generate_ returns the stored value to the user.</div>
<div class="">Eventually the user is going to call _generate_ again,</div>
<div class="">which must _run_ the Continuation again.</div>
<div class="">The JVM assumes that _run_ is always run on the "thread" to which the Continuation was bound.</div>
<div class="">Wouldn't it be enough to constrain _generate_ to be run always on the same thread,</div>
<div class="">a restriction which could be guaranteed by an assert?</div>
<div class=""><br class="">
</div>
</div>
-- <br class="">
<div dir="ltr" class="gmail_signature">
<div dir="ltr" class="">
<div class="">Pedro Lamarão</div>
</div>
</div>
</div>
</div>
</blockquote>
</div>
<br class="">
</div>
</body>
</html>