From 8044b1d7ce73cf5c790dcb8e9fb5b587bedbc843 Mon Sep 17 00:00:00 2001 From: olsner Date: Tue, 25 Nov 2003 02:11:06 +0000 Subject: [PATCH] Some thread utility classes - CLocker and CScopeLock, both using pthread_mutex_(un)lock to do their stuff This was SVN commit r83. --- source/ps/ThreadUtil.h | 128 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 128 insertions(+) create mode 100755 source/ps/ThreadUtil.h diff --git a/source/ps/ThreadUtil.h b/source/ps/ThreadUtil.h new file mode 100755 index 0000000000..a5398aef2c --- /dev/null +++ b/source/ps/ThreadUtil.h @@ -0,0 +1,128 @@ +/* +ThreadUtil.h - Thread Utility Functions +by Simon Brenner +simon.brenner@home.se + +--Overview-- + +Contains three classes: CMutex, CRWLock and CLocker. +CMutex is a Mutual Exclusion lock that can be locked and unlocked. The Lock +function waits for the current lock holder (if any) to release the lock and +then acquires it. + +CRWLock is a lock where any number of readers *or* one writer can hold the lock. + +CLocker is a generic wrapper class that can wrap any data class with any locker +class. + +--Example-- + +CMutex usage: + + CMutex protectData; + + void function() + { + protectData.Lock(); + .. do stuff with data .. + protectData.Unlock(); + } + +CLocker usage: + + class CDataClass {}; + CLocker instance; + + void function() + { + instance.Lock(); + .. do stuff with instance .. + instance.Unlock(); + } + +CLocker usage 2: + + class CDataClass {}; + class CCustomLockerClass { void MyOwnLock(); void MyOwnUnlock(); }; + + CLocker instance; + + void function() + { + instance.MyOwnLock(); + .. do stuff with instance .. + instance.MyOwnUnlock(); + } + +--More Info-- + + +*/ + +#ifndef _ThreadUtil_h +#define _ThreadUtil_h + +//-------------------------------------------------------- +// Includes / Compiler directives +//-------------------------------------------------------- + +#include "posix.h" + +//------------------------------------------------- +// Types +//------------------------------------------------- + +//------------------------------------------------- +// Declarations +//------------------------------------------------- + +// CScopeLock +// ---------------------------------------------------------------------| Class +// Locks a CMutex over the objects lifetime +class CScopeLock +{ +public: + inline CScopeLock(pthread_mutex_t &mutex): m_Mutex(mutex) + { + pthread_mutex_lock(&m_Mutex); + } + inline ~CScopeLock() + { + pthread_mutex_unlock(&m_Mutex); + } + +private: + pthread_mutex_t &m_Mutex; +}; + +// CLocker +// ---------------------------------------------------------------------| Class +// This will not give access to the wrapped class constructors directly, +// to use them you have to do CLocker(CWrappedClass(args)) + +template +struct CLocker: public _T +{ + inline CLocker() + {} + + inline CLocker(const _T &arg): _T(arg) + {} + + inline CLocker(_T &arg): _T(arg) + {} + + inline void Lock() + { + pthread_mutex_lock(&m_Mutex); + } + + inline void Unlock() + { + pthread_mutex_unlock(&m_Mutex); + } + + pthread_mutex_t m_Mutex; +}; + +#endif