Fix JIT on separate thread

When JIT_IN_SEPARATE_THREAD is defined we generate Reactor routines on
LLVM in a new thread. Since concurrent code generation was enabled by
making ::jit thread-local, we need to assign it the pointer of the
thread that creates the new thread.

Bug: b/153803432
Change-Id: Ia7454ac5823d61a1fb139b337d2af2de3a5104af
Reviewed-on: https://swiftshader-review.googlesource.com/c/SwiftShader/+/44048
Tested-by: Nicolas Capens <nicolascapens@google.com>
Reviewed-by: Ben Clayton <bclayton@google.com>
diff --git a/src/Reactor/LLVMReactor.cpp b/src/Reactor/LLVMReactor.cpp
index 48861e9..6e184b8 100644
--- a/src/Reactor/LLVMReactor.cpp
+++ b/src/Reactor/LLVMReactor.cpp
@@ -634,7 +634,11 @@
 {
 	std::shared_ptr<Routine> routine;
 
-	auto acquire = [&]() {
+	auto acquire = [&](std::unique_ptr<rr::JITBuilder> jitBuilder) {
+		// ::jit is thread-local, so when this is executed on a separate thread (see JIT_IN_SEPARATE_THREAD)
+		// it needs to be assigned the value from the parent thread.
+		jit = std::move(jitBuilder);
+
 		auto cfg = cfgEdit.apply(jit->config);
 
 		if(jit->builder->GetInsertBlock()->empty() || !jit->builder->GetInsertBlock()->back().isTerminator())
@@ -691,10 +695,10 @@
 	// FIXME(b/149829034): This is not a long-term solution. Reactor has no control
 	// over the threading and stack sizes of its users, so this should be addressed
 	// at a higher level instead.
-	std::thread thread(acquire);
+	std::thread thread(acquire, std::move(jit));
 	thread.join();
 #else
-	acquire();
+	acquire(std::move(jit));
 #endif
 
 	return routine;