From 7c5254c80b715b807a200ba9074f6ee30f3e207e Mon Sep 17 00:00:00 2001 From: Kjell Hedstrom Date: Mon, 2 Mar 2015 02:02:43 -0700 Subject: [PATCH] thread_local issues on Linux, using atomic instead --- src/crashhandler_windows.cpp | 2 +- src/g2log.cpp | 11 +++++++---- src/g2log.hpp | 11 ++++------- src/stacktrace_windows.cpp | 2 +- 4 files changed, 13 insertions(+), 13 deletions(-) diff --git a/src/crashhandler_windows.cpp b/src/crashhandler_windows.cpp index cd4ecce..cebd874 100644 --- a/src/crashhandler_windows.cpp +++ b/src/crashhandler_windows.cpp @@ -30,7 +30,7 @@ std::atomic gBlockForFatal {true}; LPTOP_LEVEL_EXCEPTION_FILTER g_previous_unexpected_exception_handler = nullptr; #if !(defined(DISABLE_FATAL_SIGNALHANDLING)) -g2_thread_local bool g_installed_thread_signal_handler = false; +thread_local bool g_installed_thread_signal_handler = false; #endif #if !(defined(DISABLE_VECTORED_EXCEPTIONHANDLING)) diff --git a/src/g2log.cpp b/src/g2log.cpp index 7573e94..62e84f2 100644 --- a/src/g2log.cpp +++ b/src/g2log.cpp @@ -24,6 +24,8 @@ #include #include #include +#include +#include #include "std2_make_unique.hpp" #include "g2logworker.hpp" @@ -42,7 +44,7 @@ const std::function g_pre_fatal_hook_that_does_nothing = []{ /*does std::function g_fatal_pre_logging_hook; -g2_thread_local size_t g_fatal_hook_recursive_counter = {0}; +std::atomic g_fatal_hook_recursive_counter = {0}; } @@ -74,6 +76,8 @@ void initializeLogging(LogWorker* bgworker) { // by default the pre fatal logging hook does nothing // if it WOULD do something it would happen in setFatalPreLoggingHook(g_pre_fatal_hook_that_does_nothing); + // recurvise crash counter re-set to zero + g_fatal_hook_recursive_counter.store(0); } @@ -158,7 +162,7 @@ void saveMessage(const char* entry, const char* file, int line, const char* func // In case the fatal_pre logging actually will cause a crash in its turn // let's not do recursive crashing! setFatalPreLoggingHook(g_pre_fatal_hook_that_does_nothing); - ++g_fatal_hook_recursive_counter; // thread_local counter + ++g_fatal_hook_recursive_counter; // thread safe counter // "benign" race here. If two threads crashes, with recursive crashes // then it's possible that the "other" fatal stack trace will be shown // that's OK since it was anyhow the first crash detected @@ -166,12 +170,11 @@ void saveMessage(const char* entry, const char* file, int line, const char* func fatalhook(); message.get()->write().append(stack_trace); - if (g_fatal_hook_recursive_counter > 1) { + if (g_fatal_hook_recursive_counter.load() > 1) { message.get()->write() .append("\n\n\nWARNING\n" "A recursive crash detected. It is likely the hook set with 'setFatalPreLoggingHook(...)' is responsible\n\n") .append("---First crash stacktrace: ").append(first_stack_trace).append("\n---End of first stacktrace\n"); - } FatalMessagePtr fatal_message{ std2::make_unique(*(message._move_only.get()), fatal_signal) }; // At destruction, flushes fatal message to g2LogWorker diff --git a/src/g2log.hpp b/src/g2log.hpp index 43491b0..c5c592c 100644 --- a/src/g2log.hpp +++ b/src/g2log.hpp @@ -33,15 +33,12 @@ #define __PRETTY_FUNCTION__ __FUNCTION__ #endif - // thread_local doesn't exist on VS2013 but it might soon? (who knows) -// to avoid future issues, let's define g2_thread_local that should continue // to work after Microsoft has updated to be C++11 compliant -#if (defined(WIN32) || defined(_WIN32) || defined(__WIN32__)) -#define g2_thread_local __declspec(thread) -#else -#define g2_thread_local thread_local -#endif +#if !(defined(thread_local)) && (defined(WIN32) || defined(_WIN32) || defined(__WIN32__)) +#define thread_local __declspec(thread) +#endif + /** namespace for LOG() and CHECK() frameworks * History lesson: Why the names 'g2' and 'g2log'?: diff --git a/src/stacktrace_windows.cpp b/src/stacktrace_windows.cpp index 1cf0612..6f596d7 100644 --- a/src/stacktrace_windows.cpp +++ b/src/stacktrace_windows.cpp @@ -35,7 +35,7 @@ #define g2_MAP_PAIR_STRINGIFY(x) {x, #x} namespace { -g2_thread_local size_t g_thread_local_recursive_crash_check = 0; +thread_local size_t g_thread_local_recursive_crash_check = 0; const std::map kExceptionsAsText = { g2_MAP_PAIR_STRINGIFY(EXCEPTION_ACCESS_VIOLATION)