<div dir="ltr">Hi Petros,<div><br></div><div>It does look like a bug in our end. We do enforce ECMA's load-acquire, store-release semantics for volatiles.</div><div><br></div><div>Can we integrate your test case into mono?</div><div><br></div><div><br></div><div>--</div><div>Rodrigo</div></div><div class="gmail_extra"><br><div class="gmail_quote">On Thu, Jul 7, 2016 at 11:05 AM, Petros Douvantzis <span dir="ltr"><<a href="mailto:petrakeas@gmail.com" target="_blank">petrakeas@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><div class="gmail_default" style="font-family:arial,helvetica,sans-serif;color:rgb(68,68,68)">​Hello Miguel,</div><div class="gmail_default" style="font-family:arial,helvetica,sans-serif;color:rgb(68,68,68)"><br></div><div class="gmail_default" style="font-family:arial,helvetica,sans-serif;color:rgb(68,68,68)">The initial code does <b>not </b>have the field marked as volatile. However, it may work in Net 2.0 because it has stronger memory guarantees than the ECMA. </div><div class="gmail_default" style="font-family:arial,helvetica,sans-serif;color:rgb(68,68,68)"><br></div><div class="gmail_default" style="font-family:arial,helvetica,sans-serif;color:rgb(68,68,68)">So, the articles continues saying "<i>Making the instance variable volatile can make it work</i>". So, <b>if</b> the field were volatile, it would work in every ECMA implementation.</div><div class="gmail_default" style="font-family:arial,helvetica,sans-serif;color:rgb(68,68,68)"><br></div><div class="gmail_default" style="font-family:arial,helvetica,sans-serif;color:rgb(68,68,68)">Also, I tried using:<span class=""><br><span style="color:rgb(34,34,34);font-family:arial,sans-serif;font-size:12.8px">adb shell setprop debug.mono.env "'MONO_ENV_OPTIONS=-O=-</span><span style="color:rgb(34,34,34);font-family:arial,sans-serif;font-size:12.8px">intrins'"<br></span></span>with no difference in the outcome.</div><div class="gmail_default" style="font-family:arial,helvetica,sans-serif;color:rgb(68,68,68)"><br></div><div class="gmail_default" style="font-family:arial,helvetica,sans-serif;color:rgb(68,68,68)">Best,</div></div><div class="HOEnZb"><div class="h5"><div class="gmail_extra"><br><div class="gmail_quote">On Thu, Jul 7, 2016 at 5:59 PM, Miguel de Icaza <span dir="ltr"><<a href="mailto:miguel@microsoft.com" target="_blank">miguel@microsoft.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">







<div bgcolor="white" lang="EN-US" link="blue" vlink="purple">
<div>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:Calibri">Hello Petros,<u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:Calibri"><u></u> <u></u></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:Calibri">That blog post on double-check-locking explicitly states that without extra steps the pattern would not work.   Maybe I missed something?<u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:Calibri"><u></u> <u></u></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:Calibri">From that post:<u></u><u></u></span></p>
<p class="MsoNormal" style="margin-left:.75in">
<u></u><span style="font-size:10.0pt;font-family:Symbol;color:black"><span>·<span style="font:7.0pt "Times New Roman"">        
</span></span></span><u></u><span style="font-family:Calibri;color:black">Without any memory barriers, it's broken in the ECMA CLI specification too. It's possible that under the .NET 2.0 memory model (which is stronger than the ECMA spec) it's safe, but
 I'd rather not rely on those stronger semantics, especially if there's any doubt as to the safety. Making the </span><span style="font-size:10.0pt;font-family:"Courier New";color:black">instance</span><span style="font-family:Calibri;color:black"> variable
 volatile can make it work, as would explicit memory barrier calls, although in the latter case even experts can't agree exactly which barriers are required. I tend to try to avoid situations where experts don't agree what's right and what's wrong!<u></u><u></u></span></p>
<p class="MsoNormal" style="margin-left:.25in"><u></u> <u></u></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:Calibri"><u></u> <u></u></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:Calibri"><u></u> <u></u></span></p>
<div style="border:none;border-top:solid #b5c4df 1.0pt;padding:3.0pt 0in 0in 0in">
<p class="MsoNormal"><b><span style="font-family:Calibri;color:black">From: </span>
</b><span style="font-family:Calibri;color:black">Petros Douvantzis <<a href="mailto:petrakeas@gmail.com" target="_blank">petrakeas@gmail.com</a>><br>
<b>Date: </b>Thursday, July 7, 2016 at 3:54 AM<br>
<b>To: </b>"<a href="mailto:mono-devel-list@lists.ximian.com" target="_blank">mono-devel-list@lists.ximian.com</a>" <<a href="mailto:mono-devel-list@lists.ximian.com" target="_blank">mono-devel-list@lists.ximian.com</a>>, Miguel de Icaza <<a href="mailto:miguel@microsoft.com" target="_blank">miguel@microsoft.com</a>><br>
<b>Cc: </b>Rodrigo Kumpera <<a href="mailto:kumpera@gmail.com" target="_blank">kumpera@gmail.com</a>></span></p><div><div><br>
<b>Subject: </b>Re: [Mono-dev] Volatile fields don't enforce acquire - release semantics like Volatile.Read() and Volatile.Write()<u></u><u></u></div></div><p></p>
</div><div><div>
<div>
<p class="MsoNormal"><u></u> <u></u></p>
</div>
<blockquote style="border:none;border-left:solid #b5c4df 4.5pt;padding:0in 0in 0in 4.0pt;margin-left:3.75pt;margin-right:0in">
<div>
<div>
<div>
<div>
<p class="MsoNormal"><span style="font-family:Arial;color:#444444">Hello Miguel,<u></u><u></u></span></p>
</div>
<div>
<p class="MsoNormal"><span style="font-family:Arial;color:#444444"><u></u> <u></u></span></p>
</div>
<div>
<p class="MsoNormal"><span style="font-family:Arial;color:#444444">I see your point. Even worse, it's a bit ambiguous of what guarantees does the volatile field make. For example in
<a href="https://na01.safelinks.protection.outlook.com/?url=https%3a%2f%2fmsdn.microsoft.com%2fen-us%2flibrary%2fx13ttww7.aspx&data=01%7c01%7cmiguel%40microsoft.com%7cbdcbcb26fc8744e9b67d08d3a63c01c8%7c72f988bf86f141af91ab2d7cd011db47%7c1&sdata=s02wpedE5%2fUhB9yawFzf3QlY51QePjK5rUj1c16Selk%3d" target="_blank">
MSDN</a>  nothing is mentioned about fences or memory re-orders. In that sense, Mono is correct. However, in ECMA 335 they mention acquire-release semantics such as the ones in the Volatile class you mentioned.<u></u><u></u></span></p>
</div>
<div>
<p class="MsoNormal"><span style="font-family:Arial;color:#444444"><u></u> <u></u></span></p>
</div>
<div>
<p class="MsoNormal"><span style="font-family:Arial;color:#444444">One this to consider is that if the Volatile field does not have these semantics, then<b> lazy initialization</b> that relies on a volatile field and a lock (
<a href="https://na01.safelinks.protection.outlook.com/?url=http%3a%2f%2fcsharpindepth.com%2fArticles%2fGeneral%2fSingleton.aspx&data=01%7c01%7cmiguel%40microsoft.com%7cbdcbcb26fc8744e9b67d08d3a63c01c8%7c72f988bf86f141af91ab2d7cd011db47%7c1&sdata=yk%2bj4W775WX%2b82pOGWXA4xyURhfDzV1XSvJle2p3L2w%3d" target="_blank">
double-check locking</a> ) is <b>broken </b>in Mono for iOS and Android, because there is a chance that a half created object is viewed by another thread. The way the volatile field is supposed to help, is explained
<a href="https://na01.safelinks.protection.outlook.com/?url=https%3a%2f%2fmsdn.microsoft.com%2fen-us%2fmagazine%2fjj883956.aspx&data=01%7c01%7cmiguel%40microsoft.com%7cbdcbcb26fc8744e9b67d08d3a63c01c8%7c72f988bf86f141af91ab2d7cd011db47%7c1&sdata=Edq89e1H1sGysBYfBQFrb9WUTXczZe0ZlQfh1FQJvJc%3d" target="_blank">
in this post</a>. <u></u><u></u></span></p>
</div>
<div>
<p class="MsoNormal"><span style="font-family:Arial;color:#444444"><u></u> <u></u></span></p>
</div>
<div>
<p class="MsoNormal"><span style="font-family:Arial;color:#444444">The only way to make this work right now is using the Volatile class, but most probably someone would expect the volatile field to work.<u></u><u></u></span></p>
</div>
<div>
<p class="MsoNormal"><span style="font-family:Arial;color:#444444"><u></u> <u></u></span></p>
</div>
<div>
<p class="MsoNormal"><span style="font-family:Arial;color:#444444">Best,<u></u><u></u></span></p>
</div>
</div>
<div>
<p class="MsoNormal"><u></u> <u></u></p>
<div>
<p class="MsoNormal">On Wed, Jul 6, 2016 at 9:24 PM, Miguel de Icaza <<a href="mailto:miguel@microsoft.com" target="_blank">miguel@microsoft.com</a>> wrote:<u></u><u></u></p>
<blockquote style="border:none;border-left:solid #cccccc 1.0pt;padding:0in 0in 0in 6.0pt;margin-left:4.8pt;margin-right:0in">
<div>
<div>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:Calibri">Hello,</span><u></u><u></u></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:Calibri"> </span><u></u><u></u></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:Calibri">I am not convinced that this is a bug worth fixing.</span><u></u><u></u></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:Calibri"> </span><u></u><u></u></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:Calibri">I think this requires some thinking.  While this might have been the intended visible behavior from C#, this predates the extensive
 use of C# beyond the x86 platform.   I believe this is why the Volatile APIs were introduced.</span><u></u><u></u></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:Calibri"> </span><u></u><u></u></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:Calibri">Consder the documentation for it:</span><u></u><u></u></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:Calibri"> </span><u></u><u></u></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:Calibri"><a href="https://na01.safelinks.protection.outlook.com/?url=https%3a%2f%2fmsdn.microsoft.com%2fen-us%2flibrary%2fgg712971(v%3dvs.110).aspx&data=01%7c01%7cmiguel%40microsoft.com%7cbdcbcb26fc8744e9b67d08d3a63c01c8%7c72f988bf86f141af91ab2d7cd011db47%7c1&sdata=U4RIuTFZa6oqRlI3uSyP2q8by90V0mpKDfgmnqQNUZI%3d" target="_blank">https://msdn.microsoft.com/en-us/library/gg712971(v=vs.110).aspx</a></span><u></u><u></u></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:Calibri"> </span><u></u><u></u></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:Calibri">If the language/runtime already provided this support, there would be no need for these volatile methods in the first place.</span><u></u><u></u></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:Calibri"> </span><u></u><u></u></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:Calibri">Miguel.</span><u></u><u></u></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:Calibri"> </span><u></u><u></u></p>
