Browse Source

[2198] Use a mutex from LoggerManager during logging calls

... for mutual exclusion among threads.
Mukund Sivaraman 12 years ago
parent
commit
2caf9ff647

+ 3 - 1
src/lib/log/Makefile.am

@@ -48,5 +48,7 @@ if USE_CLANGPP
 libb10_log_la_CXXFLAGS += -Wno-error
 libb10_log_la_CXXFLAGS += -Wno-error
 endif
 endif
 libb10_log_la_CPPFLAGS = $(AM_CPPFLAGS) $(LOG4CPLUS_INCLUDES)
 libb10_log_la_CPPFLAGS = $(AM_CPPFLAGS) $(LOG4CPLUS_INCLUDES)
-libb10_log_la_LIBADD   = $(top_builddir)/src/lib/util/libb10-util.la $(LOG4CPLUS_LIBS)
+libb10_log_la_LIBADD   = $(top_builddir)/src/lib/util/libb10-util.la
+libb10_log_la_LIBADD  += $(top_builddir)/src/lib/util/threads/libb10-threads.la
+libb10_log_la_LIBADD  += $(LOG4CPLUS_LIBS)
 libb10_log_la_LDFLAGS = -no-undefined -version-info 1:0:0
 libb10_log_la_LDFLAGS = -no-undefined -version-info 1:0:0

+ 5 - 1
src/lib/log/logger_impl.cc

@@ -29,6 +29,7 @@
 #include <log/logger_level.h>
 #include <log/logger_level.h>
 #include <log/logger_level_impl.h>
 #include <log/logger_level_impl.h>
 #include <log/logger_name.h>
 #include <log/logger_name.h>
+#include <log/logger_manager.h>
 #include <log/message_dictionary.h>
 #include <log/message_dictionary.h>
 #include <log/message_types.h>
 #include <log/message_types.h>
 
 
@@ -123,9 +124,12 @@ LoggerImpl::setInterprocessSync(isc::util::InterprocessSync* sync) {
 
 
 void
 void
 LoggerImpl::outputRaw(const Severity& severity, const string& message) {
 LoggerImpl::outputRaw(const Severity& severity, const string& message) {
+    // Use a mutex locker for mutual exclusion from other threads in
+    // this process.
+    isc::util::thread::Mutex::Locker mutex_locker(LoggerManager::getMutex());
+
     // Use an interprocess sync locker for mutual exclusion from other
     // Use an interprocess sync locker for mutual exclusion from other
     // processes to avoid log messages getting interspersed.
     // processes to avoid log messages getting interspersed.
-
     InterprocessSyncLocker locker(*sync_);
     InterprocessSyncLocker locker(*sync_);
 
 
     if (!locker.lock()) {
     if (!locker.lock()) {

+ 10 - 0
src/lib/log/logger_manager.cc

@@ -138,6 +138,9 @@ LoggerManager::init(const std::string& root, isc::log::Severity severity,
     if (file) {
     if (file) {
         readLocalMessageFile(file);
         readLocalMessageFile(file);
     }
     }
+
+    // Ensure that the mutex is constructed and ready at this point.
+    (void) getMutex();
 }
 }
 
 
 
 
@@ -192,5 +195,12 @@ LoggerManager::reset() {
     LoggerManagerImpl::reset(initSeverity(), initDebugLevel());
     LoggerManagerImpl::reset(initSeverity(), initDebugLevel());
 }
 }
 
 
+isc::util::thread::Mutex&
+LoggerManager::getMutex() {
+    static isc::util::thread::Mutex mutex;
+
+    return (mutex);
+}
+
 } // namespace log
 } // namespace log
 } // namespace isc
 } // namespace isc

+ 6 - 0
src/lib/log/logger_manager.h

@@ -16,6 +16,7 @@
 #define LOGGER_MANAGER_H
 #define LOGGER_MANAGER_H
 
 
 #include "exceptions/exceptions.h"
 #include "exceptions/exceptions.h"
+#include <util/threads/sync.h>
 #include <log/logger_specification.h>
 #include <log/logger_specification.h>
 
 
 // Generated if, when updating the logging specification, an unknown
 // Generated if, when updating the logging specification, an unknown
@@ -132,6 +133,11 @@ public:
     /// \param file Name of the local message file
     /// \param file Name of the local message file
     static void readLocalMessageFile(const char* file);
     static void readLocalMessageFile(const char* file);
 
 
+    /// \brief Return a process-global mutex that's used for mutual
+    /// exclusion among threads of a single process during logging
+    /// calls.
+    static isc::util::thread::Mutex& getMutex();
+
 private:
 private:
     /// \brief Initialize Processing
     /// \brief Initialize Processing
     ///
     ///