This is an automated email from the ASF dual-hosted git repository.

alexey pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/kudu.git


The following commit(s) were added to refs/heads/master by this push:
     new 8bbf0d2f0 [rpc,util] __thread --> thread_local
8bbf0d2f0 is described below

commit 8bbf0d2f05a5cd31850eaecd618ba582d9221a23
Author: Alexey Serbin <[email protected]>
AuthorDate: Wed Nov 19 15:16:09 2025 -0800

    [rpc,util] __thread --> thread_local
    
    This patch changes '__thread' GCC-specific extension to the C++11-based
    'thread_local' for better portability.  At supported platforms, Kudu
    de facto compiles with GCC/G++ and CLANG only (both support '__thread'
    and treat it the same as 'thread_local'), but it doesn't hurt to have
    more portable code.
    
    I also fixed typo in service_queue.h and changed include guards
    to 'pragma once' in threadlocal.h and striped64.h.
    
    There are no functional modifications in this changelist.
    
    Change-Id: I237f707f2a8755796ad14effeef007c670641643
    Reviewed-on: http://gerrit.cloudera.org:8080/23696
    Tested-by: Kudu Jenkins
    Reviewed-by: Ashwani Raina <[email protected]>
    Reviewed-by: Abhishek Chennaka <[email protected]>
---
 src/kudu/rpc/sasl_common.cc             |  4 ++--
 src/kudu/rpc/service_queue.h            |  2 +-
 src/kudu/util/debug/trace_event_impl.cc |  4 ++--
 src/kudu/util/debug/trace_event_impl.h  |  2 +-
 src/kudu/util/debug/unwind_safeness.cc  |  2 +-
 src/kudu/util/env_posix.cc              |  2 +-
 src/kudu/util/kernel_stack_watchdog.cc  |  2 +-
 src/kudu/util/logging.cc                |  4 ++--
 src/kudu/util/logging.h                 |  2 +-
 src/kudu/util/spinlock_profiling.cc     |  2 +-
 src/kudu/util/striped64.cc              |  2 +-
 src/kudu/util/striped64.h               |  7 ++-----
 src/kudu/util/thread.cc                 |  2 +-
 src/kudu/util/thread.h                  |  2 +-
 src/kudu/util/threadlocal.h             | 29 +++++++++++++----------------
 src/kudu/util/threadlocal_cache.h       |  2 +-
 src/kudu/util/trace.cc                  |  2 +-
 src/kudu/util/trace.h                   |  2 +-
 18 files changed, 34 insertions(+), 40 deletions(-)

diff --git a/src/kudu/rpc/sasl_common.cc b/src/kudu/rpc/sasl_common.cc
index ab0c3b758..57c877838 100644
--- a/src/kudu/rpc/sasl_common.cc
+++ b/src/kudu/rpc/sasl_common.cc
@@ -57,7 +57,7 @@ const char* const kSaslMechGSSAPI = "GSSAPI";
 extern const size_t kSaslMaxBufSize = 1024;
 
 // See WrapSaslCall().
-static __thread string* g_auth_failure_capture = nullptr;
+static thread_local string* g_auth_failure_capture = nullptr;
 
 // Determine whether initialization was ever called
 static Status sasl_init_status = Status::OK();
