⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 _synchobj.h

📁 http代理程序
💻 H
字号:


/************************************
  REVISION LOG ENTRY
  Revision By: Alex Turc ( alex_turc@hotmail.com )
  Revised on 6/9/00 12:15:45 AM
  Comments: Synchronization objects
 ************************************/

#ifndef ___synchobj_h__
#define ___synchobj_h__

#include "winbase.h"
#include "_exception.h"
#include "_synchop.h"

namespace extension
{

//////////////
// Exceptions
//////////////

/*
Generic synchronization exception
*/
class synchobj_exception :
	public extended_exception
{
public:

	synchobj_exception( HRESULT nResult, char* pDescription = "Synchronization Object Exception", char* pFile = NULL, long nLine = 0 ) :
		extended_exception( nResult, pDescription, pFile, nLine )
	{	
	}
};

/*
Mutex class. Uses Win32 Mutex object.
*/
template< class L >
class mutex :
	protected L
{
public:

	// Generic lock for this sybchronization object
 	typedef generic_lock< mutex< L > > generic_lock;

	
	// Function name	: mutex
	// Description	    : 
	// Return type		: 
	// Argument         : LPCTSTR szName = NULL
	mutex( LPCTSTR szName = NULL )
		throw( synchobj_exception )
	{
		HANDLE hMutex = ::CreateMutex( NULL, FALSE, szName );
		if( hMutex == NULL )
			throw_exception( synchobj_exception, ::GetLastError(), "Error occured when creating the mutex" );
		
		operator()( hMutex );
	}

	// Function name	: mutex
	// Description	    : Already constructed mutex
	// Return type		: 
	// Argument         : HANDLE& hMutex
	mutex( HANDLE& hMutex )
		throw( synchobj_exception )
	{
		operator()( hMutex );
	}

	
	// Function name	: lock
	// Description	    : 
	// Return type		: void 
	void lock()
	{
		::WaitForSingleObject( m_Object, INFINITE );
	}

	// Unlock method
	void unlock()
	{
		::ReleaseMutex( m_Object );
	}
};

/*
Manager for mutex object
*/
struct _mutex_manager :
	public _object_manager< HANDLE >
{

	// Function name	: init_object
	// Description	    : Init the managed object
	// Return type		: void 
	// Argument         : managed_type& a
	void init_object( managed_type& a )
	{
		a = NULL;
	}

	// Function name	: delete_object
	// Description	    : Deletes the pointer
	// Return type		: void 
	// Argument         : managed_type& a
	void delete_object( managed_type& a )
	{
		if( a != NULL )
		{
			::CloseHandle( a );
			a = NULL;
		}
	}
};


// Smart mutex
typedef mutex< reference_counted_object< HANDLE, _mutex_manager > > s_mutex;
// Automatic mutex
typedef mutex< automatic_object< HANDLE, _mutex_manager > > a_mutex;



/*
Fake mutex. If required as template parameter but not needed as implementation
*/
class fake_mutex
{
public:

	// Generic lock for this sybchronization object
	typedef generic_lock< fake_mutex > generic_lock;

	// Lock
	void lock()
	{
	}

	// Unlock
	void unlock()
	{
	}
};

/*
Critical Section class. Uses Win32 critical section object.
*/
class critical_section
{
public:

	// Generic lock for this sybchronization object
	typedef generic_lock< critical_section > generic_lock;	

	// Function name	: critical_section
	// Description	    : Constructor for a critical section object
	// Return type		: 
	critical_section()
	{
		::InitializeCriticalSection( &m_hCriticalSection );		
	}

	// Function name	: ~critical_section
	// Description	    : Destructor
	// Return type		: virtual 
	virtual ~critical_section()
	{
		::DeleteCriticalSection( &m_hCriticalSection );
	}

	

	// Function name	: lock
	// Description	    : 
	// Return type		: void 
	void lock()
	{
		::EnterCriticalSection( &m_hCriticalSection );
	}

	

	// Function name	: unlock
	// Description	    : 
	// Return type		: void 
	void unlock()
	{
		::LeaveCriticalSection( &m_hCriticalSection );
	}

private:

	// Disable some operations
	critical_section( const critical_section& );
	critical_section& operator=( const critical_section& m );

	// Win32 critical section object
	CRITICAL_SECTION m_hCriticalSection;
};

/*
Fake critical section. If required as template parameter but not needed as implementation
*/
class fake_critical_section
{
public:

	// Generic lock for this sybchronization object
	typedef generic_lock< fake_critical_section > generic_lock;

	// Constructor for a fake critical section
	fake_critical_section()
	{
	}

	

	// Function name	: virtual ~fake_critical_section
	// Description	    : 
	// Return type		: 
	virtual ~fake_critical_section()
	{
	}



	// Function name	: lock
	// Description	    : 
	// Return type		: void 
	void lock()
	{
	}


	// Function name	: unlock
	// Description	    : 
	// Return type		: void 
	void unlock()
	{
	}

private:

	// Disable some operations
	fake_critical_section( const fake_critical_section& );
	fake_critical_section& operator=( const fake_critical_section&);
};

} // Namespace extension

#endif // ___synchobj_h__

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -