waiter_base.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. // Copyright 2023 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. #ifndef ABSL_SYNCHRONIZATION_INTERNAL_WAITER_BASE_H_
  16. #define ABSL_SYNCHRONIZATION_INTERNAL_WAITER_BASE_H_
  17. #include "absl/base/config.h"
  18. #include "absl/base/internal/thread_identity.h"
  19. #include "absl/synchronization/internal/kernel_timeout.h"
  20. namespace absl {
  21. ABSL_NAMESPACE_BEGIN
  22. namespace synchronization_internal {
  23. // `Waiter` is a platform specific semaphore implementation that `PerThreadSem`
  24. // waits on to implement blocking in `absl::Mutex`. Implementations should
  25. // inherit from `WaiterCrtp` and must implement `Wait()`, `Post()`, and `Poke()`
  26. // as described in `WaiterBase`. `waiter.h` selects the implementation and uses
  27. // static-dispatch for performance.
  28. class WaiterBase {
  29. public:
  30. WaiterBase() = default;
  31. // Not copyable or movable
  32. WaiterBase(const WaiterBase&) = delete;
  33. WaiterBase& operator=(const WaiterBase&) = delete;
  34. // Blocks the calling thread until a matching call to `Post()` or
  35. // `t` has passed. Returns `true` if woken (`Post()` called),
  36. // `false` on timeout.
  37. //
  38. // bool Wait(KernelTimeout t);
  39. // Restart the caller of `Wait()` as with a normal semaphore.
  40. //
  41. // void Post();
  42. // If anyone is waiting, wake them up temporarily and cause them to
  43. // call `MaybeBecomeIdle()`. They will then return to waiting for a
  44. // `Post()` or timeout.
  45. //
  46. // void Poke();
  47. // Returns the name of this implementation. Used only for debugging.
  48. //
  49. // static constexpr char kName[];
  50. // How many periods to remain idle before releasing resources
  51. #ifndef ABSL_HAVE_THREAD_SANITIZER
  52. static constexpr int kIdlePeriods = 60;
  53. #else
  54. // Memory consumption under ThreadSanitizer is a serious concern,
  55. // so we release resources sooner. The value of 1 leads to 1 to 2 second
  56. // delay before marking a thread as idle.
  57. static constexpr int kIdlePeriods = 1;
  58. #endif
  59. protected:
  60. static void MaybeBecomeIdle();
  61. };
  62. template <typename T>
  63. class WaiterCrtp : public WaiterBase {
  64. public:
  65. // Returns the Waiter associated with the identity.
  66. static T* GetWaiter(base_internal::ThreadIdentity* identity) {
  67. static_assert(
  68. sizeof(T) <= sizeof(base_internal::ThreadIdentity::WaiterState),
  69. "Insufficient space for Waiter");
  70. return reinterpret_cast<T*>(identity->waiter_state.data);
  71. }
  72. };
  73. } // namespace synchronization_internal
  74. ABSL_NAMESPACE_END
  75. } // namespace absl
  76. #endif // ABSL_SYNCHRONIZATION_INTERNAL_WAITER_BASE_H_