aboutsummaryrefslogtreecommitdiff
path: root/sp/src/utils/vmpi/threadhelpers.h
blob: bfba74f3aaf33f30e1b997cbb94f2bbc4875798b (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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose: 
//
// $NoKeywords: $
//=============================================================================//

#ifndef THREADHELPERS_H
#define THREADHELPERS_H
#ifdef _WIN32
#pragma once
#endif


#include "tier1/utllinkedlist.h"


#define SIZEOF_CS	24	// sizeof( CRITICAL_SECTION )


class CCriticalSection
{
public:
			CCriticalSection();
			~CCriticalSection();


protected:

	friend class CCriticalSectionLock;
	
	void	Lock();
	void	Unlock();


public:
	char	m_CS[SIZEOF_CS];

	// Used to protect against deadlock in debug mode.
//#if defined( _DEBUG )
	CUtlLinkedList<unsigned long,int>	m_Locks;
	char								m_DeadlockProtect[SIZEOF_CS];
//#endif
};


// Use this to lock a critical section.
class CCriticalSectionLock
{
public:
			CCriticalSectionLock( CCriticalSection *pCS );
			~CCriticalSectionLock();
	void	Lock();
	void	Unlock();

private:
	CCriticalSection	*m_pCS;
	bool				m_bLocked;
};


template< class T >
class CCriticalSectionData : private CCriticalSection
{
public:
	// You only have access to the data between Lock() and Unlock().
	T*		Lock()
	{
		CCriticalSection::Lock();
		return &m_Data;
	}
	
	void	Unlock()
	{
		CCriticalSection::Unlock();
	}

private:
	T m_Data;
};



// ------------------------------------------------------------------------------------------------ //
// CEvent.
// ------------------------------------------------------------------------------------------------ //
class CEvent
{
public:
	CEvent();
	~CEvent();

	bool Init( bool bManualReset, bool bInitialState );
	void Term();
	
	void* GetEventHandle() const;

	// Signal the event.
	bool SetEvent();

	// Unset the event's signalled status.
	bool ResetEvent();


private:
	void *m_hEvent;
};


#endif // THREADHELPERS_H