• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2025 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 TIMER_MANAGER_H
17 #define TIMER_MANAGER_H
18 
19 #include <list>
20 
21 #include "singleton.h"
22 
23 #include "util.h"
24 
25 namespace OHOS {
26 
27 extern "C" uintptr_t DFX_SetCrashObj(uint8_t type, uintptr_t addr);
28 extern "C" void DFX_ResetCrashObj(uintptr_t crashObj);
29 
30 struct CrashObjDumper {
31 public:
CrashObjDumperCrashObjDumper32     explicit CrashObjDumper(const char *str)
33     {
34         if (str == nullptr) {
35             return;
36         }
37         ptr_ = DFX_SetCrashObj(0, reinterpret_cast<uintptr_t>(str));
38     }
~CrashObjDumperCrashObjDumper39     ~CrashObjDumper()
40     {
41         DFX_ResetCrashObj(ptr_);
42     }
43 private:
44     uintptr_t ptr_ = 0;
45 };
46 
47 namespace MMI {
48 class TimerManager final {
49     DECLARE_DELAYED_SINGLETON(TimerManager);
50 public:
51     DISALLOW_COPY_AND_MOVE(TimerManager);
52     int32_t AddTimer(int32_t intervalMs, int32_t repeatCount, std::function<void()> callback,
53         const std::string &name = "");
54     int32_t AddLongTimer(int32_t intervalMs, int32_t repeatCount, std::function<void()> callback,
55         const std::string &name = "");
56     int32_t RemoveTimer(int32_t timerId, const std::string &name = "");
57     int32_t ResetTimer(int32_t timerId);
58     bool IsExist(int32_t timerId);
59     int32_t CalcNextDelay();
60     void ProcessTimers();
61     int32_t AddTimerInternal(int32_t intervalMs, int32_t repeatCount, std::function<void()> callback,
62         const std::string &name = "");
63 
64 private:
65     struct TimerItem {
66         int32_t id { 0 };
67         int32_t intervalMs { 0 };
68         int32_t repeatCount { 0 };
69         int32_t callbackCount { 0 };
70         int64_t nextCallTime { 0 };
71         std::function<void()> callback;
72         std::string name { "" };
73     };
74 private:
75     int32_t TakeNextTimerId();
76     int32_t RemoveTimerInternal(int32_t timerId, const std::string &name = "");
77     int32_t ResetTimerInternal(int32_t timerId);
78     bool IsExistInternal(int32_t timerId);
79     void InsertTimerInternal(std::unique_ptr<TimerItem>& timer);
80     int32_t CalcNextDelayInternal();
81     void ProcessTimersInternal();
82 
83 private:
84     std::list<std::unique_ptr<TimerItem>> timers_;
85     std::recursive_mutex timerMutex_;
86 };
87 
88 #define TimerMgr ::OHOS::DelayedSingleton<TimerManager>::GetInstance()
89 } // namespace MMI
90 } // namespace OHOS
91 #endif // TIMER_MANAGER_H