Based on a recent discussion, I started trying to remove the functions llvm_start_multithreaded() and llvm_stop_multithreaded() from the codebase. It turns out this is a little bit tricky. Consider the following scenario:
During program initialization, a global static object’s constructor dereferences a ManagedStatic. During dereferencing of the ManagedStatic, it needs to know whether or not to acquire the global lock in order to allocate the ManagedStatic
There are 3 possible types for the global_lock, and none of them solve the problem.
If global_lock is simply another global static, it may not have been constructed yet.
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.
If global_lock is a ManagedStatic, then it will get into an infinite recursion here when trying to allocate this ManagedStatic.
I actually started to feel this way since the first time I started looking at LLVM, but even moreso increasingly I feel that the solution is that ManagedStatics should not be allowed to be accessed until after main begins. llvm_shutdown() gives deterministic order of destroying managed statics, but we don’t have deterministic order of creation of those ManagedStatics. I have a patch up (still awaiting review) that shows a possible solution to these problems and how we might migrate the existing cases where this happens over to a ManagedStatic free static initialization.
There is only one requirement: You must be able to insert a call very early in main() that will do the ManagedStatic initialization. However, we can catch this with an assertion and all anyone would have to do is add one line to their main function. As in , all this early main() code would do is copy fields over from one structure to another.
As a side benefit, this provides the ability to “resurrect” after an llvm_shutdown(), shoudl that be desired, because the initial static state of the program always remains in-tact once main() is entered.
Thoughts? Better ideas?