1 /** 2 * Copyright 2021 Huawei Technologies Co., Ltd 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 #include "fl/server/iteration_timer.h" 18 19 namespace mindspore { 20 namespace fl { 21 namespace server { Start(const std::chrono::milliseconds & duration)22void IterationTimer::Start(const std::chrono::milliseconds &duration) { 23 if (running_.load()) { 24 MS_LOG(WARNING) << "The timer already started."; 25 return; 26 } 27 running_ = true; 28 end_time_ = CURRENT_TIME_MILLI + duration; 29 monitor_thread_ = std::thread([&]() { 30 while (running_.load()) { 31 if (CURRENT_TIME_MILLI > end_time_) { 32 timeout_callback_(false, ""); 33 running_ = false; 34 } 35 // The time tick is 1 millisecond. 36 std::this_thread::sleep_for(std::chrono::milliseconds(1)); 37 } 38 }); 39 } 40 Stop()41void IterationTimer::Stop() { 42 running_ = false; 43 if (monitor_thread_.joinable()) { 44 monitor_thread_.join(); 45 } 46 } 47 SetTimeOutCallBack(const TimeOutCb & timeout_cb)48void IterationTimer::SetTimeOutCallBack(const TimeOutCb &timeout_cb) { 49 timeout_callback_ = timeout_cb; 50 return; 51 } 52 IsTimeOut(const std::chrono::milliseconds & timestamp) const53bool IterationTimer::IsTimeOut(const std::chrono::milliseconds ×tamp) const { 54 return timestamp > end_time_ ? true : false; 55 } 56 IsRunning() const57bool IterationTimer::IsRunning() const { return running_; } 58 } // namespace server 59 } // namespace fl 60 } // namespace mindspore 61