diff --git a/CMakeLists.txt b/CMakeLists.txt index e16e464343..702e1ca6d2 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -337,9 +337,6 @@ endif() # Reset the required flags set(CMAKE_REQUIRED_FLAGS ${OLD_CMAKE_REQUIRED_FLAGS}) -# thread_local is part of C++11 and later (TODO: clean up this define) -add_definitions(-DROCKSDB_SUPPORT_THREAD_LOCAL) - option(WITH_IOSTATS_CONTEXT "Enable IO stats context" ON) if (NOT WITH_IOSTATS_CONTEXT) add_definitions(-DNIOSTATS_CONTEXT) diff --git a/build_tools/build_detect_platform b/build_tools/build_detect_platform index 1d8aa0841b..44f54075d6 100755 --- a/build_tools/build_detect_platform +++ b/build_tools/build_detect_platform @@ -800,9 +800,6 @@ if [ "$?" = 0 ]; then COMMON_FLAGS="$COMMON_FLAGS -DHAVE_UINT128_EXTENSION" fi -# thread_local is part of C++11 and later (TODO: clean up this define) -COMMON_FLAGS="$COMMON_FLAGS -DROCKSDB_SUPPORT_THREAD_LOCAL" - if [ "$FBCODE_BUILD" != "true" -a "$PLATFORM" = OS_LINUX ]; then $CXX $COMMON_FLAGS $PLATFORM_SHARED_CFLAGS -x c++ -c - -o test_dl.o 2>/dev/null <(clock) -#endif // ROCKSDB_SUPPORT_THREAD_LOCAL && !NIOSTATS_CONTEXT +#endif // !NIOSTATS_CONTEXT diff --git a/monitoring/perf_context.cc b/monitoring/perf_context.cc index 9e56f10188..493642f828 100644 --- a/monitoring/perf_context.cc +++ b/monitoring/perf_context.cc @@ -13,14 +13,8 @@ namespace ROCKSDB_NAMESPACE { // Should not be used because the counters are not thread-safe. // Put here just to make get_perf_context() simple without ifdef. PerfContext perf_context; -#elif defined(ROCKSDB_SUPPORT_THREAD_LOCAL) -#if defined(OS_SOLARIS) -__thread PerfContext perf_context; -#else // OS_SOLARIS -thread_local PerfContext perf_context; -#endif // OS_SOLARIS #else -#error "No thread-local support. Disable perf context with -DNPERF_CONTEXT." +thread_local PerfContext perf_context; #endif PerfContext* get_perf_context() { @@ -28,7 +22,7 @@ PerfContext* get_perf_context() { } PerfContext::~PerfContext() { -#if !defined(NPERF_CONTEXT) && defined(ROCKSDB_SUPPORT_THREAD_LOCAL) && !defined(OS_SOLARIS) +#if !defined(NPERF_CONTEXT) && !defined(OS_SOLARIS) ClearPerLevelPerfContext(); #endif } diff --git a/monitoring/perf_context_imp.h b/monitoring/perf_context_imp.h index d1804067cf..5b66ff2ff9 100644 --- a/monitoring/perf_context_imp.h +++ b/monitoring/perf_context_imp.h @@ -9,11 +9,11 @@ #include "util/stop_watch.h" namespace ROCKSDB_NAMESPACE { -#if defined(NPERF_CONTEXT) || !defined(ROCKSDB_SUPPORT_THREAD_LOCAL) +#if defined(NPERF_CONTEXT) extern PerfContext perf_context; #else #if defined(OS_SOLARIS) -extern __thread PerfContext perf_context_; +extern thread_local PerfContext perf_context_; #define perf_context (*get_perf_context()) #else extern thread_local PerfContext perf_context; diff --git a/monitoring/perf_level.cc b/monitoring/perf_level.cc index 27bff0d281..9190af3021 100644 --- a/monitoring/perf_level.cc +++ b/monitoring/perf_level.cc @@ -9,11 +9,7 @@ namespace ROCKSDB_NAMESPACE { -#ifdef ROCKSDB_SUPPORT_THREAD_LOCAL -__thread PerfLevel perf_level = kEnableCount; -#else -PerfLevel perf_level = kEnableCount; -#endif +thread_local PerfLevel perf_level = kEnableCount; void SetPerfLevel(PerfLevel level) { assert(level > kUninitialized); diff --git a/monitoring/perf_level_imp.h b/monitoring/perf_level_imp.h index 01277af576..68540e1257 100644 --- a/monitoring/perf_level_imp.h +++ b/monitoring/perf_level_imp.h @@ -9,10 +9,6 @@ namespace ROCKSDB_NAMESPACE { -#ifdef ROCKSDB_SUPPORT_THREAD_LOCAL -extern __thread PerfLevel perf_level; -#else -extern PerfLevel perf_level; -#endif +extern thread_local PerfLevel perf_level; } // namespace ROCKSDB_NAMESPACE diff --git a/monitoring/thread_status_updater.cc b/monitoring/thread_status_updater.cc index 267a0c0b08..9707d22656 100644 --- a/monitoring/thread_status_updater.cc +++ b/monitoring/thread_status_updater.cc @@ -16,7 +16,8 @@ namespace ROCKSDB_NAMESPACE { #ifdef ROCKSDB_USING_THREAD_STATUS -__thread ThreadStatusData* ThreadStatusUpdater::thread_status_data_ = nullptr; +thread_local ThreadStatusData* ThreadStatusUpdater::thread_status_data_ = + nullptr; void ThreadStatusUpdater::RegisterThread(ThreadStatus::ThreadType ttype, uint64_t thread_id) { diff --git a/monitoring/thread_status_updater.h b/monitoring/thread_status_updater.h index ac8c0f35da..792d4208f0 100644 --- a/monitoring/thread_status_updater.h +++ b/monitoring/thread_status_updater.h @@ -196,7 +196,7 @@ class ThreadStatusUpdater { protected: #ifdef ROCKSDB_USING_THREAD_STATUS // The thread-local variable for storing thread status. - static __thread ThreadStatusData* thread_status_data_; + static thread_local ThreadStatusData* thread_status_data_; // Returns the pointer to the thread status data only when the // thread status data is non-null and has enable_tracking == true. diff --git a/monitoring/thread_status_util.cc b/monitoring/thread_status_util.cc index 37fcb9f486..c07b85fa8c 100644 --- a/monitoring/thread_status_util.cc +++ b/monitoring/thread_status_util.cc @@ -12,9 +12,9 @@ namespace ROCKSDB_NAMESPACE { #ifdef ROCKSDB_USING_THREAD_STATUS -__thread ThreadStatusUpdater* ThreadStatusUtil::thread_updater_local_cache_ = - nullptr; -__thread bool ThreadStatusUtil::thread_updater_initialized_ = false; +thread_local ThreadStatusUpdater* + ThreadStatusUtil::thread_updater_local_cache_ = nullptr; +thread_local bool ThreadStatusUtil::thread_updater_initialized_ = false; void ThreadStatusUtil::RegisterThread(const Env* env, ThreadStatus::ThreadType thread_type) { diff --git a/monitoring/thread_status_util.h b/monitoring/thread_status_util.h index b4d97b0b63..70ef4e2ebc 100644 --- a/monitoring/thread_status_util.h +++ b/monitoring/thread_status_util.h @@ -94,7 +94,7 @@ class ThreadStatusUtil { // When this variable is set to true, thread_updater_local_cache_ // will not be updated until this variable is again set to false // in UnregisterThread(). - static __thread bool thread_updater_initialized_; + static thread_local bool thread_updater_initialized_; // The thread-local cached ThreadStatusUpdater that caches the // thread_status_updater_ of the first Env that uses any ThreadStatusUtil @@ -109,7 +109,7 @@ class ThreadStatusUtil { // When thread_updater_initialized_ is set to true, this variable // will not be updated until this thread_updater_initialized_ is // again set to false in UnregisterThread(). - static __thread ThreadStatusUpdater* thread_updater_local_cache_; + static thread_local ThreadStatusUpdater* thread_updater_local_cache_; #else static bool thread_updater_initialized_; static ThreadStatusUpdater* thread_updater_local_cache_; diff --git a/port/port_posix.h b/port/port_posix.h index e23b828239..925ec07d46 100644 --- a/port/port_posix.h +++ b/port/port_posix.h @@ -25,11 +25,6 @@ #define ROCKSDB_NOEXCEPT noexcept -// thread_local is part of C++11 and later (TODO: clean up this define) -#ifndef __thread -#define __thread thread_local -#endif - #undef PLATFORM_IS_LITTLE_ENDIAN #if defined(OS_MACOSX) #include diff --git a/port/win/port_win.h b/port/win/port_win.h index 6894758d36..1ee30cba40 100644 --- a/port/win/port_win.h +++ b/port/win/port_win.h @@ -60,11 +60,6 @@ using ssize_t = SSIZE_T; #ifdef _MSC_VER #define __attribute__(A) -// thread_local is part of C++11 and later (TODO: clean up this define) -#ifndef __thread -#define __thread thread_local -#endif - #endif namespace ROCKSDB_NAMESPACE { diff --git a/util/random.cc b/util/random.cc index f1ab029391..5d9f4bc67c 100644 --- a/util/random.cc +++ b/util/random.cc @@ -14,11 +14,7 @@ #include "port/likely.h" #include "util/thread_local.h" -#ifdef ROCKSDB_SUPPORT_THREAD_LOCAL -#define STORAGE_DECL static __thread -#else -#define STORAGE_DECL static -#endif +#define STORAGE_DECL static thread_local namespace ROCKSDB_NAMESPACE { diff --git a/util/thread_local.cc b/util/thread_local.cc index 0945b606bb..61c5f59dcf 100644 --- a/util/thread_local.cc +++ b/util/thread_local.cc @@ -140,20 +140,15 @@ private: // The private mutex. Developers should always use Mutex() instead of // using this variable directly. port::Mutex mutex_; -#ifdef ROCKSDB_SUPPORT_THREAD_LOCAL // Thread local storage - static __thread ThreadData* tls_; -#endif + static thread_local ThreadData* tls_; // Used to make thread exit trigger possible if !defined(OS_MACOSX). // Otherwise, used to retrieve thread data. pthread_key_t pthread_key_; }; - -#ifdef ROCKSDB_SUPPORT_THREAD_LOCAL -__thread ThreadData* ThreadLocalPtr::StaticMeta::tls_ = nullptr; -#endif +thread_local ThreadData* ThreadLocalPtr::StaticMeta::tls_ = nullptr; // Windows doesn't support a per-thread destructor with its // TLS primitives. So, we build it manually by inserting a @@ -263,13 +258,10 @@ ThreadLocalPtr::StaticMeta* ThreadLocalPtr::Instance() { // the following variable will go first, then OnThreadExit, therefore causing // invalid access. // - // The above problem can be solved by using thread_local to store tls_ instead - // of using __thread. The major difference between thread_local and __thread - // is that thread_local supports dynamic construction and destruction of + // The above problem can be solved by using thread_local to store tls_. + // thread_local supports dynamic construction and destruction of // non-primitive typed variables. As a result, we can guarantee the // destruction order even when the main thread dies before any child threads. - // However, thread_local is not supported in all compilers that accept -std=c++11 - // (e.g., eg Mac with XCode < 8. XCode 8+ supports thread_local). static ThreadLocalPtr::StaticMeta* inst = new ThreadLocalPtr::StaticMeta(); return inst; } @@ -328,10 +320,6 @@ ThreadLocalPtr::StaticMeta::StaticMeta() #if !defined(OS_WIN) static struct A { ~A() { -#ifndef ROCKSDB_SUPPORT_THREAD_LOCAL - ThreadData* tls_ = - static_cast(pthread_getspecific(Instance()->pthread_key_)); -#endif if (tls_) { OnThreadExit(tls_); } @@ -366,13 +354,6 @@ void ThreadLocalPtr::StaticMeta::RemoveThreadData( } ThreadData* ThreadLocalPtr::StaticMeta::GetThreadLocal() { -#ifndef ROCKSDB_SUPPORT_THREAD_LOCAL - // Make this local variable name look like a member variable so that we - // can share all the code below - ThreadData* tls_ = - static_cast(pthread_getspecific(Instance()->pthread_key_)); -#endif - if (UNLIKELY(tls_ == nullptr)) { auto* inst = Instance(); tls_ = new ThreadData(inst);