• 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 #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 {
32 ABSL_NAMESPACE_BEGIN
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 
AllocateThreadIdentityKey(ThreadIdentityReclaimerFunction reclaimer)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
65     // faster.
66     ABSL_PER_THREAD_TLS_KEYWORD ThreadIdentity* thread_identity_ptr = nullptr;
67 #elif defined(ABSL_HAVE_THREAD_LOCAL)
68     thread_local ThreadIdentity* thread_identity_ptr = nullptr;
69 #endif  // ABSL_PER_THREAD_TLS
70 #endif  // TLS or CPP11
71 
SetCurrentThreadIdentity(ThreadIdentity * identity,ThreadIdentityReclaimerFunction reclaimer)72 void SetCurrentThreadIdentity(ThreadIdentity* identity,
73                               ThreadIdentityReclaimerFunction reclaimer) {
74   assert(CurrentThreadIdentityIfPresent() == nullptr);
75   // Associate our destructor.
76   // NOTE: This call to pthread_setspecific is currently the only immovable
77   // barrier to CurrentThreadIdentity() always being async signal safe.
78 #if ABSL_THREAD_IDENTITY_MODE == ABSL_THREAD_IDENTITY_MODE_USE_POSIX_SETSPECIFIC
79   // NOTE: Not async-safe.  But can be open-coded.
80   absl::call_once(init_thread_identity_key_once, AllocateThreadIdentityKey,
81                   reclaimer);
82 
83 #if defined(__EMSCRIPTEN__) || defined(__MINGW32__) || defined(__hexagon__)
84   // Emscripten and MinGW pthread implementations does not support signals.
85   // See https://kripken.github.io/emscripten-site/docs/porting/pthreads.html
86   // for more information.
87   pthread_setspecific(thread_identity_pthread_key,
88                       reinterpret_cast<void*>(identity));
89 #else
90   // We must mask signals around the call to setspecific as with current glibc,
91   // a concurrent getspecific (needed for GetCurrentThreadIdentityIfPresent())
92   // may zero our value.
93   //
94   // While not officially async-signal safe, getspecific within a signal handler
95   // is otherwise OK.
96   sigset_t all_signals;
97   sigset_t curr_signals;
98   sigfillset(&all_signals);
99   pthread_sigmask(SIG_SETMASK, &all_signals, &curr_signals);
100   pthread_setspecific(thread_identity_pthread_key,
101                       reinterpret_cast<void*>(identity));
102   pthread_sigmask(SIG_SETMASK, &curr_signals, nullptr);
103 #endif  // !__EMSCRIPTEN__ && !__MINGW32__
104 
105 #elif ABSL_THREAD_IDENTITY_MODE == ABSL_THREAD_IDENTITY_MODE_USE_TLS
106   // NOTE: Not async-safe.  But can be open-coded.
107   absl::call_once(init_thread_identity_key_once, AllocateThreadIdentityKey,
108                   reclaimer);
109   pthread_setspecific(thread_identity_pthread_key,
110                       reinterpret_cast<void*>(identity));
111   thread_identity_ptr = identity;
112 #elif ABSL_THREAD_IDENTITY_MODE == ABSL_THREAD_IDENTITY_MODE_USE_CPP11
113   thread_local std::unique_ptr<ThreadIdentity, ThreadIdentityReclaimerFunction>
114       holder(identity, reclaimer);
115   thread_identity_ptr = identity;
116 #else
117 #error Unimplemented ABSL_THREAD_IDENTITY_MODE
118 #endif
119 }
120 
121 #if ABSL_THREAD_IDENTITY_MODE == ABSL_THREAD_IDENTITY_MODE_USE_TLS || \
122     ABSL_THREAD_IDENTITY_MODE == ABSL_THREAD_IDENTITY_MODE_USE_CPP11
123 
124 // Please see the comment on `CurrentThreadIdentityIfPresent` in
125 // thread_identity.h. When we cannot expose thread_local variables in
126 // headers, we opt for the correct-but-slower option of not inlining this
127 // function.
128 #ifndef ABSL_INTERNAL_INLINE_CURRENT_THREAD_IDENTITY_IF_PRESENT
CurrentThreadIdentityIfPresent()129 ThreadIdentity* CurrentThreadIdentityIfPresent() { return thread_identity_ptr; }
130 #endif
131 #endif
132 
ClearCurrentThreadIdentity()133 void ClearCurrentThreadIdentity() {
134 #if ABSL_THREAD_IDENTITY_MODE == ABSL_THREAD_IDENTITY_MODE_USE_TLS || \
135     ABSL_THREAD_IDENTITY_MODE == ABSL_THREAD_IDENTITY_MODE_USE_CPP11
136   thread_identity_ptr = nullptr;
137 #elif ABSL_THREAD_IDENTITY_MODE == \
138     ABSL_THREAD_IDENTITY_MODE_USE_POSIX_SETSPECIFIC
139   // pthread_setspecific expected to clear value on destruction
140   assert(CurrentThreadIdentityIfPresent() == nullptr);
141 #endif
142 }
143 
144 #if ABSL_THREAD_IDENTITY_MODE == ABSL_THREAD_IDENTITY_MODE_USE_POSIX_SETSPECIFIC
CurrentThreadIdentityIfPresent()145 ThreadIdentity* CurrentThreadIdentityIfPresent() {
146   bool initialized = pthread_key_initialized.load(std::memory_order_acquire);
147   if (!initialized) {
148     return nullptr;
149   }
150   return reinterpret_cast<ThreadIdentity*>(
151       pthread_getspecific(thread_identity_pthread_key));
152 }
153 #endif
154 
155 }  // namespace base_internal
156 ABSL_NAMESPACE_END
157 }  // namespace absl
158