• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 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 #ifndef TIMER_MGR_H
16 #define TIMER_MGR_H
17 
18 #include <memory>
19 #include <list>
20 #include <mutex>
21 #include <thread>
22 #include <condition_variable>
23 #include "id_allocator.h"
24 
25 namespace OHOS {
26 namespace IntellVoiceUtils {
27 constexpr int32_t US_PER_MS = 1000;
28 // struct for set timer
29 struct TimerCfg {
30     explicit TimerCfg(int type = 0, int64_t delayUs = 0, int cookie = 0)
typeTimerCfg31         : type(type), delayUs(delayUs), cookie(cookie)
32     {
33     };
34 
35     int type;
36     int64_t delayUs;
37     int cookie;
38 };
39 
40 // struct for callback
41 struct TimerEvent {
typeTimerEvent42     explicit TimerEvent(int type = 0, int timeId = 0, int cookie = 0) : type(type), timeId(timeId), cookie(cookie) {};
43 
44     int type;
45     int timeId;
46     int cookie;
47 };
48 
49 
50 struct ITimerObserver {
~ITimerObserverITimerObserver51     virtual ~ITimerObserver() {};
52     virtual void OnTimerEvent(TimerEvent& info) = 0;
53 };
54 
55 enum class TimerStatus {
56     TIMER_STATUS_INIT,
57     TIMER_STATUS_STARTED,
58     TIMER_STATUS_TO_QUIT
59 };
60 
61 struct TimerItem {
62     explicit TimerItem(int id = 0, int type = 0, int cookie = 0,
63         int64_t delayUs = static_cast<long long>(0), ITimerObserver* observer = nullptr);
64     bool operator==(const TimerItem& right) const
65     {
66         return tgtUs == right.tgtUs;
67     };
68     bool operator<(const TimerItem& right) const
69     {
70         return tgtUs < right.tgtUs;
71     };
72 
73     int timerId;
74     int type;
75     int cookie;
76     int64_t tgtUs;
77     ITimerObserver* observer;
78 };
79 
80 class TimerMgr : private IdAllocator {
81 public:
82     explicit TimerMgr(int maxTimerNum = 10);
83     ~TimerMgr() override;
84 
85     void Start(ITimerObserver* observer = nullptr);
86     void Stop();
87     int SetTimer(int type, int64_t delayUs, int cookie = 0, ITimerObserver* currObserver = nullptr);
88     int ResetTimer(int timerId, int type, int64_t delayUs, int cookie, ITimerObserver* currObserver);
89     void KillTimer(int& timerId);
90 
91 private:
92     void Clear();
93     void WorkThread();
94 
95 private:
96     TimerStatus status_;
97     ITimerObserver* timerObserver_;
98     std::list<std::shared_ptr<TimerItem>> itemQueue_;
99 
100     std::mutex timeMutex_;
101     std::condition_variable cv_;
102     std::thread workThread_;
103 };
104 }
105 }
106 #endif
107