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 "task_manager.h"
17
18 #include "device_auth_defines.h"
19 #include "hc_log.h"
20
21 #ifndef HICHAIN_THREAD_STACK_SIZE
22 #define HICHAIN_THREAD_STACK_SIZE 16384
23 #endif
24
25 static HcTaskThread *g_taskThread = NULL;
26
PushTask(HcTaskBase * baseTask)27 int32_t PushTask(HcTaskBase *baseTask)
28 {
29 if (g_taskThread == NULL) {
30 LOGE("Task thread is NULL!");
31 return HC_ERR_NULL_PTR;
32 }
33 g_taskThread->pushTask(g_taskThread, baseTask);
34 return HC_SUCCESS;
35 }
36
InitTaskManager(void)37 int32_t InitTaskManager(void)
38 {
39 if (g_taskThread != NULL) {
40 LOGD("Task thread is running!");
41 return HC_SUCCESS;
42 }
43 g_taskThread = (HcTaskThread *)HcMalloc(sizeof(HcTaskThread), 0);
44 if (g_taskThread == NULL) {
45 return HC_ERR_ALLOC_MEMORY;
46 }
47 int32_t res = InitHcTaskThread(g_taskThread, HICHAIN_THREAD_STACK_SIZE, "HichainThread");
48 if (res != HC_SUCCESS) {
49 LOGE("Failed to init task thread! res: %d", res);
50 HcFree(g_taskThread);
51 g_taskThread = NULL;
52 return HC_ERR_INIT_FAILED;
53 }
54 res = g_taskThread->startThread(g_taskThread);
55 if (res != HC_SUCCESS) {
56 DestroyHcTaskThread(g_taskThread);
57 HcFree(g_taskThread);
58 g_taskThread = NULL;
59 LOGE("Failed to start thread! res: %d", res);
60 return HC_ERR_INIT_FAILED;
61 }
62 return HC_SUCCESS;
63 }
64
DestroyTaskManager(void)65 void DestroyTaskManager(void)
66 {
67 if (g_taskThread != NULL) {
68 g_taskThread->stopAndClear(g_taskThread);
69 DestroyHcTaskThread(g_taskThread);
70 HcFree(g_taskThread);
71 g_taskThread = NULL;
72 }
73 }
74