[Bf-blender-cvs] [57450b4fbe1] functions: improve thread safety

Jacques Lucke noreply at git.blender.org
Sun Mar 10 13:14:34 CET 2019


Commit: 57450b4fbe1f91aef64ff0337acf6a1cb1b177f1
Author: Jacques Lucke
Date:   Sun Mar 10 12:51:44 2019 +0100
Branches: functions
https://developer.blender.org/rB57450b4fbe1f91aef64ff0337acf6a1cb1b177f1

improve thread safety

===================================================================

M	source/blender/functions/backends/llvm/llvm_types.cpp
M	source/blender/functions/backends/llvm/llvm_types.hpp
M	source/blender/functions/backends/tuple_call/fgraph_tuple_call.cpp

===================================================================

diff --git a/source/blender/functions/backends/llvm/llvm_types.cpp b/source/blender/functions/backends/llvm/llvm_types.cpp
index 9db73d130fb..2916217ac6b 100644
--- a/source/blender/functions/backends/llvm/llvm_types.cpp
+++ b/source/blender/functions/backends/llvm/llvm_types.cpp
@@ -19,10 +19,7 @@ namespace FN {
 	llvm::Type *LLVMTypeInfo::get_type(
 		llvm::LLVMContext &context) const
 	{
-		if (!m_type_per_context.contains(&context)) {
-			llvm::Type *type = this->create_type(context);
-			m_type_per_context.add(&context, type);
-		}
+		this->ensure_type_exists_for_context(context);
 		return m_type_per_context.lookup(&context);
 	}
 
@@ -32,6 +29,24 @@ namespace FN {
 		return this->get_type(context)->getPointerTo();
 	}
 
+	void LLVMTypeInfo::ensure_type_exists_for_context(llvm::LLVMContext &context) const
+	{
+		if (m_type_per_context.contains(&context)) {
+			return;
+		}
+
+		std::lock_guard<std::mutex> lock(m_type_per_context_mutex);
+
+		if (m_type_per_context.contains(&context)) {
+			return;
+		}
+
+		llvm::Type *type = this->create_type(context);
+		BLI_assert(type);
+		m_type_per_context.add(&context, type);
+		BLI_assert(m_type_per_context.contains(&context));
+	}
+
 
 	/******************** SimpleLLVMTypeInfo ********************/
 
diff --git a/source/blender/functions/backends/llvm/llvm_types.hpp b/source/blender/functions/backends/llvm/llvm_types.hpp
index c90d6caa798..6eb6e62655b 100644
--- a/source/blender/functions/backends/llvm/llvm_types.hpp
+++ b/source/blender/functions/backends/llvm/llvm_types.hpp
@@ -5,6 +5,7 @@
 #include "llvm/IR/IRBuilder.h"
 
 #include <functional>
+#include <mutex>
 
 namespace FN {
 
@@ -43,8 +44,10 @@ namespace FN {
 			llvm::Value *byte_addr) const = 0;
 
 	private:
-		/* TODO: accessing this has to be made thread safe. */
 		mutable SmallMap<llvm::LLVMContext *, llvm::Type *> m_type_per_context;
+		mutable std::mutex m_type_per_context_mutex;
+
+		void ensure_type_exists_for_context(llvm::LLVMContext &context) const;
 
 		virtual llvm::Type *create_type(
 			llvm::LLVMContext &context) const = 0;
diff --git a/source/blender/functions/backends/tuple_call/fgraph_tuple_call.cpp b/source/blender/functions/backends/tuple_call/fgraph_tuple_call.cpp
index e1cb90d8880..7f2f4e8be03 100644
--- a/source/blender/functions/backends/tuple_call/fgraph_tuple_call.cpp
+++ b/source/blender/functions/backends/tuple_call/fgraph_tuple_call.cpp
@@ -13,7 +13,20 @@ namespace FN {
 		ExecuteGraph(const FunctionGraph &function_graph)
 			: m_graph(function_graph.graph()),
 			  m_inputs(function_graph.inputs()),
-			  m_outputs(function_graph.outputs()) {}
+			  m_outputs(function_graph.outputs())
+		{
+			auto *context = new llvm::LLVMContext();
+
+			for (Node *node : m_graph->all_nodes()) {
+				if (node->function()->has_body<TupleCallBody>()) {
+					continue;
+				}
+
+				if (node->function()->has_body<LLVMBuildIRBody>()) {
+					derive_TupleCallBody_from_LLVMBuildIRBody(node->function(), *context);
+				}
+			}
+		}
 
 		void call(Tuple &fn_in, Tuple &fn_out) const override
 		{
@@ -33,7 +46,7 @@ namespace FN {
 			}
 			else {
 				Node *node = socket.node();
-				TupleCallBody *body = this->get_node_body(node);
+				TupleCallBody *body = node->function()->body<TupleCallBody>();
 
 				FN_TUPLE_STACK_ALLOC(tmp_in, body->meta_in());
 				FN_TUPLE_STACK_ALLOC(tmp_out, body->meta_out());
@@ -47,14 +60,6 @@ namespace FN {
 				Tuple::copy_element(tmp_out, socket.index(), out, out_index);
 			}
 		}
-
-		TupleCallBody *get_node_body(Node *node) const
-		{
-			if (!node->function()->has_body<TupleCallBody>()) {
-				derive_TupleCallBody_from_LLVMBuildIRBody(node->function(), *(new llvm::LLVMContext()));
-			}
-			return node->function()->body<TupleCallBody>();
-		}
 	};
 
 	void fgraph_add_TupleCallBody(



More information about the Bf-blender-cvs mailing list