notification.cc 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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 "absl/synchronization/notification.h"
  15. #include <atomic>
  16. #include "absl/base/internal/raw_logging.h"
  17. #include "absl/base/internal/tracing.h"
  18. #include "absl/synchronization/mutex.h"
  19. #include "absl/time/time.h"
  20. namespace absl {
  21. ABSL_NAMESPACE_BEGIN
  22. void Notification::Notify() {
  23. base_internal::TraceSignal(this, TraceObjectKind());
  24. MutexLock l(&this->mutex_);
  25. #ifndef NDEBUG
  26. if (ABSL_PREDICT_FALSE(notified_yet_.load(std::memory_order_relaxed))) {
  27. ABSL_RAW_LOG(
  28. FATAL,
  29. "Notify() method called more than once for Notification object %p",
  30. static_cast<void *>(this));
  31. }
  32. #endif
  33. notified_yet_.store(true, std::memory_order_release);
  34. }
  35. Notification::~Notification() {
  36. // Make sure that the thread running Notify() exits before the object is
  37. // destructed.
  38. MutexLock l(&this->mutex_);
  39. }
  40. void Notification::WaitForNotification() const {
  41. base_internal::TraceWait(this, TraceObjectKind());
  42. if (!HasBeenNotifiedInternal(&this->notified_yet_)) {
  43. this->mutex_.LockWhen(
  44. Condition(&HasBeenNotifiedInternal, &this->notified_yet_));
  45. this->mutex_.Unlock();
  46. }
  47. base_internal::TraceContinue(this, TraceObjectKind());
  48. }
  49. bool Notification::WaitForNotificationWithTimeout(
  50. absl::Duration timeout) const {
  51. base_internal::TraceWait(this, TraceObjectKind());
  52. bool notified = HasBeenNotifiedInternal(&this->notified_yet_);
  53. if (!notified) {
  54. notified = this->mutex_.LockWhenWithTimeout(
  55. Condition(&HasBeenNotifiedInternal, &this->notified_yet_), timeout);
  56. this->mutex_.Unlock();
  57. }
  58. base_internal::TraceContinue(notified ? this : nullptr, TraceObjectKind());
  59. return notified;
  60. }
  61. bool Notification::WaitForNotificationWithDeadline(absl::Time deadline) const {
  62. base_internal::TraceWait(this, TraceObjectKind());
  63. bool notified = HasBeenNotifiedInternal(&this->notified_yet_);
  64. if (!notified) {
  65. notified = this->mutex_.LockWhenWithDeadline(
  66. Condition(&HasBeenNotifiedInternal, &this->notified_yet_), deadline);
  67. this->mutex_.Unlock();
  68. }
  69. base_internal::TraceContinue(notified ? this : nullptr, TraceObjectKind());
  70. return notified;
  71. }
  72. ABSL_NAMESPACE_END
  73. } // namespace absl