[llvm] r343261 - [ORC] Lock ThreadSafeContext during Module destructing in ThreadSafeModule.

Lang Hames via llvm-commits llvm-commits at lists.llvm.org
Thu Sep 27 13:36:09 PDT 2018


Author: lhames
Date: Thu Sep 27 13:36:08 2018
New Revision: 343261

URL: http://llvm.org/viewvc/llvm-project?rev=343261&view=rev
Log:
[ORC] Lock ThreadSafeContext during Module destructing in ThreadSafeModule.

Failure to lock the context can lead to data races if other threads are
operating on other ThreadSafeModules that share the same context.

Modified:
    llvm/trunk/include/llvm/ExecutionEngine/Orc/ThreadSafeModule.h

Modified: llvm/trunk/include/llvm/ExecutionEngine/Orc/ThreadSafeModule.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/ExecutionEngine/Orc/ThreadSafeModule.h?rev=343261&r1=343260&r2=343261&view=diff
==============================================================================
--- llvm/trunk/include/llvm/ExecutionEngine/Orc/ThreadSafeModule.h (original)
+++ llvm/trunk/include/llvm/ExecutionEngine/Orc/ThreadSafeModule.h Thu Sep 27 13:36:08 2018
@@ -102,15 +102,29 @@ public:
   /// unique_ptr<LLVMContext>. This creates a new ThreadSafeContext from the
   /// given context.
   ThreadSafeModule(std::unique_ptr<Module> M, std::unique_ptr<LLVMContext> Ctx)
-      : TSCtx(std::move(Ctx)), M(std::move(M)) {}
+      : M(std::move(M)), TSCtx(std::move(Ctx)) {}
 
+  /// Construct a ThreadSafeModule from a unique_ptr<Module> and an
+  /// existing ThreadSafeContext.
   ThreadSafeModule(std::unique_ptr<Module> M, ThreadSafeContext TSCtx)
-      : TSCtx(std::move(TSCtx)), M(std::move(M)) {}
+      : M(std::move(M)), TSCtx(std::move(TSCtx)) {}
 
+  ~ThreadSafeModule() {
+    // We need to lock the context while we destruct the module.
+    if (M) {
+      auto L = getContextLock();
+      M = nullptr;
+    }
+  }
+
+  /// Get the module wrapped by this ThreadSafeModule.
   Module* getModule() { return M.get(); }
 
+  /// Take out a lock on the ThreadSafeContext for this module.
   ThreadSafeContext::Lock getContextLock() { return TSCtx.getLock(); }
 
+  /// Boolean conversion: This ThreadSafeModule will evaluate to true if it
+  /// wraps a non-null module.
   explicit operator bool() {
     if (M) {
       assert(TSCtx.getContext() && "Non-null module must have non-null context");
@@ -120,8 +134,8 @@ public:
   }
 
 private:
-  ThreadSafeContext TSCtx;
   std::unique_ptr<Module> M;
+  ThreadSafeContext TSCtx;
 };
 
 using GVPredicate = std::function<bool(const GlobalValue&)>;




More information about the llvm-commits mailing list