• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 uint32_t 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 = reinterpret_cast<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     if (info == nullptr) {
102         return -1;
103     }
104     info->delay = delay;
105     info->remain = delay;
106     info->isPeriodic = isPeriodic;
107     info->userCallback = userCallback;
108     info->userData = userData;
109     *timerHandle = info;
110     info->timerHandle = reinterpret_cast<timerHandle_t *>(info);
111     QAutoLockGuard lockGuard(mutexlock_);
112     timerList_.append(info);
113     return 0;
114 }
115 
StopTimerTaskInner(const timerHandle_t timerHandle)116 int TimerThread::StopTimerTaskInner(const timerHandle_t timerHandle)
117 {
118     QAutoLockGuard lockGuard(mutexlock_);
119     QMutableListIterator<const TimerInfo *> listIterator(timerList_);
120     while (listIterator.hasNext()) {
121         const TimerInfo *info = const_cast<TimerInfo *>(listIterator.next());
122         if (info == nullptr) {
123             continue;
124         }
125         if (info->timerHandle == timerHandle) {
126             listIterator.remove();
127             delete info;
128             info = nullptr;
129         }
130     }
131     return 0;
132 }
133 } // namespace ACELite
134 } // namespace OHOS
135