GameUI: cleanup and fix race condition

* Use CThreadFastMutex instead
* Rename ISurface to IDebugSurface to prevent a potential name collision with VGUI's ISurface iface if we ever add it
* Fix race condition when setting CPylon::m_HostIP from materialsystem thread, this has been dispatched to the main thread
* Marked 'pylon_matchmaking_hostname' FCVAR_MATERIAL_SYSTEM_THREAD as we use it in the server browser panel.
This commit is contained in:
Kawe Mazidjatari 2024-01-21 23:06:26 +01:00
parent f6e6e7c30b
commit 8911bfa81a
7 changed files with 40 additions and 30 deletions

View File

@ -518,7 +518,7 @@ void ConVar_StaticInit(void)
net_processTimeBudget = ConVar::StaticCreate("net_processTimeBudget" ,"200" , FCVAR_RELEASE , "Net message process time budget in milliseconds (removing netchannel if exceeded).", true, 0.f, false, 0.f, nullptr, "0 = disabled");
//-------------------------------------------------------------------------
// NETWORKSYSTEM |
pylon_matchmaking_hostname = ConVar::StaticCreate("pylon_matchmaking_hostname", "ms.r5reloaded.com", FCVAR_RELEASE, "Holds the pylon matchmaking hostname.", false, 0.f, false, 0.f, &MP_HostName_Changed_f, nullptr);
pylon_matchmaking_hostname = ConVar::StaticCreate("pylon_matchmaking_hostname", "ms.r5reloaded.com", FCVAR_RELEASE | FCVAR_MATERIAL_SYSTEM_THREAD, "Holds the pylon matchmaking hostname.", false, 0.f, false, 0.f, &MP_HostName_Changed_f, nullptr);
pylon_host_update_interval = ConVar::StaticCreate("pylon_host_update_interval", "5" , FCVAR_RELEASE, "Length of time in seconds between each status update interval to master server.", true, 5.f, false, 0.f, nullptr, nullptr);
pylon_showdebuginfo = ConVar::StaticCreate("pylon_showdebuginfo" , "0" , FCVAR_RELEASE, "Shows debug output for pylon.", false, 0.f, false, 0.f, nullptr, nullptr);
//-------------------------------------------------------------------------

View File

