Unverified Commit 07931b0b authored by Dominik Charousset's avatar Dominik Charousset Committed by GitHub

Merge pull request #996

Remove CAF_NO_THREAD_LOCAL and its workarounds
parents 0964a009 cc0b51c2
......@@ -10,6 +10,10 @@
# error "No support for 'if constexpr' (__cpp_if_constexpr)"
#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**) {
return 0;
}
......@@ -108,9 +108,6 @@
# define CAF_ANNOTATE_FALLTHROUGH [[clang::fallthrough]]
# define CAF_COMPILER_VERSION \
(__clang_major__ * 10000 + __clang_minor__ * 100 + __clang_patchlevel__)
# if !__has_feature(cxx_thread_local)
# define CAF_NO_THREAD_LOCAL
# endif
#elif defined(__GNUC__)
# define CAF_GCC
# define CAF_LIKELY(x) __builtin_expect((x), 1)
......@@ -146,12 +143,6 @@
(__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__)
// disable thread_local on GCC/macOS due to heap-use-after-free bug:
// https://gcc.gnu.org/bugzilla/show_bug.cgi?id=67135
# ifdef __APPLE__
# define CAF_NO_THREAD_LOCAL
# endif
# ifdef __MINGW32__
# define CAF_NO_THREAD_LOCAL
# endif
#elif defined(_MSC_VER)
# define CAF_MSVC
# define CAF_LIKELY(x) x
......
......@@ -352,12 +352,6 @@ private:
// References the parent system.
actor_system& system_;
// Guards aids_.
detail::shared_spinlock aids_lock_;
// Maps thread IDs to actor IDs.
std::unordered_map<std::thread::id, actor_id> aids_;
// Identifies the thread that called `logger::start`.
std::thread::id parent_thread_;
......
......@@ -18,14 +18,15 @@
#include "caf/logger.hpp"
#include <ctime>
#include <thread>
#include <cstring>
#include <iomanip>
#include <fstream>
#include <algorithm>
#include <condition_variable>
#include <cstring>
#include <ctime>
#include <fstream>
#include <iomanip>
#include <thread>
#include <unordered_map>
#include <utility>
#include "caf/config.hpp"
......@@ -47,6 +48,12 @@ namespace caf {
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[] = {
"QUIET",
"",
......@@ -188,48 +195,6 @@ string_view reduce_symbol(std::ostream& out, string_view 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
logger::config::config()
......@@ -295,29 +260,14 @@ std::string logger::line_builder::get() const {
}
// returns the actor ID for the current thread
actor_id logger::thread_local_aid() {
shared_lock<detail::shared_spinlock> guard{aids_lock_};
auto i = aids_.find(std::this_thread::get_id());
if (i != aids_.end())
return i->second;
return 0;
return current_actor_id;
}
actor_id logger::thread_local_aid(actor_id aid) {
auto tid = std::this_thread::get_id();
upgrade_lock<detail::shared_spinlock> guard{aids_lock_};
auto i = aids_.find(tid);
if (i != aids_.end()) {
// we modify it despite the shared lock because the elements themselves
// are considered thread-local
auto res = i->second;
i->second = aid;
return res;
}
// upgrade to unique lock and insert new element
upgrade_to_unique_lock<detail::shared_spinlock> uguard{guard};
aids_.emplace(tid, aid);
return 0; // was empty before
std::swap(current_actor_id, aid);
return aid;
}
void logger::log(event&& x) {
......@@ -328,14 +278,11 @@ void logger::log(event&& x) {
}
void logger::set_current_actor_system(actor_system* x) {
if (x != nullptr)
set_current_logger(&x->logger());
else
set_current_logger(nullptr);
current_logger_ptr.reset(x != nullptr ? &x->logger() : nullptr);
}
logger* logger::current_logger() {
return get_current_logger();
return current_logger_ptr.get();
}
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