<div style="border:none;border-top:solid #b5c4df 1.0pt;padding:3.0pt 0in 0in 0in">
<p class="MsoNormal"><b><span style="font-family:Calibri;color:black">From:
</span></b><span style="font-family:Calibri;color:black"><<a href="mailto:mono-devel-list-bounces@lists.ximian.com" target="_blank">mono-devel-list-bounces@lists.ximian.com</a>> on behalf of Rodrigo Kumpera <<a href="mailto:kumpera@gmail.com" target="_blank">kumpera@gmail.com</a>><br>
<b>Date: </b>Wednesday, July 6, 2016 at 1:27 PM<br>
<b>To: </b>petrakeas <<a href="mailto:petrakeas@gmail.com" target="_blank">petrakeas@gmail.com</a>><br>
<b>Cc: </b>"<a href="mailto:mono-devel-list@lists.ximian.com" target="_blank">mono-devel-list@lists.ximian.com</a>" <<a href="mailto:mono-devel-list@lists.ximian.com" target="_blank">mono-devel-list@lists.ximian.com</a>><br>
<b>Subject: </b>Re: [Mono-dev] Volatile fields don't enforce acquire - release semantics like Volatile.Read() and Volatile.Write()</span><u></u><u></u></p>
</div>
<div>
<div>
<div>
<p class="MsoNormal"> <u></u><u></u></p>
</div>
<blockquote style="border:none;border-left:solid #b5c4df 4.5pt;padding:0in 0in 0in 4.0pt;margin-left:3.75pt;margin-top:5.0pt;margin-right:0in;margin-bottom:5.0pt">
<div>
<div>
<div>
<p class="MsoNormal">Yes, it looks like a bug.<u></u><u></u></p>
</div>
<div>
<p class="MsoNormal"> <u></u><u></u></p>
<div>
<p class="MsoNormal">On Wed, Jul 6, 2016 at 11:13 AM, petrakeas <<a href="mailto:petrakeas@gmail.com" target="_blank">petrakeas@gmail.com</a>> wrote:<u></u><u></u></p>
<blockquote style="border:none;border-left:solid #cccccc 1.0pt;padding:0in 0in 0in 6.0pt;margin-left:4.8pt;margin-top:5.0pt;margin-right:0in;margin-bottom:5.0pt">
<p class="MsoNormal">According to C#  specification<br>
<<a href="https://na01.safelinks.protection.outlook.com/?url=https%3a%2f%2fmsdn.microsoft.com%2fen-us%2flibrary%2fms228593.aspx&data=01%7c01%7cmiguel%40microsoft.com%7cf3c960accdeb43d8500208d3a5c2d9ae%7c72f988bf86f141af91ab2d7cd011db47%7c1&sdata=66AJc2tU2gcy4vutTkC%2b4bPl3MxAnAiXd4POGNZ3ivA%3d" target="_blank">https://msdn.microsoft.com/en-us/library/ms228593.aspx</a>> 
 :<br>
<br>
•       A read of a volatile field is called a volatile read. A volatile read has<span style="font-family:MingLiU"><br>
</span>“acquire semantics”; that is, it is guaranteed to occur prior to any<span style="font-family:MingLiU"><br>
</span>references to memory that occur after it in the instruction sequence.<span style="font-family:MingLiU"><br>
</span>•       A write of a volatile field is called a volatile write. A volatile write<span style="font-family:MingLiU"><br>
</span>has “release semantics”; that is, it is guaranteed to happen after any<span style="font-family:MingLiU"><br>
</span>memory references prior to the write instruction in the instruction<br>
sequence.<br>
<br>
The spec presents  an example<br>
<<a href="https://na01.safelinks.protection.outlook.com/?url=https%3a%2f%2fmsdn.microsoft.com%2fen-us%2flibrary%2faa645755(v%3dvs.71).aspx&data=01%7c01%7cmiguel%40microsoft.com%7cf3c960accdeb43d8500208d3a5c2d9ae%7c72f988bf86f141af91ab2d7cd011db47%7c1&sdata=cFpmsRLE5a248vj3svbpsmOBouE%2bOxE%2fwDMWjd0YkhU%3d" target="_blank">https://msdn.microsoft.com/en-us/library/aa645755(v=vs.71).aspx</a>> 
  where<br>
one thread writes "data" on a non volatile variable and "publishes" the<br>
result by writing on a volatile variable that acts as a flag. The other<br>
thread checks the volatile flag and if set, it accesses the non-volatile<br>
variable that is now *guaranteed* to contain the data.<br>
<br>
It seems that Mono 4.4 (the one used in Xamarin) does not enforce these<br>
semantics or in other words does not prevent memory re-ordering in Android<br>
and iOS that have relaxed memory models due to their CPU.<br>
<br>
I have created an a test that reproduces the problem in iOS and Android<br>
Program.cs <<a href="https://na01.safelinks.protection.outlook.com/?url=http%3a%2f%2fmono.1490590.n4.nabble.com%2ffile%2fn4668111%2fProgram.cs&data=01%7c01%7cmiguel%40microsoft.com%7cf3c960accdeb43d8500208d3a5c2d9ae%7c72f988bf86f141af91ab2d7cd011db47%7c1&sdata=H7V6%2bpq4jV8Kw7QdgZMVJRav%2b1XovSCuIY3PgRFaJrk%3d" target="_blank">http://mono.1490590.n4.nabble.com/file/n4668111/Program.cs</a>> 
 .<br>
