We need to use double-checked locking for lazy initialization in this case when running multithreaded.

llvm-svn: 73636
This commit is contained in:
Owen Anderson 2009-06-17 21:16:20 +00:00
parent 667cd9abe2
commit eae9772679
1 changed files with 15 additions and 1 deletions

View File

@ -19,6 +19,8 @@
#include "llvm/ModuleProvider.h" #include "llvm/ModuleProvider.h"
#include "llvm/ADT/STLExtras.h" #include "llvm/ADT/STLExtras.h"
#include "llvm/Support/ManagedStatic.h" #include "llvm/Support/ManagedStatic.h"
#include "llvm/Support/Threading.h"
#include "llvm/System/Atomic.h"
#include <algorithm> #include <algorithm>
#include <map> #include <map>
#include <set> #include <set>
@ -192,8 +194,20 @@ static std::vector<PassRegistrationListener*> *Listeners = 0;
// ressurection after llvm_shutdown is run. // ressurection after llvm_shutdown is run.
static PassRegistrar *getPassRegistrar() { static PassRegistrar *getPassRegistrar() {
static PassRegistrar *PassRegistrarObj = 0; static PassRegistrar *PassRegistrarObj = 0;
// Use double-checked locking to safely initialize the registrar when
// we're running in multithreaded mode.
if (!PassRegistrarObj) 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; return PassRegistrarObj;
} }