Skip to content

Commit b3fb251

Browse files
committed
all: Use RAII for lock / unlock the mutex
1 parent 6164713 commit b3fb251

6 files changed

Lines changed: 129 additions & 141 deletions

File tree

public/tier0/stackstats.h

Lines changed: 60 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,10 @@ struct CCallStackStatsGatherer_FunctionTable_t {
5252
// translator function
5353
class CCallStackStatsGatherer_Standardized_t {
5454
public:
55-
CCallStackStatsGatherer_Standardized_t(void){};
55+
CCallStackStatsGatherer_Standardized_t(void) {};
5656
CCallStackStatsGatherer_Standardized_t(
5757
void *pThis, const CCallStackStatsGatherer_FunctionTable_t &FnTable)
58-
: pGatherer(pThis), pFunctionTable(&FnTable){};
58+
: pGatherer(pThis), pFunctionTable(&FnTable) {};
5959

6060
// go ahead and create some helper functions that are likely to be used by
6161
// helper code
@@ -85,7 +85,7 @@ class CCallStackStatsGatherer_StructAccessor_Base {
8585
public:
8686
CCallStackStatsGatherer_StructAccessor_Base(
8787
const CCallStackStatsGatherer_Standardized_t &Gatherer, int iEntryIndex)
88-
: m_Gatherer(Gatherer), m_iEntryIndex(iEntryIndex){};
88+
: m_Gatherer(Gatherer), m_iEntryIndex(iEntryIndex) {};
8989

9090
protected:
9191
uint32
@@ -108,7 +108,7 @@ class CCallStackStatsGatherer_StatMutexPool {
108108
void LockEntry(uint32 iEntryIndex, bool bLock) {
109109
#if defined(ENABLE_STACK_STATS_GATHERING)
110110
static_assert((SHAREDENTRYMUTEXES & (SHAREDENTRYMUTEXES - 1)) ==
111-
0); // must be a power of 2
111+
0); // must be a power of 2
112112

113113
if (bLock) {
114114
m_IndividualEntryMutexes[iEntryIndex & (SHAREDENTRYMUTEXES - 1)].Lock();
@@ -126,10 +126,11 @@ class CCallStackStatsGatherer_StatMutexPool {
126126
// tracking. CAPTUREDCALLSTACKLENGTH - The maximum length of your stack trace
127127
// that we'll use to distinguish entries STACKACQUISITIONFUNCTION - The function
128128
// to use to gather the current call stack. GetCallStack() is safe,
129-
// GetCallStack_Fast() is faster, but has special requirements STATMUTEXHANDLER -
130-
// If you want automatic mutex management for your individual entries, supply a
131-
// handler here. You'll need to not only call GetEntry(), but lock/unlock the
132-
//entry while accessing it. TEMPLATIZEDMEMORYALLOCATOR - We'll need to allocate
129+
// GetCallStack_Fast() is faster, but has special requirements STATMUTEXHANDLER
130+
// - If you want automatic mutex management for your individual entries, supply
131+
// a handler here. You'll need to
132+
// not only call GetEntry(), but lock/unlock the
133+
// entry while accessing it. TEMPLATIZEDMEMORYALLOCATOR - We'll need to allocate
133134
// memory, supply an allocator if you want to manage that
134135
template <class STATSTRUCT, size_t CAPTUREDCALLSTACKLENGTH,
135136
FN_GetCallStack STACKACQUISITIONFUNCTION = GetCallStack,
@@ -148,9 +149,9 @@ class CCallStackStatsGatherer : public STATMUTEXHANDLER {
148149
CCallStackStatsGatherer_StructAccessor_Base<STATSTRUCT> GetEntry(
149150
void *const
150151
CallStack[CAPTUREDCALLSTACKLENGTH]); // get the entry using some
151-
// callstack grabbed a while ago.
152-
// Assumes ALL invalid entries
153-
// have been nullified
152+
// callstack grabbed a while
153+
// ago. Assumes ALL invalid
154+
// entries have been nullified
154155
CCallStackStatsGatherer_StructAccessor_Base<STATSTRUCT> GetEntry(
155156
void *const CallStack[CAPTUREDCALLSTACKLENGTH],
156157
uint32 iValidEntries); // same as above, but does the work of nullifying
@@ -266,8 +267,7 @@ class CCallStackStatsGatherer : public STATMUTEXHANDLER {
266267

267268
void KeepSubTree(CCallStackStatsGatherer_Standardized_t &SubTree) {
268269
AUTO_LOCK_FM(m_SubTreeMutex);
269-
for (typename StoredSubTreeVector_t::iterator treeIter =
270-
m_StoredSubTrees.begin();
270+
for (auto treeIter = m_StoredSubTrees.begin();
271271
treeIter != m_StoredSubTrees.end(); ++treeIter) {
272272
if (SubTree.pGatherer == treeIter->pGatherer) return;
273273
}
@@ -309,7 +309,8 @@ class CCallStackStatsGatherer : public STATMUTEXHANDLER {
309309
// szTemp ), "\n\t" );
310310

311311
// Warning( "Attempting to link
312-
// trees:\n=======================ONE=======================\n\t%s\n", szTemp
312+
// trees:\n=======================ONE=======================\n\t%s\n",
313+
// szTemp
313314
// ); TranslateStackInfo( treeIter->Stack, CAPTUREDCALLSTACKLENGTH, szTemp,
314315
// sizeof( szTemp ), "\n\t" ); Warning(
315316
// "=======================TWO=======================\n\t%s\n", szTemp );
@@ -324,7 +325,8 @@ class CCallStackStatsGatherer : public STATMUTEXHANDLER {
324325
if (CallStackIn[i] == pMatchAddress) {
325326
// TranslateStackInfo( CallStackIn, i, szTemp, sizeof( szTemp ), "\n\t"
326327
// ); Warning(
327-
// "======================MATCH======================\n\t%s\n", szTemp );
328+
// "======================MATCH======================\n\t%s\n", szTemp
329+
// );
328330

329331
CallStackOut[i] =
330332
treeIter->tree
@@ -341,7 +343,7 @@ class CCallStackStatsGatherer : public STATMUTEXHANDLER {
341343
}
342344

343345
struct StatIndex_t {
344-
StatIndex_t(void) : m_Index((unsigned int)-1){};
346+
StatIndex_t(void) : m_Index((unsigned int)-1) {};
345347
unsigned int m_Index;
346348
};
347349

@@ -384,7 +386,7 @@ class CCallStackStatsGatherer : public STATMUTEXHANDLER {
384386
// access the same index simultaneously
385387
CThreadRWLock m_StatEntryLock;
386388

387-
#else //#if defined( ENABLE_STACK_STATS_GATHERING )
389+
#else // #if defined( ENABLE_STACK_STATS_GATHERING )
388390

389391
STATSTRUCT m_SingleEntry; // the class is disabled, we'll always return this
390392
// same struct
@@ -395,7 +397,7 @@ class CCallStackStatsGatherer : public STATMUTEXHANDLER {
395397
return 0;
396398
}
397399

398-
#endif //#if defined( ENABLE_STACK_STATS_GATHERING )
400+
#endif // #if defined( ENABLE_STACK_STATS_GATHERING )
399401
};
400402

401403
template <class STATSTRUCT, size_t CAPTUREDCALLSTACKLENGTH,
@@ -477,13 +479,13 @@ PLATFORM_INTERFACE size_t _CCallStackStatsGatherer_Write_FieldDescriptions(
477479
iWroteBytes += _CCallStackStatsGatherer_Write_FieldDescriptions( \
478480
GetStatStructFieldDescriptions(), pDescribeWriteBuffer + iWroteBytes, \
479481
iDescribeMaxLength - iWroteBytes);
480-
//#define WRITE_STATSTRUCT_FIELDMERGESCRIPT( scriptMergeLanguage ) iWroteBytes
482+
// #define WRITE_STATSTRUCT_FIELDMERGESCRIPT( scriptMergeLanguage ) iWroteBytes
481483
//+= _CCallStackStatsGatherer_Write_FieldMergeScript(
482-
//GetStatStructFieldDescriptions(),
483-
//CallStackStatStructDescFuncs::scriptMergeLanguage, pDescribeWriteBuffer +
484-
//iWroteBytes, iDescribeMaxLength - iWroteBytes );
484+
// GetStatStructFieldDescriptions(),
485+
// CallStackStatStructDescFuncs::scriptMergeLanguage, pDescribeWriteBuffer +
486+
// iWroteBytes, iDescribeMaxLength - iWroteBytes );
485487

486-
#else //#if defined( ENABLE_STACK_STATS_GATHERING )
488+
#else // #if defined( ENABLE_STACK_STATS_GATHERING )
487489

488490
#define DECLARE_CALLSTACKSTATSTRUCT()
489491
#define BEGIN_STATSTRUCTDESCRIPTION(className)
@@ -495,9 +497,9 @@ PLATFORM_INTERFACE size_t _CCallStackStatsGatherer_Write_FieldDescriptions(
495497
#define END_STATSTRUCTFIELDDESCRIPTION()
496498

497499
#define WRITE_STATSTRUCT_FIELDDESCRIPTION()
498-
//#define WRITE_STATSTRUCT_FIELDMERGESCRIPT( scriptMergeLanguage )
500+
// #define WRITE_STATSTRUCT_FIELDMERGESCRIPT( scriptMergeLanguage )
499501

500-
#endif //#if defined( ENABLE_STACK_STATS_GATHERING )
502+
#endif // #if defined( ENABLE_STACK_STATS_GATHERING )
501503

502504
template <class STATSTRUCT, size_t CAPTUREDCALLSTACKLENGTH,
503505
FN_GetCallStack STACKACQUISITIONFUNCTION, typename STATMUTEXHANDLER,
@@ -700,19 +702,18 @@ const CCallStackStatsGatherer_FunctionTable_t &CCallStackStatsGatherer<
700702
STATSTRUCT, CAPTUREDCALLSTACKLENGTH, STACKACQUISITIONFUNCTION,
701703
STATMUTEXHANDLER, TEMPLATIZEDMEMORYALLOCATOR>::GetFunctionTable(void) {
702704
static CCallStackStatsGatherer_FunctionTable_t retVal = {
703-
GetDumpInfo,
704-
PushSubTree,
705-
PopSubTree,
705+
GetDumpInfo,
706+
PushSubTree,
707+
PopSubTree,
706708
#if defined(ENABLE_STACK_STATS_GATHERING)
707-
STATSTRUCT::DescribeCallStackStatStruct,
709+
STATSTRUCT::DescribeCallStackStatStruct,
708710
#else
709-
NULL_DescribeCallStackStatStruct,
711+
NULL_DescribeCallStackStatStruct,
710712
#endif
711-
SyncMutexes,
712-
GetEntry,
713-
ApplyTreeAccessLock,
714-
LockEntry
715-
};
713+
SyncMutexes,
714+
GetEntry,
715+
ApplyTreeAccessLock,
716+
LockEntry};
716717

717718
return retVal;
718719
}
@@ -773,9 +774,8 @@ void CCallStackStatsGatherer<STATSTRUCT, CAPTUREDCALLSTACKLENGTH,
773774
pushVal.Stack[i] = NULL;
774775
}
775776

776-
pParentCast->m_SubTreeMutex.Lock();
777+
AUTO_LOCK(pParentCast->m_SubTreeMutex);
777778
pParentCast->m_PushedSubTrees.push_back(pushVal);
778-
pParentCast->m_SubTreeMutex.Unlock();
779779
#endif
780780
}
781781

@@ -801,11 +801,12 @@ void CCallStackStatsGatherer<
801801
STATMUTEXHANDLER, TEMPLATIZEDMEMORYALLOCATOR>::PopSubTree(void *pParent) {
802802
#if defined(ENABLE_STACK_STATS_GATHERING)
803803
ThisCast *pParentCast = (ThisCast *)pParent;
804-
pParentCast->m_SubTreeMutex.Lock();
804+
805805
unsigned long iThreadID = ThreadGetCurrentId();
806806

807-
for (typename PushedSubTreeVector_t::reverse_iterator treeIter =
808-
pParentCast->m_PushedSubTrees.rbegin();
807+
AUTO_LOCK(pParentCast->m_SubTreeMutex);
808+
809+
for (auto treeIter = pParentCast->m_PushedSubTrees.rbegin();
809810
treeIter != pParentCast->m_PushedSubTrees.rend(); ++treeIter) {
810811
if (treeIter->iThreadID == iThreadID) {
811812
++treeIter; //[24.4.1/1] &*(reverse_iterator(i)) == &*(i - 1)
@@ -814,8 +815,6 @@ void CCallStackStatsGatherer<
814815
break;
815816
}
816817
}
817-
818-
pParentCast->m_SubTreeMutex.Unlock();
819818
#endif
820819
}
821820

@@ -833,14 +832,12 @@ void CCallStackStatsGatherer<STATSTRUCT, CAPTUREDCALLSTACKLENGTH,
833832
pParentCast->m_IndexMapMutex.Lock();
834833
pParentCast->m_SubTreeMutex.Lock();
835834

836-
for (typename StoredSubTreeVector_t::iterator treeIter =
837-
pParentCast->m_StoredSubTrees.begin();
835+
for (auto treeIter = pParentCast->m_StoredSubTrees.begin();
838836
treeIter != pParentCast->m_StoredSubTrees.end(); ++treeIter) {
839837
treeIter->pFunctionTable->pfn_SyncMutexes(treeIter->pGatherer, true);
840838
}
841839
} else {
842-
for (typename StoredSubTreeVector_t::iterator treeIter =
843-
pParentCast->m_StoredSubTrees.begin();
840+
for (auto treeIter = pParentCast->m_StoredSubTrees.begin();
844841
treeIter != pParentCast->m_StoredSubTrees.end(); ++treeIter) {
845842
treeIter->pFunctionTable->pfn_SyncMutexes(treeIter->pGatherer, false);
846843
}
@@ -906,15 +903,16 @@ void CCallStackStatsGatherer<STATSTRUCT, CAPTUREDCALLSTACKLENGTH,
906903
TEMPLATIZEDMEMORYALLOCATOR>::Reset(void) {
907904
#if defined(ENABLE_STACK_STATS_GATHERING)
908905
m_StatEntryLock.LockForWrite();
909-
m_IndexMapMutex.Lock();
910-
m_SubTreeMutex.Lock();
911906

912-
m_StatEntries.clear();
913-
m_IndexMap.clear();
914-
m_StoredSubTrees.clear();
907+
{
908+
AUTO_LOCK(m_IndexMapMutex);
909+
AUTO_LOCK(m_SubTreeMutex);
910+
911+
m_StatEntries.clear();
912+
m_IndexMap.clear();
913+
m_StoredSubTrees.clear();
914+
}
915915

916-
m_SubTreeMutex.Unlock();
917-
m_IndexMapMutex.Unlock();
918916
m_StatEntryLock.UnlockWrite();
919917
#endif
920918
}
@@ -953,20 +951,20 @@ class PLATFORM_CLASS CallStackStatStructDescFuncs {
953951

954952
#if 0 // embedded script handling not ready yet
955953
// this is expected to write a piece of script code into the body of a
956-
// function that will merge two values of this type The function will have
957-
// two parameters, "mergeTo", and "mergeFrom". Both are tables with your
958-
// field defined by name So, an example to merge an integer count defined
959-
// as "foo" in the stack struct would look like this "mergeTo.foo +=
960-
// mergeFrom.foo\n"
954+
// function that will merge two values of this type The function will
955+
// have two parameters, "mergeTo", and "mergeFrom". Both are tables with
956+
// your field defined by name So, an example to merge an integer count
957+
// defined as "foo" in the stack struct would look like this "mergeTo.foo
958+
// += mergeFrom.foo\n"
961959
virtual size_t DescribeMergeOperation( MergeScript_Language scriptLanguage, uint8 *pDescribeWriteBuffer, size_t iDescribeMaxLength ) = 0;
962960
#endif
963961

964962
// reserve your description field versions here to avoid stomping others
965963
enum DescribeFieldVersions_t {
966964
DFV_BasicStatStructFieldTypes_t, // Format: 1 byte
967-
// BasicStatStructFieldTypes_t type, 4 byte
968-
// field offset, null terminated string
969-
// field name
965+
// BasicStatStructFieldTypes_t type, 4
966+
// byte field offset, null terminated
967+
// string field name
970968
};
971969

972970
const char *m_szFieldName;
@@ -1033,7 +1031,7 @@ class PLATFORM_CLASS BasicStatStructFieldDesc
10331031
public:
10341032
BasicStatStructFieldDesc(BasicStatStructFieldTypes_t type,
10351033
BasicStatStructFieldCombineMethods_t combineMethod)
1036-
: m_Type(type), m_Combine(combineMethod){};
1034+
: m_Type(type), m_Combine(combineMethod) {};
10371035
size_t DescribeField(uint8 *pDescribeWriteBuffer, size_t iDescribeMaxLength);
10381036
#if 0 // embedded script handling not ready yet
10391037
size_t DescribeMergeOperation( MergeScript_Language scriptLanguage, uint8 *pDescribeWriteBuffer, size_t iDescribeMaxLength );

public/tier0/threadtools.h

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1914,9 +1914,7 @@ class PLATFORM_CLASS CThread {
19141914
ThreadId_t GetThreadID() const { return (ThreadId_t)m_threadId; }
19151915

19161916
#ifdef PLATFORM_WINDOWS
1917-
ThreadHandle_t GetThreadHandle() const {
1918-
return (ThreadHandle_t)m_hThread;
1919-
}
1917+
ThreadHandle_t GetThreadHandle() const { return (ThreadHandle_t)m_hThread; }
19201918

19211919
static unsigned long __stdcall ThreadProc(void *pv);
19221920
typedef unsigned long(__stdcall *ThreadProc_t)(void *);
@@ -2081,18 +2079,22 @@ class CMessageQueue {
20812079
void WaitMessage(T *pMsg) {
20822080
for (;;) {
20832081
while (!MessageWaiting()) SignalEvent.Wait();
2084-
QueueAccessMutex.Lock();
2085-
if (!Head) {
2086-
// multiple readers could make this null
2087-
QueueAccessMutex.Unlock();
2088-
continue;
2082+
2083+
MsgNode *remove_this;
2084+
2085+
{
2086+
AUTO_LOCK(QueueAccessMutex);
2087+
if (!Head) {
2088+
// multiple readers could make this null
2089+
continue;
2090+
}
2091+
*(pMsg) = Head->Data;
2092+
remove_this = Head;
2093+
Head = Head->Next;
2094+
if (!Head) // if empty, fix tail ptr
2095+
Tail = NULL;
20892096
}
2090-
*(pMsg) = Head->Data;
2091-
MsgNode *remove_this = Head;
2092-
Head = Head->Next;
2093-
if (!Head) // if empty, fix tail ptr
2094-
Tail = NULL;
2095-
QueueAccessMutex.Unlock();
2097+
20962098
delete remove_this;
20972099
break;
20982100
}
@@ -2102,7 +2104,8 @@ class CMessageQueue {
21022104
MsgNode *new1 = new MsgNode;
21032105
new1->Data = Msg;
21042106
new1->Next = NULL;
2105-
QueueAccessMutex.Lock();
2107+
2108+
AUTO_LOCK(QueueAccessMutex);
21062109
if (Tail) {
21072110
Tail->Next = new1;
21082111
Tail = new1;
@@ -2111,7 +2114,6 @@ class CMessageQueue {
21112114
Tail = new1;
21122115
}
21132116
SignalEvent.Set();
2114-
QueueAccessMutex.Unlock();
21152117
}
21162118
};
21172119

@@ -2304,21 +2306,19 @@ inline CThreadRWLock::CThreadRWLock()
23042306
m_nPendingReaders(0) {}
23052307

23062308
inline void CThreadRWLock::LockForRead() {
2307-
m_mutex.Lock();
2309+
AUTO_LOCK(m_mutex);
23082310
if (m_nWriters) {
23092311
WaitForRead();
23102312
}
23112313
m_nActiveReaders++;
2312-
m_mutex.Unlock();
23132314
}
23142315

23152316
inline void CThreadRWLock::UnlockRead() {
2316-
m_mutex.Lock();
2317+
AUTO_LOCK(m_mutex);
23172318
m_nActiveReaders--;
23182319
if (m_nActiveReaders == 0 && m_nWriters != 0) {
23192320
m_CanWrite.Set();
23202321
}
2321-
m_mutex.Unlock();
23222322
}
23232323

23242324
//-----------------------------------------------------------------------------

0 commit comments

Comments
 (0)