[PATCH] Removing the static initializer in ManagedStatic.cpp by using llvm_call_once to initialize the ManagedStatic mutex.

Chris Bieneman beanz at apple.com
Thu Oct 30 11:11:24 PDT 2014


If I understand correctly this can still be safe as long as the flag is defined as a static global right? It will keep a static initializer around on Windows, but it should be safe.

-Chris

> On Oct 30, 2014, at 11:08 AM, Reid Kleckner <rnk at google.com> wrote:
> 
> So, bad news, `std::atomic<int>` has a non-trivial default constructor in the MSVC STL. This means the code `void foo() { static std::atomic<int> flag; }` uses a non-threadsafe guard variable to initialize `flag`. Worse, the guard variable is a bitfield, so it is very much not threadsafe. See the code for foo here:
>    mov         eax,dword ptr [?$S1@?1??foo@@YAXXZ at 4IA]            
>    test        al,1                                               
>    jne         initialized
>    or          eax,1                                              
>    mov         ecx,offset ?flag@?1??foo@@YAXXZ at 4U?$atomic at H@std@@A
>    mov         dword ptr [?$S1@?1??foo@@YAXXZ at 4IA],eax            
>    xor         eax,eax                                            
>    xchg        eax,dword ptr [ecx]                                
>  initialized:
>    ret                                                            
> 
> So... I'm running out of ideas. :( We can wait 3 years and wait for MSVC 14 to be our baseline, and then the problem of thread-safe static initialization will go away.
> 
> http://reviews.llvm.org/D5922
> 
> 





More information about the llvm-commits mailing list