Skip to content

Commit 7be4f2b

Browse files
committed
Implemented thread synchronization for maps and set
1 parent ee3b7b1 commit 7be4f2b

File tree

3 files changed

+54
-21
lines changed

3 files changed

+54
-21
lines changed

Client/mods/deathmatch/logic/CClientGame.cpp

Lines changed: 41 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6845,8 +6845,15 @@ void CClientGame::RestreamModel ( unsigned short usModel )
68456845

68466846
}
68476847

6848+
void CClientGame::InsertIFPPointerToMap ( const unsigned int u32BlockNameHash, const std::shared_ptr < CClientIFP > & pIFP )
6849+
{
6850+
std::lock_guard < std::mutex > mutexGuardedLock ( m_MutexOfIfpPointersMap );
6851+
m_mapOfIfpPointers [ u32BlockNameHash ] = pIFP;
6852+
}
6853+
68486854
std::shared_ptr < CClientIFP > CClientGame::GetIFPPointerFromMap ( const unsigned int u32BlockNameHash )
68496855
{
6856+
std::lock_guard < std::mutex > mutexGuardedLock ( m_MutexOfIfpPointersMap );
68506857
auto it = m_mapOfIfpPointers.find ( u32BlockNameHash );
68516858
if ( it != m_mapOfIfpPointers.end ( ) )
68526859
{
@@ -6855,19 +6862,39 @@ std::shared_ptr < CClientIFP > CClientGame::GetIFPPointerFromMap ( const unsigne
68556862
return nullptr;
68566863
}
68576864

6865+
void CClientGame::RemoveIFPPointerFromMap ( const unsigned int u32BlockNameHash )
6866+
{
6867+
std::lock_guard < std::mutex > mutexGuardedLock ( m_MutexOfIfpPointersMap );
6868+
m_mapOfIfpPointers.erase ( u32BlockNameHash );
6869+
}
6870+
6871+
6872+
void CClientGame::InsertPedPointerToSet ( CClientPed * pPed )
6873+
{
6874+
std::lock_guard < std::mutex > mutexGuardedLock ( m_MutexOfPedPointersSet );
6875+
m_setOfPedPointers.insert ( pPed );
6876+
}
6877+
6878+
void CClientGame::RemovePedPointerFromSet ( CClientPed * pPed )
6879+
{
6880+
std::lock_guard < std::mutex > mutexGuardedLock ( m_MutexOfPedPointersSet );
6881+
m_setOfPedPointers.erase ( pPed );
6882+
}
6883+
68586884
CClientPed * CClientGame::GetClientPedByClump ( const RpClump & Clump )
68596885
{
6860-
for ( auto it = m_mapOfPedPointers.begin(); it != m_mapOfPedPointers.end(); it++ )
6886+
std::lock_guard < std::mutex > mutexGuardedLock ( m_MutexOfPedPointersSet );
6887+
for ( auto it = m_setOfPedPointers.begin ( ); it != m_setOfPedPointers.end ( ); it++ )
68616888
{
6862-
CEntity * pEntity = it->first->GetGameEntity();
6889+
CEntity * pEntity = (*it)->GetGameEntity();
68636890
if ( pEntity != nullptr )
68646891
{
6865-
if ( pEntity->GetRpClump() != nullptr)
6892+
if ( pEntity->GetRpClump() != nullptr )
68666893
{
68676894
const RpClump & entityClump = *pEntity->GetRpClump();
68686895
if ( std::addressof ( entityClump ) == std::addressof ( Clump ) )
68696896
{
6870-
return it->first;
6897+
return *it;
68716898
}
68726899
}
68736900
}
@@ -6877,38 +6904,41 @@ CClientPed * CClientGame::GetClientPedByClump ( const RpClump & Clump )
68776904

68786905
void CClientGame::OnClientIFPUnload ( const std::shared_ptr < CClientIFP > & IFP )
68796906
{
6907+
std::lock_guard < std::mutex > mutexGuardedLock ( m_MutexOfPedPointersSet );
68806908
IFP->MarkAsUnloading ( );
6881-
for ( auto it = m_mapOfPedPointers.begin(); it != m_mapOfPedPointers.end(); it++ )
6909+
for ( auto it = m_setOfPedPointers.begin ( ); it != m_setOfPedPointers.end ( ); it++ )
68826910
{
68836911
// Remove IFP animations from replaced animations of peds/players
6884-
it->first->RestoreAnimations ( IFP );
6912+
(*it)->RestoreAnimations ( IFP );
68856913

68866914
// Make sure that streamed in pulses or changing model does not accidently
68876915
// play our custom animation. We can do that by making the custom animation
68886916
// untriggerable
6889-
if ( it->first->GetCustomAnimationBlockNameHash ( ) == IFP->GetBlockNameHash ( ) )
6917+
if ( (*it)->GetCustomAnimationBlockNameHash ( ) == IFP->GetBlockNameHash ( ) )
68906918
{
6891-
if ( it->first->IsCustomAnimationPlaying ( ) )
6919+
if ( (*it)->IsCustomAnimationPlaying ( ) )
68926920
{
6893-
it->first->SetCustomAnimationUntriggerable ( );
6921+
(*it)->SetCustomAnimationUntriggerable ( );
68946922
}
68956923

68966924
// Important! As we are using a shared_ptr, we need to decrement the reference counter
68976925
// by setting the shared_ptr to nullptr, this will avoid memory leak
6898-
if ( !it->first->IsNextAnimationCustom ( ) && it->first->IsCurrentAnimationCustom ( ) )
6926+
if ( !(*it)->IsNextAnimationCustom ( ) && (*it)->IsCurrentAnimationCustom ( ) )
68996927
{
6900-
it->first->DereferenceCustomAnimationBlock ();
6928+
(*it)->DereferenceCustomAnimationBlock ();
69016929
}
69026930
}
69036931
}
69046932
}
69056933

69066934
void CClientGame::InsertAnimationAssociationToMap ( CAnimBlendAssociationSAInterface * pAnimAssociation, const std::shared_ptr < CIFPAnimations > & pIFPAnimations )
69076935
{
6936+
std::lock_guard < std::mutex > mutexGuardedLock ( m_MutexOfAnimationAssociationsMap );
69086937
m_mapOfCustomAnimationAssociations [ pAnimAssociation ] = pIFPAnimations;
69096938
}
69106939

69116940
void CClientGame::RemoveAnimationAssociationFromMap ( CAnimBlendAssociationSAInterface * pAnimAssociation )
69126941
{
6942+
std::lock_guard < std::mutex > mutexGuardedLock ( m_MutexOfAnimationAssociationsMap );
69136943
m_mapOfCustomAnimationAssociations.erase ( pAnimAssociation );
69146944
}

Client/mods/deathmatch/logic/CClientGame.h

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -593,12 +593,12 @@ class CClientGame
593593
void SetFileCacheRoot ( void );
594594
const char* GetFileCacheRoot ( void ) { return m_strFileCacheRoot; }
595595

596-
inline void InsertIFPPointerToMap ( const unsigned int u32BlockNameHash, const std::shared_ptr < CClientIFP > & pIFP ) { m_mapOfIfpPointers [ u32BlockNameHash ] = pIFP; }
597-
inline void RemoveIFPPointerFromMap ( const unsigned int u32BlockNameHash ) { m_mapOfIfpPointers.erase ( u32BlockNameHash ); }
596+
void InsertIFPPointerToMap ( const unsigned int u32BlockNameHash, const std::shared_ptr < CClientIFP > & pIFP );
597+
void RemoveIFPPointerFromMap ( const unsigned int u32BlockNameHash );
598598
std::shared_ptr < CClientIFP > GetIFPPointerFromMap ( const unsigned int u32BlockNameHash );
599599

600-
void InsertPedPointerToMap ( CClientPed * pPed ) { m_mapOfPedPointers [ pPed ] = true; }
601-
void RemovePedPointerFromMap ( CClientPed * pPed ) { m_mapOfPedPointers.erase ( pPed ); }
600+
void InsertPedPointerToSet ( CClientPed * pPed );
601+
void RemovePedPointerFromSet ( CClientPed * pPed );
602602
CClientPed * GetClientPedByClump ( const RpClump & Clump );
603603

604604
void OnClientIFPUnload ( const std::shared_ptr < CClientIFP > & IFP );
@@ -821,9 +821,13 @@ class CClientGame
821821

822822
// (unsigned int) Key is the hash of custom block name that is supplied to engineLoadIFP
823823
std::map < unsigned int, std::shared_ptr < CClientIFP > > m_mapOfIfpPointers;
824+
mutable std::mutex m_MutexOfIfpPointersMap;
824825

825-
std::map < CClientPed *, bool > m_mapOfPedPointers;
826-
AnimAssociations_type m_mapOfCustomAnimationAssociations;
826+
std::set < CClientPed * > m_setOfPedPointers;
827+
mutable std::mutex m_MutexOfPedPointersSet;
828+
829+
AnimAssociations_type m_mapOfCustomAnimationAssociations;
830+
mutable std::mutex m_MutexOfAnimationAssociationsMap;
827831
};
828832

829833
extern CClientGame* g_pClientGame;

Client/mods/deathmatch/logic/CClientPed.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -247,13 +247,13 @@ void CClientPed::Init ( CClientManager* pManager, unsigned long ulModelID, bool
247247
SetArmor ( 0.0f );
248248
}
249249

250-
g_pClientGame->InsertPedPointerToMap ( this );
250+
g_pClientGame->InsertPedPointerToSet ( this );
251251
}
252252

253253

254254
CClientPed::~CClientPed ( void )
255255
{
256-
g_pClientGame->RemovePedPointerFromMap ( this );
256+
g_pClientGame->RemovePedPointerFromSet ( this );
257257

258258
// Remove from the ped manager
259259
m_pManager->GetPedManager ()->RemoveFromList ( this );
@@ -6210,7 +6210,6 @@ void CClientPed::ReplaceAnimation ( CAnimBlendHierarchy * pInternalAnimHierarchy
62106210
SReplacedAnimation replacedAnimation;
62116211
replacedAnimation.pIFP = pIFP;
62126212
replacedAnimation.pAnimationHierarchy = pCustomAnimHierarchy;
6213-
62146213
m_mapOfReplacedAnimations [ pInternalAnimHierarchy->GetInterface () ] = replacedAnimation;
62156214
}
62166215

@@ -6228,7 +6227,7 @@ void CClientPed::RestoreAnimations ( const std::shared_ptr < CClientIFP > & IFP
62286227
void CClientPed::RestoreAnimations ( CAnimBlock & animationBlock )
62296228
{
62306229
const size_t cAnimations = animationBlock.GetAnimationCount ( );
6231-
for ( size_t i = 0; i < cAnimations; i++)
6230+
for ( size_t i = 0; i < cAnimations; i++ )
62326231
{
62336232
auto pAnimHierarchyInterface = animationBlock.GetAnimationHierarchyInterface ( i );
62346233
m_mapOfReplacedAnimations.erase ( pAnimHierarchyInterface );

0 commit comments

Comments
 (0)