abseil-cpp/absl/base/internal/thread_identity.cc
Go to the documentation of this file.
1 // Copyright 2017 The Abseil Authors.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #include "absl/base/internal/thread_identity.h"
16 
17 #if !defined(_WIN32) || defined(__MINGW32__)
18 #include <pthread.h>
19 #include <signal.h>
20 #endif
21 
22 #include <atomic>
23 #include <cassert>
24 #include <memory>
25 
26 #include "absl/base/attributes.h"
27 #include "absl/base/call_once.h"
28 #include "absl/base/internal/raw_logging.h"
29 #include "absl/base/internal/spinlock.h"
30 
31 namespace absl {
33 namespace base_internal {
34 
35 #if ABSL_THREAD_IDENTITY_MODE != ABSL_THREAD_IDENTITY_MODE_USE_CPP11
36 namespace {
37 // Used to co-ordinate one-time creation of our pthread_key
38 absl::once_flag init_thread_identity_key_once;
39 pthread_key_t thread_identity_pthread_key;
40 std::atomic<bool> pthread_key_initialized(false);
41 
42 void AllocateThreadIdentityKey(ThreadIdentityReclaimerFunction reclaimer) {
43  pthread_key_create(&thread_identity_pthread_key, reclaimer);
44  pthread_key_initialized.store(true, std::memory_order_release);
45 }
46 } // namespace
47 #endif
48 
49 #if ABSL_THREAD_IDENTITY_MODE == ABSL_THREAD_IDENTITY_MODE_USE_TLS || \
50  ABSL_THREAD_IDENTITY_MODE == ABSL_THREAD_IDENTITY_MODE_USE_CPP11
51 // The actual TLS storage for a thread's currently associated ThreadIdentity.
52 // This is referenced by inline accessors in the header.
53 // "protected" visibility ensures that if multiple instances of Abseil code
54 // exist within a process (via dlopen() or similar), references to
55 // thread_identity_ptr from each instance of the code will refer to
56 // *different* instances of this ptr.
57 // Apple platforms have the visibility attribute, but issue a compile warning
58 // that protected visibility is unsupported.
59 ABSL_CONST_INIT // Must come before __attribute__((visibility("protected")))
60 #if ABSL_HAVE_ATTRIBUTE(visibility) && !defined(__APPLE__)
61 __attribute__((visibility("protected")))
62 #endif // ABSL_HAVE_ATTRIBUTE(visibility) && !defined(__APPLE__)
63 #if ABSL_PER_THREAD_TLS
64 // Prefer __thread to thread_local as benchmarks indicate it is a bit faster.
65 ABSL_PER_THREAD_TLS_KEYWORD ThreadIdentity* thread_identity_ptr = nullptr;
66 #elif defined(ABSL_HAVE_THREAD_LOCAL)
67 thread_local ThreadIdentity* thread_identity_ptr = nullptr;
68 #endif // ABSL_PER_THREAD_TLS
69 #endif // TLS or CPP11
70 
72  ThreadIdentity* identity, ThreadIdentityReclaimerFunction reclaimer) {
73  assert(CurrentThreadIdentityIfPresent() == nullptr);
74  // Associate our destructor.
75  // NOTE: This call to pthread_setspecific is currently the only immovable
76  // barrier to CurrentThreadIdentity() always being async signal safe.
77 #if ABSL_THREAD_IDENTITY_MODE == ABSL_THREAD_IDENTITY_MODE_USE_POSIX_SETSPECIFIC
78  // NOTE: Not async-safe. But can be open-coded.
79  absl::call_once(init_thread_identity_key_once, AllocateThreadIdentityKey,
80  reclaimer);
81 
82 #if defined(__EMSCRIPTEN__) || defined(__MINGW32__)
83  // Emscripten and MinGW pthread implementations does not support signals.
84  // See https://kripken.github.io/emscripten-site/docs/porting/pthreads.html
85  // for more information.
86  pthread_setspecific(thread_identity_pthread_key,
87  reinterpret_cast<void*>(identity));
88 #else
89  // We must mask signals around the call to setspecific as with current glibc,
90  // a concurrent getspecific (needed for GetCurrentThreadIdentityIfPresent())
91  // may zero our value.
92  //
93  // While not officially async-signal safe, getspecific within a signal handler
94  // is otherwise OK.
95  sigset_t all_signals;
96  sigset_t curr_signals;
97  sigfillset(&all_signals);
98  pthread_sigmask(SIG_SETMASK, &all_signals, &curr_signals);
99  pthread_setspecific(thread_identity_pthread_key,
100  reinterpret_cast<void*>(identity));
101  pthread_sigmask(SIG_SETMASK, &curr_signals, nullptr);
102 #endif // !__EMSCRIPTEN__ && !__MINGW32__
103 
104 #elif ABSL_THREAD_IDENTITY_MODE == ABSL_THREAD_IDENTITY_MODE_USE_TLS
105  // NOTE: Not async-safe. But can be open-coded.
106  absl::call_once(init_thread_identity_key_once, AllocateThreadIdentityKey,
107  reclaimer);
108  pthread_setspecific(thread_identity_pthread_key,
109  reinterpret_cast<void*>(identity));
110  thread_identity_ptr = identity;
111 #elif ABSL_THREAD_IDENTITY_MODE == ABSL_THREAD_IDENTITY_MODE_USE_CPP11
112  thread_local std::unique_ptr<ThreadIdentity, ThreadIdentityReclaimerFunction>
113  holder(identity, reclaimer);
114  thread_identity_ptr = identity;
115 #else
116 #error Unimplemented ABSL_THREAD_IDENTITY_MODE
117 #endif
118 }
119 
120 #if ABSL_THREAD_IDENTITY_MODE == ABSL_THREAD_IDENTITY_MODE_USE_TLS || \
121  ABSL_THREAD_IDENTITY_MODE == ABSL_THREAD_IDENTITY_MODE_USE_CPP11
122 
123 // Please see the comment on `CurrentThreadIdentityIfPresent` in
124 // thread_identity.h. When we cannot expose thread_local variables in
125 // headers, we opt for the correct-but-slower option of not inlining this
126 // function.
127 #ifndef ABSL_INTERNAL_INLINE_CURRENT_THREAD_IDENTITY_IF_PRESENT
128 ThreadIdentity* CurrentThreadIdentityIfPresent() { return thread_identity_ptr; }
129 #endif
130 #endif
131 
133 #if ABSL_THREAD_IDENTITY_MODE == ABSL_THREAD_IDENTITY_MODE_USE_TLS || \
134  ABSL_THREAD_IDENTITY_MODE == ABSL_THREAD_IDENTITY_MODE_USE_CPP11
135  thread_identity_ptr = nullptr;
136 #elif ABSL_THREAD_IDENTITY_MODE == \
137  ABSL_THREAD_IDENTITY_MODE_USE_POSIX_SETSPECIFIC
138  // pthread_setspecific expected to clear value on destruction
139  assert(CurrentThreadIdentityIfPresent() == nullptr);
140 #endif
141 }
142 
143 #if ABSL_THREAD_IDENTITY_MODE == ABSL_THREAD_IDENTITY_MODE_USE_POSIX_SETSPECIFIC
144 ThreadIdentity* CurrentThreadIdentityIfPresent() {
145  bool initialized = pthread_key_initialized.load(std::memory_order_acquire);
146  if (!initialized) {
147  return nullptr;
148  }
149  return reinterpret_cast<ThreadIdentity*>(
150  pthread_getspecific(thread_identity_pthread_key));
151 }
152 #endif
153 
154 } // namespace base_internal
156 } // namespace absl
absl::base_internal::ThreadIdentity
Definition: abseil-cpp/absl/base/internal/thread_identity.h:137
ABSL_CONST_INIT
#define ABSL_CONST_INIT
Definition: abseil-cpp/absl/base/attributes.h:716
ABSL_NAMESPACE_END
#define ABSL_NAMESPACE_END
Definition: third_party/abseil-cpp/absl/base/config.h:171
initialized
bool initialized
Definition: timer_generic.cc:223
absl::base_internal::CurrentThreadIdentityIfPresent
ThreadIdentity * CurrentThreadIdentityIfPresent()
Definition: abseil-cpp/absl/base/internal/thread_identity.cc:128
absl::call_once
void call_once(absl::once_flag &flag, Callable &&fn, Args &&... args)
Definition: abseil-cpp/absl/base/call_once.h:206
ABSL_NAMESPACE_BEGIN
#define ABSL_NAMESPACE_BEGIN
Definition: third_party/abseil-cpp/absl/base/config.h:170
__attribute__
__attribute__(void) start
ABSL_PER_THREAD_TLS_KEYWORD
#define ABSL_PER_THREAD_TLS_KEYWORD
Definition: abseil-cpp/absl/base/internal/per_thread_tls.h:48
absl::base_internal::SetCurrentThreadIdentity
ABSL_CONST_INIT void SetCurrentThreadIdentity(ThreadIdentity *identity, ThreadIdentityReclaimerFunction reclaimer)
Definition: abseil-cpp/absl/base/internal/thread_identity.cc:71
absl::base_internal::ClearCurrentThreadIdentity
void ClearCurrentThreadIdentity()
Definition: abseil-cpp/absl/base/internal/thread_identity.cc:132
absl::base_internal::ThreadIdentityReclaimerFunction
void(*)(void *) ThreadIdentityReclaimerFunction
Definition: abseil-cpp/absl/base/internal/thread_identity.h:175
absl
Definition: abseil-cpp/absl/algorithm/algorithm.h:31
absl::once_flag
Definition: abseil-cpp/absl/base/call_once.h:85


grpc
Author(s):
autogenerated on Fri May 16 2025 03:00:35