1 /*
2 * Copyright (c) 2021 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 "timer_thread.h"
17
18 #include "q_auto_lock_guard.h"
19
20 using namespace OHOS::ACELite;
21
22 static TimerThread *g_timerInstance = nullptr;
23
InitTimerTask()24 int InitTimerTask()
25 {
26 // default
27 return 0;
28 }
29
StartTimerTask(bool isPeriodic,const unsigned int delay,void * userCallback,void * userData,timerHandle_t * timerHandle)30 int StartTimerTask(bool isPeriodic,
31 const unsigned int delay,
32 void *userCallback,
33 void *userData,
34 timerHandle_t *timerHandle)
35 {
36 if (g_timerInstance == nullptr) {
37 return -1;
38 }
39
40 return g_timerInstance->StartTimerTaskInner(isPeriodic, delay, userCallback, userData, timerHandle);
41 }
42
StopTimerTask(const timerHandle_t timerHandle)43 int StopTimerTask(const timerHandle_t timerHandle)
44 {
45 if (g_timerInstance == nullptr) {
46 return -1;
47 }
48
49 return g_timerInstance->StopTimerTaskInner(timerHandle);
50 }
51
52 namespace OHOS {
53 namespace ACELite {
54 const static unsigned long TIMER_STEP = 10;
55 typedef void (*UserCallbackFunc)(void *data);
56
run()57 void TimerThread::run()
58 {
59 g_timerInstance = this;
60 taskQuitFlag = false;
61 while (!taskQuitFlag) {
62 Traverse();
63 QThread::msleep(TIMER_STEP); // calculate once every 10ms
64 }
65 }
66
Quit()67 void TimerThread::Quit()
68 {
69 taskQuitFlag = true;
70 }
71
Traverse()72 void TimerThread::Traverse()
73 {
74 QAutoLockGuard lockGuard(mutexlock_);
75 QMutableListIterator<const TimerInfo *> listIterator(timerList_);
76 while (listIterator.hasNext()) {
77 TimerInfo *info = const_cast<TimerInfo *>(listIterator.next());
78 if (info->remain > 0) {
79 info->remain -= TIMER_STEP;
80 } else {
81 UserCallbackFunc func = (UserCallbackFunc)(info->userCallback);
82 func(info->userData);
83 if (info->isPeriodic) {
84 info->remain = info->delay;
85 } else {
86 listIterator.remove();
87 delete info;
88 info = nullptr;
89 }
90 }
91 }
92 }
93
StartTimerTaskInner(bool isPeriodic,const unsigned int delay,void * userCallback,void * userData,timerHandle_t * timerHandle)94 int TimerThread::StartTimerTaskInner(bool isPeriodic,
95 const unsigned int delay,
96 void *userCallback,
97 void *userData,
98 timerHandle_t *timerHandle)
99 {
100 auto info = new TimerInfo();
101 info->delay = info->remain = delay;
102 info->isPeriodic = isPeriodic;
103 info->userCallback = userCallback;
104 info->userData = userData;
105 *timerHandle = info;
106 info->timerHandle = (timerHandle_t *)info;
107 QAutoLockGuard lockGuard(mutexlock_);
108 timerList_.append(info);
109 return 0;
110 }
111
StopTimerTaskInner(const timerHandle_t timerHandle)112 int TimerThread::StopTimerTaskInner(const timerHandle_t timerHandle)
113 {
114 QAutoLockGuard lockGuard(mutexlock_);
115 QMutableListIterator<const TimerInfo *> listIterator(timerList_);
116 while (listIterator.hasNext()) {
117 const TimerInfo *info = const_cast<TimerInfo *>(listIterator.next());
118 if (info == nullptr) {
119 continue;
120 }
121 if (info->timerHandle == timerHandle) {
122 listIterator.remove();
123 delete info;
124 info = nullptr;
125 }
126 }
127 return 0;
128 }
129 } // namespace ACELite
130 } // namespace OHOS
131