From: Owen Anderson Date: Wed, 17 Jun 2009 21:16:20 +0000 (+0000) Subject: We need to use double-checked locking for lazy initialization in this case when runni... X-Git-Url: http://demsky.eecs.uci.edu/git/?a=commitdiff_plain;h=f2986e6ca9be11f794d3d77325945a437ba133a1;p=oota-llvm.git We need to use double-checked locking for lazy initialization in this case when running multithreaded. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@73636 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/lib/VMCore/Pass.cpp b/lib/VMCore/Pass.cpp index 6db5d7e24c5..bacabc96d76 100644 --- a/lib/VMCore/Pass.cpp +++ b/lib/VMCore/Pass.cpp @@ -19,6 +19,8 @@ #include "llvm/ModuleProvider.h" #include "llvm/ADT/STLExtras.h" #include "llvm/Support/ManagedStatic.h" +#include "llvm/Support/Threading.h" +#include "llvm/System/Atomic.h" #include #include #include @@ -192,8 +194,20 @@ static std::vector *Listeners = 0; // ressurection after llvm_shutdown is run. static PassRegistrar *getPassRegistrar() { static PassRegistrar *PassRegistrarObj = 0; + + // Use double-checked locking to safely initialize the registrar when + // we're running in multithreaded mode. if (!PassRegistrarObj) - PassRegistrarObj = new PassRegistrar(); + if (llvm_is_multithreaded()) { + llvm_acquire_global_lock(); + if (!PassRegistrarObj) { + PassRegistrar* tmp = new PassRegistrar(); + sys::MemoryFence(); + PassRegistrarObj = tmp; + } + llvm_release_global_lock(); + } else + PassRegistrarObj = new PassRegistrar(); return PassRegistrarObj; }