Single-thread the game console

Run all task in the same thread used to run the browser/console frame and remove all mutexes.
This commit is contained in:
Kawe Mazidjatari 2022-08-20 01:48:42 +02:00
parent aa807b565d
commit 52e19e9ad6
7 changed files with 33 additions and 38 deletions

View File

@ -16,6 +16,8 @@ public:
virtual void Think(void);
virtual void RunFrame(void);
virtual void RunTask(void){};
virtual void DrawSurface(void);
void BrowserPanel(void);

View File

@ -139,25 +139,32 @@ void CConsole::RunFrame(void)
}
//-----------------------------------------------------------------------------
// Purpose: runs tasks for the console while not being drawn
// Purpose: runs tasks for the console while not being drawn
// (!!! RunTask and RunFrame must be called from the same thread !!!)
//-----------------------------------------------------------------------------
void CConsole::RunTask()
{
if (m_Logger.GetTotalLines() > con_max_size_logvector->GetInt())
{
while (m_Logger.GetTotalLines() > con_max_size_logvector->GetInt())
{
m_Logger.RemoveLine(0);
m_nScrollBack++;
m_nSelectBack++;
}
m_Logger.MoveSelection(m_nSelectBack, false);
m_Logger.MoveCursor(m_nSelectBack, false);
m_nSelectBack = 0;
}
}
//-----------------------------------------------------------------------------
// Purpose: think
//-----------------------------------------------------------------------------
void CConsole::Think(void)
{
for (;;) // Loop running at 100-tps.
{
if (m_Logger.GetTotalLines() > con_max_size_logvector->GetInt())
{
while (m_Logger.GetTotalLines() > con_max_size_logvector->GetInt())
{
m_Logger.RemoveLine(0);
m_nScrollBack++;
m_nSelectBack++;
}
m_Logger.MoveSelection(m_nSelectBack, false);
m_Logger.MoveCursor(m_nSelectBack, false);
m_nSelectBack = 0;
}
while (m_vHistory.size() > 512)
{
m_vHistory.erase(m_vHistory.begin());

View File

@ -16,6 +16,8 @@ public:
virtual void Think(void);
virtual void RunFrame(void);
virtual void RunTask(void);
virtual void DrawSurface(void);
void OptionsPanel(void);

View File

@ -8,6 +8,7 @@ public:
virtual bool Init() = 0;
virtual void Think() = 0;
virtual void RunFrame() = 0;
virtual void RunTask() = 0;
virtual void DrawSurface() = 0;
virtual void SetStyleVar() = 0;
};

View File

@ -165,8 +165,8 @@ public:
bool HasSelection() const;
void MoveSelection(int aLines, bool aForward = true);
void RemoveLine(int aStart, int aEnd, bool aInternal = false);
void RemoveLine(int aIndex, bool aInternal = false);
void RemoveLine(int aStart, int aEnd);
void RemoveLine(int aIndex);
private:
struct LoggerState_t
@ -229,7 +229,6 @@ private:
Lines m_Lines;
LoggerState_t m_State;
std::mutex m_Mutex;
std::string m_svLineBuffer;
public:
ImGuiTextFilter m_itFilter;

View File

@ -494,33 +494,21 @@ bool CTextLogger::IsOnWordBoundary(const Coordinates & aAt) const
return isspace(line[cindex].m_Char) != isspace(line[cindex - 1].m_Char);
}
void CTextLogger::RemoveLine(int aStart, int aEnd, bool aInternal)
void CTextLogger::RemoveLine(int aStart, int aEnd)
{
if (!aInternal)
m_Mutex.lock();
assert(aEnd >= aStart);
assert(m_Lines.size() > (size_t)(aEnd - aStart));
m_Lines.erase(m_Lines.begin() + aStart, m_Lines.begin() + aEnd);
assert(!m_Lines.empty());
if (!aInternal)
m_Mutex.unlock();
}
void CTextLogger::RemoveLine(int aIndex, bool aInternal)
void CTextLogger::RemoveLine(int aIndex)
{
if (!aInternal)
m_Mutex.lock();
assert(m_Lines.size() > 1);
m_Lines.erase(m_Lines.begin() + aIndex);
assert(!m_Lines.empty());
if (!aInternal)
m_Mutex.unlock();
}
CTextLogger::Line& CTextLogger::InsertLine(int aIndex)
@ -689,8 +677,6 @@ void CTextLogger::HandleMouseInputs(bool bHoveredScrollbar, bool bActiveScrollba
void CTextLogger::Render()
{
m_Mutex.lock();
ImGui::PushStyleVar(ImGuiStyleVar_ItemSpacing, ImVec2(0.0f, 0.0f));
ImGuiWindow* pWindow = ImGui::GetCurrentWindow();
@ -880,7 +866,6 @@ void CTextLogger::Render()
ImGui::PopAllowKeyboardFocus();
ImGui::PopStyleVar();
m_Mutex.unlock();
}
void CTextLogger::Copy(bool aCopyAll)
@ -1047,8 +1032,6 @@ void CTextLogger::SetTabSize(int aValue)
void CTextLogger::InsertText(const ConLog_t & aValue)
{
m_Mutex.lock();
if (!aValue.m_svConLog.empty())
{
Coordinates pos = GetActualLastLineCoordinates();
@ -1058,8 +1041,6 @@ void CTextLogger::InsertText(const ConLog_t & aValue)
totalLines += InsertTextAt(pos, aValue.m_svConLog.c_str(), aValue.m_imColor);
}
m_Mutex.unlock();
}
void CTextLogger::MoveUp(int aAmount, bool aSelect)

View File

@ -273,6 +273,9 @@ void DrawImGui()
ImGui::NewFrame();
g_pBrowser->RunTask();
g_pConsole->RunTask();
if (g_pBrowser->m_bActivate)
{
g_pInputSystem->EnableInput(false); // Disable input to game when browser is drawn.