From 25551673815d6bbaf415302dc7dd0ab6b9c48ac6 Mon Sep 17 00:00:00 2001 From: RaphaelIT7 <64648134+RaphaelIT7@users.noreply.github.com> Date: Mon, 17 Nov 2025 00:14:42 +0100 Subject: [PATCH] filesystem: use a RWLock instead --- filesystem/basefilesystem.cpp | 6 +++--- filesystem/basefilesystem.h | 15 ++++++++++----- 2 files changed, 13 insertions(+), 8 deletions(-) diff --git a/filesystem/basefilesystem.cpp b/filesystem/basefilesystem.cpp index b3d6cd144..9cdd17cff 100644 --- a/filesystem/basefilesystem.cpp +++ b/filesystem/basefilesystem.cpp @@ -1458,7 +1458,7 @@ void CBaseFileSystem::AddSearchPath( const char *pPath, const char *pathID, Sear //----------------------------------------------------------------------------- int CBaseFileSystem::GetSearchPath( const char *pathID, bool bGetPackFiles, OUT_Z_CAP(maxLenInChars) char *pDest, intp maxLenInChars ) { - AUTO_LOCK( m_SearchPathsMutex ); + AUTO_LOCK_READ( m_SearchPathsMutex ); // Build up result into string object // dimhotepus: Use std::string and reserve capacity. @@ -1582,7 +1582,7 @@ CBaseFileSystem::CSearchPath *CBaseFileSystem::FindWritePath( const char *pFilen { CUtlSymbol lookup = g_PathIDTable.AddString( pathID ); - AUTO_LOCK( m_SearchPathsMutex ); + AUTO_LOCK_READ( m_SearchPathsMutex ); // a pathID has been specified, find the first match in the path list intp c = m_SearchPaths.Count(); @@ -1964,7 +1964,7 @@ bool CBaseFileSystem::UnzipFile( const char *pFileName, const char *pPath, const //----------------------------------------------------------------------------- void CBaseFileSystem::RemoveAllSearchPaths( void ) { - AUTO_LOCK( m_SearchPathsMutex ); + AUTO_LOCK_WRITE( m_SearchPathsMutex ); m_SearchPaths.Purge(); } diff --git a/filesystem/basefilesystem.h b/filesystem/basefilesystem.h index c3c79391c..87f400fe5 100644 --- a/filesystem/basefilesystem.h +++ b/filesystem/basefilesystem.h @@ -576,9 +576,9 @@ abstract_class CBaseFileSystem : public CTier1AppSystem< IFileSystem > if ( *ppszFilename && !Q_IsAbsolutePath( *ppszFilename ) ) { // Copy paths to minimize mutex lock time - pFileSystem->m_SearchPathsMutex.Lock(); + pFileSystem->m_SearchPathsMutex.LockForRead(); CopySearchPaths( pFileSystem->m_SearchPaths ); - pFileSystem->m_SearchPathsMutex.Unlock(); + pFileSystem->m_SearchPathsMutex.UnlockRead(); pFileSystem->FixUpPath ( *ppszFilename, m_Filename ); } @@ -607,9 +607,9 @@ abstract_class CBaseFileSystem : public CTier1AppSystem< IFileSystem > m_pathID = UTL_INVAL_SYMBOL; } // Copy paths to minimize mutex lock time - pFileSystem->m_SearchPathsMutex.Lock(); + pFileSystem->m_SearchPathsMutex.LockForRead(); CopySearchPaths( pFileSystem->m_SearchPaths ); - pFileSystem->m_SearchPathsMutex.Unlock(); + pFileSystem->m_SearchPathsMutex.UnlockRead(); m_Filename[0] = '\0'; } @@ -658,7 +658,12 @@ abstract_class CBaseFileSystem : public CTier1AppSystem< IFileSystem > // logging functions CUtlVector< FileSystemLoggingFunc_t > m_LogFuncs; - CThreadMutex m_SearchPathsMutex; + // RaphaelIT7: A CThreadRWLock/CThreadSpinRWLock should perform better than a CThreadFastMutex +#if defined(WIN32) || defined(_WIN32) + CThreadSpinRWLock m_SearchPathsMutex; +#else + CThreadRWLock m_SearchPathsMutex; +#endif CUtlVector< CSearchPath > m_SearchPaths; CUtlVector m_PathIDInfos; CUtlLinkedList m_FindData;