<div dir="ltr"><br><div class="gmail_extra"><br><div class="gmail_quote">On Fri, Jun 12, 2015 at 7:56 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">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 class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<br>
Thanks,<br>
<br>
Mehdi<br>
<div class="HOEnZb"><div class="h5"><br>
> On Jun 11, 2015, at 3:22 PM, Reid Kleckner <<a href="mailto:reid@kleckner.net">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: <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=PQ2P-bjnfcfhaYWsP66bEht1TDbgjWW6EYL3kCOzufw&s=HZ4CC7X_VVaO8U-gLj2t4onrg30BpgPY22kyW-yYd9c&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: <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=PQ2P-bjnfcfhaYWsP66bEht1TDbgjWW6EYL3kCOzufw&s=N8z0mYbuo4fP6vreoJolRqh0uaQD6ofZZo3UpPooZEs&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>
> <a href="mailto:llvm-commits@cs.uiuc.edu">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><br>
<br>
<br>
_______________________________________________<br>
llvm-commits mailing list<br>
<a href="mailto:llvm-commits@cs.uiuc.edu">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><br>
</div></div></blockquote></div><br></div></div>