fix: replace shutdown flag with atomic variable for thread safety

This commit is contained in:
kuwacom 2026-03-08 22:27:14 +09:00
parent ed5bf4d376
commit aa6692426d

View file

@ -32,6 +32,8 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <atomic>
extern ATOM MyRegisterClass(HINSTANCE hInstance); extern ATOM MyRegisterClass(HINSTANCE hInstance);
extern BOOL InitInstance(HINSTANCE hInstance, int nCmdShow); extern BOOL InitInstance(HINSTANCE hInstance, int nCmdShow);
extern HRESULT InitDevice(); extern HRESULT InitDevice();
@ -65,16 +67,20 @@ struct DedicatedServerConfig
bool showHelp; bool showHelp;
}; };
static volatile bool g_shutdownRequested = false; static std::atomic<bool> g_shutdownRequested(false);
static const DWORD kDefaultAutosaveIntervalMs = 60 * 1000; static const DWORD kDefaultAutosaveIntervalMs = 60 * 1000;
static const int kServerActionPad = 0; static const int kServerActionPad = 0;
static bool IsShutdownRequested()
{
return g_shutdownRequested.load();
}
namespace ServerRuntime namespace ServerRuntime
{ {
void RequestDedicatedServerShutdown() void RequestDedicatedServerShutdown()
{ {
g_shutdownRequested = true; g_shutdownRequested.store(true);
app.m_bShutdown = true;
} }
} }
@ -552,7 +558,7 @@ int main(int argc, char **argv)
C4JThread *startThread = new C4JThread(&CGameNetworkManager::RunNetworkGameThreadProc, (LPVOID)param, "RunNetworkGame"); C4JThread *startThread = new C4JThread(&CGameNetworkManager::RunNetworkGameThreadProc, (LPVOID)param, "RunNetworkGame");
startThread->Run(); startThread->Run();
while (startThread->isRunning() && !g_shutdownRequested) while (startThread->isRunning() && !IsShutdownRequested())
{ {
TickCoreSystems(); TickCoreSystems();
Sleep(10); Sleep(10);
@ -574,7 +580,7 @@ int main(int argc, char **argv)
LogStartupStep("server startup complete"); LogStartupStep("server startup complete");
LogInfof("startup", "Dedicated server listening on %s:%d", g_Win64MultiplayerIP, g_Win64MultiplayerPort); LogInfof("startup", "Dedicated server listening on %s:%d", g_Win64MultiplayerIP, g_Win64MultiplayerPort);
if (worldBootstrap.status == eWorldBootstrap_CreatedNew && !g_shutdownRequested && !app.m_bShutdown) if (worldBootstrap.status == eWorldBootstrap_CreatedNew && !IsShutdownRequested() && !app.m_bShutdown)
{ {
// Windows64 suppresses saveToDisc right after new world creation // Windows64 suppresses saveToDisc right after new world creation
// Dedicated Server explicitly runs the initial save here // Dedicated Server explicitly runs the initial save here
@ -601,13 +607,13 @@ int main(int argc, char **argv)
ServerRuntime::ServerCli serverCli; ServerRuntime::ServerCli serverCli;
serverCli.Start(); serverCli.Start();
while (!g_shutdownRequested && !app.m_bShutdown) while (!IsShutdownRequested() && !app.m_bShutdown)
{ {
TickCoreSystems(); TickCoreSystems();
HandleXuiActions(); HandleXuiActions();
serverCli.Poll(); serverCli.Poll();
if (g_shutdownRequested || app.m_bShutdown) if (IsShutdownRequested() || app.m_bShutdown)
{ {
break; break;
} }
@ -638,6 +644,7 @@ int main(int argc, char **argv)
Sleep(10); Sleep(10);
} }
serverCli.Stop(); serverCli.Stop();
app.m_bShutdown = true;
LogInfof("shutdown", "Dedicated server stopped"); LogInfof("shutdown", "Dedicated server stopped");
MinecraftServer *server = MinecraftServer::getInstance(); MinecraftServer *server = MinecraftServer::getInstance();