nghttpx: Use thread_local if it is available

This commit is contained in:
Tatsuhiro Tsujikawa 2016-10-23 19:10:56 +09:00
parent 6c882e1ece
commit 177d51ddab
4 changed files with 47 additions and 9 deletions

View File

@ -254,6 +254,21 @@ std::atomic_store(&a, p);
[have_atomic_std_shared_ptr=no [have_atomic_std_shared_ptr=no
AC_MSG_RESULT([no])]) AC_MSG_RESULT([no])])
# Check that thread_local storage specifier is available
AC_MSG_CHECKING([whether thread_local storage class specifier is available.])
AC_COMPILE_IFELSE([AC_LANG_PROGRAM(
,
[[
thread_local int a = 0;
(void)a;
]])],
[AC_DEFINE([HAVE_THREAD_LOCAL], [1],
[Define to 1 if you have thread_local storage specifier.])
have_thread_local=yes
AC_MSG_RESULT([yes])],
[have_Thread_local=no
AC_MSG_RESULT([no])])
CXXFLAGS=$save_CXXFLAGS CXXFLAGS=$save_CXXFLAGS
AC_LANG_POP() AC_LANG_POP()

View File

@ -3723,7 +3723,7 @@ int main(int argc, char **argv) {
LOG(NOTICE) << "Shutdown momentarily"; LOG(NOTICE) << "Shutdown momentarily";
delete log_config(); delete_log_config();
return 0; return 0;
} }

View File

@ -33,12 +33,24 @@ LogConfig::LogConfig()
: accesslog_fd(-1), errorlog_fd(-1), errorlog_tty(false) {} : accesslog_fd(-1), errorlog_fd(-1), errorlog_tty(false) {}
#ifndef NOTHREADS #ifndef NOTHREADS
static pthread_key_t lckey; #ifdef HAVE_THREAD_LOCAL
static pthread_once_t lckey_once = PTHREAD_ONCE_INIT; namespace {
thread_local std::unique_ptr<LogConfig> config = make_unique<LogConfig>();
} // namespace
static void make_key(void) { pthread_key_create(&lckey, NULL); } LogConfig *log_config() { return config.get(); }
void delete_log_config() {}
#else // !HAVE_THREAD_LOCAL
namespace {
pthread_key_t lckey;
pthread_once_t lckey_once = PTHREAD_ONCE_INIT;
} // namespace
LogConfig *log_config(void) { namespace {
void make_key() { pthread_key_create(&lckey, NULL); }
} // namespace
LogConfig *log_config() {
pthread_once(&lckey_once, make_key); pthread_once(&lckey_once, make_key);
LogConfig *config = (LogConfig *)pthread_getspecific(lckey); LogConfig *config = (LogConfig *)pthread_getspecific(lckey);
if (!config) { if (!config) {
@ -47,9 +59,17 @@ LogConfig *log_config(void) {
} }
return config; return config;
} }
#else
static LogConfig *config = new LogConfig(); void delete_log_config() { delete log_config(); }
LogConfig *log_config(void) { return config; } #endif // !HAVE_THREAD_LOCAL
#else // NOTHREADS
namespace {
std::unique_ptr<LogConfig> config = make_unique<LogConfig>();
} // namespace
LogConfig *log_config() { return config.get(); }
void delete_log_config() {}
#endif // NOTHREADS #endif // NOTHREADS
void LogConfig::update_tstamp( void LogConfig::update_tstamp(

View File

@ -54,7 +54,10 @@ struct LogConfig {
// We need LogConfig per thread to avoid data race around opening file // We need LogConfig per thread to avoid data race around opening file
// descriptor for log files. // descriptor for log files.
extern LogConfig *log_config(void); LogConfig *log_config();
// Deletes log_config
void delete_log_config();
} // namespace shrpx } // namespace shrpx