GLES: Fix race in Renderer destruction

The clipper and blitter were being destructed before the worker threads were stopped.

Also nullify after deleting other fields - this would have been much harder to detect if the clipper and blitter were not already being nullified after destruction.

Tested with:
./unittests --gtest_repeat=100 --gtest_filter=SwiftShaderTest.TransformFeedback_DrawArraysInstanced

Bug: b/124288179
Change-Id: Iefea829dfb8556c877c0a086253c5924c54aa638
Reviewed-on: https://swiftshader-review.googlesource.com/c/24748
Reviewed-by: Nicolas Capens <nicolascapens@google.com>
Kokoro-Presubmit: kokoro <noreply+kokoro@google.com>
Tested-by: Ben Clayton <bclayton@google.com>
diff --git a/src/Device/Renderer.cpp b/src/Device/Renderer.cpp
index d481f27..f713bd7 100644
--- a/src/Device/Renderer.cpp
+++ b/src/Device/Renderer.cpp
@@ -175,7 +175,10 @@
 
 	Renderer::~Renderer()
 	{
+		sync->lock(EXCLUSIVE);
 		sync->destruct();
+		terminateThreads();
+		sync->unlock();
 
 		delete clipper;
 		clipper = nullptr;
@@ -183,15 +186,17 @@
 		delete blitter;
 		blitter = nullptr;
 
-		terminateThreads();
 		delete resumeApp;
+		resumeApp = nullptr;
 
 		for(int draw = 0; draw < DRAW_COUNT; draw++)
 		{
 			delete drawCall[draw];
+			drawCall[draw] = nullptr;
 		}
 
 		delete swiftConfig;
+		swiftConfig = nullptr;
 	}
 
 	// This object has to be mem aligned
diff --git a/src/Renderer/Renderer.cpp b/src/Renderer/Renderer.cpp
index 27c2194..87b8dd1 100644
--- a/src/Renderer/Renderer.cpp
+++ b/src/Renderer/Renderer.cpp
@@ -209,7 +209,10 @@
 
 	Renderer::~Renderer()
 	{
+		sync->lock(EXCLUSIVE);
 		sync->destruct();
+		terminateThreads();
+		sync->unlock();
 
 		delete clipper;
 		clipper = nullptr;
@@ -217,15 +220,17 @@
 		delete blitter;
 		blitter = nullptr;
 
-		terminateThreads();
 		delete resumeApp;
+		resumeApp = nullptr;
 
 		for(int draw = 0; draw < DRAW_COUNT; draw++)
 		{
 			delete drawCall[draw];
+			drawCall[draw] = nullptr;
 		}
 
 		delete swiftConfig;
+		swiftConfig = nullptr;
 	}
 
 	// This object has to be mem aligned