1 /*
2 * Copyright (C) 2021-2023 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 static HcTaskThread *g_taskThread = NULL;
22
PushTask(HcTaskBase * baseTask)23 int32_t PushTask(HcTaskBase *baseTask)
24 {
25 if (g_taskThread == NULL) {
26 LOGE("Task thread is NULL!");
27 return HC_ERR_NULL_PTR;
28 }
29 g_taskThread->pushTask(g_taskThread, baseTask);
30 return HC_SUCCESS;
31 }
32
InitTaskManager(void)33 int32_t InitTaskManager(void)
34 {
35 if (g_taskThread != NULL) {
36 LOGD("Task thread is running!");
37 return HC_SUCCESS;
38 }
39 g_taskThread = (HcTaskThread *)HcMalloc(sizeof(HcTaskThread), 0);
40 if (g_taskThread == NULL) {
41 return HC_ERR_ALLOC_MEMORY;
42 }
43 int32_t res = InitHcTaskThread(g_taskThread, DEV_AUTH_WORK_THREAD_STACK_SIZE, "DevAuthWork");
44 if (res != HC_SUCCESS) {
45 LOGE("Failed to init task thread! res: %d", res);
46 HcFree(g_taskThread);
47 g_taskThread = NULL;
48 return HC_ERR_INIT_FAILED;
49 }
50 res = g_taskThread->startThread(g_taskThread);
51 if (res != HC_SUCCESS) {
52 DestroyHcTaskThread(g_taskThread);
53 HcFree(g_taskThread);
54 g_taskThread = NULL;
55 LOGE("Failed to start thread! res: %d", res);
56 return HC_ERR_INIT_FAILED;
57 }
58 return HC_SUCCESS;
59 }
60
DestroyTaskManager(void)61 void DestroyTaskManager(void)
62 {
63 if (g_taskThread != NULL) {
64 g_taskThread->stopAndClear(g_taskThread);
65 DestroyHcTaskThread(g_taskThread);
66 HcFree(g_taskThread);
67 g_taskThread = NULL;
68 }
69 }
70