summaryrefslogblamecommitdiffstats
path: root/source/cCriticalSection.h
blob: 8faa04765d7f6b9c63ddba462d89c74a0190e0f4 (plain) (tree)
1
2
3
4
5
6
7
8
9
10
 

             



 


                       
                                

                             


                           
         






                                                                                    









                                                                                 
                                                                                                                                        

                                                                                                                  
                         


                                          
                                          






















                                                                                 

#pragma once





class cCriticalSection
{
public:
	cCriticalSection(void);
	~cCriticalSection();

	void Lock(void);
	void Unlock(void);
	
private:

	#ifdef _WIN32
		CRITICAL_SECTION m_CriticalSection;
	#else  // _WIN32
		void* m_CriticalSectionPtr; // Pointer to a CRITICAL_SECTION object
		void* m_Attributes;
	#endif  // else _WIN32
};




/// RAII for cCriticalSection - locks the CS on creation, unlocks on destruction
class cCSLock
{
	cCriticalSection * m_CS;

	// Unlike a cCriticalSection, this object should be used from a single thread, therefore access to m_IsLocked is not threadsafe
	// In Windows, it is an error to call cCriticalSection::Unlock() multiple times if the lock is not held, 
	// therefore we need to check this value whether we are locked or not.
	bool m_IsLocked;
	
public:
	cCSLock(cCriticalSection * a_CS);
	cCSLock(cCriticalSection & a_CS);
	~cCSLock();
	
	// Temporarily unlock or re-lock:
	void Lock(void);
	void Unlock(void);
} ;





/// Temporary RAII unlock for a cCSLock. Useful for unlock-wait-relock scenarios
class cCSUnlock
{
	cCSLock & m_Lock;
public:
	cCSUnlock(cCSLock & a_Lock);
	~cCSUnlock();
} ;