]> git.lizzy.rs Git - minetest.git/commitdiff
Thread: fix a crash on Windows due to data race condition on Thread::m_start_finished...
authorLoïc Blot <nerzhul@users.noreply.github.com>
Tue, 10 Oct 2017 10:27:08 +0000 (12:27 +0200)
committerSmallJoker <mk939@ymail.com>
Sun, 3 Jun 2018 15:31:59 +0000 (17:31 +0200)
src/threading/thread.cpp

index 1909da61dff607788f5cbe25ec524a77958ef2f2..e3dde24cd32f6e34ebe2bac938b145d131b298aa 100644 (file)
@@ -103,8 +103,8 @@ Thread::~Thread()
        kill();
 
        // Make sure start finished mutex is unlocked before it's destroyed
-       m_start_finished_mutex.try_lock();
-       m_start_finished_mutex.unlock();
+       if (m_start_finished_mutex.try_lock())
+               m_start_finished_mutex.unlock();
 
 }
 
@@ -267,6 +267,10 @@ DWORD WINAPI Thread::threadProc(LPVOID param)
        thr->m_retval = thr->run();
 
        thr->m_running = false;
+       // Unlock m_start_finished_mutex to prevent data race condition on Windows.
+       // On Windows with VS2017 build TerminateThread is called and this mutex is not
+       // released. We try to unlock it from caller thread and it's refused by system.
+       thr->m_start_finished_mutex.unlock();
        g_logger.deregisterThread();
 
        // 0 is returned here to avoid an unnecessary ifdef clause