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

📄 sync_wrapper.hpp

📁 比STL和BOOST更强大的智能指针库!!! 私藏很久的老底与大家一起分享了.
💻 HPP
字号:
#ifndef SYNC_WRAPPER_H_BY_DAVID_MAISONAVE_HEADER_GUARD_
#define SYNC_WRAPPER_H_BY_DAVID_MAISONAVE_HEADER_GUARD_

/*!
@class mutex_wrapper
@brief mutex_wrapper is a class that implements intrusive lock functions for target type.
In order to use the intrusive_lock_policy, the target type requires two intrusive
lock functions.\n
void intrusive_ptr_lock(T * p);\n
void intrusive_ptr_unlock(T * p);\n
Moreover, the target type  also needs to store lock handle(s) and/or lock state.
The mutex_wrapper class can be used to automatically add the required lock implementation
to the target type, and therefore allow for the use of a synchronized smart pointer 
on any type.
\n
<b>Example Usage</b>
@include ../examples/example_intrusive_lock_policy_usage.hxx
\n
@see smart_ptr, intrusive_lock_policy, scope_lock
\n
@note
Although the mutex_wrapper is used with intrusive_lock_policy, it can be
used with ownership policy types that have ref_count_policy or
ref_link_policy as the sub policy.
*/
#include <stdlib.h>

#ifdef _WIN32
template<class T>
class mutex_wrapper : public T
{
	HANDLE m_hndl;
	inline void lock(){WaitForSingleObject(m_hndl, INFINITE);}
	inline void unlock(){ReleaseMutex(m_hndl);}
	mutex_wrapper& operator=(const mutex_wrapper&);
public:
	inline mutex_wrapper():m_hndl(CreateMutex(NULL, FALSE, NULL)){}
	template<class T1> mutex_wrapper(T1& t1):T(t1), m_hndl(CreateMutex(NULL, FALSE, NULL)){}
	template<class T1, class T2> mutex_wrapper(T1& t1, T2& t2):T(t1, t2), m_hndl(CreateMutex(NULL, FALSE, NULL)){}
	template<class T1, class T2, class T3> mutex_wrapper(T1& t1, T2& t2, T3& t3):T(t1, t2, t3), m_hndl(CreateMutex(NULL, FALSE, NULL)){}
	inline ~mutex_wrapper()
	{
		ReleaseMutex(m_hndl);
		CloseHandle(m_hndl);
	}
	inline friend void intrusive_ptr_lock(mutex_wrapper<T> *p){p->lock();}
	inline friend void intrusive_ptr_unlock(mutex_wrapper<T> *p){p->unlock();}
};

template<class T>
class critical_sect_wrapper : public T
{
	CRITICAL_SECTION m_cs;
	inline void lock(){EnterCriticalSection(&m_cs);}
	inline void unlock(){LeaveCriticalSection(&m_cs);}
	critical_sect_wrapper& operator=(const critical_sect_wrapper&);
public:
	inline critical_sect_wrapper(){InitializeCriticalSection(&m_cs);}
	template<class T1> critical_sect_wrapper(T1& t1):T(t1){InitializeCriticalSection(&m_cs);}
	template<class T1, class T2> critical_sect_wrapper(T1& t1, T2& t2):T(t1, t2){InitializeCriticalSection(&m_cs);}
	template<class T1, class T2, class T3> critical_sect_wrapper(T1& t1, T2& t2, T3& t3):T(t1, t2, t3){InitializeCriticalSection(&m_cs);}
	inline ~critical_sect_wrapper(){DeleteCriticalSection(&m_cs);}
	inline friend void intrusive_ptr_lock(critical_sect_wrapper *p){p->lock();}
	inline friend void intrusive_ptr_unlock(critical_sect_wrapper *p){p->unlock();}
};

