<div dir="ltr"><br><div class="gmail_extra"><br><div class="gmail_quote">On Fri, Jun 12, 2015 at 10:06 AM, Mehdi Amini <span dir="ltr"><<a href="mailto:mehdi.amini@apple.com" target="_blank">mehdi.amini@apple.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word">Thanks for the answer.<div><br><div><span class=""><blockquote type="cite"><div>On Jun 12, 2015, at 9:42 AM, David Majnemer <<a href="mailto:david.majnemer@gmail.com" target="_blank">david.majnemer@gmail.com</a>> wrote:</div><br><div><br><br style="font-family:Helvetica;font-size:12px;font-style:normal;font-variant:normal;font-weight:normal;letter-spacing:normal;line-height:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px"><div class="gmail_quote" style="font-family:Helvetica;font-size:12px;font-style:normal;font-variant:normal;font-weight:normal;letter-spacing:normal;line-height:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px">On Fri, Jun 12, 2015 at 7:56 AM, Mehdi Amini<span> </span><span dir="ltr"><<a href="mailto:mehdi.amini@apple.com" target="_blank">mehdi.amini@apple.com</a>></span><span> </span>wrote:<br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">Hi Reid,<br><br>Can you explain how the change makes it thread safe? It’s not obvious to me right now.<br></blockquote><div><br></div><div>In the MSVC world, loads marked volatile have acquire semantics and stores marked volatile have release semantics.  We actually don't need such strong semantics here, just a monotonic guarantee on the loads and stores which volatile will more or less give us on GCC (for mingw builds and the like).</div><div><br></div><div>It contains a benign race but we are OK with that (two threads might both compute the allocation granularity but they will both produce the same result.)</div></div></div></blockquote><div><br></div></span><div>I see, we’re fine with a race on the initialization in the sense that it can happen multiple times (since I guess getAllocationGranularity() is pure) but it has to be visible in each thread. </div><div><br></div><div>Also I read online that MSVC 2013 should support thread-safe local static initialization (see: <a href="https://urldefense.proofpoint.com/v2/url?u=http-3A__herbsutter.com_2013_09_09_visual-2Dstudio-2D2013-2Drc-2Dis-2Dnow-2Davailable_&d=AwMFaQ&c=8hUWFZcy2Z-Za5rBPlktOQ&r=mQ4LZ2PUj9hpadE3cDHZnIdEwhEBrbAstXeMaFoB9tg&m=uTIEM_DzFnTdewRKmxzkbjNYzBwrdu1KwJ6fh4sH-Pg&s=neqLThgsIZBQosrobi7EZCZxIbZzVZCYgUEiwz1H41o&e=" target="_blank">http://herbsutter.com/2013/09/09/visual-studio-2013-rc-is-now-available/</a> )</div><div>Isn’t MSVC2013 the current minimal version for LLVM?</div></div></div></div></blockquote><div><br></div><div>That blog post didn't make things entirely clear.  Herb is referring to the VS "14" CTP which has this support.  The first stable version to support this will be MSVC 2015.</div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word"><div><div><div><br></div><div>Thanks,</div><div><br></div><div>— </div><span class="HOEnZb"><font color="#888888"><div>Mehdi</div></font></span><div><div class="h5"><div><br></div><br><blockquote type="cite"><div><div class="gmail_quote" style="font-family:Helvetica;font-size:12px;font-style:normal;font-variant:normal;font-weight:normal;letter-spacing:normal;line-height:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px"><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"><br>Thanks,<br><br>Mehdi<br><div><div><br>> On Jun 11, 2015, at 3:22 PM, Reid Kleckner <<a href="mailto:reid@kleckner.net" target="_blank">reid@kleckner.net</a>> wrote:<br>><br>> Author: rnk<br>> Date: Thu Jun 11 17:22:45 2015<br>> New Revision: 239566<br>><br>> URL:<span> </span><a href="https://urldefense.proofpoint.com/v2/url?u=http-3A__llvm.org_viewvc_llvm-2Dproject-3Frev-3D239566-26view-3Drev&d=AwMFaQ&c=8hUWFZcy2Z-Za5rBPlktOQ&r=mQ4LZ2PUj9hpadE3cDHZnIdEwhEBrbAstXeMaFoB9tg&m=uTIEM_DzFnTdewRKmxzkbjNYzBwrdu1KwJ6fh4sH-Pg&s=E6qr78K-uitCPtsWlaJusr4B33VY2X3_IwemdnRjDfA&e=" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project?rev=239566&view=rev</a><br>> Log:<br>> [Support] Fix a race initializing a static local in MSVC<br>><br>> static local initialization isn't thread safe with MSVC and a race was<br>> reported in PR23817. We can't use std::atomic because it's not trivially<br>> constructible, so instead do some lame volatile global integer<br>> manipulation.<br>><br>> Modified:<br>>    llvm/trunk/lib/Support/Windows/Memory.inc<br>><br>> Modified: llvm/trunk/lib/Support/Windows/Memory.inc<br>> URL:<span> </span><a href="https://urldefense.proofpoint.com/v2/url?u=http-3A__llvm.org_viewvc_llvm-2Dproject_llvm_trunk_lib_Support_Windows_Memory.inc-3Frev-3D239566-26r1-3D239565-26r2-3D239566-26view-3Ddiff&d=AwMFaQ&c=8hUWFZcy2Z-Za5rBPlktOQ&r=mQ4LZ2PUj9hpadE3cDHZnIdEwhEBrbAstXeMaFoB9tg&m=uTIEM_DzFnTdewRKmxzkbjNYzBwrdu1KwJ6fh4sH-Pg&s=KViOGoaskUDhL0ACNVS7mnh1JnW8UFaZbznJtLD3pis&e=" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Support/Windows/Memory.inc?rev=239566&r1=239565&r2=239566&view=diff</a><br>> ==============================================================================<br>> --- llvm/trunk/lib/Support/Windows/Memory.inc (original)<br>> +++ llvm/trunk/lib/Support/Windows/Memory.inc Thu Jun 11 17:22:45 2015<br>> @@ -78,7 +78,15 @@ MemoryBlock Memory::allocateMappedMemory<br>>   // While we'd be happy to allocate single pages, the Windows allocation<br>>   // granularity may be larger than a single page (in practice, it is 64K)<br>>   // so mapping less than that will create an unreachable fragment of memory.<br>> -  static const size_t Granularity = getAllocationGranularity();<br>> +  // Avoid using one-time initialization of static locals here, since they<br>> +  // aren't thread safe with MSVC.<br>> +  static volatile size_t GranularityCached;<br>> +  size_t Granularity = GranularityCached;<br>> +  if (Granularity == 0) {<br>> +    Granularity = getAllocationGranularity();<br>> +    GranularityCached = Granularity;<br>> +  }<br>> +<br>>   const size_t NumBlocks = (NumBytes+Granularity-1)/Granularity;<br>><br>>   uintptr_t Start = NearBlock ? reinterpret_cast<uintptr_t>(NearBlock->base()) +<br>><br>><br>> _______________________________________________<br>> llvm-commits mailing list<br>><span> </span><a href="mailto:llvm-commits@cs.uiuc.edu" target="_blank">llvm-commits@cs.uiuc.edu</a><br>><span> </span><a href="http://lists.cs.uiuc.edu/mailman/listinfo/llvm-commits" rel="noreferrer" target="_blank">http://lists.cs.uiuc.edu/mailman/listinfo/llvm-commits</a><br><br><br>_______________________________________________<br>llvm-commits mailing list<br><a href="mailto:llvm-commits@cs.uiuc.edu" target="_blank">llvm-commits@cs.uiuc.edu</a><br><a href="http://lists.cs.uiuc.edu/mailman/listinfo/llvm-commits" rel="noreferrer" target="_blank">http://lists.cs.uiuc.edu/mailman/listinfo/llvm-commits</a></div></div></blockquote></div></div></blockquote></div></div></div><br></div></div></blockquote></div><br></div></div>