diff options
author | Mukund Sivaraman <muks@isc.org> | 2012-10-16 20:47:08 +0200 |
---|---|---|
committer | Mukund Sivaraman <muks@isc.org> | 2012-10-22 01:44:38 +0200 |
commit | 73f49fc9d917907e6b8990e137cd8414acb852ce (patch) | |
tree | 2063be8257f0720f55abf4703cb5db0dbfb0ccb8 | |
parent | [2198] Check for EDEADLK too when using Mutex::tryLock() (diff) | |
download | kea-73f49fc9d917907e6b8990e137cd8414acb852ce.tar.xz kea-73f49fc9d917907e6b8990e137cd8414acb852ce.zip |
[2198] Avoid static destruction fiasco with InterprocessSyncFile and logger
-rw-r--r-- | src/lib/util/interprocess_sync_file.cc | 38 | ||||
-rw-r--r-- | src/lib/util/interprocess_sync_file.h | 13 |
2 files changed, 34 insertions, 17 deletions
diff --git a/src/lib/util/interprocess_sync_file.cc b/src/lib/util/interprocess_sync_file.cc index 6da3f146d6..509a19c7c2 100644 --- a/src/lib/util/interprocess_sync_file.cc +++ b/src/lib/util/interprocess_sync_file.cc @@ -16,9 +16,6 @@ #include <boost/weak_ptr.hpp> -#include <map> -#include <string> - #include <stdlib.h> #include <string.h> #include <unistd.h> @@ -31,21 +28,13 @@ using namespace isc::util::thread; namespace isc { namespace util { -namespace { // unnamed namespace - -typedef std::map<std::string, boost::weak_ptr<Mutex> > SyncMap; - -Mutex sync_map_mutex; -SyncMap sync_map; - -} // end of unnamed namespace - InterprocessSyncFile::InterprocessSyncFile(const std::string& task_name) : InterprocessSync(task_name), fd_(-1) { - Mutex::Locker locker(sync_map_mutex); + Mutex::Locker locker(getSyncMapMutex()); + SyncMap& sync_map = getSyncMap(); SyncMap::iterator it = sync_map.find(task_name); if (it != sync_map.end()) { mutex_ = it->second.lock(); @@ -66,13 +55,14 @@ InterprocessSyncFile::~InterprocessSyncFile() { // it. } - Mutex::Locker locker(sync_map_mutex); + Mutex::Locker locker(getSyncMapMutex()); // Unref the shared mutex. locker_.reset(); mutex_.reset(); // Remove name from the map if it is unused anymore. + SyncMap& sync_map = getSyncMap(); SyncMap::iterator it = sync_map.find(task_name_); assert(it != sync_map.end()); @@ -84,6 +74,26 @@ InterprocessSyncFile::~InterprocessSyncFile() { // destruction when basic block is exited. } +InterprocessSyncFile::SyncMap& +InterprocessSyncFile::getSyncMap() { + // avoid static destruction fiasco when the SyncMap is destroyed + // before clients which use it such as logger objects. This leaks, + // but isn't a growing leak. + static SyncMap* sync_map = new SyncMap; + + return (*sync_map); +} + +Mutex& +InterprocessSyncFile::getSyncMapMutex() { + // avoid static destruction fiasco when the Mutex is destroyed + // before clients which use it such as logger objects. This leaks, + // but isn't a growing leak. + static Mutex* sync_map_mutex = new Mutex; + + return (*sync_map_mutex); +} + bool InterprocessSyncFile::do_lock(int cmd, short l_type) { // Open lock file only when necessary (i.e., here). This is so that diff --git a/src/lib/util/interprocess_sync_file.h b/src/lib/util/interprocess_sync_file.h index b0fe81f906..e4fb3241d7 100644 --- a/src/lib/util/interprocess_sync_file.h +++ b/src/lib/util/interprocess_sync_file.h @@ -21,6 +21,9 @@ #include <boost/shared_ptr.hpp> +#include <map> +#include <string> + namespace isc { namespace util { @@ -81,12 +84,16 @@ protected: bool unlock(); private: + typedef boost::shared_ptr<isc::util::thread::Mutex> MutexPtr; + typedef boost::shared_ptr<isc::util::thread::Mutex::Locker> LockerPtr; + typedef std::map<std::string, boost::weak_ptr<isc::util::thread::Mutex> > + SyncMap; + + SyncMap& getSyncMap(); + isc::util::thread::Mutex& getSyncMapMutex(); bool do_lock(int cmd, short l_type); int fd_; ///< The descriptor for the open file - - typedef boost::shared_ptr<isc::util::thread::Mutex> MutexPtr; - typedef boost::shared_ptr<isc::util::thread::Mutex::Locker> LockerPtr; MutexPtr mutex_; ///< A mutex for mutual exclusion among threads LockerPtr locker_; ///< A locker on mutex_ }; |