]> git.ipfire.org Git - thirdparty/pdns.git/commitdiff
remove the lock from our Logger class and replace it by thread specific storage of...
authorbert hubert <bert.hubert@netherlabs.nl>
Fri, 16 Jan 2015 14:29:34 +0000 (15:29 +0100)
committerbert hubert <bert.hubert@netherlabs.nl>
Fri, 16 Jan 2015 14:29:34 +0000 (15:29 +0100)
pdns/logger.cc
pdns/logger.hh

index 5e6fd2b8b0d171e6addf1cb906f742184b001444..cea0ca1fd5e347c62cb90530277f0e25c0fb90c0 100644 (file)
@@ -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;
 }
index 2c3b4496366546261e4899b3506bc5f8bc23392e..e1d1d826fb2252c607d772d299530e8e1c7ba475 100644 (file)
@@ -24,7 +24,6 @@
 /* (C) 2002 POWERDNS.COM BV */
 
 #include <string>
-#include <map>
 #include <ctime>
 #include <iostream>
 #include <sstream>
@@ -81,8 +80,18 @@ 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;
@@ -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="");