From ca07bd2c3430d6cb9c9fdb560f5b2608c0b07216 Mon Sep 17 00:00:00 2001 From: Remi Gacogne Date: Fri, 18 May 2018 12:33:11 +0200 Subject: [PATCH] logger: User thread_local instead of pthread_getspecific() --- pdns/logger.cc | 45 ++++++++++----------------------------------- pdns/logger.hh | 8 ++------ 2 files changed, 12 insertions(+), 41 deletions(-) diff --git a/pdns/logger.cc b/pdns/logger.cc index 1766d35ae..b1b0487fe 100644 --- a/pdns/logger.cc +++ b/pdns/logger.cc @@ -31,10 +31,8 @@ extern StatBag S; #include "lock.hh" #include "namespaces.hh" -pthread_once_t Logger::s_once; -pthread_key_t Logger::g_loggerKey; - Logger g_log("", LOG_DAEMON); +thread_local Logger::PerThread Logger::t_perThread; void Logger::log(const string &msg, Urgency u) { @@ -129,52 +127,29 @@ void Logger::setName(const string &_name) open(); } -void Logger::initKey() -{ - if(pthread_key_create(&g_loggerKey, perThreadDestructor)) - unixDie("Creating thread key for logger"); -} - Logger::Logger(const string &n, int facility) : name(n), flags(LOG_PID|LOG_NDELAY), d_facility(facility), d_loglevel(Logger::None), consoleUrgency(Error), opened(false), d_disableSyslog(false) { - if(pthread_once(&s_once, initKey)) - unixDie("Creating thread key for logger"); - open(); } Logger& Logger::operator<<(Urgency u) { - getPerThread()->d_urgency=u; + getPerThread().d_urgency=u; return *this; } -void Logger::perThreadDestructor(void* buf) +Logger::PerThread& Logger::getPerThread() { - PerThread* pt = (PerThread*) buf; - delete pt; -} - -Logger::PerThread* Logger::getPerThread() -{ - void *buf=pthread_getspecific(g_loggerKey); - PerThread* ret; - if(buf) - ret = (PerThread*) buf; - else { - ret = new PerThread(); - pthread_setspecific(g_loggerKey, (void*)ret); - } - return ret; + return t_perThread; } Logger& Logger::operator<<(const string &s) { - PerThread* pt =getPerThread(); - pt->d_output.append(s); + PerThread& pt = getPerThread(); + pt.d_output.append(s); return *this; } @@ -244,11 +219,11 @@ Logger& Logger::operator<<(long i) Logger& Logger::operator<<(ostream & (&)(ostream &)) { - PerThread* pt =getPerThread(); + PerThread& pt = getPerThread(); - log(pt->d_output, pt->d_urgency); - pt->d_output.clear(); - pt->d_urgency=Info; + 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 3fb97ee0f..544a4d69d 100644 --- a/pdns/logger.hh +++ b/pdns/logger.hh @@ -26,7 +26,6 @@ #include #include #include -#include #include "namespaces.hh" #include "dnsname.hh" @@ -101,11 +100,10 @@ private: string d_output; Urgency d_urgency; }; - static void initKey(); - static void perThreadDestructor(void *); - PerThread* getPerThread(); + PerThread& getPerThread(); void open(); + static thread_local PerThread t_perThread; string name; int flags; int d_facility; @@ -115,8 +113,6 @@ private: bool d_disableSyslog; bool d_timestamps{true}; bool d_prefixed{false}; - static pthread_once_t s_once; - static pthread_key_t g_loggerKey; }; extern Logger g_log; -- 2.49.0