Browse Source

Fix race condition when a thread is destroyed right after thread:start

Sasha Szpakowski 2 years ago
parent
commit
02d2dc9427
1 changed files with 12 additions and 1 deletions
  1. 12 1
      src/modules/thread/sdl/Thread.cpp

+ 12 - 1
src/modules/thread/sdl/Thread.cpp

@@ -48,13 +48,24 @@ bool Thread::start()
 #endif
 
 	Lock l(mutex);
+
 	if (running)
 		return false;
+
 	if (thread) // Clean old handle up
 		SDL_WaitThread(thread, nullptr);
+
+	// Keep the threadable around until the thread is done with it.
+	// This is done before thread_runner executes because there can be a delay
+	// between CreateThread and the start of the thread code's execution.
+	t->retain();
+
 	thread = SDL_CreateThread(thread_runner, t->getThreadName(), this);
 	running = (thread != nullptr);
 
+	if (!running)
+		t->release(); // thread_runner is never called in this situation.
+
 	return running;
 }
 
@@ -80,7 +91,6 @@ bool Thread::isRunning()
 int Thread::thread_runner(void *data)
 {
 	Thread *self = (Thread *) data; // some compilers don't like 'this'
-	self->t->retain();
 
 	self->t->threadFunction();
 
@@ -89,6 +99,7 @@ int Thread::thread_runner(void *data)
 		self->running = false;
 	}
 
+	// This was retained in start().
 	self->t->release();
 	return 0;
 }