stdcpp_waiter.cc 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. #include "y_absl/synchronization/internal/stdcpp_waiter.h"
  15. #ifdef Y_ABSL_INTERNAL_HAVE_STDCPP_WAITER
  16. #include <chrono> // NOLINT(build/c++11)
  17. #include <condition_variable> // NOLINT(build/c++11)
  18. #include <mutex> // NOLINT(build/c++11)
  19. #include "y_absl/base/config.h"
  20. #include "y_absl/base/internal/raw_logging.h"
  21. #include "y_absl/base/internal/thread_identity.h"
  22. #include "y_absl/base/optimization.h"
  23. #include "y_absl/synchronization/internal/kernel_timeout.h"
  24. namespace y_absl {
  25. Y_ABSL_NAMESPACE_BEGIN
  26. namespace synchronization_internal {
  27. #ifdef Y_ABSL_INTERNAL_NEED_REDUNDANT_CONSTEXPR_DECL
  28. constexpr char StdcppWaiter::kName[];
  29. #endif
  30. StdcppWaiter::StdcppWaiter() : waiter_count_(0), wakeup_count_(0) {}
  31. bool StdcppWaiter::Wait(KernelTimeout t) {
  32. std::unique_lock<std::mutex> lock(mu_);
  33. ++waiter_count_;
  34. // Loop until we find a wakeup to consume or timeout.
  35. // Note that, since the thread ticker is just reset, we don't need to check
  36. // whether the thread is idle on the very first pass of the loop.
  37. bool first_pass = true;
  38. while (wakeup_count_ == 0) {
  39. if (!first_pass) MaybeBecomeIdle();
  40. // No wakeups available, time to wait.
  41. if (!t.has_timeout()) {
  42. cv_.wait(lock);
  43. } else {
  44. auto wait_result = t.SupportsSteadyClock() && t.is_relative_timeout()
  45. ? cv_.wait_for(lock, t.ToChronoDuration())
  46. : cv_.wait_until(lock, t.ToChronoTimePoint());
  47. if (wait_result == std::cv_status::timeout) {
  48. --waiter_count_;
  49. return false;
  50. }
  51. }
  52. first_pass = false;
  53. }
  54. // Consume a wakeup and we're done.
  55. --wakeup_count_;
  56. --waiter_count_;
  57. return true;
  58. }
  59. void StdcppWaiter::Post() {
  60. std::lock_guard<std::mutex> lock(mu_);
  61. ++wakeup_count_;
  62. InternalCondVarPoke();
  63. }
  64. void StdcppWaiter::Poke() {
  65. std::lock_guard<std::mutex> lock(mu_);
  66. InternalCondVarPoke();
  67. }
  68. void StdcppWaiter::InternalCondVarPoke() {
  69. if (waiter_count_ != 0) {
  70. cv_.notify_one();
  71. }
  72. }
  73. } // namespace synchronization_internal
  74. Y_ABSL_NAMESPACE_END
  75. } // namespace y_absl
  76. #endif // Y_ABSL_INTERNAL_HAVE_STDCPP_WAITER