diff --git a/CMakeLists.txt b/CMakeLists.txt index c84076d..5f17ee8 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -46,6 +46,7 @@ option_if_not_defined(MARL_BUILD_EXAMPLES "Build example applications" OFF) option_if_not_defined(MARL_BUILD_TESTS "Build tests" OFF) option_if_not_defined(MARL_BUILD_BENCHMARKS "Build benchmarks" OFF) option_if_not_defined(MARL_BUILD_SHARED "Build marl as a shared / dynamic library (default static)" OFF) +option_if_not_defined(MARL_USE_PTHREAD_THREAD_LOCAL "Use pthreads for thread local storage" OFF) option_if_not_defined(MARL_ASAN "Build marl with address sanitizer" OFF) option_if_not_defined(MARL_MSAN "Build marl with memory sanitizer" OFF) option_if_not_defined(MARL_TSAN "Build marl with thread sanitizer" OFF) @@ -224,6 +225,11 @@ function(marl_set_target_options target) endif() endif(MARL_WARNINGS_AS_ERRORS) + if(MARL_USE_PTHREAD_THREAD_LOCAL) + target_compile_definitions(${target} PRIVATE "MARL_USE_PTHREAD_THREAD_LOCAL=1") + target_link_libraries(${target} PUBLIC pthread) + endif() + if(MARL_ASAN) target_compile_options(${target} PUBLIC "-fsanitize=address") target_link_libraries(${target} PUBLIC "-fsanitize=address") diff --git a/include/marl/sanitizers.h b/include/marl/sanitizers.h index 6993066..3f26d4a 100644 --- a/include/marl/sanitizers.h +++ b/include/marl/sanitizers.h @@ -78,14 +78,4 @@ #define THREAD_SANITIZER_ONLY(x) #endif // THREAD_SANITIZER_ENABLED -// The MSAN_UNPOISON macro marks uninitialized memory as initialized for MSAN. -// It can be used to suppress false-positive MSAN errors before reading -// thread-local variables. See https://github.com/google/sanitizers/issues/1265 -#if MEMORY_SANITIZER_ENABLED -#include -#define MSAN_UNPOISON(p, size) __msan_unpoison(p, size) -#else -#define MSAN_UNPOISON(p, size) -#endif - #endif // marl_sanitizers_h diff --git a/include/marl/scheduler.h b/include/marl/scheduler.h index 48acf02..b3159b8 100644 --- a/include/marl/scheduler.h +++ b/include/marl/scheduler.h @@ -24,6 +24,7 @@ #include "sanitizers.h" #include "task.h" #include "thread.h" +#include "thread_local.h" #include #include @@ -464,7 +465,7 @@ class Scheduler { }; // The current worker bound to the current thread. - static thread_local Worker* current; + MARL_DECLARE_THREAD_LOCAL(Worker*, current); Mode const mode; Scheduler* const scheduler; @@ -492,7 +493,7 @@ class Scheduler { static void setBound(Scheduler* scheduler); // The scheduler currently bound to the current thread. - static thread_local Scheduler* bound; + MARL_DECLARE_THREAD_LOCAL(Scheduler*, bound); // The immutable configuration used to build the scheduler. const Config cfg; @@ -574,7 +575,6 @@ bool Scheduler::Fiber::wait( } Scheduler::Worker* Scheduler::Worker::getCurrent() { - MSAN_UNPOISON(¤t, sizeof(Worker*)); return Worker::current; } diff --git a/include/marl/thread_local.h b/include/marl/thread_local.h new file mode 100644 index 0000000..375ba5f --- /dev/null +++ b/include/marl/thread_local.h @@ -0,0 +1,67 @@ +// Copyright 2023 The Marl Authors. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// https://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +// A wrapper around a thread_local variable, or a pthread key + +#ifndef marl_thread_local_h +#define marl_thread_local_h + +#ifdef MARL_USE_PTHREAD_THREAD_LOCAL +#include "debug.h" + +#include +#include + +template +class ThreadLocal { + static_assert(std::is_pointer::value, + "The current implementation of ThreadLocal requires that T " + "must be a pointer"); + + public: + inline ThreadLocal(T v) { + pthread_key_create(&key, NULL); + pthread_setspecific(key, v); + } + inline ~ThreadLocal() { pthread_key_delete(key); } + inline operator T() const { return static_cast(pthread_getspecific(key)); } + inline ThreadLocal& operator=(T v) { + pthread_setspecific(key, v); + return *this; + } + + private: + ThreadLocal(const ThreadLocal&) = delete; + ThreadLocal& operator=(const ThreadLocal&) = delete; + + pthread_key_t key; +}; + +#define MARL_DECLARE_THREAD_LOCAL(TYPE, NAME) static ThreadLocal NAME +#define MARL_INSTANTIATE_THREAD_LOCAL(TYPE, NAME, VALUE) \ + ThreadLocal NAME { \ + VALUE \ + } + +#else + +#define MARL_DECLARE_THREAD_LOCAL(TYPE, NAME) static thread_local TYPE NAME +#define MARL_INSTANTIATE_THREAD_LOCAL(TYPE, NAME, VALUE) \ + thread_local TYPE NAME { \ + VALUE \ + } + +#endif + +#endif // marl_thread_local_h diff --git a/src/scheduler.cpp b/src/scheduler.cpp index 2f32582..f5e9df0 100644 --- a/src/scheduler.cpp +++ b/src/scheduler.cpp @@ -84,15 +84,13 @@ namespace marl { //////////////////////////////////////////////////////////////////////////////// // Scheduler //////////////////////////////////////////////////////////////////////////////// -thread_local Scheduler* Scheduler::bound = nullptr; +MARL_INSTANTIATE_THREAD_LOCAL(Scheduler*, Scheduler::bound, nullptr); Scheduler* Scheduler::get() { - MSAN_UNPOISON(&bound, sizeof(Scheduler*)); return bound; } void Scheduler::setBound(Scheduler* scheduler) { - MSAN_UNPOISON(&bound, sizeof(Scheduler*)); bound = scheduler; } @@ -354,7 +352,9 @@ bool Scheduler::WaitingFibers::Timeout::operator<(const Timeout& o) const { //////////////////////////////////////////////////////////////////////////////// // Scheduler::Worker //////////////////////////////////////////////////////////////////////////////// -thread_local Scheduler::Worker* Scheduler::Worker::current = nullptr; +MARL_INSTANTIATE_THREAD_LOCAL(Scheduler::Worker*, + Scheduler::Worker::current, + nullptr); Scheduler::Worker::Worker(Scheduler* scheduler, Mode mode, uint32_t id) : id(id),