message: Improve the performance of WaitForNotification/WaitForNotificationWithTimeout() when Notification is already notified.
diff: @@ -29,7 +29,12 @@ namespace tensorflow {
  class Notification {
  public:
  Notification() : notified_(0) {}
  - ~Notification() {}
  + ~Notification() {
  + // In case the notification is being used to synchronize its own deletion,
  + // force any prior notifier to leave its critical section before the object
  + // is destroyed.
  + mutex_lock l(mu_);
  + }
  void Notify() {
  mutex_lock l(mu_);
  @@ -43,22 +48,28 @@ class Notification {
  }
  void WaitForNotification() {
  + if (!HasBeenNotified()) {
  mutex_lock l(mu_);
  while (!HasBeenNotified()) {
  cv_.wait(l);
  }
  }
  + }
  private:
  friend bool WaitForNotificationWithTimeout(Notification* n,
  int64 timeout_in_us);
  bool WaitForNotificationWithTimeout(int64 timeout_in_us) {
  + bool notified = HasBeenNotified();
  + if (!notified) {
  mutex_lock l(mu_);
  - while (!HasBeenNotified() &&
  + do {
  + notified = HasBeenNotified();
  + } while (!notified &&
  cv_.wait_for(l, std::chrono::microseconds(timeout_in_us)) !=
  - std::cv_status::timeout) {
  + std::cv_status::timeout);
  }
  - return HasBeenNotified();
  + return notified;
  }
  mutex mu_; // protects mutations of notified_
  