Emit TraceObserved on already-notified absl::Notification waits

Calling `absl::Notification::WaitForNotification()`, `WaitForNotificationWithTimeout()`, or `WaitForNotificationWithDeadline()` on an already-notified notification emits `TraceWait` and `TraceContinue` even though the call never blocks. In hot loops this floods tracing listeners with paired wait events and pays for two tracing hook calls instead of one on the fast path.

Check `HasBeenNotifiedInternal()` before `TraceWait` in all three wait methods, emit `TraceObserved` on the already-notified fast path to match `HasBeenNotified()`, and emit `TraceWait` and `TraceContinue` only on the blocking slow path.

PiperOrigin-RevId: 1032484577
diff --git a/absl/synchronization/notification.cc b/absl/synchronization/notification.cc
index 63da068..b9b11fb 100644
--- a/absl/synchronization/notification.cc
+++ b/absl/synchronization/notification.cc
@@ -49,36 +49,40 @@
 }
 
 void Notification::WaitForNotification() const {
-  base_internal::TraceWait(this, TraceObjectKind());
-  if (!HasBeenNotifiedInternal(&this->notified_yet_)) {
-    this->mutex_.LockWhen(
-        Condition(&HasBeenNotifiedInternal, &this->notified_yet_));
-    this->mutex_.unlock();
+  if (HasBeenNotifiedInternal(&this->notified_yet_)) {
+    base_internal::TraceObserved(this, TraceObjectKind());
+    return;
   }
+  base_internal::TraceWait(this, TraceObjectKind());
+  this->mutex_.LockWhen(
+      Condition(&HasBeenNotifiedInternal, &this->notified_yet_));
+  this->mutex_.unlock();
   base_internal::TraceContinue(this, TraceObjectKind());
 }
 
 bool Notification::WaitForNotificationWithTimeout(
     absl::Duration timeout) const {
-  base_internal::TraceWait(this, TraceObjectKind());
-  bool notified = HasBeenNotifiedInternal(&this->notified_yet_);
-  if (!notified) {
-    notified = this->mutex_.LockWhenWithTimeout(
-        Condition(&HasBeenNotifiedInternal, &this->notified_yet_), timeout);
-    this->mutex_.unlock();
+  if (HasBeenNotifiedInternal(&this->notified_yet_)) {
+    base_internal::TraceObserved(this, TraceObjectKind());
+    return true;
   }
+  base_internal::TraceWait(this, TraceObjectKind());
+  bool notified = this->mutex_.LockWhenWithTimeout(
+      Condition(&HasBeenNotifiedInternal, &this->notified_yet_), timeout);
+  this->mutex_.unlock();
   base_internal::TraceContinue(notified ? this : nullptr, TraceObjectKind());
   return notified;
 }
 
 bool Notification::WaitForNotificationWithDeadline(absl::Time deadline) const {
-  base_internal::TraceWait(this, TraceObjectKind());
-  bool notified = HasBeenNotifiedInternal(&this->notified_yet_);
-  if (!notified) {
-    notified = this->mutex_.LockWhenWithDeadline(
-        Condition(&HasBeenNotifiedInternal, &this->notified_yet_), deadline);
-    this->mutex_.unlock();
+  if (HasBeenNotifiedInternal(&this->notified_yet_)) {
+    base_internal::TraceObserved(this, TraceObjectKind());
+    return true;
   }
+  base_internal::TraceWait(this, TraceObjectKind());
+  bool notified = this->mutex_.LockWhenWithDeadline(
+      Condition(&HasBeenNotifiedInternal, &this->notified_yet_), deadline);
+  this->mutex_.unlock();
   base_internal::TraceContinue(notified ? this : nullptr, TraceObjectKind());
   return notified;
 }
diff --git a/absl/synchronization/notification_test.cc b/absl/synchronization/notification_test.cc
index ded80a5..b0cdf7b 100644
--- a/absl/synchronization/notification_test.cc
+++ b/absl/synchronization/notification_test.cc
@@ -151,6 +151,7 @@
 thread_local TraceRecord tls_wait;
 thread_local TraceRecord tls_continue;
 thread_local TraceRecord tls_observed;
+thread_local Notification* tls_notify_on_wait = nullptr;
 
 }  // namespace
 
@@ -160,6 +161,11 @@
 void ABSL_INTERNAL_C_SYMBOL(AbslInternalTraceWait)(const void* object,
                                                    ObjectKind kind) {
   tls_wait = {object, kind};
+  if (tls_notify_on_wait != nullptr) {
+    Notification* to_notify = tls_notify_on_wait;
+    tls_notify_on_wait = nullptr;
+    to_notify->Notify();
+  }
 }
 
 void ABSL_INTERNAL_C_SYMBOL(AbslInternalTraceContinue)(const void* object,
@@ -188,26 +194,65 @@
 
 TEST(NotificationTest, TracesWaitForNotification) {
   Notification n;
-  n.Notify();
-  tls_wait = tls_continue = {};
+
+  tls_wait = tls_continue = tls_observed = {};
+  tls_notify_on_wait = &n;
   n.WaitForNotification();
   EXPECT_EQ(tls_wait, TraceRecord(&n, ObjectKind::kNotification));
   EXPECT_EQ(tls_continue, TraceRecord(&n, ObjectKind::kNotification));
+  EXPECT_EQ(tls_observed, TraceRecord(nullptr, ObjectKind::kUnknown));
+
+  tls_wait = tls_continue = tls_observed = {};
+  n.WaitForNotification();
+  EXPECT_EQ(tls_wait, TraceRecord(nullptr, ObjectKind::kUnknown));
+  EXPECT_EQ(tls_continue, TraceRecord(nullptr, ObjectKind::kUnknown));
+  EXPECT_EQ(tls_observed, TraceRecord(&n, ObjectKind::kNotification));
 }
 
 TEST(NotificationTest, TracesWaitForNotificationWithTimeout) {
   Notification n;
 
-  tls_wait = tls_continue = {};
-  n.WaitForNotificationWithTimeout(absl::Milliseconds(1));
+  tls_wait = tls_continue = tls_observed = {};
+  EXPECT_FALSE(n.WaitForNotificationWithTimeout(absl::Milliseconds(1)));
   EXPECT_EQ(tls_wait, TraceRecord(&n, ObjectKind::kNotification));
   EXPECT_EQ(tls_continue, TraceRecord(nullptr, ObjectKind::kNotification));
+  EXPECT_EQ(tls_observed, TraceRecord(nullptr, ObjectKind::kUnknown));
 
-  n.Notify();
-  tls_wait = tls_continue = {};
-  n.WaitForNotificationWithTimeout(absl::Milliseconds(1));
+  tls_wait = tls_continue = tls_observed = {};
+  tls_notify_on_wait = &n;
+  EXPECT_TRUE(n.WaitForNotificationWithTimeout(absl::InfiniteDuration()));
   EXPECT_EQ(tls_wait, TraceRecord(&n, ObjectKind::kNotification));
   EXPECT_EQ(tls_continue, TraceRecord(&n, ObjectKind::kNotification));
+  EXPECT_EQ(tls_observed, TraceRecord(nullptr, ObjectKind::kUnknown));
+
+  tls_wait = tls_continue = tls_observed = {};
+  EXPECT_TRUE(n.WaitForNotificationWithTimeout(absl::Milliseconds(1)));
+  EXPECT_EQ(tls_wait, TraceRecord(nullptr, ObjectKind::kUnknown));
+  EXPECT_EQ(tls_continue, TraceRecord(nullptr, ObjectKind::kUnknown));
+  EXPECT_EQ(tls_observed, TraceRecord(&n, ObjectKind::kNotification));
+}
+
+TEST(NotificationTest, TracesWaitForNotificationWithDeadline) {
+  Notification n;
+
+  tls_wait = tls_continue = tls_observed = {};
+  EXPECT_FALSE(n.WaitForNotificationWithDeadline(absl::InfinitePast()));
+  EXPECT_EQ(tls_wait, TraceRecord(&n, ObjectKind::kNotification));
+  EXPECT_EQ(tls_continue, TraceRecord(nullptr, ObjectKind::kNotification));
+  EXPECT_EQ(tls_observed, TraceRecord(nullptr, ObjectKind::kUnknown));
+
+  tls_wait = tls_continue = tls_observed = {};
+  tls_notify_on_wait = &n;
+  EXPECT_TRUE(n.WaitForNotificationWithDeadline(absl::InfiniteFuture()));
+  EXPECT_EQ(tls_wait, TraceRecord(&n, ObjectKind::kNotification));
+  EXPECT_EQ(tls_continue, TraceRecord(&n, ObjectKind::kNotification));
+  EXPECT_EQ(tls_observed, TraceRecord(nullptr, ObjectKind::kUnknown));
+
+  tls_wait = tls_continue = tls_observed = {};
+  EXPECT_TRUE(n.WaitForNotificationWithDeadline(absl::InfinitePast()));
+  EXPECT_EQ(tls_wait, TraceRecord(nullptr, ObjectKind::kUnknown));
+  EXPECT_EQ(tls_continue, TraceRecord(nullptr, ObjectKind::kUnknown));
+  EXPECT_EQ(tls_observed, TraceRecord(&n, ObjectKind::kNotification));
 }
 
 TEST(NotificationTest, TracesHasBeenNotified) {