notification.h revision ead1d9ba76af1fc721ed16c11812a2783d41133f
1b04c9f48ce69377a1172fd58e8ab210b435f359eDerek Murray/* Copyright 2015 The TensorFlow Authors. All Rights Reserved.
2b04c9f48ce69377a1172fd58e8ab210b435f359eDerek Murray
3b04c9f48ce69377a1172fd58e8ab210b435f359eDerek MurrayLicensed under the Apache License, Version 2.0 (the "License");
4b04c9f48ce69377a1172fd58e8ab210b435f359eDerek Murrayyou may not use this file except in compliance with the License.
5b04c9f48ce69377a1172fd58e8ab210b435f359eDerek MurrayYou may obtain a copy of the License at
6b04c9f48ce69377a1172fd58e8ab210b435f359eDerek Murray
7b04c9f48ce69377a1172fd58e8ab210b435f359eDerek Murray    http://www.apache.org/licenses/LICENSE-2.0
8b04c9f48ce69377a1172fd58e8ab210b435f359eDerek Murray
9b04c9f48ce69377a1172fd58e8ab210b435f359eDerek MurrayUnless required by applicable law or agreed to in writing, software
10b04c9f48ce69377a1172fd58e8ab210b435f359eDerek Murraydistributed under the License is distributed on an "AS IS" BASIS,
11b04c9f48ce69377a1172fd58e8ab210b435f359eDerek MurrayWITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12b04c9f48ce69377a1172fd58e8ab210b435f359eDerek MurraySee the License for the specific language governing permissions and
13b04c9f48ce69377a1172fd58e8ab210b435f359eDerek Murraylimitations under the License.
14b04c9f48ce69377a1172fd58e8ab210b435f359eDerek Murray==============================================================================*/
15b04c9f48ce69377a1172fd58e8ab210b435f359eDerek Murray
16b04c9f48ce69377a1172fd58e8ab210b435f359eDerek Murray#ifndef TENSORFLOW_CORE_PLATFORM_DEFAULT_NOTIFICATION_H_
17b04c9f48ce69377a1172fd58e8ab210b435f359eDerek Murray#define TENSORFLOW_CORE_PLATFORM_DEFAULT_NOTIFICATION_H_
18b04c9f48ce69377a1172fd58e8ab210b435f359eDerek Murray
19b04c9f48ce69377a1172fd58e8ab210b435f359eDerek Murray#include <assert.h>
20ead1d9ba76af1fc721ed16c11812a2783d41133fA. Unique TensorFlower#include <atomic>              // NOLINT
21b04c9f48ce69377a1172fd58e8ab210b435f359eDerek Murray#include <chrono>              // NOLINT
22b04c9f48ce69377a1172fd58e8ab210b435f359eDerek Murray#include <condition_variable>  // NOLINT
23b04c9f48ce69377a1172fd58e8ab210b435f359eDerek Murray
24b04c9f48ce69377a1172fd58e8ab210b435f359eDerek Murray#include "tensorflow/core/platform/mutex.h"
25b04c9f48ce69377a1172fd58e8ab210b435f359eDerek Murray#include "tensorflow/core/platform/types.h"
26b04c9f48ce69377a1172fd58e8ab210b435f359eDerek Murray
27b04c9f48ce69377a1172fd58e8ab210b435f359eDerek Murraynamespace tensorflow {
28b04c9f48ce69377a1172fd58e8ab210b435f359eDerek Murray
29b04c9f48ce69377a1172fd58e8ab210b435f359eDerek Murrayclass Notification {
30b04c9f48ce69377a1172fd58e8ab210b435f359eDerek Murray public:
31ead1d9ba76af1fc721ed16c11812a2783d41133fA. Unique TensorFlower  Notification() : notified_(0) {}
32b04c9f48ce69377a1172fd58e8ab210b435f359eDerek Murray  ~Notification() {}
33b04c9f48ce69377a1172fd58e8ab210b435f359eDerek Murray
34b04c9f48ce69377a1172fd58e8ab210b435f359eDerek Murray  void Notify() {
35b04c9f48ce69377a1172fd58e8ab210b435f359eDerek Murray    mutex_lock l(mu_);
36ead1d9ba76af1fc721ed16c11812a2783d41133fA. Unique TensorFlower    assert(!HasBeenNotified());
37ead1d9ba76af1fc721ed16c11812a2783d41133fA. Unique TensorFlower    notified_.store(true, std::memory_order_release);
38b04c9f48ce69377a1172fd58e8ab210b435f359eDerek Murray    cv_.notify_all();
39b04c9f48ce69377a1172fd58e8ab210b435f359eDerek Murray  }
40b04c9f48ce69377a1172fd58e8ab210b435f359eDerek Murray
41ead1d9ba76af1fc721ed16c11812a2783d41133fA. Unique TensorFlower  bool HasBeenNotified() const {
42ead1d9ba76af1fc721ed16c11812a2783d41133fA. Unique TensorFlower    return notified_.load(std::memory_order_acquire);
43b04c9f48ce69377a1172fd58e8ab210b435f359eDerek Murray  }
44b04c9f48ce69377a1172fd58e8ab210b435f359eDerek Murray
45b04c9f48ce69377a1172fd58e8ab210b435f359eDerek Murray  void WaitForNotification() {
46b04c9f48ce69377a1172fd58e8ab210b435f359eDerek Murray    mutex_lock l(mu_);
47ead1d9ba76af1fc721ed16c11812a2783d41133fA. Unique TensorFlower    while (!HasBeenNotified()) {
48b04c9f48ce69377a1172fd58e8ab210b435f359eDerek Murray      cv_.wait(l);
49b04c9f48ce69377a1172fd58e8ab210b435f359eDerek Murray    }
50b04c9f48ce69377a1172fd58e8ab210b435f359eDerek Murray  }
51b04c9f48ce69377a1172fd58e8ab210b435f359eDerek Murray
52b04c9f48ce69377a1172fd58e8ab210b435f359eDerek Murray private:
53b04c9f48ce69377a1172fd58e8ab210b435f359eDerek Murray  friend bool WaitForNotificationWithTimeout(Notification* n,
549d3eebb35ae339bfc8d58f56bb912336ca733e2dYuan Yu                                             int64 timeout_in_us);
559d3eebb35ae339bfc8d58f56bb912336ca733e2dYuan Yu  bool WaitForNotificationWithTimeout(int64 timeout_in_us) {
56b04c9f48ce69377a1172fd58e8ab210b435f359eDerek Murray    mutex_lock l(mu_);
57ead1d9ba76af1fc721ed16c11812a2783d41133fA. Unique TensorFlower    while (!HasBeenNotified() &&
58b48cfaea2aea3707a33e60c10385a87e37101b95A. Unique TensorFlower           cv_.wait_for(l, std::chrono::microseconds(timeout_in_us)) !=
59b48cfaea2aea3707a33e60c10385a87e37101b95A. Unique TensorFlower               std::cv_status::timeout) {
60b48cfaea2aea3707a33e60c10385a87e37101b95A. Unique TensorFlower    }
61ead1d9ba76af1fc721ed16c11812a2783d41133fA. Unique TensorFlower    return HasBeenNotified();
62b04c9f48ce69377a1172fd58e8ab210b435f359eDerek Murray  }
63b04c9f48ce69377a1172fd58e8ab210b435f359eDerek Murray
64ead1d9ba76af1fc721ed16c11812a2783d41133fA. Unique TensorFlower  mutex mu_;                    // protects mutations of notified_
65ead1d9ba76af1fc721ed16c11812a2783d41133fA. Unique TensorFlower  condition_variable cv_;       // signalled when notified_ becomes non-zero
66ead1d9ba76af1fc721ed16c11812a2783d41133fA. Unique TensorFlower  std::atomic<bool> notified_;  // mutations under mu_
67b04c9f48ce69377a1172fd58e8ab210b435f359eDerek Murray};
68b04c9f48ce69377a1172fd58e8ab210b435f359eDerek Murray
69b04c9f48ce69377a1172fd58e8ab210b435f359eDerek Murrayinline bool WaitForNotificationWithTimeout(Notification* n,
709d3eebb35ae339bfc8d58f56bb912336ca733e2dYuan Yu                                           int64 timeout_in_us) {
719d3eebb35ae339bfc8d58f56bb912336ca733e2dYuan Yu  return n->WaitForNotificationWithTimeout(timeout_in_us);
72b04c9f48ce69377a1172fd58e8ab210b435f359eDerek Murray}
73b04c9f48ce69377a1172fd58e8ab210b435f359eDerek Murray
74b04c9f48ce69377a1172fd58e8ab210b435f359eDerek Murray}  // namespace tensorflow
75b04c9f48ce69377a1172fd58e8ab210b435f359eDerek Murray
76b04c9f48ce69377a1172fd58e8ab210b435f359eDerek Murray#endif  // TENSORFLOW_CORE_PLATFORM_DEFAULT_NOTIFICATION_H_
77