// Copyright 2017 The Abseil Authors. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // https://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. // // Each active thread has an ThreadIdentity that may represent the thread in // various level interfaces. ThreadIdentity objects are never deallocated. // When a thread terminates, its ThreadIdentity object may be reused for a // thread created later. #ifndef ABSL_BASE_INTERNAL_THREAD_IDENTITY_H_ #define ABSL_BASE_INTERNAL_THREAD_IDENTITY_H_ #ifndef _WIN32 #include <pthread.h> // Defines __GOOGLE_GRTE_VERSION__ (via glibc-specific features.h) when // supported. #include <unistd.h> #endif #include <atomic> #include <cstdint> #include "absl/base/config.h" #include "absl/base/internal/per_thread_tls.h" #include "absl/base/optimization.h" namespace absl { ABSL_NAMESPACE_BEGIN struct SynchLocksHeld; struct SynchWaitParams; namespace base_internal { class SpinLock; struct ThreadIdentity; // Used by the implementation of absl::Mutex and absl::CondVar. struct PerThreadSynch { … }; // The instances of this class are allocated in NewThreadIdentity() with an // alignment of PerThreadSynch::kAlignment and never destroyed. Initialization // should happen in OneTimeInitThreadIdentity(). // // Instances may be reused by new threads - fields should be reset in // ResetThreadIdentityBetweenReuse(). // // NOTE: The layout of fields in this structure is critical, please do not // add, remove, or modify the field placements without fully auditing the // layout. struct ThreadIdentity { … }; // Returns the ThreadIdentity object representing the calling thread; guaranteed // to be unique for its lifetime. The returned object will remain valid for the // program's lifetime; although it may be re-assigned to a subsequent thread. // If one does not exist, return nullptr instead. // // Does not malloc(*), and is async-signal safe. // [*] Technically pthread_setspecific() does malloc on first use; however this // is handled internally within tcmalloc's initialization already. Note that // darwin does *not* use tcmalloc, so this can catch you if using MallocHooks // on Apple platforms. Whatever function is calling your MallocHooks will need // to watch for recursion on Apple platforms. // // New ThreadIdentity objects can be constructed and associated with a thread // by calling GetOrCreateCurrentThreadIdentity() in per-thread-sem.h. ThreadIdentity* CurrentThreadIdentityIfPresent(); ThreadIdentityReclaimerFunction; // Sets the current thread identity to the given value. 'reclaimer' is a // pointer to the global function for cleaning up instances on thread // destruction. void SetCurrentThreadIdentity(ThreadIdentity* identity, ThreadIdentityReclaimerFunction reclaimer); // Removes the currently associated ThreadIdentity from the running thread. // This must be called from inside the ThreadIdentityReclaimerFunction, and only // from that function. void ClearCurrentThreadIdentity(); // May be chosen at compile time via: -DABSL_FORCE_THREAD_IDENTITY_MODE=<mode // index> #ifdef ABSL_THREAD_IDENTITY_MODE_USE_POSIX_SETSPECIFIC #error ABSL_THREAD_IDENTITY_MODE_USE_POSIX_SETSPECIFIC cannot be directly set #else #define ABSL_THREAD_IDENTITY_MODE_USE_POSIX_SETSPECIFIC … #endif #ifdef ABSL_THREAD_IDENTITY_MODE_USE_TLS #error ABSL_THREAD_IDENTITY_MODE_USE_TLS cannot be directly set #else #define ABSL_THREAD_IDENTITY_MODE_USE_TLS … #endif #ifdef ABSL_THREAD_IDENTITY_MODE_USE_CPP11 #error ABSL_THREAD_IDENTITY_MODE_USE_CPP11 cannot be directly set #else #define ABSL_THREAD_IDENTITY_MODE_USE_CPP11 … #endif #ifdef ABSL_THREAD_IDENTITY_MODE #error ABSL_THREAD_IDENTITY_MODE cannot be directly set #elif defined(ABSL_FORCE_THREAD_IDENTITY_MODE) #define ABSL_THREAD_IDENTITY_MODE … #elif defined(_WIN32) && !defined(__MINGW32__) #define ABSL_THREAD_IDENTITY_MODE … #elif defined(__APPLE__) && defined(ABSL_HAVE_THREAD_LOCAL) #define ABSL_THREAD_IDENTITY_MODE … #elif ABSL_PER_THREAD_TLS && defined(__GOOGLE_GRTE_VERSION__) && \ (__GOOGLE_GRTE_VERSION__ >= 20140228L) // Support for async-safe TLS was specifically added in GRTEv4. It's not // present in the upstream eglibc. // Note: Current default for production systems. #define ABSL_THREAD_IDENTITY_MODE … #else #define ABSL_THREAD_IDENTITY_MODE … #endif #if ABSL_THREAD_IDENTITY_MODE == ABSL_THREAD_IDENTITY_MODE_USE_TLS || \ ABSL_THREAD_IDENTITY_MODE == ABSL_THREAD_IDENTITY_MODE_USE_CPP11 #if ABSL_PER_THREAD_TLS ABSL_CONST_INIT extern ABSL_PER_THREAD_TLS_KEYWORD ThreadIdentity* thread_identity_ptr; #elif defined(ABSL_HAVE_THREAD_LOCAL) ABSL_CONST_INIT extern thread_local ThreadIdentity* thread_identity_ptr; #else #error Thread-local storage not detected on this platform #endif // thread_local variables cannot be in headers exposed by DLLs or in certain // build configurations on Apple platforms. However, it is important for // performance reasons in general that `CurrentThreadIdentityIfPresent` be // inlined. In the other cases we opt to have the function not be inlined. Note // that `CurrentThreadIdentityIfPresent` is declared above so we can exclude // this entire inline definition. #if !defined(__APPLE__) && !defined(ABSL_BUILD_DLL) && \ !defined(ABSL_CONSUME_DLL) #define ABSL_INTERNAL_INLINE_CURRENT_THREAD_IDENTITY_IF_PRESENT … #endif #ifdef ABSL_INTERNAL_INLINE_CURRENT_THREAD_IDENTITY_IF_PRESENT inline ThreadIdentity* CurrentThreadIdentityIfPresent() { return thread_identity_ptr; } #endif #elif ABSL_THREAD_IDENTITY_MODE != \ ABSL_THREAD_IDENTITY_MODE_USE_POSIX_SETSPECIFIC #error Unknown ABSL_THREAD_IDENTITY_MODE #endif } // namespace base_internal ABSL_NAMESPACE_END } // namespace absl #endif // ABSL_BASE_INTERNAL_THREAD_IDENTITY_H_