diff options
Diffstat (limited to 'source/modules/asura-utils/Threads/Mutex.h')
| -rw-r--r-- | source/modules/asura-utils/Threads/Mutex.h | 128 | 
1 files changed, 128 insertions, 0 deletions
diff --git a/source/modules/asura-utils/Threads/Mutex.h b/source/modules/asura-utils/Threads/Mutex.h new file mode 100644 index 0000000..4269c05 --- /dev/null +++ b/source/modules/asura-utils/Threads/Mutex.h @@ -0,0 +1,128 @@ +#ifndef _ASURA_MUTEX_H_ +#define _ASURA_MUTEX_H_ + +#include <asura-utils/Type.h> +#include <asura-utils/Classes.h> + +#include "../UtilsConfig.h" + +#if ASURA_THREAD_WIN32 +#include <windows.h> +#endif + +namespace_begin(AsuraEngine) +namespace_begin(Threads) + +#define ASURA_MUTEX_MAXWAIT (~(uint32)0) + +class MutexImpl; + +class Mutex +{ +public: + +	Mutex(); +	~Mutex(); + +	void Lock(); +	void Unlock(); + +private: + +	// ֹ +	Mutex(const Mutex&); +	Mutex& operator=(const Mutex&); + +	MutexImpl* m_Impl; + +}; + +class _mutex_locker +{ +public: +	_mutex_locker(Mutex& mutex) +		: m(mutex) +	{ +		m.Lock(); +	}; +	~_mutex_locker() +	{ +		m.Unlock(); +	} +	operator bool() { return false; }; +private:  +	void* operator new(size_t); +	Mutex& m; +}; + +#define lock(m) \ +if(_mutex_locker _asura_mutex_locker = m){} else + +ASURA_ABSTRACT class MutexImpl +{ +public: + +	MutexImpl() {}; +	virtual ~MutexImpl() {}; + +	virtual void Lock() = 0;  +	virtual void Unlock() = 0; + +}; + +#if ASURA_MUTEX_WIN32_CRITICLE_SECTION + +//https://blog.csdn.net/l799623787/article/details/18259949 +class MutexImplWin32_CS ASURA_FINAL : public MutexImpl +{ +public: + +	MutexImplWin32_CS(); +	~MutexImplWin32_CS(); + +	void Lock() override; +	void Unlock() override; + +private: + +	//HANDLE m_Handle; +	CRITICAL_SECTION m_Mutex; + +}; + +#endif // ASURA_MUTEX_WIN32_CRITICLE_SECTION + +#if ASURA_MUTEX_WIN32_KERNAL_MUTEX + +class MutexImplWin32_KM ASURA_FINAL : public MutexImpl +{ +public: + +	MutexImplWin32_KM(); +	~MutexImplWin32_KM(); + +	void Lock() override; +	void Unlock() override; + +private: + +	HANDLE m_Handle; + +}; + +#endif // ASURA_MUTEX_WIN32_KERNAL_MUTEX + +#if ASURA_THREAD_STD  + +class MutexImplSTD ASURA_FINAL : public MutexImpl +{ +}; + +#endif // ASURA_THREAD_STD  + +namespace_end +namespace_end + +namespace AEThreading = AsuraEngine::Threads; + +#endif
\ No newline at end of file  | 
