• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2022-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 #ifndef NET_MANAGER_TIMER_H
17 #define NET_MANAGER_TIMER_H
18 
19 #include <atomic>
20 #include <chrono>
21 #include <condition_variable>
22 #include <functional>
23 #include <memory>
24 #include <mutex>
25 #include <thread>
26 
27 #include "ffrt.h"
28 #include "ffrt_inner.h"
29 
30 namespace OHOS {
31 namespace NetManagerStandard {
32 static constexpr const int TIMER_MAX_INTERVAL_MS = 200;
33 class FfrtTimer {
34 public:
FfrtTimer()35     FfrtTimer() : stopStatus_(true), tryStopFlag_(false) {}
36 
FfrtTimer(const FfrtTimer & timer)37     FfrtTimer(const FfrtTimer &timer)
38     {
39         stopStatus_ = timer.stopStatus_.load();
40         tryStopFlag_ = timer.tryStopFlag_.load();
41     }
42 
~FfrtTimer()43     ~FfrtTimer()
44     {
45         Stop();
46         StopPro();
47     }
48 
StartPro(uint64_t interval,void * data,void (* taskFun)(void *))49     void StartPro(uint64_t interval, void *data, void (*taskFun)(void *))
50     {
51         StopPro();
52         timer_ = ffrt_timer_start(ffrt_qos_default, interval, data, taskFun, true);
53     }
54 
StopPro()55     void StopPro()
56     {
57         if (timer_ != ffrt_error) {
58             auto ret = ffrt_timer_stop(ffrt_qos_default, timer_);
59         }
60     }
61 
Start(int interval,std::function<void ()> taskFun)62     void Start(int interval, std::function<void()> taskFun)
63     {
64         if (stopStatus_ == false) {
65             return;
66         }
67         stopStatus_ = false;
68         std::function<void()> startTask = [this, interval, taskFun]() {
69             while (!tryStopFlag_) {
70                 OneTiming(interval);
71                 if (!tryStopFlag_) {
72                     taskFun();
73                 }
74             }
75 
76             std::lock_guard<ffrt::mutex> locker(mutex_);
77             stopStatus_ = true;
78             timerCond_.notify_one();
79         };
80         ffrt::submit(std::move(startTask), {}, {}, ffrt::task_attr().name("timeStartTask"));
81     }
82 
Stop()83     void Stop()
84     {
85         if (stopStatus_ || tryStopFlag_) {
86             return;
87         }
88         tryStopFlag_ = true;
89         std::unique_lock<ffrt::mutex> locker(mutex_);
90         timerCond_.wait(locker, [this] { return stopStatus_ == true; });
91 
92         if (stopStatus_ == true) {
93             tryStopFlag_ = false;
94         }
95     }
96 
97 private:
OneTiming(int time)98     void OneTiming(int time)
99     {
100         int repeatCount = (time > TIMER_MAX_INTERVAL_MS) ? (time / TIMER_MAX_INTERVAL_MS) : 0;
101         int remainTime = (time > TIMER_MAX_INTERVAL_MS) ? (time % TIMER_MAX_INTERVAL_MS) : time;
102         while (!tryStopFlag_) {
103             if (repeatCount > 0) {
104                 ffrt::this_task::sleep_for(std::chrono::milliseconds(TIMER_MAX_INTERVAL_MS));
105             } else {
106                 if (remainTime) {
107                     ffrt::this_task::sleep_for(std::chrono::milliseconds(remainTime));
108                 }
109                 break;
110             }
111             repeatCount--;
112         }
113     }
114 
115 private:
116     std::atomic<bool> stopStatus_;
117     std::atomic<bool> tryStopFlag_;
118     ffrt::mutex mutex_;
119     ffrt::condition_variable timerCond_;
120     ffrt_timer_t timer_ = ffrt_error;
121 };
122 } // namespace NetManagerStandard
123 } // namespace OHOS
124 #endif // NET_MANAGER_TIMER_H
125