abseil-cpp/absl/synchronization/notification.cc
Go to the documentation of this file.
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 #include "absl/synchronization/notification.h"
16 
17 #include <atomic>
18 
19 #include "absl/base/attributes.h"
20 #include "absl/base/internal/raw_logging.h"
21 #include "absl/synchronization/mutex.h"
22 #include "absl/time/time.h"
23 
24 namespace absl {
26 
28  MutexLock l(&this->mutex_);
29 
30 #ifndef NDEBUG
31  if (ABSL_PREDICT_FALSE(notified_yet_.load(std::memory_order_relaxed))) {
33  FATAL,
34  "Notify() method called more than once for Notification object %p",
35  static_cast<void *>(this));
36  }
37 #endif
38 
39  notified_yet_.store(true, std::memory_order_release);
40 }
41 
43  // Make sure that the thread running Notify() exits before the object is
44  // destructed.
45  MutexLock l(&this->mutex_);
46 }
47 
51  &this->notified_yet_));
52  this->mutex_.Unlock();
53  }
54 }
55 
57  absl::Duration timeout) const {
58  bool notified = HasBeenNotifiedInternal(&this->notified_yet_);
59  if (!notified) {
60  notified = this->mutex_.LockWhenWithTimeout(
62  this->mutex_.Unlock();
63  }
64  return notified;
65 }
66 
68  bool notified = HasBeenNotifiedInternal(&this->notified_yet_);
69  if (!notified) {
70  notified = this->mutex_.LockWhenWithDeadline(
71  Condition(&HasBeenNotifiedInternal, &this->notified_yet_), deadline);
72  this->mutex_.Unlock();
73  }
74  return notified;
75 }
76 
78 } // namespace absl
ABSL_PREDICT_FALSE
#define ABSL_PREDICT_FALSE(x)
Definition: abseil-cpp/absl/base/optimization.h:180
absl::Notification::notified_yet_
std::atomic< bool > notified_yet_
Definition: abseil-cpp/absl/synchronization/notification.h:118
absl::Time
Definition: third_party/abseil-cpp/absl/time/time.h:642
absl::Mutex::Unlock
void Unlock() ABSL_UNLOCK_FUNCTION()
absl::Notification::mutex_
Mutex mutex_
Definition: abseil-cpp/absl/synchronization/notification.h:117
ABSL_NAMESPACE_END
#define ABSL_NAMESPACE_END
Definition: third_party/abseil-cpp/absl/base/config.h:171
absl::Mutex::LockWhenWithDeadline
bool LockWhenWithDeadline(const Condition &cond, absl::Time deadline) ABSL_EXCLUSIVE_LOCK_FUNCTION()
Definition: abseil-cpp/absl/synchronization/mutex.cc:1515
benchmark::Condition
std::condition_variable Condition
Definition: benchmark/src/mutex.h:69
absl::MutexLock
Definition: abseil-cpp/absl/synchronization/mutex.h:525
ABSL_NAMESPACE_BEGIN
#define ABSL_NAMESPACE_BEGIN
Definition: third_party/abseil-cpp/absl/base/config.h:170
absl::Notification::Notify
void Notify()
Definition: abseil-cpp/absl/synchronization/notification.cc:27
absl::Notification::WaitForNotificationWithDeadline
bool WaitForNotificationWithDeadline(absl::Time deadline) const
Definition: abseil-cpp/absl/synchronization/notification.cc:67
absl::Notification::HasBeenNotifiedInternal
static bool HasBeenNotifiedInternal(const std::atomic< bool > *notified_yet)
Definition: abseil-cpp/absl/synchronization/notification.h:112
absl::Duration
Definition: third_party/abseil-cpp/absl/time/time.h:159
FATAL
#define FATAL(msg)
Definition: task.h:88
absl::Mutex::LockWhen
void LockWhen(const Condition &cond) ABSL_EXCLUSIVE_LOCK_FUNCTION()
Definition: abseil-cpp/absl/synchronization/mutex.cc:1503
absl::Notification::~Notification
~Notification()
Definition: abseil-cpp/absl/synchronization/notification.cc:42
absl::Notification::WaitForNotificationWithTimeout
bool WaitForNotificationWithTimeout(absl::Duration timeout) const
Definition: abseil-cpp/absl/synchronization/notification.cc:56
absl
Definition: abseil-cpp/absl/algorithm/algorithm.h:31
absl::Mutex::LockWhenWithTimeout
bool LockWhenWithTimeout(const Condition &cond, absl::Duration timeout) ABSL_EXCLUSIVE_LOCK_FUNCTION()
Definition: abseil-cpp/absl/synchronization/mutex.cc:1511
ABSL_RAW_LOG
#define ABSL_RAW_LOG(severity,...)
Definition: abseil-cpp/absl/base/internal/raw_logging.h:44
timeout
uv_timer_t timeout
Definition: libuv/docs/code/uvwget/main.c:9
absl::Notification::WaitForNotification
void WaitForNotification() const
Definition: abseil-cpp/absl/synchronization/notification.cc:48


grpc
Author(s):
autogenerated on Fri May 16 2025 02:59:32