|
|
//----------------------------------------------------------------------------
// Laser Guard Ltd.
// Copyright � 2000-2002. All Rights Reserved.
// Version 1.0 01/01/2000
// Last modification: 08/10/2003
// PROJECT:
// SUBSYSTEM:
// FILE: LgLock.h
// AUTHOR: Sergey Chizhevskiy
// OVERVIEW
// ~~~~~~~~
// Header file of LgLock.h
//----------------------------------------------------------------------------
#ifndef __LG__LOCK__H__INCLUDED__
#define __LG__LOCK__H__INCLUDED__
class CLgCriticalSection{public: CLgCriticalSection() { memset( &m_CriticalSection, 0, sizeof(m_CriticalSection) ); ::InitializeCriticalSection( &m_CriticalSection ); } // have to override to prevent section spreading
CLgCriticalSection( CLgCriticalSection& ) { memset( &m_CriticalSection, 0, sizeof(m_CriticalSection) ); ::InitializeCriticalSection( &m_CriticalSection ); } ~CLgCriticalSection() { ::DeleteCriticalSection( &m_CriticalSection ); memset( &m_CriticalSection, 0, sizeof(m_CriticalSection) ); }
public: // have to override to prevent section spreading
CLgCriticalSection& operator=( CLgCriticalSection& ) { return *this; }
public: void Lock() { ::EnterCriticalSection( &m_CriticalSection ); } void Unlock() { ::LeaveCriticalSection( &m_CriticalSection ); }
private: CRITICAL_SECTION m_CriticalSection;};
class CLgMutex{public: CLgMutex( BOOL bInitiallyOwn = FALSE, LPCTSTR lpszName = NULL ) { m_hMutex = NULL; Create( bInitiallyOwn, lpszName ); } ~CLgMutex() { Close(); }
public: BOOL Create( BOOL bInitiallyOwn, LPCTSTR lpszName ) { Close(); return ( m_hMutex = ::CreateMutex( NULL, bInitiallyOwn, lpszName ) ) != NULL; }
BOOL Open( DWORD dwDesiredAccess, BOOL bInheritHandle, LPCTSTR lpName ) { Close(); return ( m_hMutex = ::OpenMutex( dwDesiredAccess, bInheritHandle, lpName ) ) != NULL; }
BOOL Release() { if( !m_hMutex ) return FALSE;
return ::ReleaseMutex( m_hMutex ); }
void Close() { if( m_hMutex ) { ::CloseHandle( m_hMutex ); m_hMutex = NULL; } }
public: operator HANDLE() { return m_hMutex; }
private: HANDLE m_hMutex;};
class CLgEvent{public: CLgEvent( BOOL bManualReset = FALSE, BOOL bInitialState = FALSE, LPCTSTR lpName = NULL ) { m_hEvent = NULL; Create( bManualReset, bInitialState, lpName ); } // have to override to prevent section spreading
CLgEvent( CLgEvent& ) { m_hEvent = NULL; Create( FALSE, FALSE, NULL ); } ~CLgEvent() { Close(); }
public: // have to override to prevent section spreading
CLgEvent& operator=( CLgEvent& ) { return *this; }
public: BOOL Create( BOOL bManualReset, BOOL bInitialState, LPCTSTR lpName ) { Close(); return (m_hEvent = ::CreateEvent( NULL, bManualReset, bInitialState, lpName )) != NULL; }
BOOL Open( DWORD dwDesiredAccess, BOOL bInheritHandle, LPCTSTR lpName ) { Close(); return (m_hEvent = ::OpenEvent( dwDesiredAccess, bInheritHandle, lpName )) != NULL; }
void Close() { if( m_hEvent ) ::CloseHandle( m_hEvent ); }
BOOL Set() { if( m_hEvent ) return ::SetEvent( m_hEvent );
return FALSE; }
BOOL Pulse() { if( m_hEvent ) return ::PulseEvent( m_hEvent );
return FALSE; }
BOOL Reset() { if( m_hEvent ) return ::ResetEvent( m_hEvent ); return FALSE; }
public: operator HANDLE() { return m_hEvent; }
private: HANDLE m_hEvent;};
class CLgLocker{public: CLgLocker( CLgCriticalSection * pCriticalSection ) { m_pCriticalSection = pCriticalSection; m_pCriticalSection->Lock(); } ~CLgLocker() { m_pCriticalSection->Unlock(); }
private: CLgCriticalSection *m_pCriticalSection;};
#endif
|