Permalink
Browse files

remove the lock from our Logger class and replace it by thread specif…

…ic storage of the log line being constructed
  • Loading branch information...
1 parent ef445ae commit d1449e4d073595e1e1581804f121fc90e37158bf @ahupowerdns ahupowerdns committed Jan 16, 2015
Showing with 52 additions and 25 deletions.
  1. +38 −21 pdns/logger.cc
  2. +14 −4 pdns/logger.hh
View
@@ -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,39 +86,56 @@ 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;
flags=LOG_PID|LOG_NDELAY;
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;
}
View
@@ -24,7 +24,6 @@
/* (C) 2002 POWERDNS.COM BV */
#include <string>
-#include <map>
#include <ctime>
#include <iostream>
#include <sstream>
@@ -81,16 +80,27 @@ public:
Logger& operator<<(std::ostream & (&)(std::ostream &)); //!< this is to recognise the endl, and to commit the log
private:
- map<pthread_t,string>d_strings;
- map<pthread_t,Urgency> 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;
int d_facility;
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="");

0 comments on commit d1449e4

Please sign in to comment.