From: Michal 'vorner' Vaner Date: Wed, 5 Sep 2012 08:14:32 +0000 (+0200) Subject: [2202] Debug-only method Mutex::locked X-Git-Tag: trac2402_base~79^2~18 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=7cd33029c63a0cd0889dba94709cf6cadaea48ab;p=thirdparty%2Fkea.git [2202] Debug-only method Mutex::locked It can be used to check the thing is locked. --- diff --git a/src/lib/util/threads/lock.cc b/src/lib/util/threads/lock.cc index 86c4463aad..16e59bd04c 100644 --- a/src/lib/util/threads/lock.cc +++ b/src/lib/util/threads/lock.cc @@ -136,6 +136,12 @@ Mutex::unlock() { } } +// TODO: Disable in non-debug build +bool +Mutex::locked() const { + return (impl_->locked != 0); +} + } } } diff --git a/src/lib/util/threads/lock.h b/src/lib/util/threads/lock.h index 60ab5db24c..10a7878afa 100644 --- a/src/lib/util/threads/lock.h +++ b/src/lib/util/threads/lock.h @@ -115,6 +115,14 @@ public: private: Mutex* mutex_; }; + /// \brief If the mutex is currently locked + /// + /// This is debug aiding method only. And it might be unavailable in + /// non-debug build (because keeping the state might be needlesly + /// slow). + /// + /// \todo Disable in non-debug build + bool locked() const; private: friend class Locker; struct Impl; diff --git a/src/lib/util/threads/tests/lock_unittest.cc b/src/lib/util/threads/tests/lock_unittest.cc index 19c1074561..3a5701255c 100644 --- a/src/lib/util/threads/tests/lock_unittest.cc +++ b/src/lib/util/threads/tests/lock_unittest.cc @@ -27,7 +27,9 @@ namespace { // Test a recursive mutex can be locked multiple times TEST(MutexTest, recursiveLockMultiple) { Mutex mutex(true); + EXPECT_FALSE(mutex.locked()); // Debug-only build Mutex::Locker l1(mutex); + EXPECT_TRUE(mutex.locked()); // Debug-only build Mutex::Locker l2(mutex); Mutex::Locker l3(mutex); Mutex::Locker l4(mutex); @@ -39,10 +41,13 @@ TEST(MutexTest, lockMultiple) { // TODO: Once we support non-debug mutexes, disable the test if we compile // with them. Mutex mutex; + EXPECT_FALSE(mutex.locked()); // Debug-only build Mutex::Locker l1(mutex); + EXPECT_TRUE(mutex.locked()); // Debug-only build EXPECT_THROW({ Mutex::Locker l2(mutex); // Attempt to lock again. }, isc::InvalidOperation); + EXPECT_TRUE(mutex.locked()); // Debug-only build } // Destroying a locked mutex is a bad idea as well