[LLVMdev] Use of statics and ManagedStatics in LLVM

David Blaikie dblaikie at gmail.com
Mon Jun 9 12:07:17 PDT 2014


On Mon, Jun 9, 2014 at 12:05 PM, Zachary Turner <zturner at google.com> wrote:
> On Mon, Jun 9, 2014 at 12:00 PM, David Blaikie <dblaikie at gmail.com> wrote:
>>
>> On Mon, Jun 9, 2014 at 11:44 AM, Zachary Turner <zturner at google.com>
>> wrote:
>> > 2) If global_lock is a raw pointer to a mutex, it would have to be
>> > explicitly allocated, and we can't guarantee this during static
>> > initialization.
>>
>> Can't guarantee that the memory allocation will succeed? But it could
>> be done without a memory allocation - by placement new-ing into
>> existing memory.
>
>
> It's more like you have nowhere to explicitly allocate it to guarantee that
> it will have been allocated by the time the other static object is trying to
> use it.  You can null-check it and lazily allocate, but this same null-check
> will be executed after main() enters, every time the ManagedStatic is
> accessed, and in that case it will be racy against whenever you shutdown and
> free the mutex.  Or, if someone did a refactor and removed the last instance
> of a ManagedStatic access from global static constructor, then the first
> access to any ManagedStatic would be racy.

Right - see my later comment about ensuring the mutex itself is lazily
"last chance" initialized by a global ctor of its own.



More information about the llvm-dev mailing list