Merge branch 'icing-pthread-getspecific'

This commit is contained in:
Tatsuhiro Tsujikawa 2015-03-05 02:07:11 +09:00
commit 838fb33892
8 changed files with 33 additions and 38 deletions

View File

@ -200,21 +200,6 @@ std::vector<std::future<int>> v;
[have_std_future=no [have_std_future=no
AC_MSG_RESULT([no])]) AC_MSG_RESULT([no])])
# Check that thread_local is available.
AC_MSG_CHECKING([whether thread_local is available])
AC_COMPILE_IFELSE([AC_LANG_PROGRAM(
[[
]],
[[
thread_local int n;
]])],
[AC_DEFINE([HAVE_THREAD_LOCAL], [1],
[Define to 1 if you have the `thread_local`.])
have_thread_local=yes
AC_MSG_RESULT([yes])],
[have_thread_local=no
AC_MSG_RESULT([no])])
# Check that std::map::emplace is available for g++-4.7. # Check that std::map::emplace is available for g++-4.7.
AC_MSG_CHECKING([whether std::map::emplace is available]) AC_MSG_CHECKING([whether std::map::emplace is available])
AC_COMPILE_IFELSE([AC_LANG_PROGRAM( AC_COMPILE_IFELSE([AC_LANG_PROGRAM(
@ -631,11 +616,10 @@ if test "x$debug" != "xno"; then
fi fi
enable_threads=yes enable_threads=yes
# Some platform does not have working std::future or thread_local. We # Some platform does not have working std::future. We disable
# disable threading for those platforms. # threading for those platforms.
if test "x$threads" != "xyes" || if test "x$threads" != "xyes" ||
test "x$have_std_future" != "xyes" || test "x$have_std_future" != "xyes"; then
test "x$have_thread_local" != "xyes"; then
enable_threads=no enable_threads=no
AC_DEFINE([NOTHREADS], [1], [Define to 1 if you want to disable threads.]) AC_DEFINE([NOTHREADS], [1], [Define to 1 if you want to disable threads.])
fi fi

View File

@ -1884,16 +1884,16 @@ int main(int argc, char **argv) {
} }
if (get_config()->uid != 0) { if (get_config()->uid != 0) {
if (log_config->accesslog_fd != -1 && if (log_config()->accesslog_fd != -1 &&
fchown(log_config->accesslog_fd, get_config()->uid, fchown(log_config()->accesslog_fd, get_config()->uid,
get_config()->gid) == -1) { get_config()->gid) == -1) {
auto error = errno; auto error = errno;
LOG(WARN) << "Changing owner of access log file failed: " LOG(WARN) << "Changing owner of access log file failed: "
<< strerror(error); << strerror(error);
} }
if (log_config->errorlog_fd != -1 && if (log_config()->errorlog_fd != -1 &&
fchown(log_config->errorlog_fd, get_config()->uid, get_config()->gid) == fchown(log_config()->errorlog_fd, get_config()->uid,
-1) { get_config()->gid) == -1) {
auto error = errno; auto error = errno;
LOG(WARN) << "Changing owner of error log file failed: " LOG(WARN) << "Changing owner of error log file failed: "
<< strerror(error); << strerror(error);

View File

@ -358,7 +358,7 @@ int HttpDownstreamConnection::push_request_headers() {
if (LOG_ENABLED(INFO)) { if (LOG_ENABLED(INFO)) {
const char *hdrp; const char *hdrp;
std::string nhdrs; std::string nhdrs;
if (log_config->errorlog_tty) { if (log_config()->errorlog_tty) {
nhdrs = http::colorizeHeaders(hdrs.c_str()); nhdrs = http::colorizeHeaders(hdrs.c_str());
hdrp = nhdrs.c_str(); hdrp = nhdrs.c_str();
} else { } else {

View File

@ -808,7 +808,7 @@ int HttpsUpstream::on_downstream_abort_request(Downstream *downstream,
void HttpsUpstream::log_response_headers(const std::string &hdrs) const { void HttpsUpstream::log_response_headers(const std::string &hdrs) const {
const char *hdrp; const char *hdrp;
std::string nhdrs; std::string nhdrs;
if (log_config->errorlog_tty) { if (log_config()->errorlog_tty) {
nhdrs = http::colorizeHeaders(hdrs.c_str()); nhdrs = http::colorizeHeaders(hdrs.c_str());
hdrp = nhdrs.c_str(); hdrp = nhdrs.c_str();
} else { } else {

View File

@ -99,7 +99,7 @@ Log::~Log() {
return; return;
} }
auto lgconf = log_config; auto lgconf = log_config();
if (!log_enabled(severity_) || if (!log_enabled(severity_) ||
(lgconf->errorlog_fd == -1 && !get_config()->errorlog_syslog)) { (lgconf->errorlog_fd == -1 && !get_config()->errorlog_syslog)) {
@ -159,7 +159,7 @@ std::pair<OutputIterator, size_t> copy(const char *src, size_t avail,
} // namespace } // namespace
void upstream_accesslog(const std::vector<LogFragment> &lfv, LogSpec *lgsp) { void upstream_accesslog(const std::vector<LogFragment> &lfv, LogSpec *lgsp) {
auto lgconf = log_config; auto lgconf = log_config();
if (lgconf->accesslog_fd == -1 && !get_config()->accesslog_syslog) { if (lgconf->accesslog_fd == -1 && !get_config()->accesslog_syslog) {
return; return;
@ -272,7 +272,7 @@ void upstream_accesslog(const std::vector<LogFragment> &lfv, LogSpec *lgsp) {
int reopen_log_files() { int reopen_log_files() {
int res = 0; int res = 0;
auto lgconf = log_config; auto lgconf = log_config();
if (lgconf->accesslog_fd != -1) { if (lgconf->accesslog_fd != -1) {
close(lgconf->accesslog_fd); close(lgconf->accesslog_fd);

View File

@ -97,8 +97,8 @@ private:
static int severity_thres_; static int severity_thres_;
}; };
#define TTY_HTTP_HD (log_config->errorlog_tty ? "\033[1;34m" : "") #define TTY_HTTP_HD (log_config()->errorlog_tty ? "\033[1;34m" : "")
#define TTY_RST (log_config->errorlog_tty ? "\033[0m" : "") #define TTY_RST (log_config()->errorlog_tty ? "\033[0m" : "")
enum LogFragmentType { enum LogFragmentType {
SHRPX_LOGF_NONE, SHRPX_LOGF_NONE,

View File

@ -33,9 +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
thread_local static pthread_key_t lckey;
static pthread_once_t lckey_once = PTHREAD_ONCE_INIT;
static void make_key(void) { pthread_key_create(&lckey, NULL); }
LogConfig *log_config(void) {
pthread_once(&lckey_once, make_key);
LogConfig *config = (LogConfig *)pthread_getspecific(lckey);
if (!config) {
config = new LogConfig();
pthread_setspecific(lckey, config);
}
return config;
}
#else
static LogConfig *config = new LogConfig();
LogConfig *log_config(void) { return config; }
#endif // NOTHREADS #endif // NOTHREADS
LogConfig *log_config = new LogConfig();
void void
LogConfig::update_tstamp(const std::chrono::system_clock::time_point &now) { LogConfig::update_tstamp(const std::chrono::system_clock::time_point &now) {

View File

@ -46,11 +46,7 @@ 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 extern LogConfig *log_config(void);
#ifndef NOTHREADS
thread_local
#endif // NOTHREADS
LogConfig *log_config;
} // namespace shrpx } // namespace shrpx