From cd41220a4456bc92b7720aab07504a7fff19ca98 Mon Sep 17 00:00:00 2001 From: H-M-H Date: Wed, 4 May 2016 15:32:24 +0200 Subject: [PATCH] wait for score-threads on shutdown --- src/engine/server.h | 4 +++- src/engine/server/server.cpp | 2 +- src/game/server/gamecontext.cpp | 5 ++++- src/game/server/gamecontext.h | 2 +- src/game/server/score.h | 3 +++ src/game/server/score/file_score.cpp | 5 +++++ src/game/server/score/file_score.h | 2 ++ src/game/server/score/sql_score.cpp | 22 +++++++++++++++++++++- src/game/server/score/sql_score.h | 14 +++++++++++++- 9 files changed, 53 insertions(+), 6 deletions(-) diff --git a/src/engine/server.h b/src/engine/server.h index 60c844506..be3655a1b 100644 --- a/src/engine/server.h +++ b/src/engine/server.h @@ -176,7 +176,9 @@ public: virtual void OnInit() = 0; virtual void OnConsoleInit() = 0; virtual void OnMapChange(char *pNewMapName, int MapNameSize) = 0; - virtual void OnShutdown() = 0; + + // FullShutdown is true if the program is about to exit (not if the map is changed) + virtual void OnShutdown(bool FullShutdown = false) = 0; virtual void OnTick() = 0; virtual void OnPreSnap() = 0; diff --git a/src/engine/server/server.cpp b/src/engine/server/server.cpp index 467d28ecb..97111ddd8 100644 --- a/src/engine/server/server.cpp +++ b/src/engine/server/server.cpp @@ -1828,7 +1828,7 @@ int CServer::Run() m_Fifo.Shutdown(); #endif - GameServer()->OnShutdown(); + GameServer()->OnShutdown(true); m_pMap->Unload(); if(m_pCurrentMapData) diff --git a/src/game/server/gamecontext.cpp b/src/game/server/gamecontext.cpp index 5eafd0e5e..4bdb285e3 100644 --- a/src/game/server/gamecontext.cpp +++ b/src/game/server/gamecontext.cpp @@ -2705,8 +2705,11 @@ void CGameContext::OnMapChange(char *pNewMapName, int MapNameSize) str_copy(m_aDeleteTempfile, aTemp, sizeof(m_aDeleteTempfile)); } -void CGameContext::OnShutdown() +void CGameContext::OnShutdown(bool FullShutdown) { + if (FullShutdown) + Score()->OnShutdown(); + DeleteTempfile(); Console()->ResetServerGameSettings(); Layers()->Dest(); diff --git a/src/game/server/gamecontext.h b/src/game/server/gamecontext.h index a5f72ec96..84fa13f42 100644 --- a/src/game/server/gamecontext.h +++ b/src/game/server/gamecontext.h @@ -197,7 +197,7 @@ public: virtual void OnInit(); virtual void OnConsoleInit(); virtual void OnMapChange(char *pNewMapName, int MapNameSize); - virtual void OnShutdown(); + virtual void OnShutdown(bool FullShutdown = false); virtual void OnTick(); virtual void OnPreSnap(); diff --git a/src/game/server/score.h b/src/game/server/score.h index e4ab6a704..9e6a7614c 100644 --- a/src/game/server/score.h +++ b/src/game/server/score.h @@ -65,6 +65,9 @@ public: virtual void SaveTeam(int Team, const char* Code, int ClientID, const char* Server) = 0; virtual void LoadTeam(const char* Code, int ClientID) = 0; + + // called when the server is shut down but not on mapchange/reload + virtual void OnShutdown() = 0; }; #endif diff --git a/src/game/server/score/file_score.cpp b/src/game/server/score/file_score.cpp index c45c42565..677684dea 100644 --- a/src/game/server/score/file_score.cpp +++ b/src/game/server/score/file_score.cpp @@ -351,3 +351,8 @@ void CFileScore::LoadTeam(const char* Code, int ClientID) str_format(aBuf, sizeof(aBuf), "Save-function not supported in file based servers"); GameServer()->SendChatTarget(ClientID, aBuf); } + +void CFileScore::OnShutdown() +{ + ; +} diff --git a/src/game/server/score/file_score.h b/src/game/server/score/file_score.h index ed79894ab..fa7c7ceda 100644 --- a/src/game/server/score/file_score.h +++ b/src/game/server/score/file_score.h @@ -84,6 +84,8 @@ public: virtual void RandomUnfinishedMap(int ClientID, int stars); virtual void SaveTeam(int Team, const char* Code, int ClientID, const char* Server); virtual void LoadTeam(const char* Code, int ClientID); + + virtual void OnShutdown(); }; #endif diff --git a/src/game/server/score/sql_score.cpp b/src/game/server/score/sql_score.cpp index 29f30cb05..a448d5071 100644 --- a/src/game/server/score/sql_score.cpp +++ b/src/game/server/score/sql_score.cpp @@ -23,6 +23,8 @@ const char* CSqlData::ms_pMap = 0; bool CSqlData::ms_GameContextAvailable = false; int CSqlData::ms_Instance = 0; +int CSqlExecData::ms_InstanceCount = 0; + CSqlScore::CSqlScore(CGameContext *pGameServer) : m_pGameServer(pGameServer), m_pServer(pGameServer->Server()) @@ -36,7 +38,7 @@ m_pServer(pGameServer->Server()) CSqlData::ms_pMap = m_aMap; CSqlData::ms_GameContextAvailable = true; - CSqlData::ms_Instance++; + ++CSqlData::ms_Instance; CSqlConnector::ResetReachable(); @@ -50,6 +52,24 @@ CSqlScore::~CSqlScore() CSqlData::ms_GameContextAvailable = false; } +void CSqlScore::OnShutdown() +{ + CSqlData::ms_GameContextAvailable = false; + int i = 0; + while (CSqlExecData::ms_InstanceCount != 0) + { + // print a log about every two seconds + if (i % 20 == 0) + { + char aBuf[128]; + str_format(aBuf, sizeof(aBuf), "Waiting for score-threads to complete (%d left)", CSqlExecData::ms_InstanceCount); + dbg_msg("sql", aBuf); + } + ++i; + thread_sleep(100); + } +} + void CSqlScore::ExecSqlFunc(void *pUser) { CSqlExecData* pData = (CSqlExecData *)pUser; diff --git a/src/game/server/score/sql_score.h b/src/game/server/score/sql_score.h index 730d6c09d..a5788f48d 100644 --- a/src/game/server/score/sql_score.h +++ b/src/game/server/score/sql_score.h @@ -61,10 +61,20 @@ struct CSqlExecData m_pFuncPtr(pFuncPtr), m_pSqlData(pSqlData), m_ReadOnly(ReadOnly) - {} + { + ++ms_InstanceCount; + } + ~CSqlExecData() + { + --ms_InstanceCount; + } + bool (*m_pFuncPtr) (CSqlServer*, CSqlData *, bool); CSqlData *m_pSqlData; bool m_ReadOnly; + + // keeps track of score-threads + static int ms_InstanceCount; }; struct CSqlPlayerData : CSqlData @@ -190,6 +200,8 @@ public: virtual void RandomUnfinishedMap(int ClientID, int stars); virtual void SaveTeam(int Team, const char* Code, int ClientID, const char* Server); virtual void LoadTeam(const char* Code, int ClientID); + + virtual void OnShutdown(); }; #endif