1 /*
2  * Copyright (C) 2021 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 TELEPHONY_TIMER_H
17 #define TELEPHONY_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 "telephony_log_wrapper.h"
28 
29 namespace OHOS {
30 namespace Telephony {
31 static constexpr const char* TIMER_START_THREAD = "timerStartThread";
32 
33 class Timer {
34 public:
Timer()35     Timer() : stopStatus_(true), tryStopFlag_(false) {}
36 
Timer(const Timer & timer)37     Timer(const Timer &timer)
38     {
39         stopStatus_ = timer.stopStatus_.load();
40         tryStopFlag_ = timer.tryStopFlag_.load();
41     }
42 
~Timer()43     ~Timer()
44     {
45         stop();
46     }
47 
start(int interval,std::function<void ()> taskFun)48     void start(int interval, std::function<void()> taskFun)
49     {
50         if (stopStatus_ == false) {
51             return;
52         }
53         stopStatus_ = false;
54         std::thread([this, interval, taskFun]() {
55             pthread_setname_np(pthread_self(), TIMER_START_THREAD);
56             while (!tryStopFlag_) {
57                 std::this_thread::sleep_for(std::chrono::milliseconds(interval));
58                 taskFun();
59             }
60 
61             {
62                 std::lock_guard<std::mutex> locker(mutex_);
63                 stopStatus_ = true;
64                 tryStopFlag_ = false;
65                 timerCond_.notify_one();
66             }
67         }).detach();
68     }
69 
stop()70     void stop()
71     {
72         if (stopStatus_ || tryStopFlag_) {
73             return;
74         }
75         tryStopFlag_ = true;
76         {
77             std::unique_lock<std::mutex> locker(mutex_);
78             timerCond_.wait(locker, [this] { return stopStatus_ == true; });
79 
80             if (stopStatus_ == true)
81                 tryStopFlag_ = false;
82         }
83     }
84 
ThreadExit()85     void ThreadExit()
86     {
87         std::lock_guard<std::mutex> locker(mutex_);
88         stopStatus_ = true;
89         tryStopFlag_ = true;
90     }
91 
92 private:
93     std::atomic<bool> stopStatus_;
94     std::atomic<bool> tryStopFlag_;
95     std::mutex mutex_;
96     std::condition_variable timerCond_;
97 };
98 } // namespace Telephony
99 } // namespace OHOS
100 #endif // TELEPHONY_TIMER_H
101