thread_identity.cc 6.5 KB

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