create_thread_identity.cc 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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 <stdint.h>
  15. #include <new>
  16. // This file is a no-op if the required LowLevelAlloc support is missing.
  17. #include "absl/base/internal/low_level_alloc.h"
  18. #include "absl/synchronization/internal/waiter.h"
  19. #ifndef ABSL_LOW_LEVEL_ALLOC_MISSING
  20. #include <string.h>
  21. #include "absl/base/attributes.h"
  22. #include "absl/base/internal/spinlock.h"
  23. #include "absl/base/internal/thread_identity.h"
  24. #include "absl/synchronization/internal/per_thread_sem.h"
  25. namespace absl {
  26. ABSL_NAMESPACE_BEGIN
  27. namespace synchronization_internal {
  28. // ThreadIdentity storage is persistent, we maintain a free-list of previously
  29. // released ThreadIdentity objects.
  30. ABSL_CONST_INIT static base_internal::SpinLock freelist_lock(
  31. absl::kConstInit, base_internal::SCHEDULE_KERNEL_ONLY);
  32. ABSL_CONST_INIT static base_internal::ThreadIdentity* thread_identity_freelist;
  33. // A per-thread destructor for reclaiming associated ThreadIdentity objects.
  34. // Since we must preserve their storage, we cache them for re-use instead of
  35. // truly destructing the object.
  36. static void ReclaimThreadIdentity(void* v) {
  37. base_internal::ThreadIdentity* identity =
  38. static_cast<base_internal::ThreadIdentity*>(v);
  39. // all_locks might have been allocated by the Mutex implementation.
  40. // We free it here when we are notified that our thread is dying.
  41. if (identity->per_thread_synch.all_locks != nullptr) {
  42. base_internal::LowLevelAlloc::Free(identity->per_thread_synch.all_locks);
  43. }
  44. // We must explicitly clear the current thread's identity:
  45. // (a) Subsequent (unrelated) per-thread destructors may require an identity.
  46. // We must guarantee a new identity is used in this case (this instructor
  47. // will be reinvoked up to PTHREAD_DESTRUCTOR_ITERATIONS in this case).
  48. // (b) ThreadIdentity implementations may depend on memory that is not
  49. // reinitialized before reuse. We must allow explicit clearing of the
  50. // association state in this case.
  51. base_internal::ClearCurrentThreadIdentity();
  52. {
  53. base_internal::SpinLockHolder l(&freelist_lock);
  54. identity->next = thread_identity_freelist;
  55. thread_identity_freelist = identity;
  56. }
  57. }
  58. // Return value rounded up to next multiple of align.
  59. // Align must be a power of two.
  60. static intptr_t RoundUp(intptr_t addr, intptr_t align) {
  61. return (addr + align - 1) & ~(align - 1);
  62. }
  63. void OneTimeInitThreadIdentity(base_internal::ThreadIdentity* identity) {
  64. PerThreadSem::Init(identity);
  65. identity->ticker.store(0, std::memory_order_relaxed);
  66. identity->wait_start.store(0, std::memory_order_relaxed);
  67. identity->is_idle.store(false, std::memory_order_relaxed);
  68. }
  69. static void ResetThreadIdentityBetweenReuse(
  70. base_internal::ThreadIdentity* identity) {
  71. base_internal::PerThreadSynch* pts = &identity->per_thread_synch;
  72. pts->next = nullptr;
  73. pts->skip = nullptr;
  74. pts->may_skip = false;
  75. pts->waitp = nullptr;
  76. pts->suppress_fatal_errors = false;
  77. pts->readers = 0;
  78. pts->priority = 0;
  79. pts->next_priority_read_cycles = 0;
  80. pts->state.store(base_internal::PerThreadSynch::State::kAvailable,
  81. std::memory_order_relaxed);
  82. pts->maybe_unlocking = false;
  83. pts->wake = false;
  84. pts->cond_waiter = false;
  85. pts->all_locks = nullptr;
  86. identity->blocked_count_ptr = nullptr;
  87. identity->ticker.store(0, std::memory_order_relaxed);
  88. identity->wait_start.store(0, std::memory_order_relaxed);
  89. identity->is_idle.store(false, std::memory_order_relaxed);
  90. identity->next = nullptr;
  91. }
  92. static base_internal::ThreadIdentity* NewThreadIdentity() {
  93. base_internal::ThreadIdentity* identity = nullptr;
  94. {
  95. // Re-use a previously released object if possible.
  96. base_internal::SpinLockHolder l(&freelist_lock);
  97. if (thread_identity_freelist) {
  98. identity = thread_identity_freelist; // Take list-head.
  99. thread_identity_freelist = thread_identity_freelist->next;
  100. }
  101. }
  102. if (identity == nullptr) {
  103. // Allocate enough space to align ThreadIdentity to a multiple of
  104. // PerThreadSynch::kAlignment. This space is never released (it is
  105. // added to a freelist by ReclaimThreadIdentity instead).
  106. void* allocation = base_internal::LowLevelAlloc::Alloc(
  107. sizeof(*identity) + base_internal::PerThreadSynch::kAlignment - 1);
  108. // Round up the address to the required alignment.
  109. identity = reinterpret_cast<base_internal::ThreadIdentity*>(
  110. RoundUp(reinterpret_cast<intptr_t>(allocation),
  111. base_internal::PerThreadSynch::kAlignment));
  112. // Note that *identity is never constructed.
  113. // TODO(b/357097463): change this "one time init" to be a proper
  114. // constructor.
  115. OneTimeInitThreadIdentity(identity);
  116. }
  117. ResetThreadIdentityBetweenReuse(identity);
  118. return identity;
  119. }
  120. // Allocates and attaches ThreadIdentity object for the calling thread. Returns
  121. // the new identity.
  122. // REQUIRES: CurrentThreadIdentity(false) == nullptr
  123. base_internal::ThreadIdentity* CreateThreadIdentity() {
  124. base_internal::ThreadIdentity* identity = NewThreadIdentity();
  125. // Associate the value with the current thread, and attach our destructor.
  126. base_internal::SetCurrentThreadIdentity(identity, ReclaimThreadIdentity);
  127. return identity;
  128. }
  129. } // namespace synchronization_internal
  130. ABSL_NAMESPACE_END
  131. } // namespace absl
  132. #endif // ABSL_LOW_LEVEL_ALLOC_MISSING