diff --git a/docs/ProgrammersManual.rst b/docs/ProgrammersManual.rst index a7b28b36ca1d..4973e5c66719 100644 --- a/docs/ProgrammersManual.rst +++ b/docs/ProgrammersManual.rst @@ -2170,13 +2170,46 @@ compiler, consider compiling LLVM and LLVM-GCC in single-threaded mode, and using the resultant compiler to build a copy of LLVM with multithreading support. +.. _startmultithreaded: + +Entering and Exiting Multithreaded Mode +--------------------------------------- + +In order to properly protect its internal data structures while avoiding +excessive locking overhead in the single-threaded case, the LLVM must intialize +certain data structures necessary to provide guards around its internals. To do +so, the client program must invoke ``llvm_start_multithreaded()`` before making +any concurrent LLVM API calls. To subsequently tear down these structures, use +the ``llvm_stop_multithreaded()`` call. You can also use the +``llvm_is_multithreaded()`` call to check the status of multithreaded mode. + +Note that both of these calls must be made *in isolation*. That is to say that +no other LLVM API calls may be executing at any time during the execution of +``llvm_start_multithreaded()`` or ``llvm_stop_multithreaded``. It is the +client's responsibility to enforce this isolation. + +The return value of ``llvm_start_multithreaded()`` indicates the success or +failure of the initialization. Failure typically indicates that your copy of +LLVM was built without multithreading support, typically because GCC atomic +intrinsics were not found in your system compiler. In this case, the LLVM API +will not be safe for concurrent calls. However, it *will* be safe for hosting +threaded applications in the JIT, though :ref:`care must be taken +` to ensure that side exits and the like do not accidentally +result in concurrent LLVM API calls. + .. _shutdown: Ending Execution with ``llvm_shutdown()`` ----------------------------------------- When you are done using the LLVM APIs, you should call ``llvm_shutdown()`` to -deallocate memory used for internal structures. +deallocate memory used for internal structures. This will also invoke +``llvm_stop_multithreaded()`` if LLVM is operating in multithreaded mode. As +such, ``llvm_shutdown()`` requires the same isolation guarantees as +``llvm_stop_multithreaded()``. + +Note that, if you use scope-based shutdown, you can use the +``llvm_shutdown_obj`` class, which calls ``llvm_shutdown()`` in its destructor. .. _managedstatic: @@ -2184,11 +2217,20 @@ Lazy Initialization with ``ManagedStatic`` ------------------------------------------ ``ManagedStatic`` is a utility class in LLVM used to implement static -initialization of static resources, such as the global type tables. In a -single-threaded environment, it implements a simple lazy initialization scheme. -When LLVM is compiled with support for multi-threading, however, it uses +initialization of static resources, such as the global type tables. Before the +invocation of ``llvm_shutdown()``, it implements a simple lazy initialization +scheme. Once ``llvm_start_multithreaded()`` returns, however, it uses double-checked locking to implement thread-safe lazy initialization. +Note that, because no other threads are allowed to issue LLVM API calls before +``llvm_start_multithreaded()`` returns, it is possible to have +``ManagedStatic``\ s of ``llvm::sys::Mutex``\ s. + +The ``llvm_acquire_global_lock()`` and ``llvm_release_global_lock`` APIs provide +access to the global lock used to implement the double-checked locking for lazy +initialization. These should only be used internally to LLVM, and only if you +know what you're doing! + .. _llvmcontext: Achieving Isolation with ``LLVMContext`` diff --git a/include/llvm-c/Core.h b/include/llvm-c/Core.h index 8693a3020aba..0e78ed71fa9a 100644 --- a/include/llvm-c/Core.h +++ b/include/llvm-c/Core.h @@ -2848,13 +2848,16 @@ void LLVMDisposePassManager(LLVMPassManagerRef PM); * @{ */ -/** Deprecated: Multi-threading can only be enabled/disabled with the compile - time define LLVM_ENABLE_THREADS. This function always returns - LLVMIsMultithreaded(). */ +/** Allocate and initialize structures needed to make LLVM safe for + multithreading. The return value indicates whether multithreaded + initialization succeeded. Must be executed in isolation from all + other LLVM api calls. + @see llvm::llvm_start_multithreaded */ LLVMBool LLVMStartMultithreaded(void); -/** Deprecated: Multi-threading can only be enabled/disabled with the compile - time define LLVM_ENABLE_THREADS. */ +/** Deallocate structures necessary to make LLVM safe for multithreading. + Must be executed in isolation from all other LLVM api calls. + @see llvm::llvm_stop_multithreaded */ void LLVMStopMultithreaded(void); /** Check whether LLVM is executing in thread-safe mode or not. diff --git a/include/llvm/ExecutionEngine/ExecutionEngine.h b/include/llvm/ExecutionEngine/ExecutionEngine.h index d349af810650..e5dab6191ab6 100644 --- a/include/llvm/ExecutionEngine/ExecutionEngine.h +++ b/include/llvm/ExecutionEngine/ExecutionEngine.h @@ -22,10 +22,10 @@ #include "llvm/IR/ValueMap.h" #include "llvm/MC/MCCodeGenInfo.h" #include "llvm/Support/ErrorHandling.h" +#include "llvm/Support/Mutex.h" #include "llvm/Target/TargetMachine.h" #include "llvm/Target/TargetOptions.h" #include -#include #include #include @@ -42,6 +42,7 @@ class JITEventListener; class JITMemoryManager; class MachineCodeInfo; class Module; +class MutexGuard; class ObjectCache; class RTDyldMemoryManager; class Triple; @@ -58,7 +59,7 @@ class ExecutionEngineState { public: struct AddressMapConfig : public ValueMapConfig { typedef ExecutionEngineState *ExtraData; - static std::recursive_mutex *getMutex(ExecutionEngineState *EES); + static sys::Mutex *getMutex(ExecutionEngineState *EES); static void onDelete(ExecutionEngineState *EES, const GlobalValue *Old); static void onRAUW(ExecutionEngineState *, const GlobalValue *, const GlobalValue *); @@ -163,7 +164,7 @@ class ExecutionEngine { /// lock - This lock protects the ExecutionEngine, MCJIT, JIT, JITResolver and /// JITEmitter classes. It must be held while changing the internal state of /// any of those classes. - std::recursive_mutex lock; + sys::Mutex lock; //===--------------------------------------------------------------------===// // ExecutionEngine Startup diff --git a/include/llvm/IR/ValueMap.h b/include/llvm/IR/ValueMap.h index 17b0fe01b116..f196f334b605 100644 --- a/include/llvm/IR/ValueMap.h +++ b/include/llvm/IR/ValueMap.h @@ -28,9 +28,9 @@ #include "llvm/ADT/DenseMap.h" #include "llvm/IR/ValueHandle.h" +#include "llvm/Support/Mutex.h" #include "llvm/Support/type_traits.h" #include -#include namespace llvm { @@ -45,7 +45,7 @@ class ValueMapConstIterator; /// This class defines the default behavior for configurable aspects of /// ValueMap<>. User Configs should inherit from this class to be as compatible /// as possible with future versions of ValueMap. -template +template struct ValueMapConfig { typedef MutexT mutex_type; @@ -216,11 +216,11 @@ class ValueMapCallbackVH : public CallbackVH { ValueMapCallbackVH Copy(*this); typename Config::mutex_type *M = Config::getMutex(Copy.Map->Data); if (M) - M->lock(); + M->acquire(); Config::onDelete(Copy.Map->Data, Copy.Unwrap()); // May destroy *this. Copy.Map->Map.erase(Copy); // Definitely destroys *this. if (M) - M->unlock(); + M->release(); } void allUsesReplacedWith(Value *new_key) override { assert(isa(new_key) && @@ -229,7 +229,7 @@ class ValueMapCallbackVH : public CallbackVH { ValueMapCallbackVH Copy(*this); typename Config::mutex_type *M = Config::getMutex(Copy.Map->Data); if (M) - M->lock(); + M->acquire(); KeyT typed_new_key = cast(new_key); // Can destroy *this: @@ -245,7 +245,7 @@ class ValueMapCallbackVH : public CallbackVH { } } if (M) - M->unlock(); + M->release(); } }; diff --git a/include/llvm/Support/ManagedStatic.h b/include/llvm/Support/ManagedStatic.h index d8fbfeb8e20c..1bb8cea092f9 100644 --- a/include/llvm/Support/ManagedStatic.h +++ b/include/llvm/Support/ManagedStatic.h @@ -103,6 +103,9 @@ void llvm_shutdown(); /// llvm_shutdown() when it is destroyed. struct llvm_shutdown_obj { llvm_shutdown_obj() { } + explicit llvm_shutdown_obj(bool multithreaded) { + if (multithreaded) llvm_start_multithreaded(); + } ~llvm_shutdown_obj() { llvm_shutdown(); } }; diff --git a/include/llvm/Support/Threading.h b/include/llvm/Support/Threading.h index 7e8758407c7c..a7e8774558d5 100644 --- a/include/llvm/Support/Threading.h +++ b/include/llvm/Support/Threading.h @@ -7,8 +7,7 @@ // //===----------------------------------------------------------------------===// // -// This file declares helper functions for running LLVM in a multi-threaded -// environment. +// TThis file defines llvm_start_multithreaded() and friends. // //===----------------------------------------------------------------------===// @@ -16,10 +15,32 @@ #define LLVM_SUPPORT_THREADING_H namespace llvm { - /// Returns true if LLVM is compiled with support for multi-threading, and - /// false otherwise. + /// llvm_start_multithreaded - Allocate and initialize structures needed to + /// make LLVM safe for multithreading. The return value indicates whether + /// multithreaded initialization succeeded. LLVM will still be operational + /// on "failed" return, and will still be safe for hosting threading + /// applications in the JIT, but will not be safe for concurrent calls to the + /// LLVM APIs. + /// THIS MUST EXECUTE IN ISOLATION FROM ALL OTHER LLVM API CALLS. + bool llvm_start_multithreaded(); + + /// llvm_stop_multithreaded - Deallocate structures necessary to make LLVM + /// safe for multithreading. + /// THIS MUST EXECUTE IN ISOLATION FROM ALL OTHER LLVM API CALLS. + void llvm_stop_multithreaded(); + + /// llvm_is_multithreaded - Check whether LLVM is executing in thread-safe + /// mode or not. bool llvm_is_multithreaded(); + /// acquire_global_lock - Acquire the global lock. This is a no-op if called + /// before llvm_start_multithreaded(). + void llvm_acquire_global_lock(); + + /// release_global_lock - Release the global lock. This is a no-op if called + /// before llvm_start_multithreaded(). + void llvm_release_global_lock(); + /// llvm_execute_on_thread - Execute the given \p UserFn on a separate /// thread, passing it the provided \p UserData. /// diff --git a/lib/ExecutionEngine/ExecutionEngine.cpp b/lib/ExecutionEngine/ExecutionEngine.cpp index 553ceb4575d5..9154fe2f5ff4 100644 --- a/lib/ExecutionEngine/ExecutionEngine.cpp +++ b/lib/ExecutionEngine/ExecutionEngine.cpp @@ -166,7 +166,7 @@ void *ExecutionEngineState::RemoveMapping(const GlobalValue *ToUnmap) { } void ExecutionEngine::addGlobalMapping(const GlobalValue *GV, void *Addr) { - std::lock_guard locked(lock); + MutexGuard locked(lock); DEBUG(dbgs() << "JIT: Map \'" << GV->getName() << "\' to [" << Addr << "]\n";); @@ -184,14 +184,14 @@ void ExecutionEngine::addGlobalMapping(const GlobalValue *GV, void *Addr) { } void ExecutionEngine::clearAllGlobalMappings() { - std::lock_guard locked(lock); + MutexGuard locked(lock); EEState.getGlobalAddressMap().clear(); EEState.getGlobalAddressReverseMap().clear(); } void ExecutionEngine::clearGlobalMappingsFromModule(Module *M) { - std::lock_guard locked(lock); + MutexGuard locked(lock); for (Module::iterator FI = M->begin(), FE = M->end(); FI != FE; ++FI) EEState.RemoveMapping(FI); @@ -201,7 +201,7 @@ void ExecutionEngine::clearGlobalMappingsFromModule(Module *M) { } void *ExecutionEngine::updateGlobalMapping(const GlobalValue *GV, void *Addr) { - std::lock_guard locked(lock); + MutexGuard locked(lock); ExecutionEngineState::GlobalAddressMapTy &Map = EEState.getGlobalAddressMap(); @@ -228,7 +228,7 @@ void *ExecutionEngine::updateGlobalMapping(const GlobalValue *GV, void *Addr) { } void *ExecutionEngine::getPointerToGlobalIfAvailable(const GlobalValue *GV) { - std::lock_guard locked(lock); + MutexGuard locked(lock); ExecutionEngineState::GlobalAddressMapTy::iterator I = EEState.getGlobalAddressMap().find(GV); @@ -236,7 +236,7 @@ void *ExecutionEngine::getPointerToGlobalIfAvailable(const GlobalValue *GV) { } const GlobalValue *ExecutionEngine::getGlobalValueAtAddress(void *Addr) { - std::lock_guard locked(lock); + MutexGuard locked(lock); // If we haven't computed the reverse mapping yet, do so first. if (EEState.getGlobalAddressReverseMap().empty()) { @@ -555,7 +555,7 @@ void *ExecutionEngine::getPointerToGlobal(const GlobalValue *GV) { if (Function *F = const_cast(dyn_cast(GV))) return getPointerToFunction(F); - std::lock_guard locked(lock); + MutexGuard locked(lock); if (void *P = EEState.getGlobalAddressMap()[GV]) return P; @@ -1346,7 +1346,7 @@ ExecutionEngineState::ExecutionEngineState(ExecutionEngine &EE) : EE(EE), GlobalAddressMap(this) { } -std::recursive_mutex * +sys::Mutex * ExecutionEngineState::AddressMapConfig::getMutex(ExecutionEngineState *EES) { return &EES->EE.lock; } diff --git a/lib/ExecutionEngine/JIT/JIT.cpp b/lib/ExecutionEngine/JIT/JIT.cpp index 463fa299b3f3..83ec9784b98e 100644 --- a/lib/ExecutionEngine/JIT/JIT.cpp +++ b/lib/ExecutionEngine/JIT/JIT.cpp @@ -96,18 +96,18 @@ namespace { /// bugpoint or gdb users to search for a function by name without any context. class JitPool { SmallPtrSet JITs; // Optimize for process containing just 1 JIT. - mutable std::recursive_mutex Lock; + mutable sys::Mutex Lock; public: void Add(JIT *jit) { - std::lock_guard guard(Lock); + MutexGuard guard(Lock); JITs.insert(jit); } void Remove(JIT *jit) { - std::lock_guard guard(Lock); + MutexGuard guard(Lock); JITs.erase(jit); } void *getPointerToNamedFunction(const char *Name) const { - std::lock_guard guard(Lock); + MutexGuard guard(Lock); assert(JITs.size() != 0 && "No Jit registered"); //search function in every instance of JIT for (SmallPtrSet::const_iterator Jit = JITs.begin(), @@ -150,7 +150,7 @@ JIT::JIT(Module *M, TargetMachine &tm, TargetJITInfo &tji, AllJits->Add(this); // Add target data - std::lock_guard locked(lock); + MutexGuard locked(lock); FunctionPassManager &PM = jitstate->getPM(); M->setDataLayout(TM.getDataLayout()); PM.add(new DataLayoutPass(M)); @@ -177,7 +177,7 @@ JIT::~JIT() { /// addModule - Add a new Module to the JIT. If we previously removed the last /// Module, we need re-initialize jitstate with a valid Module. void JIT::addModule(Module *M) { - std::lock_guard locked(lock); + MutexGuard locked(lock); if (Modules.empty()) { assert(!jitstate && "jitstate should be NULL if Modules vector is empty!"); @@ -206,7 +206,7 @@ void JIT::addModule(Module *M) { bool JIT::removeModule(Module *M) { bool result = ExecutionEngine::removeModule(M); - std::lock_guard locked(lock); + MutexGuard locked(lock); if (jitstate && jitstate->getModule() == M) { delete jitstate; @@ -408,13 +408,13 @@ GenericValue JIT::runFunction(Function *F, void JIT::RegisterJITEventListener(JITEventListener *L) { if (!L) return; - std::lock_guard locked(lock); + MutexGuard locked(lock); EventListeners.push_back(L); } void JIT::UnregisterJITEventListener(JITEventListener *L) { if (!L) return; - std::lock_guard locked(lock); + MutexGuard locked(lock); std::vector::reverse_iterator I= std::find(EventListeners.rbegin(), EventListeners.rend(), L); if (I != EventListeners.rend()) { @@ -426,14 +426,14 @@ void JIT::NotifyFunctionEmitted( const Function &F, void *Code, size_t Size, const JITEvent_EmittedFunctionDetails &Details) { - std::lock_guard locked(lock); + MutexGuard locked(lock); for (unsigned I = 0, S = EventListeners.size(); I < S; ++I) { EventListeners[I]->NotifyFunctionEmitted(F, Code, Size, Details); } } void JIT::NotifyFreeingMachineCode(void *OldPtr) { - std::lock_guard locked(lock); + MutexGuard locked(lock); for (unsigned I = 0, S = EventListeners.size(); I < S; ++I) { EventListeners[I]->NotifyFreeingMachineCode(OldPtr); } @@ -444,7 +444,7 @@ void JIT::NotifyFreeingMachineCode(void *OldPtr) { /// GlobalAddress[F] with the address of F's machine code. /// void JIT::runJITOnFunction(Function *F, MachineCodeInfo *MCI) { - std::lock_guard locked(lock); + MutexGuard locked(lock); class MCIListener : public JITEventListener { MachineCodeInfo *const MCI; @@ -505,7 +505,7 @@ void *JIT::getPointerToFunction(Function *F) { if (void *Addr = getPointerToGlobalIfAvailable(F)) return Addr; // Check if function already code gen'd - std::lock_guard locked(lock); + MutexGuard locked(lock); // Now that this thread owns the lock, make sure we read in the function if it // exists in this Module. @@ -534,7 +534,7 @@ void *JIT::getPointerToFunction(Function *F) { } void JIT::addPointerToBasicBlock(const BasicBlock *BB, void *Addr) { - std::lock_guard locked(lock); + MutexGuard locked(lock); BasicBlockAddressMapTy::iterator I = getBasicBlockAddressMap().find(BB); @@ -546,7 +546,7 @@ void JIT::addPointerToBasicBlock(const BasicBlock *BB, void *Addr) { } void JIT::clearPointerToBasicBlock(const BasicBlock *BB) { - std::lock_guard locked(lock); + MutexGuard locked(lock); getBasicBlockAddressMap().erase(BB); } @@ -555,7 +555,7 @@ void *JIT::getPointerToBasicBlock(BasicBlock *BB) { (void)getPointerToFunction(BB->getParent()); // resolve basic block address - std::lock_guard locked(lock); + MutexGuard locked(lock); BasicBlockAddressMapTy::iterator I = getBasicBlockAddressMap().find(BB); @@ -592,7 +592,7 @@ void *JIT::getPointerToNamedFunction(const std::string &Name, /// variable, possibly emitting it to memory if needed. This is used by the /// Emitter. void *JIT::getOrEmitGlobalVariable(const GlobalVariable *GV) { - std::lock_guard locked(lock); + MutexGuard locked(lock); void *Ptr = getPointerToGlobalIfAvailable(GV); if (Ptr) return Ptr; @@ -666,7 +666,7 @@ char* JIT::getMemoryForGV(const GlobalVariable* GV) { size_t S = getDataLayout()->getTypeAllocSize(GlobalType); size_t A = getDataLayout()->getPreferredAlignment(GV); if (GV->isThreadLocal()) { - std::lock_guard locked(lock); + MutexGuard locked(lock); Ptr = TJI.allocateThreadLocalMemory(S); } else if (TJI.allocateSeparateGVMemory()) { if (A <= 8) { @@ -687,7 +687,7 @@ char* JIT::getMemoryForGV(const GlobalVariable* GV) { } void JIT::addPendingFunction(Function *F) { - std::lock_guard locked(lock); + MutexGuard locked(lock); jitstate->getPendingFunctions().push_back(F); } diff --git a/lib/ExecutionEngine/JIT/JITEmitter.cpp b/lib/ExecutionEngine/JIT/JITEmitter.cpp index 1e1f5d55802c..50b8c10b638b 100644 --- a/lib/ExecutionEngine/JIT/JITEmitter.cpp +++ b/lib/ExecutionEngine/JIT/JITEmitter.cpp @@ -40,6 +40,7 @@ #include "llvm/Support/ErrorHandling.h" #include "llvm/Support/ManagedStatic.h" #include "llvm/Support/Memory.h" +#include "llvm/Support/MutexGuard.h" #include "llvm/Support/raw_ostream.h" #include "llvm/Target/TargetInstrInfo.h" #include "llvm/Target/TargetJITInfo.h" @@ -49,7 +50,6 @@ #ifndef NDEBUG #include #endif -#include using namespace llvm; #define DEBUG_TYPE "jit" @@ -230,22 +230,22 @@ namespace { std::map Map; /// Guards Map from concurrent accesses. - mutable std::recursive_mutex Lock; + mutable sys::Mutex Lock; public: /// Registers a Stub to be resolved by Resolver. void RegisterStubResolver(void *Stub, JITResolver *Resolver) { - std::lock_guard guard(Lock); + MutexGuard guard(Lock); Map.insert(std::make_pair(Stub, Resolver)); } /// Unregisters the Stub when it's invalidated. void UnregisterStubResolver(void *Stub) { - std::lock_guard guard(Lock); + MutexGuard guard(Lock); Map.erase(Stub); } /// Returns the JITResolver instance that owns the Stub. JITResolver *getResolverFromStub(void *Stub) const { - std::lock_guard guard(Lock); + MutexGuard guard(Lock); // The address given to us for the stub may not be exactly right, it might // be a little bit after the stub. As such, use upper_bound to find it. // This is the same trick as in LookupFunctionFromCallSite from @@ -258,7 +258,7 @@ namespace { /// True if any stubs refer to the given resolver. Only used in an assert(). /// O(N) bool ResolverHasStubs(JITResolver* Resolver) const { - std::lock_guard guard(Lock); + MutexGuard guard(Lock); for (std::map::const_iterator I = Map.begin(), E = Map.end(); I != E; ++I) { if (I->second == Resolver) @@ -494,7 +494,7 @@ JITResolver::~JITResolver() { /// getLazyFunctionStubIfAvailable - This returns a pointer to a function stub /// if it has already been created. void *JITResolver::getLazyFunctionStubIfAvailable(Function *F) { - std::lock_guard guard(TheJIT->lock); + MutexGuard locked(TheJIT->lock); // If we already have a stub for this function, recycle it. return state.getFunctionToLazyStubMap().lookup(F); @@ -503,7 +503,7 @@ void *JITResolver::getLazyFunctionStubIfAvailable(Function *F) { /// getFunctionStub - This returns a pointer to a function stub, creating /// one on demand as needed. void *JITResolver::getLazyFunctionStub(Function *F) { - std::lock_guard guard(TheJIT->lock); + MutexGuard locked(TheJIT->lock); // If we already have a lazy stub for this function, recycle it. void *&Stub = state.getFunctionToLazyStubMap()[F]; @@ -564,7 +564,7 @@ void *JITResolver::getLazyFunctionStub(Function *F) { /// getGlobalValueIndirectSym - Return a lazy pointer containing the specified /// GV address. void *JITResolver::getGlobalValueIndirectSym(GlobalValue *GV, void *GVAddress) { - std::lock_guard guard(TheJIT->lock); + MutexGuard locked(TheJIT->lock); // If we already have a stub for this global variable, recycle it. void *&IndirectSym = state.getGlobalToIndirectSymMap()[GV]; @@ -622,7 +622,7 @@ void *JITResolver::JITCompilerFn(void *Stub) { // Only lock for getting the Function. The call getPointerToFunction made // in this function might trigger function materializing, which requires // JIT lock to be unlocked. - std::lock_guard guard(JR->TheJIT->lock); + MutexGuard locked(JR->TheJIT->lock); // The address given to us for the stub may not be exactly right, it might // be a little bit after the stub. As such, use upper_bound to find it. @@ -654,7 +654,7 @@ void *JITResolver::JITCompilerFn(void *Stub) { } // Reacquire the lock to update the GOT map. - std::lock_guard locked(JR->TheJIT->lock); + MutexGuard locked(JR->TheJIT->lock); // We might like to remove the call site from the CallSiteToFunction map, but // we can't do that! Multiple threads could be stuck, waiting to acquire the diff --git a/lib/ExecutionEngine/MCJIT/MCJIT.cpp b/lib/ExecutionEngine/MCJIT/MCJIT.cpp index f149edcb7b04..e9ba96a6496f 100644 --- a/lib/ExecutionEngine/MCJIT/MCJIT.cpp +++ b/lib/ExecutionEngine/MCJIT/MCJIT.cpp @@ -26,6 +26,7 @@ #include "llvm/Support/DynamicLibrary.h" #include "llvm/Support/ErrorHandling.h" #include "llvm/Support/MemoryBuffer.h" +#include "llvm/Support/MutexGuard.h" #include "llvm/Target/TargetLowering.h" using namespace llvm; @@ -65,7 +66,7 @@ MCJIT::MCJIT(Module *m, TargetMachine *tm, RTDyldMemoryManager *MM, } MCJIT::~MCJIT() { - std::lock_guard locked(lock); + MutexGuard locked(lock); // FIXME: We are managing our modules, so we do not want the base class // ExecutionEngine to manage them as well. To avoid double destruction // of the first (and only) module added in ExecutionEngine constructor @@ -101,12 +102,12 @@ MCJIT::~MCJIT() { } void MCJIT::addModule(Module *M) { - std::lock_guard locked(lock); + MutexGuard locked(lock); OwnedModules.addModule(M); } bool MCJIT::removeModule(Module *M) { - std::lock_guard locked(lock); + MutexGuard locked(lock); return OwnedModules.removeModule(M); } @@ -128,12 +129,12 @@ void MCJIT::addArchive(object::Archive *A) { void MCJIT::setObjectCache(ObjectCache* NewCache) { - std::lock_guard locked(lock); + MutexGuard locked(lock); ObjCache = NewCache; } ObjectBufferStream* MCJIT::emitObject(Module *M) { - std::lock_guard locked(lock); + MutexGuard locked(lock); // This must be a module which has already been added but not loaded to this // MCJIT instance, since these conditions are tested by our caller, @@ -173,7 +174,7 @@ ObjectBufferStream* MCJIT::emitObject(Module *M) { void MCJIT::generateCodeForModule(Module *M) { // Get a thread lock to make sure we aren't trying to load multiple times - std::lock_guard locked(lock); + MutexGuard locked(lock); // This must be a module which has already been added to this MCJIT instance. assert(OwnedModules.ownsModule(M) && @@ -213,7 +214,7 @@ void MCJIT::generateCodeForModule(Module *M) { } void MCJIT::finalizeLoadedModules() { - std::lock_guard locked(lock); + MutexGuard locked(lock); // Resolve any outstanding relocations. Dyld.resolveRelocations(); @@ -229,7 +230,7 @@ void MCJIT::finalizeLoadedModules() { // FIXME: Rename this. void MCJIT::finalizeObject() { - std::lock_guard locked(lock); + MutexGuard locked(lock); for (ModulePtrSet::iterator I = OwnedModules.begin_added(), E = OwnedModules.end_added(); @@ -242,7 +243,7 @@ void MCJIT::finalizeObject() { } void MCJIT::finalizeModule(Module *M) { - std::lock_guard locked(lock); + MutexGuard locked(lock); // This must be a module which has already been added to this MCJIT instance. assert(OwnedModules.ownsModule(M) && "MCJIT::finalizeModule: Unknown module."); @@ -267,7 +268,7 @@ uint64_t MCJIT::getExistingSymbolAddress(const std::string &Name) { Module *MCJIT::findModuleForSymbol(const std::string &Name, bool CheckFunctionsOnly) { - std::lock_guard locked(lock); + MutexGuard locked(lock); // If it hasn't already been generated, see if it's in one of our modules. for (ModulePtrSet::iterator I = OwnedModules.begin_added(), @@ -291,7 +292,7 @@ Module *MCJIT::findModuleForSymbol(const std::string &Name, uint64_t MCJIT::getSymbolAddress(const std::string &Name, bool CheckFunctionsOnly) { - std::lock_guard locked(lock); + MutexGuard locked(lock); // First, check to see if we already have this symbol. uint64_t Addr = getExistingSymbolAddress(Name); @@ -335,7 +336,7 @@ uint64_t MCJIT::getSymbolAddress(const std::string &Name, } uint64_t MCJIT::getGlobalValueAddress(const std::string &Name) { - std::lock_guard locked(lock); + MutexGuard locked(lock); uint64_t Result = getSymbolAddress(Name, false); if (Result != 0) finalizeLoadedModules(); @@ -343,7 +344,7 @@ uint64_t MCJIT::getGlobalValueAddress(const std::string &Name) { } uint64_t MCJIT::getFunctionAddress(const std::string &Name) { - std::lock_guard locked(lock); + MutexGuard locked(lock); uint64_t Result = getSymbolAddress(Name, true); if (Result != 0) finalizeLoadedModules(); @@ -352,7 +353,7 @@ uint64_t MCJIT::getFunctionAddress(const std::string &Name) { // Deprecated. Use getFunctionAddress instead. void *MCJIT::getPointerToFunction(Function *F) { - std::lock_guard locked(lock); + MutexGuard locked(lock); if (F->isDeclaration() || F->hasAvailableExternallyLinkage()) { bool AbortOnFailure = !F->hasExternalWeakLinkage(); @@ -551,13 +552,13 @@ void *MCJIT::getPointerToNamedFunction(const std::string &Name, void MCJIT::RegisterJITEventListener(JITEventListener *L) { if (!L) return; - std::lock_guard locked(lock); + MutexGuard locked(lock); EventListeners.push_back(L); } void MCJIT::UnregisterJITEventListener(JITEventListener *L) { if (!L) return; - std::lock_guard locked(lock); + MutexGuard locked(lock); SmallVector::reverse_iterator I= std::find(EventListeners.rbegin(), EventListeners.rend(), L); if (I != EventListeners.rend()) { @@ -566,14 +567,14 @@ void MCJIT::UnregisterJITEventListener(JITEventListener *L) { } } void MCJIT::NotifyObjectEmitted(const ObjectImage& Obj) { - std::lock_guard locked(lock); + MutexGuard locked(lock); MemMgr.notifyObjectLoaded(this, &Obj); for (unsigned I = 0, S = EventListeners.size(); I < S; ++I) { EventListeners[I]->NotifyObjectEmitted(Obj); } } void MCJIT::NotifyFreeingObject(const ObjectImage& Obj) { - std::lock_guard locked(lock); + MutexGuard locked(lock); for (unsigned I = 0, S = EventListeners.size(); I < S; ++I) { EventListeners[I]->NotifyFreeingObject(Obj); } diff --git a/lib/IR/Core.cpp b/lib/IR/Core.cpp index 68b9baa2baa2..197b6cb9054e 100644 --- a/lib/IR/Core.cpp +++ b/lib/IR/Core.cpp @@ -2702,10 +2702,11 @@ void LLVMDisposePassManager(LLVMPassManagerRef PM) { /*===-- Threading ------------------------------------------------------===*/ LLVMBool LLVMStartMultithreaded() { - return LLVMIsMultithreaded(); + return llvm_start_multithreaded(); } void LLVMStopMultithreaded() { + llvm_stop_multithreaded(); } LLVMBool LLVMIsMultithreaded() { diff --git a/lib/Support/ManagedStatic.cpp b/lib/Support/ManagedStatic.cpp index da01195a9df6..7ebfd3b8af8b 100644 --- a/lib/Support/ManagedStatic.cpp +++ b/lib/Support/ManagedStatic.cpp @@ -15,32 +15,15 @@ #include "llvm/Config/config.h" #include "llvm/Support/Atomic.h" #include -#include using namespace llvm; static const ManagedStaticBase *StaticList = nullptr; -// ManagedStatics can get created during execution of static constructors. As a -// result, we cannot use a global static std::mutex object for the lock since it -// may not have been constructed. Instead, we do a call-once initialization of -// a pointer to a mutex. -static std::once_flag MutexInitializationFlag; -static std::recursive_mutex* ManagedStaticMutex = nullptr; - -// Not all supported platforms (in particular VS2012) have thread-safe function -// static initialization, so roll our own. -static std::recursive_mutex& GetManagedStaticMutex() { - std::call_once(MutexInitializationFlag, - []() { ManagedStaticMutex = new std::recursive_mutex(); } ); - - return *ManagedStaticMutex; -} - void ManagedStaticBase::RegisterManagedStatic(void *(*Creator)(), void (*Deleter)(void*)) const { assert(Creator); if (llvm_is_multithreaded()) { - std::lock_guard Lock(GetManagedStaticMutex()); + llvm_acquire_global_lock(); if (!Ptr) { void* tmp = Creator(); @@ -60,6 +43,8 @@ void ManagedStaticBase::RegisterManagedStatic(void *(*Creator)(), Next = StaticList; StaticList = this; } + + llvm_release_global_lock(); } else { assert(!Ptr && !DeleterFn && !Next && "Partially initialized ManagedStatic!?"); @@ -90,8 +75,6 @@ void ManagedStaticBase::destroy() const { /// llvm_shutdown - Deallocate and destroy all ManagedStatic variables. void llvm::llvm_shutdown() { - std::lock_guard Lock(GetManagedStaticMutex()); - while (StaticList) StaticList->destroy(); } diff --git a/lib/Support/Threading.cpp b/lib/Support/Threading.cpp index ca7f3f64aa37..1acfa79b11d5 100644 --- a/lib/Support/Threading.cpp +++ b/lib/Support/Threading.cpp @@ -7,8 +7,7 @@ // //===----------------------------------------------------------------------===// // -// This file defines helper functions for running LLVM in a multi-threaded -// environment. +// This file implements llvm_start_multithreaded() and friends. // //===----------------------------------------------------------------------===// @@ -20,14 +19,50 @@ using namespace llvm; -bool llvm::llvm_is_multithreaded() { +static bool multithreaded_mode = false; + +static sys::Mutex* global_lock = nullptr; + +bool llvm::llvm_start_multithreaded() { #if LLVM_ENABLE_THREADS != 0 + assert(!multithreaded_mode && "Already multithreaded!"); + multithreaded_mode = true; + global_lock = new sys::Mutex(true); + + // We fence here to ensure that all initialization is complete BEFORE we + // return from llvm_start_multithreaded(). + sys::MemoryFence(); return true; #else return false; #endif } +void llvm::llvm_stop_multithreaded() { +#if LLVM_ENABLE_THREADS != 0 + assert(multithreaded_mode && "Not currently multithreaded!"); + + // We fence here to insure that all threaded operations are complete BEFORE we + // return from llvm_stop_multithreaded(). + sys::MemoryFence(); + + multithreaded_mode = false; + delete global_lock; +#endif +} + +bool llvm::llvm_is_multithreaded() { + return multithreaded_mode; +} + +void llvm::llvm_acquire_global_lock() { + if (multithreaded_mode) global_lock->acquire(); +} + +void llvm::llvm_release_global_lock() { + if (multithreaded_mode) global_lock->release(); +} + #if LLVM_ENABLE_THREADS != 0 && defined(HAVE_PTHREAD_H) #include diff --git a/lib/Support/Timer.cpp b/lib/Support/Timer.cpp index 210bda754e74..61465ae5e8be 100644 --- a/lib/Support/Timer.cpp +++ b/lib/Support/Timer.cpp @@ -19,7 +19,6 @@ #include "llvm/Support/Format.h" #include "llvm/Support/ManagedStatic.h" #include "llvm/Support/Mutex.h" -#include "llvm/Support/MutexGuard.h" #include "llvm/Support/Process.h" #include "llvm/Support/raw_ostream.h" using namespace llvm; @@ -85,13 +84,14 @@ static TimerGroup *getDefaultTimerGroup() { sys::MemoryFence(); if (tmp) return tmp; - sys::SmartScopedLock Lock(*TimerLock); + llvm_acquire_global_lock(); tmp = DefaultTimerGroup; if (!tmp) { tmp = new TimerGroup("Miscellaneous Ungrouped Timers"); sys::MemoryFence(); DefaultTimerGroup = tmp; } + llvm_release_global_lock(); return tmp; } diff --git a/unittests/IR/ValueMapTest.cpp b/unittests/IR/ValueMapTest.cpp index 51acd2fc641a..248740aeb726 100644 --- a/unittests/IR/ValueMapTest.cpp +++ b/unittests/IR/ValueMapTest.cpp @@ -186,19 +186,19 @@ struct LockMutex : ValueMapConfig { }; static void onRAUW(const ExtraData &Data, KeyT Old, KeyT New) { *Data.CalledRAUW = true; - EXPECT_FALSE(Data.M->try_lock()) << "Mutex should already be locked."; + EXPECT_FALSE(Data.M->tryacquire()) << "Mutex should already be locked."; } static void onDelete(const ExtraData &Data, KeyT Old) { *Data.CalledDeleted = true; - EXPECT_FALSE(Data.M->try_lock()) << "Mutex should already be locked."; + EXPECT_FALSE(Data.M->tryacquire()) << "Mutex should already be locked."; } static MutexT *getMutex(const ExtraData &Data) { return Data.M; } }; #if LLVM_ENABLE_THREADS TYPED_TEST(ValueMapTest, LocksMutex) { - std::mutex M; // Not recursive. + sys::Mutex M(false); // Not recursive. bool CalledRAUW = false, CalledDeleted = false; - typedef LockMutex ConfigType; + typedef LockMutex ConfigType; typename ConfigType::ExtraData Data = {&M, &CalledRAUW, &CalledDeleted}; ValueMap VM(Data); VM[this->BitcastV.get()] = 7; diff --git a/unittests/Support/ManagedStatic.cpp b/unittests/Support/ManagedStatic.cpp index 153884ba4298..ad2fc977e6d7 100644 --- a/unittests/Support/ManagedStatic.cpp +++ b/unittests/Support/ManagedStatic.cpp @@ -47,6 +47,7 @@ TEST(Initialize, MultipleThreads) { void *p1 = test1::allocate_stack(a1); void *p2 = test1::allocate_stack(a2); + llvm_start_multithreaded(); pthread_t t1, t2; pthread_create(&t1, &a1, test1::helper, nullptr); pthread_create(&t2, &a2, test1::helper, nullptr); @@ -54,6 +55,7 @@ TEST(Initialize, MultipleThreads) { pthread_join(t2, nullptr); free(p1); free(p2); + llvm_stop_multithreaded(); } #endif