1 /* 2 * Copyright (c) 2022-2024 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 DM_TIMER_H 17 #define DM_TIMER_H 18 19 #include <atomic> 20 #include <chrono> 21 #include <condition_variable> 22 #include <functional> 23 #include <map> 24 #include <mutex> 25 #include <queue> 26 #include <vector> 27 28 namespace OHOS { 29 namespace DistributedHardware { 30 constexpr int32_t DELAY_TICK_MILLSECONDS = 1000; 31 typedef std::chrono::steady_clock::time_point timerPoint; 32 typedef std::chrono::steady_clock steadyClock; 33 typedef std::chrono::duration<int64_t, std::ratio<1, 1000>> timerDuration; 34 using TimerCallback = std::function<void (std::string name)>; 35 const int32_t MILLISECOND_TO_SECOND = 1000; 36 const int32_t MIN_TIME_OUT = 0; 37 const int32_t MAX_TIME_OUT = 300; 38 39 class Timer { 40 public: 41 Timer(std::string name, int32_t time, TimerCallback callback); ~Timer()42 ~Timer() {}; 43 public: 44 std::string timerName_; 45 timerPoint expire_; 46 bool state_; 47 int32_t timeOut_; 48 TimerCallback callback_; 49 }; 50 51 struct TimerCmpare { 52 public: operatorTimerCmpare53 bool operator () (std::shared_ptr<Timer> frontTimer, std::shared_ptr<Timer> timer) 54 { 55 int32_t frontTimerOut = frontTimer->timeOut_ - (std::chrono::duration_cast<timerDuration>(steadyClock::now() 56 - frontTimer->expire_).count() / MILLISECOND_TO_SECOND); 57 int32_t timerOut = timer->timeOut_ - (std::chrono::duration_cast<timerDuration>(steadyClock::now() 58 - timer->expire_).count() / MILLISECOND_TO_SECOND); 59 return frontTimerOut > timerOut; 60 } 61 }; 62 63 class DmTimer { 64 public: 65 DmTimer(); 66 ~DmTimer(); 67 68 /** 69 * @tc.name: DmTimer::StartTimer 70 * @tc.desc: start timer running 71 * @tc.type: FUNC 72 */ 73 int32_t StartTimer(std::string name, int32_t timeOut, TimerCallback callback); 74 75 /** 76 * @tc.name: DmTimer::DeleteTimer 77 * @tc.desc: delete timer 78 * @tc.type: FUNC 79 */ 80 int32_t DeleteTimer(std::string timerName); 81 82 /** 83 * @tc.name: DmTimer::DeleteAll 84 * @tc.desc: delete all timer 85 * @tc.type: FUNC 86 */ 87 int32_t DeleteAll(); 88 89 /** 90 * @tc.name: DmTimer::TimerRunning 91 * @tc.desc: timer running 92 * @tc.type: FUNC 93 */ 94 int32_t TimerRunning(); 95 96 private: 97 void DeleteVector(std::string name); 98 99 private: 100 mutable std::mutex timerMutex_; 101 mutable std::mutex timerStateMutex_; 102 std::priority_queue<std::shared_ptr<Timer>, std::vector<std::shared_ptr<Timer>>, TimerCmpare> timerQueue_; 103 std::vector<std::shared_ptr<Timer>> timerVec_; 104 std::atomic<bool> timerState_ {false}; 105 std::condition_variable runTimerCondition_; 106 std::condition_variable stopTimerCondition_; 107 }; 108 } 109 } 110 #endif