Commit 8748f5a9 authored by Dominik Charousset's avatar Dominik Charousset

Remove CAF_NO_THREAD_LOCAL and its workarounds

parent eb27e86a
...@@ -10,6 +10,10 @@ ...@@ -10,6 +10,10 @@
# error "No support for 'if constexpr' (__cpp_if_constexpr)" # error "No support for 'if constexpr' (__cpp_if_constexpr)"
#endif #endif
// Unfortunately there's no feature test macro for thread_local. By putting this
// here, at least we'll get a compiler error on unsupported platforms.
[[maybe_unused]] thread_local int foo;
int main(int, char**) { int main(int, char**) {
return 0; return 0;
} }
...@@ -107,9 +107,6 @@ ...@@ -107,9 +107,6 @@
# define CAF_ANNOTATE_FALLTHROUGH [[clang::fallthrough]] # define CAF_ANNOTATE_FALLTHROUGH [[clang::fallthrough]]
# define CAF_COMPILER_VERSION \ # define CAF_COMPILER_VERSION \
(__clang_major__ * 10000 + __clang_minor__ * 100 + __clang_patchlevel__) (__clang_major__ * 10000 + __clang_minor__ * 100 + __clang_patchlevel__)
# if !__has_feature(cxx_thread_local)
# define CAF_NO_THREAD_LOCAL
# endif
#elif defined(__GNUC__) #elif defined(__GNUC__)
# define CAF_GCC # define CAF_GCC
# define CAF_LIKELY(x) __builtin_expect((x), 1) # define CAF_LIKELY(x) __builtin_expect((x), 1)
...@@ -145,9 +142,6 @@ ...@@ -145,9 +142,6 @@
(__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__) (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__)
// disable thread_local on GCC/macOS due to heap-use-after-free bug: // disable thread_local on GCC/macOS due to heap-use-after-free bug:
// https://gcc.gnu.org/bugzilla/show_bug.cgi?id=67135 // https://gcc.gnu.org/bugzilla/show_bug.cgi?id=67135
# ifdef __APPLE__
# define CAF_NO_THREAD_LOCAL
# endif
#elif defined(_MSC_VER) #elif defined(_MSC_VER)
# define CAF_MSVC # define CAF_MSVC
# define CAF_LIKELY(x) x # define CAF_LIKELY(x) x
......
...@@ -18,14 +18,15 @@ ...@@ -18,14 +18,15 @@
#include "caf/logger.hpp" #include "caf/logger.hpp"
#include <ctime>
#include <thread>
#include <cstring>
#include <iomanip>
#include <fstream>
#include <algorithm> #include <algorithm>
#include <condition_variable> #include <condition_variable>
#include <cstring>
#include <ctime>
#include <fstream>
#include <iomanip>
#include <thread>
#include <unordered_map> #include <unordered_map>
#include <utility>
#include "caf/config.hpp" #include "caf/config.hpp"
...@@ -47,6 +48,12 @@ namespace caf { ...@@ -47,6 +48,12 @@ namespace caf {
namespace { namespace {
// Stores the ID of the currently running actor.
thread_local actor_id current_actor_id;
// Stores a pointer to the system-wide logger.
thread_local intrusive_ptr<logger> current_logger_ptr;
constexpr string_view log_level_name[] = { constexpr string_view log_level_name[] = {
"QUIET", "QUIET",
"", "",
...@@ -188,48 +195,6 @@ string_view reduce_symbol(std::ostream& out, string_view symbol) { ...@@ -188,48 +195,6 @@ string_view reduce_symbol(std::ostream& out, string_view symbol) {
return symbol; return symbol;
} }
#if defined(CAF_NO_THREAD_LOCAL)
pthread_key_t s_key;
pthread_once_t s_key_once = PTHREAD_ONCE_INIT;
void logger_ptr_destructor(void* ptr) {
if (ptr != nullptr) {
intrusive_ptr_release(reinterpret_cast<logger*>(ptr));
}
}
void make_logger_ptr() {
pthread_key_create(&s_key, logger_ptr_destructor);
}
void set_current_logger(logger* x) {
pthread_once(&s_key_once, make_logger_ptr);
logger_ptr_destructor(pthread_getspecific(s_key));
if (x != nullptr)
intrusive_ptr_add_ref(x);
pthread_setspecific(s_key, x);
}
logger* get_current_logger() {
pthread_once(&s_key_once, make_logger_ptr);
return reinterpret_cast<logger*>(pthread_getspecific(s_key));
}
#else // !CAF_NO_THREAD_LOCAL
thread_local intrusive_ptr<logger> current_logger;
inline void set_current_logger(logger* x) {
current_logger.reset(x);
}
inline logger* get_current_logger() {
return current_logger.get();
}
#endif // CAF_NO_THREAD_LOCAL
} // namespace } // namespace
logger::config::config() logger::config::config()
...@@ -296,15 +261,13 @@ std::string logger::line_builder::get() const { ...@@ -296,15 +261,13 @@ std::string logger::line_builder::get() const {
// returns the actor ID for the current thread // returns the actor ID for the current thread
thread_local actor_id _currentActoId;
actor_id logger::thread_local_aid() { actor_id logger::thread_local_aid() {
return _currentActoId; return current_actor_id;
} }
actor_id logger::thread_local_aid(actor_id aid) { actor_id logger::thread_local_aid(actor_id aid) {
auto old = _currentActoId; std::swap(current_actor_id, aid);
_currentActoId = aid; return aid;
return old;
} }
void logger::log(event&& x) { void logger::log(event&& x) {
...@@ -315,14 +278,11 @@ void logger::log(event&& x) { ...@@ -315,14 +278,11 @@ void logger::log(event&& x) {
} }
void logger::set_current_actor_system(actor_system* x) { void logger::set_current_actor_system(actor_system* x) {
if (x != nullptr) current_logger_ptr.reset(x != nullptr ? &x->logger() : nullptr);
set_current_logger(&x->logger());
else
set_current_logger(nullptr);
} }
logger* logger::current_logger() { logger* logger::current_logger() {
return get_current_logger(); return current_logger_ptr.get();
} }
bool logger::accepts(unsigned level, atom_value cname) { bool logger::accepts(unsigned level, atom_value cname) {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment