r5sdk/r5dev/tier0/threadtools.cpp
Kawe Mazidjatari 6cf88dc16c Engine: render ImGui in main thread and fix many threading bugs
ImGui drawing code now takes place in the main thread, a snapshot of the render data is created in CMaterialSystem::SwapBuffers(), and is being rendered in the render thread right before SpinPresent().

The reason why this was necessary, is because ConVar::GetString() isn't thread safe if its not marked FCVAR_MATERIAL_SYSTEM_THREAD or FCVAR_ACCESSIBLE_FROM_THREADS, and we used it for the console suggestions window, which iterates over every ConVar, accessible from threads or not.
2024-04-05 18:17:12 +02:00

105 lines
2.5 KiB
C++
Raw Blame History

//===== Copyright <20> 1996-2005, Valve Corporation, All rights reserved. ======//
//
// Purpose: Thread tools
//
// $Workfile: $
// $NoKeywords: $
//===========================================================================//
#include "tier0/threadtools.h"
#define INIT_SEM_COUNT 0
#define MAX_SEM_COUNT 1
int CThreadFastMutex::Lock(void)
{
const DWORD threadId = GetCurrentThreadId();
LONG result = ThreadInterlockedCompareExchange((volatile LONG*)&m_lAddend, 0, 1);
if (result)
{
if (m_nOwnerID == threadId)
{
return ++m_nDepth;
}
LONG cycle = 1;
while (true)
{
LONG64 delay = (10 * cycle);
if (delay)
{
do
{
ThreadPause();
--delay;
} while (delay);
}
result = ThreadInterlockedCompareExchange((volatile LONG*)&m_lAddend, 0, 1);
if (!result)
break;
if (++cycle > 5)
{
if (_InterlockedIncrement((volatile LONG*)&m_lAddend) != 1)
{
if (!m_hSemaphore)
{
HANDLE hSemaphore = CreateSemaphoreA(
NULL, INIT_SEM_COUNT, MAX_SEM_COUNT, NULL);
if (ThreadInterlockedCompareExchange64(
(volatile LONG64*)&m_hSemaphore, NULL, (LONG64)hSemaphore))
CloseHandle(hSemaphore);
}
WaitForSingleObject(m_hSemaphore, INFINITE);
}
m_nOwnerID = threadId;
m_nDepth = 1;
return m_nDepth;
}
}
}
m_nDepth = 1;
m_nOwnerID = threadId;
return result;
}
int CThreadFastMutex::Unlock()
{
LONG result = m_nDepth - 1;
m_nDepth = result;
if (!result)
{
m_nOwnerID = 0;
result = _InterlockedExchangeAdd((volatile LONG*)&m_lAddend, 0xFFFFFFFF);
if (result != 1)
{
if (!m_hSemaphore)
{
const HANDLE SemaphoreA = CreateSemaphoreA(NULL, INIT_SEM_COUNT, MAX_SEM_COUNT, NULL);
if (ThreadInterlockedAssignIf64((volatile LONG64*)&m_hSemaphore, NULL, (LONG64)SemaphoreA))
CloseHandle(SemaphoreA);
}
return ReleaseSemaphore(m_hSemaphore, 1, NULL);
}
}
return result;
}
// NOTE: originally the game exported 'ThreadInMainThread()' and ThreadInServerFrameThread(),
// but since the game is built static, and all instances of said functions are inline, we had
// to export the variable symbols instead and get them here to reimplement said functions.
ThreadId_t* g_ThreadMainThreadID = CModule::GetExportedSymbol(CModule::GetProcessEnvironmentBlock()->ImageBaseAddress, "g_ThreadMainThreadID").RCast<ThreadId_t*>();
ThreadId_t* g_ThreadServerFrameThreadID = CModule::GetExportedSymbol(CModule::GetProcessEnvironmentBlock()->ImageBaseAddress, "g_ThreadServerFrameThreadID").RCast<ThreadId_t*>();