create_thread_identity.cc 5.6 KB

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