[LLVMdev] Use of statics and ManagedStatics in LLVM

Zachary Turner zturner at google.com
Mon Jun 9 12:05:44 PDT 2014


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.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20140609/2c439578/attachment.html>


More information about the llvm-dev mailing list