template<class T>
class fast_mutex_wrapper : public T
{
	LONG volatile  locker;
	void lock()
	{
		// check if another thread already locked
		while (InterlockedIncrement(&locker) > 1)
		{
			InterlockedDecrement(&locker);
			Sleep(rand()%3);   // sleep 0 to 2 milliseconds before next attempt
		}
	}      
	inline void unlock(){assert (locker > 0);InterlockedDecrement(&locker);}
	fast_mutex_wrapper& operator=(const fast_mutex_wrapper&);
public:
	inline fast_mutex_wrapper():locker(0){}
	template<class T1> fast_mutex_wrapper(T1& t1):T(t1), locker(0){}
	template<class T1, class T2> fast_mutex_wrapper(T1& t1, T2& t2):T(t1, t2), locker(0){}
	template<class T1, class T2, class T3> fast_mutex_wrapper(T1& t1, T2& t2, T3& t3):T(t1, t2, t3), locker(0){}
	inline friend void intrusive_ptr_lock(fast_mutex_wrapper *p){p->lock();}
	inline friend void intrusive_ptr_unlock(fast_mutex_wrapper *p){p->unlock();}
};

#else
#ifdef __POSIX__
template<class T>
class mutex_wrapper : public T
{
	pthread_mutex_t m_mutex;
	inline void lock(){pthread_mutex_lock(&m_mutex);}
	inline void unlock(){pthread_mutex_unlock(&m_mutex);}
	mutex_wrapper& operator=(const mutex_wrapper&);
public:
	inline mutex_wrapper():m_mutex(PTHREAD_MUTEX_INITIALIZER){}
	template<class T1> mutex_wrapper(T1& t1):T(t1), m_mutex(PTHREAD_MUTEX_INITIALIZER){}
	template<class T1, class T2> mutex_wrapper(T1& t1, T2& t2):T(t1, t2), m_mutex(PTHREAD_MUTEX_INITIALIZER){}
	template<class T1, class T2, class T3> mutex_wrapper(T1& t1, T2& t2, T3& t3):T(t1, t2, t3), m_mutex(PTHREAD_MUTEX_INITIALIZER){}
	inline ~mutex_wrapper(){pthread_mutex_destroy(&m_mutex);}
	inline friend void intrusive_ptr_lock(mutex_wrapper *p){p->lock();}
	inline friend void intrusive_ptr_unlock(mutex_wrapper *p){p->unlock();}
};
#endif //__POSIX__
#endif //_WIN32

template <typename TT> void unlock_function_for_scope_lock(TT *  ptr) {
	if (!ptr) return;
	ptr->unlock();
} 

/*!
@class scope_lock
@brief The scope_lock can be used with lock policy to lock a pointer within a scope.
\n
<b>Example Usage</b>
@include ../examples/example_intrusive_lock_policy_usage.hxx
\n
@see smart_ptr, mutex_wrapper
\n
@note
When scope_lock goes out of scope, it's destructor is called, and it unlocks 
the smart_ptr.
*/
class scope_lock
{
	typedef void (*unlock_fct_Type ) (void *);
	unlock_fct_Type m_unlock_fct;
	void *m_type;
public:
	template<class T> scope_lock(T& smrtPtr, bool LockIt = true):m_type((void*)&smrtPtr)
	{
		if (LockIt) smrtPtr.lock();
		void ( *tmp ) (T *) = unlock_function_for_scope_lock<T>;
		m_unlock_fct = (unlock_fct_Type)tmp;
	}
	~scope_lock()
	{
		m_unlock_fct(m_type);
	}
};


/*! @page  Revision_History Revision History
\verbatim
$Date: 3/05/06 11:03a $	(Last Update)
$Revision: 3 $			(Last Revision)
$History: sync_wrapper.hpp $
 * 
 * *****************  Version 3  *****************
 * User: Axter        Date: 3/05/06    Time: 11:03a
 * Updated in $/SmartPointers/Smartptr
 * 
 * *****************  Version 2  *****************
 * User: Axter        Date: 2/27/06    Time: 10:13p
 * Updated in $/SmartPointers/smartptr
 * 
 * *****************  Version 1  *****************
 * User: Administrator Date: 2/27/06    Time: 7:04a
 * Created in $/SmartPointers/smartptr
\endverbatim 
*/

#endif //SYNC_WRAPPER_H_BY_DAVID_MAISONAVE_HEADER_GUARD_

⌨️ 快捷键说明

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