From dc0f89b7e9bae8486771088e88b574c567b0dddc Mon Sep 17 00:00:00 2001 From: fonsinchen Date: Sun, 16 Feb 2014 16:24:41 +0000 Subject: (svn r26342) -Add: A mutex locker class. --- src/thread/thread.h | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) (limited to 'src/thread/thread.h') diff --git a/src/thread/thread.h b/src/thread/thread.h index edb7d474e..a98dfb223 100644 --- a/src/thread/thread.h +++ b/src/thread/thread.h @@ -88,6 +88,28 @@ public: virtual void SendSignal() = 0; }; +/** + * Simple mutex locker to keep a mutex locked until the locker goes out of scope. + */ +class ThreadMutexLocker { +public: + /** + * Lock the mutex and keep it locked for the life time of this object. + * @param mutex Mutex to be locked. + */ + ThreadMutexLocker(ThreadMutex *mutex) : mutex(mutex) { mutex->BeginCritical(); } + + /** + * Unlock the mutex. + */ + ~ThreadMutexLocker() { this->mutex->EndCritical(); } + +private: + ThreadMutexLocker(const ThreadMutexLocker &) { NOT_REACHED(); } + ThreadMutexLocker &operator=(const ThreadMutexLocker &) { NOT_REACHED(); return *this; } + ThreadMutex *mutex; +}; + /** * Get number of processor cores in the system, including HyperThreading or similar. * @return Total number of processor cores. -- cgit v1.2.3-70-g09d2