mirror of https://github.com/OpenTTD/OpenTTD
Fix: on startup, NewGRF scan could case race-condition (#9382)
Creating a thread was not thread-safe. The irony. The video-driver has a function GameLoopPause() which first checks if the thread is the game-thread or not. For this it needs access to this->game_thread. This variable is set in StartNewThread(). However, due to timing, it is well possible GameLoopPause() is called from the thread well before this->game_thread is assigned. And so we have a race-condition! Simply solve this by preventing a thread to start till we are done with our bookkeeping.pull/9383/head
parent
c12a152ec9
commit
b45c006ab9
11
src/thread.h
11
src/thread.h
|
@ -14,6 +14,7 @@
|
||||||
#include "crashlog.h"
|
#include "crashlog.h"
|
||||||
#include <system_error>
|
#include <system_error>
|
||||||
#include <thread>
|
#include <thread>
|
||||||
|
#include <mutex>
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Sleep on the current thread for a defined time.
|
* Sleep on the current thread for a defined time.
|
||||||
|
@ -46,7 +47,17 @@ inline bool StartNewThread(std::thread *thr, const char *name, TFn&& _Fx, TArgs&
|
||||||
{
|
{
|
||||||
#ifndef NO_THREADS
|
#ifndef NO_THREADS
|
||||||
try {
|
try {
|
||||||
|
static std::mutex thread_startup_mutex;
|
||||||
|
std::lock_guard<std::mutex> lock(thread_startup_mutex);
|
||||||
|
|
||||||
std::thread t([] (const char *name, TFn&& F, TArgs&&... A) {
|
std::thread t([] (const char *name, TFn&& F, TArgs&&... A) {
|
||||||
|
/* Delay starting the thread till the main thread is finished
|
||||||
|
* with the administration. This prevent race-conditions on
|
||||||
|
* startup. */
|
||||||
|
{
|
||||||
|
std::lock_guard<std::mutex> lock(thread_startup_mutex);
|
||||||
|
}
|
||||||
|
|
||||||
SetCurrentThreadName(name);
|
SetCurrentThreadName(name);
|
||||||
CrashLog::InitThread();
|
CrashLog::InitThread();
|
||||||
try {
|
try {
|
||||||
|
|
Loading…
Reference in New Issue