1 /* Copyright 2021 The TensorFlow Authors. All Rights Reserved. 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 http://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 16 #include "tensorflow/core/data/service/task_remover.h" 17 18 #include "tensorflow/core/lib/gtl/cleanup.h" 19 #include "tensorflow/core/platform/env.h" 20 #include "tensorflow/core/platform/thread_annotations.h" 21 22 namespace tensorflow { 23 namespace data { 24 namespace { 25 const int64_t kWaitTimeoutUs = 10 * 1000 * 1000; // 10 seconds. 26 const int64_t kInvalidRound = -1; 27 } // namespace 28 TaskRemover(int64_t num_consumers)29TaskRemover::TaskRemover(int64_t num_consumers) 30 : num_consumers_(num_consumers) {} 31 RequestRemoval(int64_t consumer_index,int64_t round)32bool TaskRemover::RequestRemoval(int64_t consumer_index, int64_t round) { 33 mutex_lock l(mu_); 34 if (consumers_waiting_.empty()) { 35 round_ = round; 36 } 37 if (round != round_) { 38 round_ = kInvalidRound; 39 cv_.notify_all(); 40 return false; 41 } 42 consumers_waiting_.insert(consumer_index); 43 auto cleanup = gtl::MakeCleanup([&]() TF_EXCLUSIVE_LOCKS_REQUIRED(mu_) { 44 consumers_waiting_.erase(consumer_index); 45 }); 46 int64_t deadline_us = Env::Default()->NowMicros() + kWaitTimeoutUs; 47 while (round == round_ && !removed_ && 48 consumers_waiting_.size() < num_consumers_ && 49 Env::Default()->NowMicros() < deadline_us) { 50 cv_.wait_for(l, std::chrono::microseconds(deadline_us - 51 Env::Default()->NowMicros())); 52 } 53 if (removed_) { 54 return true; 55 } 56 if (consumers_waiting_.size() == num_consumers_) { 57 removed_ = true; 58 round_ = kInvalidRound; 59 cv_.notify_all(); 60 return true; 61 } 62 // If we get here it either means timeout was reached, or another consumer 63 // requested removal for a different round. 64 return false; 65 } 66 67 } // namespace data 68 } // namespace tensorflow 69