thread_identity.cc 6.1 KB

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