<br>
If the access to the volatile field is replaced by Volatile.Read() and<br>
Volatile.Write(), then no-problems occur. It seems that Volatile.Read() and<br>
Volatile.Write() implement half fences in Mono, but the volatile keyword<br>
does not.<br>
<br>
Is this a bug?<br>
<br>
<br>
<br>
<br>
--<br>
View this message in context: <a href="https://na01.safelinks.protection.outlook.com/?url=http%3a%2f%2fmono.1490590.n4.nabble.com%2fVolatile-fields-don-t-enforce-acquire-release-semantics-like-Volatile-Read-and-Volatile-Write-tp4668111.html&data=01%7c01%7cmiguel%40microsoft.com%7cf3c960accdeb43d8500208d3a5c2d9ae%7c72f988bf86f141af91ab2d7cd011db47%7c1&sdata=sqJVi9saxf7EEGpn6Wpf%2bFEeZX5yCpzK4%2b38x670OEw%3d" target="_blank">
http://mono.1490590.n4.nabble.com/Volatile-fields-don-t-enforce-acquire-release-semantics-like-Volatile-Read-and-Volatile-Write-tp4668111.html</a><br>
Sent from the Mono - Dev mailing list archive at Nabble.com.<br>
_______________________________________________<br>
Mono-devel-list mailing list<br>
<a href="mailto:Mono-devel-list@lists.ximian.com" target="_blank">Mono-devel-list@lists.ximian.com</a><br>
<a href="https://na01.safelinks.protection.outlook.com/?url=http%3a%2f%2flists.ximian.com%2fmailman%2flistinfo%2fmono-devel-list&data=01%7c01%7cmiguel%40microsoft.com%7cf3c960accdeb43d8500208d3a5c2d9ae%7c72f988bf86f141af91ab2d7cd011db47%7c1&sdata=Sb1mXUpzvfBCP0RJh%2bB2orCGoBH3eV8Z8CY10t1NbC0%3d" target="_blank">http://lists.ximian.com/mailman/listinfo/mono-devel-list</a><u></u><u></u></p>
</blockquote>
</div>
<p class="MsoNormal"> <u></u><u></u></p>
</div>
</div>
</div>
</blockquote>
</div>
</div>
</div>
</div>
</blockquote>
</div>
<p class="MsoNormal"><br>
<br clear="all">
<u></u><u></u></p>
<div>
<p class="MsoNormal"><u></u> <u></u></p>
</div>
<p class="MsoNormal">-- <u></u><u></u></p>
<div>
<div>
<div>
<div>
<p style="margin:0in;margin-bottom:.0001pt"><span style="font-size:10.0pt;font-family:Verdana;color:#666666">Petros Douvantzis</span><span style="font-family:Verdana"><u></u><u></u></span></p>
<p style="margin:0in;margin-bottom:.0001pt"><span style="font-size:10.0pt;font-family:Verdana;color:#666666">Co-founder</span><span style="font-family:Verdana"><u></u><u></u></span></p>
<p style="margin:0in;margin-bottom:.0001pt"><span style="font-size:10.0pt;font-family:Verdana;color:#666666">Horizon Video Technologies</span><span style="font-family:Verdana"><u></u><u></u></span></p>
<p style="margin:0in;margin-bottom:.0001pt"><u><span style="font-size:10.0pt;font-family:Verdana;color:#666666"><a href="https://na01.safelinks.protection.outlook.com/?url=http%3a%2f%2fhorizon.camera&data=01%7c01%7cmiguel%40microsoft.com%7cbdcbcb26fc8744e9b67d08d3a63c01c8%7c72f988bf86f141af91ab2d7cd011db47%7c1&sdata=k%2bCLVnzGEb%2fX6zRRD4SfroHMqrvOcV7WJaGEOt2KYqM%3d" target="_blank">horizon.camera</a></span></u><span style="font-family:Verdana"><u></u><u></u></span></p>
<p class="MsoNormal"><u></u> <u></u></p>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</blockquote>
</div></div></div>
</div>

</blockquote></div><br><br clear="all"><div><br></div>-- <br><div data-smartmail="gmail_signature"><div dir="ltr"><div><div dir="ltr"><span><font face="verdana, sans-serif"><p dir="ltr" style="line-height:1.15;margin-top:0pt;margin-bottom:0pt"><span style="vertical-align:baseline;white-space:pre-wrap;background-color:transparent"><font size="2" color="#666666">Petros Douvantzis</font></span></p><p style="line-height:1.15;margin-top:0pt;margin-bottom:0pt"><span style="vertical-align:baseline;white-space:pre-wrap;background-color:transparent"><font size="2" color="#666666">Co-founder</font></span></p><p dir="ltr" style="line-height:1.15;margin-top:0pt;margin-bottom:0pt"><span style="vertical-align:baseline;white-space:pre-wrap;background-color:transparent"><font size="2" color="#666666">Horizon Video Technologies</font></span></p><p dir="ltr" style="line-height:1.15;margin-top:0pt;margin-bottom:0pt"><span style="text-decoration:underline;vertical-align:baseline;white-space:pre-wrap;background-color:transparent"><font size="2" color="#666666"><a href="http://horizon.camera" target="_blank">horizon.camera</a></font></span><br></p></font></span><br></div></div></div></div>
</div>
</div></div></blockquote></div><br></div>