• 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 #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 const char* AUTHENTICATE_TIMEOUT_TASK = "deviceManagerTimer:authenticate";
31 constexpr const char* NEGOTIATE_TIMEOUT_TASK = "deviceManagerTimer:negotiate";
32 constexpr const char* CONFIRM_TIMEOUT_TASK = "deviceManagerTimer:confirm";
33 constexpr const char* INPUT_TIMEOUT_TASK = "deviceManagerTimer:input";
34 constexpr const char* ADD_TIMEOUT_TASK = "deviceManagerTimer:add";
35 constexpr const char* WAIT_NEGOTIATE_TIMEOUT_TASK = "deviceManagerTimer:waitNegotiate";
36 constexpr const char* WAIT_REQUEST_TIMEOUT_TASK = "deviceManagerTimer:waitRequest";
37 constexpr const char* STATE_TIMER_PREFIX = "deviceManagerTimer:stateTimer_";
38 
39 constexpr int32_t DELAY_TICK_MILLSECONDS = 1000;
40 typedef std::chrono::steady_clock::time_point timerPoint;
41 typedef std::chrono::steady_clock steadyClock;
42 typedef std::chrono::duration<int64_t, std::ratio<1, 1000>> timerDuration;
43 using TimerCallback = std::function<void (std::string name)>;
44 const int32_t MILLISECOND_TO_SECOND = 1000;
45 const int32_t MIN_TIME_OUT = 0;
46 const int32_t MAX_TIME_OUT = 300;
47 
48 class Timer {
49 public:
50     Timer(std::string name, int32_t time, TimerCallback callback);
~Timer()51     ~Timer() {};
52 public:
53     std::string timerName_;
54     timerPoint expire_;
55     bool state_;
56     int32_t timeOut_;
57     TimerCallback callback_;
58 };
59 
60 struct TimerCmpare {
61 public:
operatorTimerCmpare62     bool operator () (std::shared_ptr<Timer> frontTimer, std::shared_ptr<Timer> timer)
63     {
64         int32_t frontTimerOut = frontTimer->timeOut_ - (std::chrono::duration_cast<timerDuration>(steadyClock::now()
65             - frontTimer->expire_).count() / MILLISECOND_TO_SECOND);
66         int32_t timerOut = timer->timeOut_ - (std::chrono::duration_cast<timerDuration>(steadyClock::now()
67             - timer->expire_).count() / MILLISECOND_TO_SECOND);
68         return frontTimerOut > timerOut;
69     }
70 };
71 
72 class DmTimer {
73 public:
74     DmTimer();
75     ~DmTimer();
76 
77     /**
78      * @tc.name: DmTimer::StartTimer
79      * @tc.desc: start timer running
80      * @tc.type: FUNC
81      */
82     int32_t StartTimer(std::string name, int32_t time, TimerCallback callback);
83 
84     /**
85      * @tc.name: DmTimer::DeleteTimer
86      * @tc.desc: delete timer
87      * @tc.type: FUNC
88      */
89     int32_t DeleteTimer(std::string timerName);
90 
91     /**
92      * @tc.name: DmTimer::DeleteAll
93      * @tc.desc: delete all timer
94      * @tc.type: FUNC
95      */
96     int32_t DeleteAll();
97 
98     /**
99      * @tc.name: DmTimer::TimerRunning
100      * @tc.desc: timer running
101      * @tc.type: FUNC
102      */
103     int32_t TimerRunning();
104 private:
105     mutable std::mutex timerMutex_;
106     mutable std::mutex timerStateMutex_;
107     std::priority_queue<std::shared_ptr<Timer>, std::vector<std::shared_ptr<Timer>>, TimerCmpare> timerQueue_;
108     std::map<std::string, std::shared_ptr<Timer>> timerMap_;
109     std::atomic<bool> timerState_ {false};
110     std::condition_variable runTimerCondition_;
111     std::condition_variable stopTimerCondition_;
112 };
113 }
114 }
115 #endif