@@ -138,7 +138,7 @@ static int SaslGetOption(void* context, const char* 
plugin_name, const char* opt
       // The library's contract for this method is that the caller gets to keep
       // the returned buffer until the next call by the same thread, so we use 
a
       // threadlocal for the buffer.
-      static __thread char buf[4];
+      static thread_local char buf[4];
       snprintf(buf, arraysize(buf), "%d", level);
       *result = buf;
       if (len != nullptr) *len = strlen(buf);
diff --git a/src/kudu/rpc/service_queue.h b/src/kudu/rpc/service_queue.h
index 8cbf1dca6..55d9e5b63 100644
--- a/src/kudu/rpc/service_queue.h
+++ b/src/kudu/rpc/service_queue.h
@@ -198,7 +198,7 @@ class LifoServiceQueue final {
     return ret;
   }
 
-  thread_local static ConsumerState* tl_consumer_;
+  static thread_local ConsumerState* tl_consumer_;
 
   const size_t max_queue_size_;
   mutable simple_spinlock lock_;
diff --git a/src/kudu/util/debug/trace_event_impl.cc 
b/src/kudu/util/debug/trace_event_impl.cc
index 19a93c568..4ddd9a81b 100644
--- a/src/kudu/util/debug/trace_event_impl.cc
+++ b/src/kudu/util/debug/trace_event_impl.cc
@@ -62,7 +62,7 @@ using std::vector;
 namespace kudu {
 namespace debug {
 
-__thread TraceLog::PerThreadInfo* TraceLog::thread_local_info_ = nullptr;
+thread_local TraceLog::PerThreadInfo* TraceLog::thread_local_info_ = nullptr;
 
 namespace {
 
@@ -106,7 +106,7 @@ AtomicWord g_category_index = kNumBuiltinCategories;
 // The name of the current thread. This is used to decide if the current
 // thread name has changed. We combine all the seen thread names into the
 // output name for the thread.
-__thread const char* g_current_thread_name = "";
+thread_local const char* g_current_thread_name = "";
 
 static void NOTIMPLEMENTED() {
   LOG(FATAL);
diff --git a/src/kudu/util/debug/trace_event_impl.h 
b/src/kudu/util/debug/trace_event_impl.h
index 8e0023aee..ae8836dbe 100644
--- a/src/kudu/util/debug/trace_event_impl.h
+++ b/src/kudu/util/debug/trace_event_impl.h
@@ -694,7 +694,7 @@ class TraceLog {
     // Returns the old value of the member.
     ThreadLocalEventBuffer* AtomicTakeBuffer();
   };
-  static __thread PerThreadInfo* thread_local_info_;
+  static thread_local PerThreadInfo* thread_local_info_;
 
   Mutex active_threads_lock_;
   // Map of PID -> PerThreadInfo
diff --git a/src/kudu/util/debug/unwind_safeness.cc 
b/src/kudu/util/debug/unwind_safeness.cc
index c8e0adf60..94d029b25 100644
--- a/src/kudu/util/debug/unwind_safeness.cc
+++ b/src/kudu/util/debug/unwind_safeness.cc
@@ -57,7 +57,7 @@ void* g_orig_dl_iterate_phdr;
 #endif
 
 // The depth of calls into libdl.
-__thread int g_unsafeness_depth;
+thread_local int g_unsafeness_depth;
 
 // Scoped helper to track the recursion depth of calls into libdl
 struct ScopedBumpDepth {
diff --git a/src/kudu/util/env_posix.cc b/src/kudu/util/env_posix.cc
index b637cccbc..a2d3ca577 100644
--- a/src/kudu/util/env_posix.cc
+++ b/src/kudu/util/env_posix.cc
@@ -2014,7 +2014,7 @@ class PosixEnv : public Env {
 
   uint64_t gettid() override {
     static std::atomic<uint64_t> cur_thread_local_id{0};
-    static __thread uint64_t thread_local_id{0};
+    static thread_local uint64_t thread_local_id{0};
     // Platform-independent thread ID.  We can't use pthread_self here,
     // because that function returns a totally opaque ID, which can't be
     // compared via normal means.
diff --git a/src/kudu/util/kernel_stack_watchdog.cc 
b/src/kudu/util/kernel_stack_watchdog.cc
index 93dd6e382..27ba30689 100644
--- a/src/kudu/util/kernel_stack_watchdog.cc
+++ b/src/kudu/util/kernel_stack_watchdog.cc
@@ -59,7 +59,7 @@ using strings::Substitute;
 
 namespace kudu {
 
-__thread KernelStackWatchdog::TLS* KernelStackWatchdog::tls_;
+thread_local KernelStackWatchdog::TLS* KernelStackWatchdog::tls_;
 
 KernelStackWatchdog::KernelStackWatchdog()
   : log_collector_(nullptr),
diff --git a/src/kudu/util/logging.cc b/src/kudu/util/logging.cc
index 215e7999a..dea18ea42 100644
--- a/src/kudu/util/logging.cc
+++ b/src/kudu/util/logging.cc
@@ -88,7 +88,7 @@ using std::ostringstream;
 
 namespace kudu {
 
-__thread bool tls_redact_user_data = true;
+thread_local bool tls_redact_user_data = true;
 kudu::RedactContext g_should_redact;
 const char* const kRedactionMessage = "<redacted>";
 
@@ -174,7 +174,7 @@ void UnregisterLoggingCallbackUnlocked() {
 void FlushCoverageOnExit() {
   // Coverage flushing is not re-entrant, but this might be called from a
   // crash signal context, so avoid re-entrancy.
-  static __thread bool in_call = false;
+  static thread_local bool in_call = false;
   if (in_call) return;
   in_call = true;
 
diff --git a/src/kudu/util/logging.h b/src/kudu/util/logging.h
index 93ba90efd..0f5b79db6 100644
--- a/src/kudu/util/logging.h
+++ b/src/kudu/util/logging.h
@@ -82,7 +82,7 @@ namespace kudu {
 // Defaults to enabling redaction, since it's the safer default with respect to
 // leaking user data, and it's easier to identify when data is over-redacted
 // than vice-versa.
-extern __thread bool tls_redact_user_data;
+extern thread_local bool tls_redact_user_data;
 
 // Redacted log messages are replaced with this constant.
 extern const char* const kRedactionMessage;
diff --git a/src/kudu/util/spinlock_profiling.cc 
b/src/kudu/util/spinlock_profiling.cc
index 9bfdf5603..27a233a8a 100644
--- a/src/kudu/util/spinlock_profiling.cc
+++ b/src/kudu/util/spinlock_profiling.cc
@@ -221,7 +221,7 @@ void SubmitSpinLockProfileData(const void *contendedlock, 
int64_t wait_cycles) {
     return;
   }
 
-  static __thread bool in_func = false;
+  static thread_local bool in_func = false;
   if (in_func) return; // non-re-entrant
   in_func = true;
 
diff --git a/src/kudu/util/striped64.cc b/src/kudu/util/striped64.cc
index 6f60ff404..7a25dccd7 100644
--- a/src/kudu/util/striped64.cc
+++ b/src/kudu/util/striped64.cc
@@ -54,7 +54,7 @@ Cell::Cell()
 //
 // Striped64
 //
-__thread uint64_t Striped64::tls_hashcode_ = 0;
+thread_local uint64_t Striped64::tls_hashcode_ = 0;
 
 namespace {
 const uint32_t kNumCpus = sysconf(_SC_NPROCESSORS_ONLN);
diff --git a/src/kudu/util/striped64.h b/src/kudu/util/striped64.h
index 48332e1e7..a08ee1cd1 100644
--- a/src/kudu/util/striped64.h
+++ b/src/kudu/util/striped64.h
@@ -15,8 +15,7 @@
 // specific language governing permissions and limitations
 // under the License.
 
-#ifndef KUDU_UTIL_STRIPED64_H_
-#define KUDU_UTIL_STRIPED64_H_
+#pragma once
 
 #include <atomic>
 #include <cstdint>
@@ -136,7 +135,7 @@ class Striped64 {
   // Static hash code per-thread. Shared across all instances to limit 
thread-local pollution.
   // Also, if a thread hits a collision on one Striped64, it's also likely to 
collide on
   // other Striped64s too.
-  static __thread uint64_t tls_hashcode_;
+  static thread_local uint64_t tls_hashcode_;
 };
 
 // A 64-bit number optimized for high-volume concurrent updates.
@@ -164,5 +163,3 @@ class LongAdder : Striped64 {
 };
 
 } // namespace kudu
-
-#endif
diff --git a/src/kudu/util/thread.cc b/src/kudu/util/thread.cc
index c589f8f1b..c11434813 100644
--- a/src/kudu/util/thread.cc
+++ b/src/kudu/util/thread.cc
@@ -165,7 +165,7 @@ static int32_t GetSchedulingPriority() {
   return prio;
 }
 
-__thread Thread* Thread::tls_ = nullptr;
+thread_local Thread* Thread::tls_ = nullptr;
 
 // A singleton class that tracks all live threads, and groups them together 
for easy
 // auditing. Used only by Thread.
diff --git a/src/kudu/util/thread.h b/src/kudu/util/thread.h
index 8fb449f45..cb14f3fc3 100644
--- a/src/kudu/util/thread.h
+++ b/src/kudu/util/thread.h
@@ -272,7 +272,7 @@ class Thread final : public RefCountedThreadSafe<Thread> {
 
   // Thread local pointer to the current thread of execution. Will be NULL if 
the current
   // thread is not a Thread.
-  static __thread Thread* tls_;
+  static thread_local Thread* tls_;
 
   // Wait for the running thread to publish its tid.
   int64_t WaitForTid() const;
diff --git a/src/kudu/util/threadlocal.h b/src/kudu/util/threadlocal.h
index ebe1910d4..461225c80 100644
--- a/src/kudu/util/threadlocal.h
+++ b/src/kudu/util/threadlocal.h
@@ -14,8 +14,7 @@
 // KIND, either express or implied.  See the License for the
 // specific language governing permissions and limitations
 // under the License.
-#ifndef KUDU_UTIL_THREADLOCAL_H_
-#define KUDU_UTIL_THREADLOCAL_H_
+#pragma once
 
 // Block-scoped static thread local implementation.
 //
@@ -37,9 +36,9 @@
 // instance->DoSomething();
 //
 #define BLOCK_STATIC_THREAD_LOCAL(T, t, ...)                                   
 \
-static __thread T* t;                                                          
 \
+static thread_local T* t;                                                      
 \
 do {                                                                           
 \
-  if (PREDICT_FALSE(t == NULL)) {                                              
 \
+  if (PREDICT_FALSE(t == nullptr)) {                                           
 \
     t = new T(__VA_ARGS__);                                                    
 \
     threadlocal::internal::AddDestructor(threadlocal::internal::Destroy<T>, 
t); \
   }                                                                            
 \
@@ -89,20 +88,20 @@ do {
 // dtor must be destructed _after_ t, so it gets defined first.
 // Uses a mangled variable name for dtor since it must also be a member of the
 // class.
-#define DECLARE_STATIC_THREAD_LOCAL(T, t)                                      
               \
-static __thread T* t
+#define DECLARE_STATIC_THREAD_LOCAL(T, t)                                      
 \
+static thread_local T* t
 
 // You must also define the instance in the .cc file.
-#define DEFINE_STATIC_THREAD_LOCAL(T, Class, t)                                
               \
-__thread T* Class::t
+#define DEFINE_STATIC_THREAD_LOCAL(T, Class, t)                                
 \
+thread_local T* Class::t
 
 // Must be invoked at least once by each thread that will access t.
-#define INIT_STATIC_THREAD_LOCAL(T, t, ...)                                    
   \
-do {                                                                           
   \
-  if (PREDICT_FALSE(t == NULL)) {                                              
   \
-    t = new T(__VA_ARGS__);                                                    
   \
-    threadlocal::internal::AddDestructor(threadlocal::internal::Destroy<T>, 
t);   \
-  }                                                                            
   \
+#define INIT_STATIC_THREAD_LOCAL(T, t, ...)                                    
 \
+do {                                                                           
 \
+  if (PREDICT_FALSE(t == nullptr)) {                                           
 \
+    t = new T(__VA_ARGS__);                                                    
 \
+    threadlocal::internal::AddDestructor(threadlocal::internal::Destroy<T>, 
t); \
+  }                                                                            
 \
 } while (false)
 
 // Internal implementation below.
@@ -124,5 +123,3 @@ static void Destroy(void* t) {
 } // namespace internal
 } // namespace threadlocal
 } // namespace kudu
-
-#endif // KUDU_UTIL_THREADLOCAL_H_
diff --git a/src/kudu/util/threadlocal_cache.h 
b/src/kudu/util/threadlocal_cache.h
index 2b063e19c..6b7dad96e 100644
--- a/src/kudu/util/threadlocal_cache.h
+++ b/src/kudu/util/threadlocal_cache.h
@@ -108,6 +108,6 @@ class ThreadLocalCache {
 // We can't use DEFINE_STATIC_THREAD_LOCAL here because the commas in the
 // template arguments confuse the C preprocessor.
 template<class K, class T>
-__thread ThreadLocalCache<K,T>* ThreadLocalCache<K,T>::tl_instance_;
+thread_local ThreadLocalCache<K,T>* ThreadLocalCache<K,T>::tl_instance_;
 
 } // namespace kudu
diff --git a/src/kudu/util/trace.cc b/src/kudu/util/trace.cc
index 32ed78496..87283d5db 100644
--- a/src/kudu/util/trace.cc
+++ b/src/kudu/util/trace.cc
@@ -41,7 +41,7 @@ using strings::internal::SubstituteArg;
 
 namespace kudu {
 
-__thread Trace* Trace::threadlocal_trace_;
+thread_local Trace* Trace::threadlocal_trace_;
 
 Trace::Trace()
     : arena_(new ThreadSafeArena(1024)),
diff --git a/src/kudu/util/trace.h b/src/kudu/util/trace.h
index c30e241db..b6860294f 100644
--- a/src/kudu/util/trace.h
+++ b/src/kudu/util/trace.h
@@ -212,7 +212,7 @@ class Trace : public RefCountedThreadSafe<Trace> {
   // The current trace for this thread. Threads should only set this using
   // using ScopedAdoptTrace, which handles reference counting the underlying
   // object.
-  static __thread Trace* threadlocal_trace_;
+  static thread_local Trace* threadlocal_trace_;
 
   // Allocate a new entry from the arena, with enough space to hold a
   // message of length 'len'.

Reply via email to