• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #ifndef _WIN32
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/call_once.h"
27 #include "absl/base/internal/raw_logging.h"
28 #include "absl/base/internal/spinlock.h"
29 
30 namespace absl {
31 ABSL_NAMESPACE_BEGIN
32 namespace base_internal {
33 
34 #if ABSL_THREAD_IDENTITY_MODE != ABSL_THREAD_IDENTITY_MODE_USE_CPP11
35 namespace {
36 // Used to co-ordinate one-time creation of our pthread_key
37 absl::once_flag init_thread_identity_key_once;
38 pthread_key_t thread_identity_pthread_key;
39 std::atomic<bool> pthread_key_initialized(false);
40 
AllocateThreadIdentityKey(ThreadIdentityReclaimerFunction reclaimer)41 void AllocateThreadIdentityKey(ThreadIdentityReclaimerFunction reclaimer) {
42   pthread_key_create(&thread_identity_pthread_key, reclaimer);
43   pthread_key_initialized.store(true, std::memory_order_release);
44 }
45 }  // namespace
46 #endif
47 
48 #if ABSL_THREAD_IDENTITY_MODE == ABSL_THREAD_IDENTITY_MODE_USE_TLS || \
49     ABSL_THREAD_IDENTITY_MODE == ABSL_THREAD_IDENTITY_MODE_USE_CPP11
50 // The actual TLS storage for a thread's currently associated ThreadIdentity.
51 // This is referenced by inline accessors in the header.
52 // "protected" visibility ensures that if multiple instances of Abseil code
53 // exist within a process (via dlopen() or similar), references to
54 // thread_identity_ptr from each instance of the code will refer to
55 // *different* instances of this ptr.
56 #ifdef __GNUC__
57 __attribute__((visibility("protected")))
58 #endif  // __GNUC__
59 #if ABSL_PER_THREAD_TLS
60 // Prefer __thread to thread_local as benchmarks indicate it is a bit faster.
61 ABSL_PER_THREAD_TLS_KEYWORD ThreadIdentity* thread_identity_ptr = nullptr;
62 #elif defined(ABSL_HAVE_THREAD_LOCAL)
63 thread_local ThreadIdentity* thread_identity_ptr = nullptr;
64 #endif  // ABSL_PER_THREAD_TLS
65 #endif  // TLS or CPP11
66 
SetCurrentThreadIdentity(ThreadIdentity * identity,ThreadIdentityReclaimerFunction reclaimer)67 void SetCurrentThreadIdentity(
68     ThreadIdentity* identity, ThreadIdentityReclaimerFunction reclaimer) {
69   assert(CurrentThreadIdentityIfPresent() == nullptr);
70   // Associate our destructor.
71   // NOTE: This call to pthread_setspecific is currently the only immovable
72   // barrier to CurrentThreadIdentity() always being async signal safe.
73 #if ABSL_THREAD_IDENTITY_MODE == ABSL_THREAD_IDENTITY_MODE_USE_POSIX_SETSPECIFIC
74   // NOTE: Not async-safe.  But can be open-coded.
75   absl::call_once(init_thread_identity_key_once, AllocateThreadIdentityKey,
76                   reclaimer);
77 
78 #if defined(__EMSCRIPTEN__) || defined(__MINGW32__)
79   // Emscripten and MinGW pthread implementations does not support signals.
80   // See https://kripken.github.io/emscripten-site/docs/porting/pthreads.html
81   // for more information.
82   pthread_setspecific(thread_identity_pthread_key,
83                       reinterpret_cast<void*>(identity));
84 #else
85   // We must mask signals around the call to setspecific as with current glibc,
86   // a concurrent getspecific (needed for GetCurrentThreadIdentityIfPresent())
87   // may zero our value.
88   //
89   // While not officially async-signal safe, getspecific within a signal handler
90   // is otherwise OK.
91   sigset_t all_signals;
92   sigset_t curr_signals;
93   sigfillset(&all_signals);
94   pthread_sigmask(SIG_SETMASK, &all_signals, &curr_signals);
95   pthread_setspecific(thread_identity_pthread_key,
96                       reinterpret_cast<void*>(identity));
97   pthread_sigmask(SIG_SETMASK, &curr_signals, nullptr);
98 #endif  // !__EMSCRIPTEN__ && !__MINGW32__
99 
100 #elif ABSL_THREAD_IDENTITY_MODE == ABSL_THREAD_IDENTITY_MODE_USE_TLS
101   // NOTE: Not async-safe.  But can be open-coded.
102   absl::call_once(init_thread_identity_key_once, AllocateThreadIdentityKey,
103                   reclaimer);
104   pthread_setspecific(thread_identity_pthread_key,
105                       reinterpret_cast<void*>(identity));
106   thread_identity_ptr = identity;
107 #elif ABSL_THREAD_IDENTITY_MODE == ABSL_THREAD_IDENTITY_MODE_USE_CPP11
108   thread_local std::unique_ptr<ThreadIdentity, ThreadIdentityReclaimerFunction>
109       holder(identity, reclaimer);
110   thread_identity_ptr = identity;
111 #else
112 #error Unimplemented ABSL_THREAD_IDENTITY_MODE
113 #endif
114 }
115 
116 #if ABSL_THREAD_IDENTITY_MODE == ABSL_THREAD_IDENTITY_MODE_USE_TLS || \
117     ABSL_THREAD_IDENTITY_MODE == ABSL_THREAD_IDENTITY_MODE_USE_CPP11
118 
119 // Please see the comment on `CurrentThreadIdentityIfPresent` in
120 // thread_identity.h. Because DLLs cannot expose thread_local variables in
121 // headers, we opt for the correct-but-slower option of placing the definition
122 // of this function only in a translation unit inside DLL.
123 #if defined(ABSL_BUILD_DLL) || defined(ABSL_CONSUME_DLL)
CurrentThreadIdentityIfPresent()124 ThreadIdentity* CurrentThreadIdentityIfPresent() { return thread_identity_ptr; }
125 #endif
126 #endif
127 
ClearCurrentThreadIdentity()128 void ClearCurrentThreadIdentity() {
129 #if ABSL_THREAD_IDENTITY_MODE == ABSL_THREAD_IDENTITY_MODE_USE_TLS || \
130     ABSL_THREAD_IDENTITY_MODE == ABSL_THREAD_IDENTITY_MODE_USE_CPP11
131   thread_identity_ptr = nullptr;
132 #elif ABSL_THREAD_IDENTITY_MODE == \
133       ABSL_THREAD_IDENTITY_MODE_USE_POSIX_SETSPECIFIC
134   // pthread_setspecific expected to clear value on destruction
135   assert(CurrentThreadIdentityIfPresent() == nullptr);
136 #endif
137 }
138 
139 #if ABSL_THREAD_IDENTITY_MODE == ABSL_THREAD_IDENTITY_MODE_USE_POSIX_SETSPECIFIC
CurrentThreadIdentityIfPresent()140 ThreadIdentity* CurrentThreadIdentityIfPresent() {
141   bool initialized = pthread_key_initialized.load(std::memory_order_acquire);
142   if (!initialized) {
143     return nullptr;
144   }
145   return reinterpret_cast<ThreadIdentity*>(
146       pthread_getspecific(thread_identity_pthread_key));
147 }
148 #endif
149 
150 }  // namespace base_internal
151 ABSL_NAMESPACE_END
152 }  // namespace absl
153