diff --git a/src/Common.cpp b/src/Common.cpp index 3fe52bf..f7875c6 100644 --- a/src/Common.cpp +++ b/src/Common.cpp @@ -22,11 +22,20 @@ void Application::RegisterShutdownHandler(const TShutdownHandler& Handler) { } } +static bool AlreadyShuttingDown = false; void Application::GracefullyShutdown() { - info("please wait while all subsystems are shutting down..."); + if (AlreadyShuttingDown) { + info("already shutting down"); + return; + } else { + AlreadyShuttingDown = true; + } + trace("waiting for lock release"); std::unique_lock Lock(mShutdownHandlersMutex); - for (auto& Handler : mShutdownHandlers) { - Handler(); + info("please wait while all subsystems are shutting down..."); + for (size_t i = 0; i < mShutdownHandlers.size(); ++i) { + info("Subsystem " + std::to_string(i + 1) + "/" + std::to_string(mShutdownHandlers.size()) + " shutting down"); + mShutdownHandlers[i](); } } diff --git a/src/THeartbeatThread.cpp b/src/THeartbeatThread.cpp index d650966..cbd5eb7 100644 --- a/src/THeartbeatThread.cpp +++ b/src/THeartbeatThread.cpp @@ -107,10 +107,8 @@ THeartbeatThread::THeartbeatThread(TResourceManager& ResourceManager, TServer& S , mServer(Server) { Application::RegisterShutdownHandler([&] { if (mThread.joinable()) { - debug("shutting down Heartbeat"); mShutdown = true; mThread.join(); - debug("shut down Heartbeat"); } }); Start(); diff --git a/src/TLuaEngine.cpp b/src/TLuaEngine.cpp index 0ec1b07..d1300e5 100644 --- a/src/TLuaEngine.cpp +++ b/src/TLuaEngine.cpp @@ -23,10 +23,8 @@ TLuaEngine::TLuaEngine(TServer& Server, TNetwork& Network) FolderList(Path, false); mPath = Path; Application::RegisterShutdownHandler([&] {if (mThread.joinable()) { - debug("shutting down LuaEngine"); mShutdown = true; mThread.join(); - debug("shut down LuaEngine"); } }); Start(); } diff --git a/src/TNetwork.cpp b/src/TNetwork.cpp index d0972f3..3ed0c93 100644 --- a/src/TNetwork.cpp +++ b/src/TNetwork.cpp @@ -20,18 +20,14 @@ TNetwork::TNetwork(TServer& Server, TPPSMonitor& PPSMonitor, TResourceManager& R }); Application::RegisterShutdownHandler([&] { if (mUDPThread.joinable()) { - debug("shutting down TCPServer"); mShutdown = true; mUDPThread.detach(); - debug("shut down TCPServer"); } }); Application::RegisterShutdownHandler([&] { if (mTCPThread.joinable()) { - debug("shutting down TCPServer"); mShutdown = true; mTCPThread.detach(); - debug("shut down TCPServer"); } }); mTCPThread = std::thread(&TNetwork::TCPServerMain, this); diff --git a/src/TPPSMonitor.cpp b/src/TPPSMonitor.cpp index 129ab04..c544628 100644 --- a/src/TPPSMonitor.cpp +++ b/src/TPPSMonitor.cpp @@ -7,10 +7,8 @@ TPPSMonitor::TPPSMonitor(TServer& Server) Application::SetPPS("-"); Application::RegisterShutdownHandler([&] { if (mThread.joinable()) { - debug("shutting down PPSMonitor"); mShutdown = true; mThread.join(); - debug("shut down PPSMonitor"); } }); Start(); diff --git a/src/main.cpp b/src/main.cpp index 53d4a4c..9f5b339 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -56,8 +56,9 @@ int main(int argc, char** argv) try { // TODO: replace while (!Shutdown) { - std::this_thread::sleep_for(std::chrono::milliseconds(100)); + std::this_thread::sleep_for(std::chrono::milliseconds(50)); } + info("Shutdown."); } catch (const std::exception& e) { error(e.what()); Sentry.LogException(e, _file_basename, _line);