@ -219,7 +219,7 @@ void CBrowser::Think(void)
//-----------------------------------------------------------------------------
void CBrowser::DrawSurface(void)
{
std::lock_guard<std::mutex> l(m_Mutex);
AUTO_LOCK(m_Mutex);
ImGui::BeginTabBar("CompMenu");
if (ImGui::BeginTabItem("Browsing"))
@ -374,7 +374,7 @@ void CBrowser::RefreshServerList(void)
std::string svServerListMessage;
g_ServerListManager.RefreshServerList(svServerListMessage);
std::lock_guard<std::mutex> l(m_Mutex);
AUTO_LOCK(m_Mutex);
m_svServerListMessage = svServerListMessage;
}
@ -696,7 +696,7 @@ void CBrowser::UpdateHostingStatus(void)
{
case EHostStatus_t::NOT_HOSTING:
{
std::lock_guard<std::mutex> g(m_Mutex);
AUTO_LOCK(m_Mutex);
if (!m_svHostToken.empty())
{
m_svHostToken.clear();
@ -782,15 +782,22 @@ void CBrowser::SendHostingPostRequest(const NetGameServer_t& gameServer)
string svHostToken;
string svHostIp;
bool result = g_MasterServer.PostServerHost(svHostRequestMessage, svHostToken, svHostIp, gameServer);
const bool result = g_MasterServer.PostServerHost(svHostRequestMessage, svHostToken, svHostIp, gameServer);
std::lock_guard<std::mutex> l(m_Mutex);
AUTO_LOCK(m_Mutex);
m_svHostRequestMessage = svHostRequestMessage;
m_svHostToken = svHostToken;
if(svHostIp.length() != 0)
g_MasterServer.SetHostIP(svHostIp);
if (!svHostIp.empty())
{
// Must be set from the main thread, dispatch it off
// and set it at the start of the next frame.
g_TaskScheduler->Dispatch([svHostIp]()
{
g_MasterServer.SetHostIP(svHostIp);
}, 0);
}
if (result)
{
@ -847,7 +854,7 @@ void CBrowser::SettingsPanel(void)
//-----------------------------------------------------------------------------
void CBrowser::SetHostName(const char* pszHostName)
{
std::lock_guard<std::mutex> l(m_Mutex);
AUTO_LOCK(m_Mutex);
m_szMatchmakingHostName = pszHostName;
}

View File

@ -7,7 +7,7 @@
#include "public/isurfacesystem.h"
#include "thirdparty/imgui/misc/imgui_utility.h"
class CBrowser : public ISurface
class CBrowser : public IDebugSurface
{
public:
CBrowser(void);
@ -53,7 +53,7 @@ private:
ID3D11ShaderResourceView* m_idLockedIcon;
MODULERESOURCE m_rLockedIconBlob;
mutable std::mutex m_Mutex;
mutable CThreadFastMutex m_Mutex;
////////////////////
// Server List //

View File

@ -192,7 +192,7 @@ void CConsole::RunFrame(void)
void CConsole::RunTask(void)
{
// m_Logger and m_vHistory are modified.
std::lock_guard<std::mutex> l(m_Mutex);
AUTO_LOCK(m_Mutex);
ClampLogSize();
ClampHistorySize();
@ -279,7 +279,8 @@ void CConsole::DrawSurface(void)
// Mutex is locked here, as we start using/modifying
// non-atomic members that are used from several threads.
std::lock_guard<std::mutex> l(m_Mutex);
AUTO_LOCK(m_Mutex);
m_Logger.Render();
if (m_bCopyToClipBoard)
@ -1043,7 +1044,7 @@ int CConsole::TextEditCallbackStub(ImGuiInputTextCallbackData* iData)
//-----------------------------------------------------------------------------
void CConsole::AddLog(const ConLog_t& conLog)
{
std::lock_guard<std::mutex> l(m_Mutex);
AUTO_LOCK(m_Mutex);
m_Logger.InsertText(conLog);
}
@ -1073,8 +1074,9 @@ void CConsole::AddLog(const ImVec4& color, const char* fmt, ...) /*IM_FMTARGS(2)
//-----------------------------------------------------------------------------
void CConsole::RemoveLog(int nStart, int nEnd)
{
std::lock_guard<std::mutex> l(m_Mutex);
int nLines = m_Logger.GetTotalLines();
AUTO_LOCK(m_Mutex);
const int nLines = m_Logger.GetTotalLines();
if (nEnd >= nLines)
{
@ -1114,7 +1116,7 @@ void CConsole::RemoveLog(int nStart, int nEnd)
//-----------------------------------------------------------------------------
void CConsole::ClearLog(void)
{
std::lock_guard<std::mutex> l(m_Mutex);
AUTO_LOCK(m_Mutex);
m_Logger.RemoveLine(0, (m_Logger.GetTotalLines() - 1));
}
@ -1124,7 +1126,7 @@ void CConsole::ClearLog(void)
//-----------------------------------------------------------------------------
vector<string> CConsole::GetHistory(void) const
{
std::lock_guard<std::mutex> l(m_Mutex);
AUTO_LOCK(m_Mutex);
return m_vHistory;
}
@ -1133,7 +1135,8 @@ vector<string> CConsole::GetHistory(void) const
//-----------------------------------------------------------------------------
void CConsole::ClearHistory(void)
{
std::lock_guard<std::mutex> l(m_Mutex);
AUTO_LOCK(m_Mutex);
m_vHistory.clear();
BuildSummary();
}

View File

@ -6,7 +6,7 @@
#include "thirdparty/imgui/misc/imgui_logger.h"
#include "thirdparty/imgui/misc/imgui_utility.h"
class CConsole : public ISurface
class CConsole : public IDebugSurface
{
public:
enum PositionMode_t
@ -73,8 +73,8 @@ private:
const char* m_pszConsoleLabel;
const char* m_pszLoggingLabel;
char m_szInputBuf[512];
char m_szSummary[512];
char m_szWindowLabel[512];
char m_szSummary[256];
char m_szWindowLabel[128];
string m_svInputConVar;
ssize_t m_nHistoryPos;
@ -104,7 +104,7 @@ private:
ImVec2 m_ivSuggestWindowPos;
ImVec2 m_ivSuggestWindowSize;
CTextLogger m_Logger;
mutable std::mutex m_Mutex;
mutable CThreadFastMutex m_Mutex;
ImGuiInputTextFlags m_nInputFlags;
ImGuiWindowFlags m_nSuggestFlags;

View File

@ -34,17 +34,17 @@ public:
bool SendRequest(const char* endpoint, const rapidjson::Document& requestJson, rapidjson::Document& responseJson, string& outMessage, CURLINFO& status, const char* errorText = nullptr, const bool checkEula = true) const;
bool QueryServer(const char* endpoint, const char* request, string& outResponse, string& outMessage, CURLINFO& outStatus) const;
inline void SetCurrentToken(const string& token) { m_Token = token; }
inline const string& GetCurrentToken() const { return m_Token; }
inline void SetCurrentError(const string& error) { m_ErrorMsg = error; }
inline const string& GetCurrentError() const { return m_ErrorMsg; }
inline void SetHostIP(const string& ip) { m_HostIP = ip; };
inline const string& GetHostIP() const { return m_HostIP; };
inline void SetCurrentToken(const string& token) { m_Token = token; }
inline void SetCurrentError(const string& error) { m_ErrorMsg = error; }
inline void SetHostIP(const string& ip) { m_HostIP = ip; };
inline void SetLanguage(const char* lang) { m_Language = lang; };
inline const string& GetLanguage() const { return m_Language; };
private:
string m_Token;

View File

@ -1,10 +1,10 @@
#ifndef ISURFACESYSTEM_H
#define ISURFACESYSTEM_H
class ISurface
class IDebugSurface
{
public:
virtual ~ISurface() { };
virtual ~IDebugSurface() { };
virtual bool Init() = 0;
virtual void Think() = 0;
virtual void RunFrame() = 0;