]> git.ipfire.org Git - thirdparty/kea.git/commitdiff
[2198] Add Mutex::tryLock() and make lock methods public
authorMukund Sivaraman <muks@isc.org>
Sun, 21 Oct 2012 23:20:00 +0000 (04:50 +0530)
committerMukund Sivaraman <muks@isc.org>
Sun, 21 Oct 2012 23:44:36 +0000 (05:14 +0530)
tryLock() is necessary inside InterprocessSyncFile. It also doesn't
make sense afterwards to make a dynamically constructed Mutex::Locker
that lives after the basic block is exited. So lock() and unlock()
have also been made public.

I've added comments to discourage the use of these methods directly
and use the Mutex::Locker where applicable.

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

index c98a7a6b060d225eecdf68f24d033c3d12c71fd2..db3cc33fb4849205d61be2365908c1e332636f02 100644 (file)
@@ -128,6 +128,17 @@ Mutex::lock() {
     postLockAction();           // Only in debug mode
 }
 
+bool
+Mutex::tryLock() {
+    assert(impl_ != NULL);
+    const int result = pthread_mutex_trylock(&impl_->mutex);
+    if (result != 0) {
+        return (false);
+    }
+    postLockAction();           // Only in debug mode
+    return (true);
+}
+
 void
 Mutex::preUnlockAction(bool throw_ok) {
     if (impl_->locked_count == 0) {
index ff56999f99e78fdc6425044323dc51dd5b31c425..642576fa87cf31e7ab25cf712a51b540bcca44a9 100644 (file)
@@ -107,6 +107,32 @@ public:
     ///
     /// \todo Disable in non-debug build
     bool locked() const;
+
+    /// \brief Lock the mutex
+    ///
+    /// This method blocks until the mutex can be locked.
+    ///
+    /// Please consider not using this method directly and instead using
+    /// a Mutex::Locker object instead.
+    void lock();
+
+    /// \brief Try to lock the mutex
+    ///
+    /// This method doesn't block and returns immediately with a status
+    /// on whether the lock operation was successful.
+    ///
+    /// Please consider not using this method directly and instead using
+    /// a Mutex::Locker object instead.
+    ///
+    /// \return true if the lock was successful, false otherwise.
+    bool tryLock();
+
+    /// \brief Unlock the mutex
+    ///
+    /// Please consider not using this method directly and instead using
+    /// a Mutex::Locker object instead.
+    void unlock();
+
 private:
     friend class CondVar;
 
@@ -125,8 +151,6 @@ private:
 
     class Impl;
     Impl* impl_;
-    void lock();
-    void unlock();
 };
 
 /// \brief Encapsulation for a condition variable.
index 9c17e6fc65e83d2913f4da47bb11e07b03cc6e64..9deddd7fcff7e93deb198212309bdeced051833f 100644 (file)
@@ -26,6 +26,24 @@ using namespace isc::util::thread;
 
 namespace {
 
+TEST(MutexTest, direct) {
+    Mutex mutex;
+    EXPECT_FALSE(mutex.locked()); // Debug-only build
+
+    mutex.lock();
+    EXPECT_TRUE(mutex.locked()); // Debug-only build
+
+    EXPECT_FALSE(mutex.tryLock());
+
+    mutex.unlock();
+    EXPECT_FALSE(mutex.locked()); // Debug-only build
+
+    EXPECT_TRUE(mutex.tryLock());
+
+    mutex.unlock();
+    EXPECT_FALSE(mutex.locked()); // Debug-only build
+}
+
 // If we try to lock the debug mutex multiple times, it should throw.
 TEST(MutexTest, lockMultiple) {
     // TODO: Once we support non-debug mutexes, disable the test if we compile