]> git.ipfire.org Git - thirdparty/kea.git/commitdiff
[2202] Debug-only method Mutex::locked
authorMichal 'vorner' Vaner <michal.vaner@nic.cz>
Wed, 5 Sep 2012 08:14:32 +0000 (10:14 +0200)
committerMichal 'vorner' Vaner <michal.vaner@nic.cz>
Wed, 5 Sep 2012 08:14:32 +0000 (10:14 +0200)
It can be used to check the thing is locked.

src/lib/util/threads/lock.cc
src/lib/util/threads/lock.h
src/lib/util/threads/tests/lock_unittest.cc

index 86c4463aadd91c013dfd72ea4eafb6741978b109..16e59bd04c15c0a860b2113dd1ac64b7002d1710 100644 (file)
@@ -136,6 +136,12 @@ Mutex::unlock() {
     }
 }
 
+// TODO: Disable in non-debug build
+bool
+Mutex::locked() const {
+    return (impl_->locked != 0);
+}
+
 }
 }
 }
index 60ab5db24c37284a3972de8a58afff9695dde8c7..10a7878afa6fb27e469fafd2f923150e7ef5de2d 100644 (file)
@@ -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;
index 19c1074561024a7791d3906fba1201d7ac1def7d..3a5701255c29fcffadcc669a0429e2a423ed19dc 100644 (file)
@@ -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