Move the code generation mutex to Reactor.

Generation of the routines for copying surfaces and blitting
were not protected by the same critical section as the renderer.

Change-Id: I10eb386bdeebb04abc999b3313dc2c3b52adc090
Reviewed-on: https://swiftshader-review.googlesource.com/2772
Tested-by: Greg Hartman <ghartman@google.com>
Reviewed-by: Greg Hartman <ghartman@google.com>
Reviewed-by: Nicolas Capens <capn@google.com>
diff --git a/src/Reactor/Nucleus.cpp b/src/Reactor/Nucleus.cpp
index 4a819cb..49ec859 100644
--- a/src/Reactor/Nucleus.cpp
+++ b/src/Reactor/Nucleus.cpp
@@ -66,6 +66,7 @@
 	LLVMContext *Nucleus::context = 0;
 	Module *Nucleus::module = 0;
 	llvm::Function *Nucleus::function = 0;
+	BackoffLock Nucleus::codegenMutex;
 
 	class Builder : public IRBuilder<>
 	{
@@ -73,6 +74,8 @@
 
 	Nucleus::Nucleus()
 	{
+		codegenMutex.lock();   // Reactor and LLVM are currently not thread safe
+
 		InitializeNativeTarget();
 		JITEmitDebugInfo = false;
 
@@ -129,6 +132,8 @@
 		routineManager = 0;
 		function = 0;
 		module = 0;
+
+		codegenMutex.unlock();
 	}
 
 	Routine *Nucleus::acquireRoutine(const wchar_t *name, bool runOptimizations)
diff --git a/src/Reactor/Nucleus.hpp b/src/Reactor/Nucleus.hpp
index 86e2304..59d43e0 100644
--- a/src/Reactor/Nucleus.hpp
+++ b/src/Reactor/Nucleus.hpp
@@ -13,6 +13,7 @@
 #define sw_Nucleus_hpp

 

 #include "Common/Types.hpp"

+#include "Common/MutexLock.hpp"

 

 #include <stdarg.h>

 #include <vector>

@@ -218,6 +219,8 @@
 		static llvm::LLVMContext *context;

 		static llvm::Module *module;

 		static RoutineManager *routineManager;

+

+		static BackoffLock codegenMutex;

 	};

 

 	class Byte;

diff --git a/src/Renderer/Renderer.cpp b/src/Renderer/Renderer.cpp
index 3fd13bb..06236b3 100644
--- a/src/Renderer/Renderer.cpp
+++ b/src/Renderer/Renderer.cpp
@@ -69,8 +69,6 @@
 	TranscendentalPrecision rsqPrecision = ACCURATE;
 	bool perspectiveCorrection = true;
 
-	BackoffLock Renderer::codegenMutex;
-
 	struct Parameters
 	{
 		Renderer *renderer;
@@ -232,13 +230,9 @@
 				setupState = SetupProcessor::update();
 				pixelState = PixelProcessor::update();
 
-				codegenMutex.lock();
-
 				vertexRoutine = VertexProcessor::routine(vertexState);
 				setupRoutine = SetupProcessor::routine(setupState);
 				pixelRoutine = PixelProcessor::routine(pixelState);
-
-				codegenMutex.unlock();
 			}
 
 			int batch = batchSize / ms;
diff --git a/src/Renderer/Renderer.hpp b/src/Renderer/Renderer.hpp
index 6fbbfa9..0631acd 100644
--- a/src/Renderer/Renderer.hpp
+++ b/src/Renderer/Renderer.hpp
@@ -404,7 +404,6 @@
 		unsigned int qSize;
 
 		BackoffLock schedulerMutex;
-		static BackoffLock codegenMutex;
 
 		#if PERF_HUD
 			int64_t vertexTime[16];