BIND 10 trac2198, updated. 9557da6ee10d028954c054d719071753ddd0266d [2198] Throw if unlocking fails

BIND 10 source code commits bind10-changes at lists.isc.org
Tue Sep 18 05:12:52 UTC 2012


The branch, trac2198 has been updated
       via  9557da6ee10d028954c054d719071753ddd0266d (commit)
       via  89fa65e6b7bd3ccc9ac9fd2e4a3f0c93ff7bd204 (commit)
       via  107c6ee0d6462a638211b9048ab5ea5bf31418a5 (commit)
       via  7f9ba74e8dd4b00e008021080f71ec674be79ba4 (commit)
       via  fe3e1576efeb6061d853fe23cf496c3b9da889e6 (commit)
       via  49df6ef0a4202d8303adf1d2ca0c99f43b5f6b42 (commit)
      from  2e2ddb0e28f6a5b8ff263bec7a0fda0eb38730df (commit)

Those revisions listed above that are new to this repository have
not appeared on any other notification email; so we list those
revisions in full, below.

- Log -----------------------------------------------------------------
commit 9557da6ee10d028954c054d719071753ddd0266d
Author: Mukund Sivaraman <muks at isc.org>
Date:   Tue Sep 18 10:40:20 2012 +0530

    [2198] Throw if unlocking fails

commit 89fa65e6b7bd3ccc9ac9fd2e4a3f0c93ff7bd204
Author: Mukund Sivaraman <muks at isc.org>
Date:   Tue Sep 18 10:37:38 2012 +0530

    [2198] Grab sync map mutex before using sync_map.find()

commit 107c6ee0d6462a638211b9048ab5ea5bf31418a5
Author: Mukund Sivaraman <muks at isc.org>
Date:   Tue Sep 18 10:36:34 2012 +0530

    [2198] Add last error to message when throwing

commit 7f9ba74e8dd4b00e008021080f71ec674be79ba4
Author: Mukund Sivaraman <muks at isc.org>
Date:   Tue Sep 18 10:22:46 2012 +0530

    [2108] Use an RAII mutex object for exception safety

commit fe3e1576efeb6061d853fe23cf496c3b9da889e6
Author: Mukund Sivaraman <muks at isc.org>
Date:   Tue Sep 18 10:16:43 2012 +0530

    [2198] Add SyncMapMutex for RAII

commit 49df6ef0a4202d8303adf1d2ca0c99f43b5f6b42
Author: Mukund Sivaraman <muks at isc.org>
Date:   Tue Sep 18 10:16:16 2012 +0530

    [2198] Move anonymous namespace into isc::util

-----------------------------------------------------------------------

Summary of changes:
 src/lib/util/interprocess_sync_file.cc |  150 ++++++++++++++++++++++++++++----
 1 file changed, 134 insertions(+), 16 deletions(-)

-----------------------------------------------------------------------
diff --git a/src/lib/util/interprocess_sync_file.cc b/src/lib/util/interprocess_sync_file.cc
index 8bed4fe..6b33a39 100644
--- a/src/lib/util/interprocess_sync_file.cc
+++ b/src/lib/util/interprocess_sync_file.cc
@@ -27,6 +27,9 @@
 #include <pthread.h>
 #include <assert.h>
 
