1 /*
2 * Copyright (c) 2020-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 "common/task_manager.h"
17
18 #include "gfx_utils/graphic_log.h"
19 #include "hal_tick.h"
20
21 namespace OHOS {
GetInstance()22 TaskManager* TaskManager::GetInstance()
23 {
24 static TaskManager taskManager;
25 return &taskManager;
26 }
27
Add(Task * task)28 void TaskManager::Add(Task* task)
29 {
30 if (task == nullptr) {
31 return;
32 }
33
34 ListNode<Task*>* pos = list_.Begin();
35 while (pos != list_.End()) {
36 if (pos->data_ == task) {
37 GRAPHIC_LOGI("do not add task multi times");
38 return;
39 }
40 pos = pos->next_;
41 }
42
43 list_.PushBack(task);
44 }
45
Remove(Task * task)46 void TaskManager::Remove(Task* task)
47 {
48 if (task == nullptr) {
49 return;
50 }
51 ListNode<Task*>* pos = list_.Begin();
52 while (pos != list_.End()) {
53 if (pos->data_ == task) {
54 list_.Remove(pos);
55 return;
56 }
57 pos = pos->next_;
58 }
59 }
60
TaskHandler()61 void TaskManager::TaskHandler()
62 {
63 if (!canTaskRun_) {
64 return;
65 }
66
67 if (isHandlerRunning_) {
68 return;
69 }
70 isHandlerRunning_ = true;
71
72 ListNode<Task*>* node = list_.Begin();
73
74 while (node != list_.End()) {
75 Task* currentTask = node->data_;
76 currentTask->TaskExecute();
77
78 node = node->next_;
79 }
80
81 isHandlerRunning_ = false;
82 }
83
84 /**
85 * @brief TaskManager::ResetTaskHandlerMutex Reset the running flag.
86 *
87 * Because the rending implementation is shared between native and third-party APP under liteos-m, and for
88 * some exception cases (the app is destroyed unexpectedly during the task handling process), as the task
89 * handling process will set the flag to true for avoiding reentry of the handling process itself, and as
90 * TaskManager is one single instance, so must reset this flag to false for other task's normal rendring
91 * process, for example, native app.
92 */
ResetTaskHandlerMutex()93 void TaskManager::ResetTaskHandlerMutex()
94 {
95 isHandlerRunning_ = false;
96 }
97 } // namespace OHOS
98