<html><head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body>
<p><br>
</p>
<div class="moz-cite-prefix">On 27/03/2023 10:56, 刘希晨 wrote:<br>
</div>
<blockquote type="cite" cite="mid:CABUmArRVSmMkWXB_keKPj3OoXnMrbQRB3X2=PTfAKO9p0GDfCw@mail.gmail.com">
<div dir="ltr">Thank you very much for the clarification, I really
appreciate it. Perhaps you could add some instructions on the
recommended way of handling multi threads access over
MemorySegment in the documentation, so people could
understand it better! (<a href="https://urldefense.com/v3/__https://github.com/openjdk/panama-foreign/blob/foreign-memaccess*2Babi/doc/panama_memaccess.md*segments__;JSM!!ACWV5N9M2RV99hQ!NiffGyRQiYssVQb6SqWG6401Q2n9TvHH-2b3-rLq-x8PSO0w8yXU4jVfFygvKkh4qoTPuy8yGosiS7pYNsWiYFOMF0L2$" moz-do-not-send="true">https://github.com/openjdk/panama-foreign/blob/foreign-memaccess%2Babi/doc/panama_memaccess.md#segments</a>)
The API in VarHandle are not only provided for MemorySegment and
rather low-level, a newbie like me would really got confused of
which way to use<br>
</div>
</blockquote>
<p>I agree that some more clarification on this aspect at the
javadoc level would be the right way to approach this.</p>
<p>Thanks<br>
Maurizio<br>
</p>
<blockquote type="cite" cite="mid:CABUmArRVSmMkWXB_keKPj3OoXnMrbQRB3X2=PTfAKO9p0GDfCw@mail.gmail.com"><br>
<div class="gmail_quote">
<div dir="ltr" class="gmail_attr">Maurizio Cimadamore <<a href="mailto:maurizio.cimadamore@oracle.com" moz-do-not-send="true" class="moz-txt-link-freetext">maurizio.cimadamore@oracle.com</a>>
于2023年3月27日周一 17:07写道:<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>
<p>Hi,<br>
accessing a shared memory segment is pretty much the same
as accessing a byte buffer, or a Java array instance.
There are no additional memory fences that are added by
our implementation when using "plain" access (as in all
the other APIs I have listed), so, in the general case you
would probably need to use VarHandle::fullFence() or
something similar (to make sure all writes are flushed
when one thread is done with the segment).</p>
<p>It is surprising to see that it worked even w/o adding
synchronization/fencing, but that might just be "luck", or
the particular system you are using, as is often the case
with these matters. From a low-level/implementation
perspective there is absolutely no happens-before
relationship inserted between writes on a memory segment
and reads on the same segment (when using VarHandle::get -
that is - a "plain" read/write). That is, plain access
really means plain access.<br>
</p>
<p>Hope this helps.</p>
<p>Maurizio<br>
</p>
<div>On 27/03/2023 08:48, 刘希晨 wrote:<br>
</div>
<blockquote type="cite">
<div dir="ltr">
<div style="color:rgb(0,0,0);font-family:Arial;font-size:14px;margin:0px">I
have a question about using MemorySegment with
Arena.openShared().</div>
<div style="color:rgb(0,0,0);font-family:Arial;font-size:14px;margin:0px">I
don't know if the memory created by Arena.openShared()
or any other shared scope would be safely accessed by
multiple threads.</div>
<div style="color:rgb(0,0,0);font-family:Arial;font-size:14px;margin:0px">Simply
put, if a writer thread wrote something into the
MemorySegment and then passing it to the reader thread
using a BlockingQueue,</div>
<div style="color:rgb(0,0,0);font-family:Arial;font-size:14px;margin:0px">I
am confused if the writer operation would already be
visible to the reader thread.</div>
<div style="color:rgb(0,0,0);font-family:Arial;font-size:14px;margin:0px">I
have tested its visibility using jcstress and it turns
out every Varhandle.get() will successfully read the
data changed in another thread, </div>
<div style="color:rgb(0,0,0);font-family:Arial;font-size:14px;margin:0px">even
without volatile or locks, so I am confused if the jvm
has already created memory fence for memorysegment's
access.</div>
<div style="color:rgb(0,0,0);font-family:Arial;font-size:14px;margin:0px">I
hope the authorities could offer a suggested way of
accessing same memory segment in multiple threads in
Panama FFI's documentation.</div>
</div>
</blockquote>
</div>
</blockquote>
</div>
</blockquote>
</body>
</html>