15 #include "absl/base/internal/thread_identity.h"
17 #if !defined(_WIN32) || defined(__MINGW32__)
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"
33 namespace base_internal {
35 #if ABSL_THREAD_IDENTITY_MODE != ABSL_THREAD_IDENTITY_MODE_USE_CPP11
39 pthread_key_t thread_identity_pthread_key;
40 std::atomic<bool> pthread_key_initialized(
false);
43 pthread_key_create(&thread_identity_pthread_key, reclaimer);
44 pthread_key_initialized.store(
true, std::memory_order_release);
49 #if ABSL_THREAD_IDENTITY_MODE == ABSL_THREAD_IDENTITY_MODE_USE_TLS || \
50 ABSL_THREAD_IDENTITY_MODE == ABSL_THREAD_IDENTITY_MODE_USE_CPP11
60 #if ABSL_HAVE_ATTRIBUTE(visibility) && !defined(__APPLE__)
62 #endif // ABSL_HAVE_ATTRIBUTE(visibility) && !defined(__APPLE__)
63 #if ABSL_PER_THREAD_TLS
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
77 #if ABSL_THREAD_IDENTITY_MODE == ABSL_THREAD_IDENTITY_MODE_USE_POSIX_SETSPECIFIC
79 absl::call_once(init_thread_identity_key_once, AllocateThreadIdentityKey,
82 #if defined(__EMSCRIPTEN__) || defined(__MINGW32__)
86 pthread_setspecific(thread_identity_pthread_key,
87 reinterpret_cast<void*
>(identity));
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__
104 #elif ABSL_THREAD_IDENTITY_MODE == ABSL_THREAD_IDENTITY_MODE_USE_TLS
106 absl::call_once(init_thread_identity_key_once, AllocateThreadIdentityKey,
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;
116 #error Unimplemented ABSL_THREAD_IDENTITY_MODE
120 #if ABSL_THREAD_IDENTITY_MODE == ABSL_THREAD_IDENTITY_MODE_USE_TLS || \
121 ABSL_THREAD_IDENTITY_MODE == ABSL_THREAD_IDENTITY_MODE_USE_CPP11
127 #ifndef ABSL_INTERNAL_INLINE_CURRENT_THREAD_IDENTITY_IF_PRESENT
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
143 #if ABSL_THREAD_IDENTITY_MODE == ABSL_THREAD_IDENTITY_MODE_USE_POSIX_SETSPECIFIC
145 bool initialized = pthread_key_initialized.load(std::memory_order_acquire);
149 return reinterpret_cast<ThreadIdentity*
>(
150 pthread_getspecific(thread_identity_pthread_key));