per_thread_sem.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. //
  15. // PerThreadSem is a low-level synchronization primitive controlling the
  16. // runnability of a single thread, used internally by Mutex and CondVar.
  17. //
  18. // This is NOT a general-purpose synchronization mechanism, and should not be
  19. // used directly by applications. Applications should use Mutex and CondVar.
  20. //
  21. // The semantics of PerThreadSem are the same as that of a counting semaphore.
  22. // Each thread maintains an abstract "count" value associated with its identity.
  23. #ifndef ABSL_SYNCHRONIZATION_INTERNAL_PER_THREAD_SEM_H_
  24. #define ABSL_SYNCHRONIZATION_INTERNAL_PER_THREAD_SEM_H_
  25. #include <atomic>
  26. #include "absl/base/internal/thread_identity.h"
  27. #include "absl/synchronization/internal/create_thread_identity.h"
  28. #include "absl/synchronization/internal/kernel_timeout.h"
  29. namespace absl {
  30. ABSL_NAMESPACE_BEGIN
  31. class Mutex;
  32. namespace synchronization_internal {
  33. class PerThreadSem {
  34. public:
  35. PerThreadSem() = delete;
  36. PerThreadSem(const PerThreadSem&) = delete;
  37. PerThreadSem& operator=(const PerThreadSem&) = delete;
  38. // Routine invoked periodically (once a second) by a background thread.
  39. // Has no effect on user-visible state.
  40. static void Tick(base_internal::ThreadIdentity* identity);
  41. // ---------------------------------------------------------------------------
  42. // Routines used by autosizing threadpools to detect when threads are
  43. // blocked. Each thread has a counter pointer, initially zero. If non-zero,
  44. // the implementation atomically increments the counter when it blocks on a
  45. // semaphore, a decrements it again when it wakes. This allows a threadpool
  46. // to keep track of how many of its threads are blocked.
  47. // SetThreadBlockedCounter() should be used only by threadpool
  48. // implementations. GetThreadBlockedCounter() should be used by modules that
  49. // block threads; if the pointer returned is non-zero, the location should be
  50. // incremented before the thread blocks, and decremented after it wakes.
  51. static void SetThreadBlockedCounter(std::atomic<int> *counter);
  52. static std::atomic<int> *GetThreadBlockedCounter();
  53. private:
  54. // Create the PerThreadSem associated with "identity". Initializes count=0.
  55. // REQUIRES: May only be called by ThreadIdentity.
  56. static inline void Init(base_internal::ThreadIdentity* identity);
  57. // Increments "identity"'s count.
  58. static inline void Post(base_internal::ThreadIdentity* identity);
  59. // Waits until either our count > 0 or t has expired.
  60. // If count > 0, decrements count and returns true. Otherwise returns false.
  61. // !t.has_timeout() => Wait(t) will return true.
  62. static inline bool Wait(KernelTimeout t);
  63. // Permitted callers.
  64. friend class PerThreadSemTest;
  65. friend class absl::Mutex;
  66. friend void OneTimeInitThreadIdentity(absl::base_internal::ThreadIdentity*);
  67. };
  68. } // namespace synchronization_internal
  69. ABSL_NAMESPACE_END
  70. } // namespace absl
  71. // In some build configurations we pass --detect-odr-violations to the
  72. // gold linker. This causes it to flag weak symbol overrides as ODR
  73. // violations. Because ODR only applies to C++ and not C,
  74. // --detect-odr-violations ignores symbols not mangled with C++ names.
  75. // By changing our extension points to be extern "C", we dodge this
  76. // check.
  77. extern "C" {
  78. void ABSL_INTERNAL_C_SYMBOL(AbslInternalPerThreadSemInit)(
  79. absl::base_internal::ThreadIdentity* identity);
  80. void ABSL_INTERNAL_C_SYMBOL(AbslInternalPerThreadSemPost)(
  81. absl::base_internal::ThreadIdentity* identity);
  82. bool ABSL_INTERNAL_C_SYMBOL(AbslInternalPerThreadSemWait)(
  83. absl::synchronization_internal::KernelTimeout t);
  84. void ABSL_INTERNAL_C_SYMBOL(AbslInternalPerThreadSemPoke)(
  85. absl::base_internal::ThreadIdentity* identity);
  86. } // extern "C"
  87. void absl::synchronization_internal::PerThreadSem::Init(
  88. absl::base_internal::ThreadIdentity* identity) {
  89. ABSL_INTERNAL_C_SYMBOL(AbslInternalPerThreadSemInit)(identity);
  90. }
  91. void absl::synchronization_internal::PerThreadSem::Post(
  92. absl::base_internal::ThreadIdentity* identity) {
  93. ABSL_INTERNAL_C_SYMBOL(AbslInternalPerThreadSemPost)(identity);
  94. }
  95. bool absl::synchronization_internal::PerThreadSem::Wait(
  96. absl::synchronization_internal::KernelTimeout t) {
  97. return ABSL_INTERNAL_C_SYMBOL(AbslInternalPerThreadSemWait)(t);
  98. }
  99. #endif // ABSL_SYNCHRONIZATION_INTERNAL_PER_THREAD_SEM_H_