From: bert hubert Date: Fri, 16 Jan 2015 14:29:34 +0000 (+0100) Subject: remove the lock from our Logger class and replace it by thread specific storage of... X-Git-Tag: rec-3.7.0-rc1~20 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=d1449e4d073595e1e1581804f121fc90e37158bf;p=thirdparty%2Fpdns.git remove the lock from our Logger class and replace it by thread specific storage of the log line being constructed --- diff --git a/pdns/logger.cc b/pdns/logger.cc index 5e6fd2b8b0..cea0ca1fd5 100644 --- a/pdns/logger.cc +++ b/pdns/logger.cc @@ -29,6 +29,9 @@ extern StatBag S; #include "namespaces.hh" +pthread_once_t Logger::s_once; +pthread_key_t Logger::s_loggerKey; + Logger &theL(const string &pname) { static Logger l("", LOG_DAEMON); @@ -83,6 +86,12 @@ void Logger::setName(const string &_name) open(); } +void Logger::initKey() +{ + if(pthread_key_create(&s_loggerKey, perThreadDestructor)) + unixDie("Creating thread key for logger"); +} + Logger::Logger(const string &n, int facility) { opened=false; @@ -90,32 +99,43 @@ Logger::Logger(const string &n, int facility) d_facility=facility; consoleUrgency=Error; name=n; - pthread_mutex_init(&lock,0); + + if(pthread_once(&s_once, initKey)) + unixDie("Creating thread key for logger"); + open(); } Logger& Logger::operator<<(Urgency u) { - pthread_mutex_lock(&lock); - - d_outputurgencies[pthread_self()]=u; - - pthread_mutex_unlock(&lock); + getPerThread()->d_urgency=u; return *this; } -Logger& Logger::operator<<(const string &s) +void Logger::perThreadDestructor(void* buf) { - pthread_mutex_lock(&lock); - - if(!d_outputurgencies.count(pthread_self())) // default urgency - d_outputurgencies[pthread_self()]=Info; + PerThread* pt = (PerThread*) buf; + delete pt; +} - // if(d_outputurgencies[pthread_self()]<=(unsigned int)consoleUrgency) // prevent building strings we won't ever print - d_strings[pthread_self()].append(s); +Logger::PerThread* Logger::getPerThread() +{ + void *buf=pthread_getspecific(s_loggerKey); + PerThread* ret; + if(buf) + ret = (PerThread*) buf; + else { + ret = new PerThread(); + pthread_setspecific(s_loggerKey, (void*)ret); + } + return ret; +} - pthread_mutex_unlock(&lock); +Logger& Logger::operator<<(const string &s) +{ + PerThread* pt =getPerThread(); + pt->d_output.append(s); return *this; } @@ -180,13 +200,10 @@ Logger& Logger::operator<<(long i) Logger& Logger::operator<<(ostream & (&)(ostream &)) { - // *this<<" ("<<(int)d_outputurgencies[pthread_self()]<<", "<<(int)consoleUrgency<<")"; - pthread_mutex_lock(&lock); - - log(d_strings[pthread_self()], d_outputurgencies[pthread_self()]); - d_strings.erase(pthread_self()); - d_outputurgencies.erase(pthread_self()); + PerThread* pt =getPerThread(); - pthread_mutex_unlock(&lock); + log(pt->d_output, pt->d_urgency); + pt->d_output.clear(); + pt->d_urgency=Info; return *this; } diff --git a/pdns/logger.hh b/pdns/logger.hh index 2c3b449636..e1d1d826fb 100644 --- a/pdns/logger.hh +++ b/pdns/logger.hh @@ -24,7 +24,6 @@ /* (C) 2002 POWERDNS.COM BV */ #include -#include #include #include #include @@ -81,8 +80,18 @@ public: Logger& operator<<(std::ostream & (&)(std::ostream &)); //!< this is to recognise the endl, and to commit the log private: - mapd_strings; - map d_outputurgencies; + struct PerThread + { + PerThread() + { + d_urgency=Info; + } + string d_output; + Urgency d_urgency; + }; + static void initKey(); + static void perThreadDestructor(void *); + PerThread* getPerThread(); void open(); string name; int flags; @@ -90,7 +99,8 @@ private: bool opened; Urgency d_loglevel; Urgency consoleUrgency; - pthread_mutex_t lock; + static pthread_once_t s_once; + static pthread_key_t s_loggerKey; }; extern Logger &theL(const string &pname="");