notification.h revision 9d3eebb35ae339bfc8d58f56bb912336ca733e2d
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> 20b04c9f48ce69377a1172fd58e8ab210b435f359eDerek Murray#include <chrono> // NOLINT 21b04c9f48ce69377a1172fd58e8ab210b435f359eDerek Murray#include <condition_variable> // NOLINT 22b04c9f48ce69377a1172fd58e8ab210b435f359eDerek Murray 23b04c9f48ce69377a1172fd58e8ab210b435f359eDerek Murray#include "tensorflow/core/platform/mutex.h" 24b04c9f48ce69377a1172fd58e8ab210b435f359eDerek Murray#include "tensorflow/core/platform/types.h" 25b04c9f48ce69377a1172fd58e8ab210b435f359eDerek Murray 26b04c9f48ce69377a1172fd58e8ab210b435f359eDerek Murraynamespace tensorflow { 27b04c9f48ce69377a1172fd58e8ab210b435f359eDerek Murray 28b04c9f48ce69377a1172fd58e8ab210b435f359eDerek Murrayclass Notification { 29b04c9f48ce69377a1172fd58e8ab210b435f359eDerek Murray public: 30b04c9f48ce69377a1172fd58e8ab210b435f359eDerek Murray Notification() : notified_(false) {} 31b04c9f48ce69377a1172fd58e8ab210b435f359eDerek Murray ~Notification() {} 32b04c9f48ce69377a1172fd58e8ab210b435f359eDerek Murray 33b04c9f48ce69377a1172fd58e8ab210b435f359eDerek Murray void Notify() { 34b04c9f48ce69377a1172fd58e8ab210b435f359eDerek Murray mutex_lock l(mu_); 35b04c9f48ce69377a1172fd58e8ab210b435f359eDerek Murray assert(!notified_); 36b04c9f48ce69377a1172fd58e8ab210b435f359eDerek Murray notified_ = true; 37b04c9f48ce69377a1172fd58e8ab210b435f359eDerek Murray cv_.notify_all(); 38b04c9f48ce69377a1172fd58e8ab210b435f359eDerek Murray } 39b04c9f48ce69377a1172fd58e8ab210b435f359eDerek Murray 40b04c9f48ce69377a1172fd58e8ab210b435f359eDerek Murray bool HasBeenNotified() { 41b04c9f48ce69377a1172fd58e8ab210b435f359eDerek Murray mutex_lock l(mu_); 42b04c9f48ce69377a1172fd58e8ab210b435f359eDerek Murray return notified_; 43b04c9f48ce69377a1172fd58e8ab210b435f359eDerek Murray } 44b04c9f48ce69377a1172fd58e8ab210b435f359eDerek Murray 45b04c9f48ce69377a1172fd58e8ab210b435f359eDerek Murray void WaitForNotification() { 46b04c9f48ce69377a1172fd58e8ab210b435f359eDerek Murray mutex_lock l(mu_); 47b04c9f48ce69377a1172fd58e8ab210b435f359eDerek Murray while (!notified_) { 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_); 579d3eebb35ae339bfc8d58f56bb912336ca733e2dYuan Yu return cv_.wait_for(l, std::chrono::microseconds(timeout_in_us), 58b04c9f48ce69377a1172fd58e8ab210b435f359eDerek Murray [this]() { return notified_; }); 59b04c9f48ce69377a1172fd58e8ab210b435f359eDerek Murray } 60b04c9f48ce69377a1172fd58e8ab210b435f359eDerek Murray 61b04c9f48ce69377a1172fd58e8ab210b435f359eDerek Murray mutex mu_; 62b04c9f48ce69377a1172fd58e8ab210b435f359eDerek Murray condition_variable cv_; 63b04c9f48ce69377a1172fd58e8ab210b435f359eDerek Murray bool notified_; 64b04c9f48ce69377a1172fd58e8ab210b435f359eDerek Murray}; 65b04c9f48ce69377a1172fd58e8ab210b435f359eDerek Murray 66b04c9f48ce69377a1172fd58e8ab210b435f359eDerek Murrayinline bool WaitForNotificationWithTimeout(Notification* n, 679d3eebb35ae339bfc8d58f56bb912336ca733e2dYuan Yu int64 timeout_in_us) { 689d3eebb35ae339bfc8d58f56bb912336ca733e2dYuan Yu return n->WaitForNotificationWithTimeout(timeout_in_us); 69b04c9f48ce69377a1172fd58e8ab210b435f359eDerek Murray} 70b04c9f48ce69377a1172fd58e8ab210b435f359eDerek Murray 71b04c9f48ce69377a1172fd58e8ab210b435f359eDerek Murray} // namespace tensorflow 72b04c9f48ce69377a1172fd58e8ab210b435f359eDerek Murray 73b04c9f48ce69377a1172fd58e8ab210b435f359eDerek Murray#endif // TENSORFLOW_CORE_PLATFORM_DEFAULT_NOTIFICATION_H_ 74