1 /*
2 * Copyright (c) 2009-2022 Huawei Technologies Co., Ltd. All rights reserved.
3 *
4 * UniProton is licensed under Mulan PSL v2.
5 * You can use this software according to the terms and conditions of the Mulan PSL v2.
6 * You may obtain a copy of Mulan PSL v2 at:
7 * http://license.coscl.org.cn/MulanPSL2
8 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
9 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
10 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
11 * See the Mulan PSL v2 for more details.
12 * Create: 2009-12-22
13 * Description: Task schedule implementation
14 */
15 #include "prt_task_internal.h"
16
17 /*
18 * 描述:AMP任务初始化
19 */
OsTskAMPInit(void)20 OS_SEC_L4_TEXT U32 OsTskAMPInit(void)
21 {
22 uintptr_t size;
23 U32 idx;
24
25 // 1为Idle任务
26 g_threadNum += (g_tskMaxNum + 1);
27 /*
28 * 线程TID = LTID|PID|COREID|GTID
29 */
30
31 /* Always reserve task idle for background. */
32 size = (OS_MAX_TCB_NUM) * sizeof(struct TagTskCb);
33
34 g_tskCbArray = (struct TagTskCb *)OsMemAllocAlign((U32)OS_MID_TSK, OS_MEM_DEFAULT_FSC_PT,
35 size, MEM_ADDR_ALIGN_016);
36 if (g_tskCbArray == NULL) {
37 return OS_ERRNO_TSK_NO_MEMORY;
38 }
39
40 /* Connect all the TCBs in a doubly linked list. */
41 if (memset_s(g_tskCbArray, size, 0, size) != EOK) {
42 OS_GOTO_SYS_ERROR1();
43 }
44
45 g_tskBaseId = OS_SYS_PID_BASE;
46
47 INIT_LIST_OBJECT(&g_tskCbFreeList);
48 for (idx = 0; idx < OS_MAX_TCB_NUM - 1; idx++) {
49 g_tskCbArray[idx].taskStatus = OS_TSK_UNUSED;
50 g_tskCbArray[idx].taskPid = (idx + g_tskBaseId);
51 ListTailAdd(&g_tskCbArray[idx].pendList, &g_tskCbFreeList);
52 }
53
54 /* 在初始化时给RUNNING_TASK的PID赋一个合法的无效值,放置在Trace使用时出现异常 */
55 RUNNING_TASK = OS_PST_ZOMBIE_TASK;
56
57 /* 在初始化时给RUNNING_TASK的PID赋一个合法的无效值,放置在Trace使用时出现异常 */
58 RUNNING_TASK->taskPid = idx + g_tskBaseId;
59
60 /* Init empty ready list for each priority. */
61 for (idx = 0; idx < OS_TSK_NUM_OF_PRIORITIES; idx++) {
62 INIT_LIST_OBJECT(&g_runQueue.readyList[idx]);
63 }
64
65 INIT_LIST_OBJECT(&g_tskSortedDelay.tskList);
66 INIT_LIST_OBJECT(&g_tskRecyleList);
67
68 /* 增加OS_TSK_INUSE状态,使得在Trace记录的第一条信息状态为OS_TSK_INUSE(创建状态) */
69 RUNNING_TASK->taskStatus = (OS_TSK_INUSE | OS_TSK_RUNNING);
70 RUNNING_TASK->priority = OS_TSK_PRIORITY_LOWEST + 1;
71
72 return OS_OK;
73 }
74
75 /*
76 * 描述:ilde任务创建.
77 */
OsIdleTskAMPCreate(void)78 OS_SEC_L4_TEXT U32 OsIdleTskAMPCreate(void)
79 {
80 U32 ret;
81 TskHandle taskHdl;
82 struct TskInitParam taskInitParam;
83 char tskName[OS_TSK_NAME_LEN] = "IdleTask";
84
85 /* Create background task. */
86 taskInitParam.taskEntry = g_tskIdleEntry;
87 taskInitParam.stackSize = g_tskModInfo.idleStackSize;
88 taskInitParam.name = tskName;
89 taskInitParam.taskPrio = OS_TSK_PRIORITY_LOWEST;
90 taskInitParam.stackAddr = 0;
91
92 /* 任务调度的必要条件就是有背景任务,此时背景任务还没有创建,因此不会发生任务切换 */
93 ret = PRT_TaskCreate(&taskHdl, &taskInitParam);
94 if (ret != OS_OK) {
95 return ret;
96 }
97 ret = PRT_TaskResume(taskHdl);
98 if (ret != OS_OK) {
99 return ret;
100 }
101 IDLE_TASK_ID = taskHdl;
102
103 return ret;
104 }
105