<div dir="ltr">Another alternative is writing to immutable static (not ManagedStatic) data structures during static initialization, and then copying them over to the ManagedStatics after main begins.<div><br></div><div>The fundamental problem, at least in my mind, is this scenario:</div>
<div><br></div><div>1) During static construction, you access a ManagedStatic, causing it to be allocated.</div><div>2) Before main exits, you call llvm_shutdown to destroy ManagedStatics, including the one allocated in #1</div>
<div>3) During static destruction, you do the inverse operation of what you did in #1.  This also accesses a ManagedStatic, after llvm_shutdown has been called, and allocates another one.</div><div><br></div><div>Plus, it only resolves ordering insofar as the ordering of the static constructors is resolved.  Which is to say, not very much.  You get deterministic destruction order, but you still get non-deterministic construction order, because you dont' know in which order the static constructors which use the ManagedStatics will be called in.</div>
</div><div class="gmail_extra"><br><br><div class="gmail_quote">On Wed, Jun 11, 2014 at 12:41 PM, David Blaikie <span dir="ltr"><<a href="mailto:dblaikie@gmail.com" target="_blank">dblaikie@gmail.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div class="">On Wed, Jun 11, 2014 at 11:54 AM, Zachary Turner <<a href="mailto:zturner@google.com">zturner@google.com</a>> wrote:<br>

> I might be able to solve the lock problem by just deleting the lock access from the destructor, as you mention, or making the lock an instance variable of the PassRegistry class.  But I still think the way I've done is the only real way to solve the problem with the PassRegistrationListener.<br>

<br>
</div>It seems like the problem with PassRegistrationListener would be<br>
solved by having a static device (other than<br>
PassRegistrationListener's ctor itself) that simply doesn't remove the<br>
listener in its dtor. The problem being then you change the behavior<br>
of non-static PassRegistrationListeners (since you have to remove the<br>
registration machinery from PassRegistrationListener's ctor/dtor).<br>
Global registration devices are pretty common.<br>
<br>
All that being said, removing global ctors is a soft goal of the<br>
project, so I'm OK doing it for those reasons - I'm just trying not to<br>
conflate different goals/problems & understand which things we're<br>
doing for what reasons.<br>
<div class=""><br>
> Ultimately, the root of the problem is that ManagedStatics are being accessed during static initialization and shutdown.<br>
<br>
</div>I don't actually see those things as inherently wrong. Something like<br>
a ManagedStatic is the only way to resolve ordering in global init<br>
(yes, the alternative is to restrict the project to never have globals<br>
that access other globals - but that's not the only correct answer).<br>
During destruction is more subtle, certainly.<br>
<div class="HOEnZb"><div class="h5"><br>
> This needs to be prevented, and I think we need to try to get to a point where we can actually enforce, by way of asserts, that you cannot touch a ManagedStatic before main is entered or after it returns.<br>
><br>
> <a href="http://reviews.llvm.org/D3996" target="_blank">http://reviews.llvm.org/D3996</a><br>
><br>
><br>
</div></div></blockquote></div><br></div>