+namespace isc {
+namespace util {
+
 namespace {
 // The size_t counts the number of current InterprocessSyncFile
 // objects with the same task name. The pthread_mutex_t is the lock that
@@ -38,13 +41,68 @@ pthread_mutex_t sync_map_mutex = PTHREAD_MUTEX_INITIALIZER;
 SyncMap sync_map;
 }
 
-namespace isc {
-namespace util {
+class SyncMapMutex {
+public:
+    SyncMapMutex() :
+        locked_(false),
+        last_(0)
+    {
+    }
+
+    bool lock() {
+        if (locked_) {
+            return (true);
+        }
+
+        last_ = pthread_mutex_lock(&sync_map_mutex);
+        if (last_ == 0) {
+            locked_ = true;
+        }
+
+        return (locked_);
+    }
+
+    bool unlock() {
+        if (!locked_) {
+            return (true);
+        }
+
+        last_ = pthread_mutex_unlock(&sync_map_mutex);
+        if (last_ == 0) {
+            locked_ = false;
+        }
+
+        return (!locked_);
+    }
+
+    int getLastStatus() {
+         return (last_);
+    }
+
+    ~SyncMapMutex() {
+        if (locked_) {
+            if (!unlock()) {
+                isc_throw(isc::InvalidOperation,
+                          "Error unlocking SyncMapMutex: "
+                          << strerror(getLastStatus()));
+            }
+        }
+    }
+
+private:
+    bool locked_;
+    int last_;
+};
 
 InterprocessSyncFile::InterprocessSyncFile(const std::string& task_name) :
     InterprocessSync(task_name), fd_(-1)
 {
-    pthread_mutex_lock(&sync_map_mutex);
+    SyncMapMutex mutex;
+    if (!mutex.lock()) {
+        isc_throw(isc::InvalidOperation,
+                  "Error locking SyncMapMutex: "
+                  << strerror(mutex.getLastStatus()));
+    }
 
     SyncMap::iterator it = sync_map.find(task_name);
     SyncMapData* data;
@@ -61,7 +119,8 @@ InterprocessSyncFile::InterprocessSyncFile(const std::string& task_name) :
     // Increment number of users for this task_name.
     data->first++;
 
-    pthread_mutex_unlock(&sync_map_mutex);
+    // `mutex` is automatically unlocked during destruction when basic
+    // block is exited.
 }
 
 InterprocessSyncFile::~InterprocessSyncFile() {
@@ -72,7 +131,12 @@ InterprocessSyncFile::~InterprocessSyncFile() {
         // it.
     }
 
-    pthread_mutex_lock(&sync_map_mutex);
+    SyncMapMutex mutex;
+    if (!mutex.lock()) {
+        isc_throw(isc::InvalidOperation,
+                  "Error locking SyncMapMutex: "
+                  << strerror(mutex.getLastStatus()));
+    }
 
     SyncMap::iterator it = sync_map.find(task_name_);
     assert(it != sync_map.end());
@@ -87,7 +151,8 @@ InterprocessSyncFile::~InterprocessSyncFile() {
         delete data;
     }
 
-    pthread_mutex_unlock(&sync_map_mutex);
+    // `mutex` is automatically unlocked during destruction when basic
+    // block is exited.
 }
 
 bool
@@ -145,11 +210,24 @@ InterprocessSyncFile::lock() {
         return (true);
     }
 
-    // First grab the thread lock...
+    SyncMapMutex mutex;
+    if (!mutex.lock()) {
+        isc_throw(isc::InvalidOperation,
+                  "Error locking SyncMapMutex: "
+                  << strerror(mutex.getLastStatus()));
+    }
+
     SyncMap::iterator it = sync_map.find(task_name_);
     assert(it != sync_map.end());
-
     SyncMapData* data = it->second;
+
+    if (!mutex.unlock()) {
+        isc_throw(isc::InvalidOperation,
+                  "Error unlocking SyncMapMutex: "
+                  << strerror(mutex.getLastStatus()));
+    }
+
+    // First grab the thread lock...
     if (pthread_mutex_lock(&data->second) != 0) {
         return (false);
     }
@@ -160,7 +238,13 @@ InterprocessSyncFile::lock() {
         return (true);
     }
 
-    pthread_mutex_unlock(&data->second);
+    int ret = pthread_mutex_unlock(&data->second);
+    if (ret != 0) {
+        isc_throw(isc::InvalidOperation,
+                  "Error unlocking InterprocessSyncFile mutex: "
+                  << strerror(ret));
+    }
+
     return (false);
 }
 
@@ -170,11 +254,24 @@ InterprocessSyncFile::tryLock() {
         return (true);
     }
 
-    // First grab the thread lock...
+    SyncMapMutex mutex;
+    if (!mutex.lock()) {
+        isc_throw(isc::InvalidOperation,
+                  "Error locking SyncMapMutex: "
+                  << strerror(mutex.getLastStatus()));
+    }
+
     SyncMap::iterator it = sync_map.find(task_name_);
     assert(it != sync_map.end());
-
     SyncMapData* data = it->second;
+
+    if (!mutex.unlock()) {
+        isc_throw(isc::InvalidOperation,
+                  "Error unlocking SyncMapMutex: "
+                  << strerror(mutex.getLastStatus()));
+    }
+
+    // First grab the thread lock...
     if (pthread_mutex_trylock(&data->second) != 0) {
         return (false);
     }
@@ -185,7 +282,13 @@ InterprocessSyncFile::tryLock() {
         return (true);
     }
 
-    pthread_mutex_unlock(&data->second);
+    int ret = pthread_mutex_unlock(&data->second);
+    if (ret != 0) {
+        isc_throw(isc::InvalidOperation,
+                  "Error unlocking InterprocessSyncFile mutex: "
+                  << strerror(ret));
+    }
+
     return (false);
 }
 
@@ -200,13 +303,28 @@ InterprocessSyncFile::unlock() {
         return (false);
     }
 
-    // ... then the thread lock.
+    SyncMapMutex mutex;
+    if (!mutex.lock()) {
+        isc_throw(isc::InvalidOperation,
+                  "Error locking SyncMapMutex: "
+                  << strerror(mutex.getLastStatus()));
+    }
+
     SyncMap::iterator it = sync_map.find(task_name_);
     assert(it != sync_map.end());
-
     SyncMapData* data = it->second;
-    if (pthread_mutex_unlock(&data->second) != 0) {
-        return (false);
+
+    if (!mutex.unlock()) {
+        isc_throw(isc::InvalidOperation,
+                  "Error unlocking SyncMapMutex: "
+                  << strerror(mutex.getLastStatus()));
+    }
+
+    int ret = pthread_mutex_unlock(&data->second);
+    if (ret != 0) {
+        isc_throw(isc::InvalidOperation,
+                  "Error unlocking InterprocessSyncFile mutex: "
+                  << strerror(ret));
     }
 
     is_locked_ = false;



More information about the bind10-changes mailing list