• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 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 "dm_constants.h"
17 #include "dm_log.h"
18 #include "dm_timer.h"
19 
20 #include <thread>
21 
22 namespace OHOS {
23 namespace DistributedHardware {
Timer(std::string name,int32_t time,TimerCallback callback)24 Timer::Timer(std::string name, int32_t time, TimerCallback callback)
25     : timerName_(name), expire_(steadyClock::now()), state_(true), timeOut_(time), callback_(callback) {};
26 
DmTimer()27 DmTimer::DmTimer()
28 {
29 }
30 
~DmTimer()31 DmTimer::~DmTimer()
32 {
33     LOGI("DmTimer destructor");
34     DeleteAll();
35     if (timerState_) {
36         std::unique_lock<std::mutex> locker(timerStateMutex_);
37         stopTimerCondition_.wait(locker, [this] { return static_cast<bool>(!timerState_); });
38     }
39 }
40 
StartTimer(std::string name,int32_t timeOut,TimerCallback callback)41 int32_t DmTimer::StartTimer(std::string name, int32_t timeOut, TimerCallback callback)
42 {
43     LOGI("DmTimer StartTimer %s", name.c_str());
44     if (name.empty() || timeOut <= MIN_TIME_OUT || timeOut > MAX_TIME_OUT || callback == nullptr) {
45         LOGI("DmTimer StartTimer input value invalid");
46         return ERR_DM_INPUT_PARA_INVALID;
47     }
48 
49     std::shared_ptr<Timer> timer = std::make_shared<Timer>(name, timeOut, callback);
50     {
51         std::lock_guard<std::mutex> locker(timerMutex_);
52         timerQueue_.push(timer);
53         timerMap_[name] = timer;
54     }
55 
56     if (timerState_) {
57         LOGI("DmTimer is running");
58         return DM_OK;
59     }
60 
61     TimerRunning();
62     {
63         std::unique_lock<std::mutex> locker(timerStateMutex_);
64         runTimerCondition_.wait(locker, [this] { return static_cast<bool>(timerState_); });
65     }
66     return DM_OK;
67 }
68 
DeleteTimer(std::string timerName)69 int32_t DmTimer::DeleteTimer(std::string timerName)
70 {
71     if (timerName.empty()) {
72         LOGE("DmTimer DeleteTimer timer is null");
73         return ERR_DM_INPUT_PARA_INVALID;
74     }
75 
76     LOGI("DmTimer DeleteTimer name %s", timerName.c_str());
77     std::lock_guard<std::mutex> locker(timerMutex_);
78     for (auto iter : timerMap_) {
79         if (iter.second->timerName_ == timerName) {
80             iter.second->state_ = false;
81         }
82     }
83     return DM_OK;
84 }
85 
DeleteAll()86 int32_t DmTimer::DeleteAll()
87 {
88     LOGI("DmTimer DeleteAll start");
89     std::lock_guard<std::mutex> locker(timerMutex_);
90     for (auto iter : timerMap_) {
91         iter.second->state_ = false;
92     }
93     return DM_OK;
94 }
95 
TimerRunning()96 int32_t DmTimer::TimerRunning()
97 {
98     std::thread([this] () {
99         {
100             timerState_ = true;
101             std::unique_lock<std::mutex> locker(timerStateMutex_);
102             runTimerCondition_.notify_one();
103         }
104         while (!timerQueue_.empty() && timerState_) {
105             std::this_thread::sleep_for(std::chrono::milliseconds(DELAY_TICK_MILLSECONDS));
106             while (std::chrono::duration_cast<timerDuration>(steadyClock::now()
107                    - timerQueue_.top()->expire_).count() / MILLISECOND_TO_SECOND >= timerQueue_.top()->timeOut_
108                    || !timerQueue_.top()->state_) {
109                 std::string name = timerQueue_.top()->timerName_;
110                 if (timerQueue_.top()->state_) {
111                     timerQueue_.top()->callback_(name);
112                 }
113 
114                 std::lock_guard<std::mutex> locker(timerMutex_);
115                 timerQueue_.pop();
116                 timerMap_.erase(name);
117                 if (timerQueue_.empty()) {
118                     break;
119                 }
120             }
121         }
122         {
123             timerState_ = false;
124             std::unique_lock<std::mutex> locker(timerStateMutex_);
125             stopTimerCondition_.notify_one();
126         }
127     }).detach();
128     return DM_OK;
129 }
130 }
131 }