Zero Hour rebuilt from EA's source: x64, Direct3D 11, VS2022/CMake, ~60 original bugs fixed, an AI that builds a base. Bring your own game.
C++ cmakecnccommand-and-conquercppdirectx11game-enginegame-preservationgeneralsrtssage-enginex64zero-hour
fix(ww3d2): stop the texture loader deadlocking on quit
Olcay Seyganb16ca3aec67d
diff --git a/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/textureloader.cpp b/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/textureloader.cppindex af0da49d2..4886e80a7 100644--- a/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/textureloader.cpp+++ b/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/textureloader.cpp@@ -337,9 +337,20 @@ void TextureLoader::Init()void TextureLoader::Deinit(){- FastCriticalSectionClass::LockClass lock(_BackgroundCriticalSection);+ //+ // Stop() sets running=false, then spins (capped at 3000ms, WWLib/thread.h) waiting for the+ // loader thread to exit before TerminateThread-ing it. This used to hold _BackgroundCriticalSection+ // for that whole wait. Thread_Function only checks running between tasks - it takes that same+ // lock first, to pop the next one (textureloader.cpp, LoaderThreadClass::Thread_Function) - so if+ // the loader thread reached that line after we grabbed the lock here, it blocked on the lock we+ // were holding while we blocked on it inside Stop(): a guaranteed deadlock, paid out as the full+ // 3-second TerminateThread ceiling on every quit that caught a texture load in flight. Stop()+ // unlocked instead: the loader either finishes its current task and exits on its own, or was+ // never near the lock and exits immediately either way.+ //_TextureLoadThread.Stop();+ FastCriticalSectionClass::LockClass lock(_BackgroundCriticalSection);ThumbnailManagerClass::Deinit();TextureLoadTaskClass::Delete_Free_Pool();}diff --git a/GeneralsMD/Code/Tests/test_wwlib.cpp b/GeneralsMD/Code/Tests/test_wwlib.cppindex 5dd512891..23613b6c6 100644--- a/GeneralsMD/Code/Tests/test_wwlib.cpp+++ b/GeneralsMD/Code/Tests/test_wwlib.cpp@@ -40,8 +40,11 @@#include "ramfile.h"#include "chunkio.h"#include "wwfile.h"+#include "thread.h"+#include "mutex.h"#include <stdlib.h>+#include <windows.h>//////////////////////////////////////////////////////////////////////////////// Helpers@@ -1299,3 +1302,77 @@ TEST(cpudetect_logs_are_printable)must never be blank. */CHECK(p[0] != '\t');}++//-------------------------------------------------------------------------------------------------+// ThreadClass::Stop() deadlock pattern -----------------------------------------------------------+//-------------------------------------------------------------------------------------------------+//+// Regression for the shutdown stall fixed in WW3D2's TextureLoader::Deinit (textureloader.cpp):+// that function used to take a FastCriticalSectionClass lock and only THEN call Stop() on the+// worker thread, which takes that same lock every time round its loop before checking whether it+// should keep running. If the worker reached its own lock acquire after the main thread grabbed+// it, both sides blocked forever - the worker on the lock, the main thread inside Stop()'s+// wait-for-exit spin - so every quit that caught the worker mid-loop paid the full 3-second+// TerminateThread ceiling. The fix is ordering: call Stop() before taking the lock, so the worker+// is free to finish its own acquire, see running go false, and exit on its own.+//+// TextureLoader itself needs a live D3D device, so this reproduces the same two primitives -+// ThreadClass and FastCriticalSectionClass - standalone. LockLoopWorker plays the loader thread's+// role exactly: grab the lock, hold it briefly, release, repeat.+//+class LockLoopWorker : public ThreadClass+{+public:+ LockLoopWorker(FastCriticalSectionClass &lock) : ThreadClass("LockLoopWorker"), m_lock(lock) {}++protected:+ virtual void Thread_Function()+ {+ while (running)+ {+ FastCriticalSectionClass::LockClass lock(m_lock);+ for (volatile int i = 0; i < 2000; ++i) {}+ }+ }++private:+ FastCriticalSectionClass &m_lock;+};++TEST(threadclass_stop_deadlocks_if_the_caller_holds_the_workers_lock)+{+ /* Pins the buggy ordering: take the lock the worker also wants, then call Stop() while still+ holding it. A short timeout keeps the (expected) deadlock from costing real seconds - what+ matters is that it reliably runs out the whole timeout instead of returning early, which is+ exactly what turned into the 3-second quit stall in the real code. */+ FastCriticalSectionClass lock;+ LockLoopWorker worker(lock);+ worker.Execute();+ ThreadClass::Sleep_Ms(20); // let it get into its loop++ unsigned start = GetTickCount();+ {+ FastCriticalSectionClass::LockClass held(lock);+ worker.Stop(300);+ }+ unsigned elapsed = GetTickCount() - start;++ CHECK(elapsed >= 250);+}++TEST(threadclass_stop_returns_promptly_when_called_unlocked)+{+ /* The fix, in the shape TextureLoader::Deinit now uses: call Stop() before taking the lock the+ worker wants. The worker finishes whatever it is doing, sees running go false at the top of+ its loop, and exits - no contention with this thread at all. */+ FastCriticalSectionClass lock;+ LockLoopWorker worker(lock);+ worker.Execute();+ ThreadClass::Sleep_Ms(20);++ unsigned start = GetTickCount();+ worker.Stop(300);+ unsigned elapsed = GetTickCount() - start;++ CHECK(elapsed < 250);+}