blob: 89164db12cf206639c2b3645faae7952781ebb14 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
|
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose:
//
//=============================================================================
#ifndef THREADSAFEREFCOUNTEDOBJECT_H
#define THREADSAFEREFCOUNTEDOBJECT_H
#ifdef _WIN32
#pragma once
#endif
// This class can be used for fast access to an object from multiple threads,
// and the main thread can wait until the threads are done using the object before it frees the object.
template< class T >
class CThreadSafeRefCountedObject
{
public:
CThreadSafeRefCountedObject( T initVal )
{
m_RefCount = 0;
m_pObject = initVal;
m_RefCount = 0;
}
void Init( T pObj )
{
Assert( ThreadInMainThread() );
Assert( !m_pObject );
m_RefCount = 0;
m_pObject = pObj;
m_RefCount = 1;
}
// Threads that access the object need to use AddRef/Release to access it.
T AddRef()
{
if ( ++m_RefCount > 1 )
{
return m_pObject;
}
else
{
// If the refcount was 0 when we called this, then the whitelist is about to be freed.
--m_RefCount;
return NULL;
}
}
void ReleaseRef( T pObj )
{
if ( --m_RefCount >= 1 )
{
Assert( m_pObject == pObj );
}
}
// The main thread can use this to access the object, since only it can Init() and Free() the object.
T GetInMainThread()
{
Assert( ThreadInMainThread() );
return m_pObject;
}
// The main thread calls this after it has released its last reference to the object.
void ResetWhenNoRemainingReferences( T newValue )
{
Assert( ThreadInMainThread() );
// Wait until we can free it.
while ( m_RefCount > 0 )
{
CThread::Sleep( 20 );
}
m_pObject = newValue;
}
private:
CInterlockedIntT<long> m_RefCount;
T m_pObject;
};
#endif // THREADSAFEREFCOUNTEDOBJECT_H
|