• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023 Huawei Device Co., Ltd.
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 "hgm_one_shot_timer.h"
17 #include "hgm_log.h"
18 #include "hgm_task_handle_thread.h"
19 
20 namespace OHOS::Rosen {
21 namespace {
22 constexpr auto ZERO = std::chrono::steady_clock::duration::zero();
23 } // namespace
24 
HgmSimpleTimer(std::string name,const Interval & interval,const StartCallback & startCallback,const ExpiredCallback & expiredCallback,std::unique_ptr<ChronoSteadyClock> clock)25 HgmSimpleTimer::HgmSimpleTimer(std::string name, const Interval& interval,
26     const StartCallback& startCallback, const ExpiredCallback& expiredCallback,
27     std::unique_ptr<ChronoSteadyClock> clock)
28     : name_(std::move(name)),
29       interval_(interval),
30       startCallback_(startCallback),
31       expiredCallback_(expiredCallback),
32       clock_(std::move(clock))
33 {}
34 
Start()35 void HgmSimpleTimer::Start()
36 {
37     if (HgmTaskHandleThread::Instance().GetQueue() == nullptr) {
38         return;
39     }
40 
41     bool isRunning = running_.exchange(true);
42     Reset();    // Reset() only take effect when running
43 
44     // start
45     if (!isRunning) {
46         if (startCallback_) {
47             HgmTaskHandleThread::Instance().PostTask(startCallback_);
48         }
49         auto time = interval_.load();
50         HgmTaskHandleThread::Instance().PostEvent(name_, [this]() { Loop(); }, time.count());
51     }
52 }
53 
Stop()54 void HgmSimpleTimer::Stop()
55 {
56     if (running_.exchange(false) && HgmTaskHandleThread::Instance().GetQueue() != nullptr) {
57         HgmTaskHandleThread::Instance().RemoveEvent(name_);
58     }
59 }
60 
Reset()61 void HgmSimpleTimer::Reset()
62 {
63     if (running_.load() && clock_ != nullptr) {
64         resetTimePoint_.store(clock_->Now());
65     }
66 }
67 
SetInterval(Interval valueMs)68 void HgmSimpleTimer::SetInterval(Interval valueMs)
69 {
70     if (interval_.load() != valueMs) {
71         interval_.store(valueMs);
72     }
73 }
74 
Loop()75 void HgmSimpleTimer::Loop()
76 {
77     auto delay = std::chrono::duration_cast<std::chrono::milliseconds>(
78         resetTimePoint_.load() + interval_.load() - clock_->Now());
79     if (delay > ZERO) {
80         // reset
81         if (running_.load() && HgmTaskHandleThread::Instance().GetQueue() != nullptr) {
82             HgmTaskHandleThread::Instance().PostEvent(name_, [this]() { Loop(); }, delay.count());
83             return;
84         }
85     } else {
86         // cb
87         if (expiredCallback_ != nullptr) {
88             HgmTaskHandleThread::Instance().PostTask(expiredCallback_);
89         }
90     }
91     running_.store(false);
92 }
93 } // namespace OHOS::Rosen