From: Mukund Sivaraman Date: Sun, 14 Oct 2012 16:52:35 +0000 (+0530) Subject: [2198] Stop using direct lock methods on Mutex inside InterprocessSyncFile X-Git-Tag: trac2402_base~10^2~10 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=a80cd447a1458b6c2d5d654f31e2e4a1a7d3f66e;p=thirdparty%2Fkea.git [2198] Stop using direct lock methods on Mutex inside InterprocessSyncFile --- diff --git a/src/lib/util/interprocess_sync_file.cc b/src/lib/util/interprocess_sync_file.cc index 71d328735b..b406e6e01c 100644 --- a/src/lib/util/interprocess_sync_file.cc +++ b/src/lib/util/interprocess_sync_file.cc @@ -34,7 +34,6 @@ namespace util { namespace { // unnamed namespace typedef std::map > SyncMap; -typedef boost::shared_ptr MutexPtr; Mutex sync_map_mutex; SyncMap sync_map; @@ -69,9 +68,11 @@ InterprocessSyncFile::~InterprocessSyncFile() { Mutex::Locker locker(sync_map_mutex); - // Unref the shared mutex first. + // Unref the shared mutex. + locker_.reset(); mutex_.reset(); + // Remove name from the map if it is unused anymore. SyncMap::iterator it = sync_map.find(task_name_); assert(it != sync_map.end()); @@ -139,20 +140,15 @@ InterprocessSyncFile::lock() { } // First grab the thread lock... - mutex_->lock(); + LockerPtr locker(new Mutex::Locker(*mutex_)); // ... then the file lock. - try { - if (do_lock(F_SETLKW, F_WRLCK)) { - is_locked_ = true; - return (true); - } - } catch (...) { - mutex_->unlock(); - throw; + if (do_lock(F_SETLKW, F_WRLCK)) { + is_locked_ = true; + locker_ = locker; + return (true); } - mutex_->unlock(); return (false); } @@ -163,23 +159,20 @@ InterprocessSyncFile::tryLock() { } // First grab the thread lock... - if (!mutex_->tryLock()) { + LockerPtr locker; + try { + locker.reset(new Mutex::Locker(*mutex_, false)); + } catch (const Mutex::Locker::AlreadyLocked&) { return (false); } // ... then the file lock. - try { - // ... then the file lock. - if (do_lock(F_SETLK, F_WRLCK)) { - is_locked_ = true; - return (true); - } - } catch (...) { - mutex_->unlock(); - throw; + if (do_lock(F_SETLK, F_WRLCK)) { + is_locked_ = true; + locker_ = locker; + return (true); } - mutex_->unlock(); return (false); } @@ -194,7 +187,7 @@ InterprocessSyncFile::unlock() { return (false); } - mutex_->unlock(); + locker_.reset(); is_locked_ = false; return (true); } diff --git a/src/lib/util/interprocess_sync_file.h b/src/lib/util/interprocess_sync_file.h index 6eb8f95e1c..b0fe81f906 100644 --- a/src/lib/util/interprocess_sync_file.h +++ b/src/lib/util/interprocess_sync_file.h @@ -86,7 +86,9 @@ private: int fd_; ///< The descriptor for the open file typedef boost::shared_ptr MutexPtr; - MutexPtr mutex_; ///< A mutex for mutual exclusion among threads + typedef boost::shared_ptr LockerPtr; + MutexPtr mutex_; ///< A mutex for mutual exclusion among threads + LockerPtr locker_; ///< A locker on mutex_ }; } // namespace util