From f295cd8484f28d634bc6c0c3c33ea77b811a94fd Mon Sep 17 00:00:00 2001 From: Ben Longbons Date: Thu, 15 Sep 2011 04:54:32 +0800 Subject: Prevent copying of MutexLocker MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit There was a bug here, which wouldn't surface if the copy was elided. Fixed by using a move constructor. Reviewed-by: Thorbjørn Lindeijer --- src/utils/mutex.h | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/src/utils/mutex.h b/src/utils/mutex.h index f98bcdfe..92bbd614 100644 --- a/src/utils/mutex.h +++ b/src/utils/mutex.h @@ -53,9 +53,13 @@ class MutexLocker { public: MutexLocker(Mutex *mutex); + MutexLocker(MutexLocker&&); ~MutexLocker(); private: + MutexLocker(const MutexLocker&); // prevent copying + MutexLocker& operator=(const MutexLocker&); + Mutex *mMutex; }; @@ -89,9 +93,15 @@ inline MutexLocker::MutexLocker(Mutex *mutex): mMutex->lock(); } +inline MutexLocker::MutexLocker(MutexLocker&& rhs): + mMutex(rhs.mMutex) +{ + rhs.mMutex = NULL; +} inline MutexLocker::~MutexLocker() { - mMutex->unlock(); + if (mMutex) + mMutex->unlock(); } #endif // MUTEX_H -- cgit v1.2.3-70-g09d2