1 /* ---------------------------------------------------------------------------- 2 * Copyright (c) Huawei Technologies Co., Ltd. 2023-2023. All rights reserved. 3 * Description: LiteOS Task Base HeadFile. 4 * Author: Huawei LiteOS Team 5 * Create: 2023-02-15 6 * Redistribution and use in source and binary forms, with or without modification, 7 * are permitted provided that the following conditions are met: 8 * 1. Redistributions of source code must retain the above copyright notice, this list of 9 * conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright notice, this list 11 * of conditions and the following disclaimer in the documentation and/or other materials 12 * provided with the distribution. 13 * 3. Neither the name of the copyright holder nor the names of its contributors may be used 14 * to endorse or promote products derived from this software without specific prior written 15 * permission. 16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 17 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 18 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 19 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR 20 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 21 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 22 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 23 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 24 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 25 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 26 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 * --------------------------------------------------------------------------- */ 28 29 #ifndef _LOS_TASK_BASE_H 30 #define _LOS_TASK_BASE_H 31 32 #include "los_list.h" 33 #include "los_task.h" 34 #include "los_sortlink_pri.h" 35 #include "los_spinlock.h" 36 #include "los_typedef.h" 37 38 #ifdef LOSCFG_BASE_IPC_EVENT 39 #include "los_event.h" 40 #endif 41 42 #ifdef LOSCFG_KERNEL_LOCKDEP 43 #include "los_lockdep.h" 44 #endif 45 46 #ifdef LOSCFG_DEBUG_SCHED_STATISTICS 47 #include "los_sched_debug_pri.h" 48 #endif 49 50 #ifdef __cplusplus 51 extern "C" { 52 #endif /* __cplusplus */ 53 54 /* The task is automatically deleted */ 55 #define OS_TASK_FLAG_DETACHED 0x0001U 56 57 /* The resources of the task are released by other task call the join func. */ 58 #define OS_TASK_FLAG_JOINABLE 0x0000U 59 60 /* The task is system-level task, like idle, swtmr and etc */ 61 #define OS_TASK_FLAG_SYSTEM 0x0002U 62 63 /* The task control block is unused */ 64 #define OS_TASK_STATUS_UNUSED 0x0001U 65 66 /* The task is suspended */ 67 #define OS_TASK_STATUS_SUSPEND 0x0002U 68 69 /* The task is ready */ 70 #define OS_TASK_STATUS_READY 0x0004U 71 72 /* The task is blocked */ 73 #define OS_TASK_STATUS_PEND 0x0008U 74 75 /* The task is running */ 76 #define OS_TASK_STATUS_RUNNING 0x0010U 77 78 /* The task is delayed */ 79 #define OS_TASK_STATUS_DELAY 0x0020U 80 81 /* The time for waiting for an event to occur expires */ 82 #define OS_TASK_STATUS_TIMEOUT 0x0040U 83 84 /* The task is pend for a period of time */ 85 #define OS_TASK_STATUS_PEND_TIME 0x0080U 86 87 #ifdef LOSCFG_SCHED_MQ 88 /* 89 * These status were added for SCHED_MQ where per-cpu locks are introduced. 90 * When task is being scheduled, enqueued, dequeued or woken up, 91 * the global scheduler lock will be switched to the per-cpu lock, 92 * where there's need for these status to represent the schedule process. 93 */ 94 #define OS_TASK_STATUS_SCHED 0x0100U /* The task is being scheduled. */ 95 #define OS_TASK_STATUS_ENQUE 0x0200U /* The Task is being enqueued. */ 96 #define OS_TASK_STATUS_DEQUE 0x0400U /* The task is being dequeued. */ 97 #define OS_TASK_STATUS_WAKEUP 0x1000U /* The task is being woken up. */ 98 99 #define OS_TASK_IS_INOPERABLE (OS_TASK_STATUS_SCHED | \ 100 OS_TASK_STATUS_ENQUE | \ 101 OS_TASK_STATUS_DEQUE | \ 102 OS_TASK_STATUS_WAKEUP) 103 #else /* LOSCFG_SCHED_SQ */ 104 #define OS_TASK_STATUS_SCHED 0x0U 105 #define OS_TASK_STATUS_ENQUE 0x0U 106 #define OS_TASK_STATUS_DEQUE 0x0U 107 #define OS_TASK_STATUS_WAKEUP 0x0U 108 109 #define OS_TASK_IS_INOPERABLE 0x0U 110 #endif 111 112 /* This task is waiting for resources to be reclaimed. */ 113 #define OS_TASK_STATUS_ZOMBIE 0x0100U 114 115 #define OS_TASK_IS_EXIT (OS_TASK_STATUS_ZOMBIE | OS_TASK_STATUS_UNUSED) 116 117 /* This Macro is used to determine the task attribute detach or join. */ 118 #ifdef LOSCFG_TASK_JOINABLE 119 #define OS_TASK_IS_JOINABLE(taskCB) ((taskCB->taskFlags & OS_TASK_FLAG_DETACHED) == 0) 120 #define OS_TASK_IS_DETACHED(taskCB) ((taskCB->taskFlags & OS_TASK_FLAG_DETACHED) == OS_TASK_FLAG_DETACHED) 121 #define OS_TASK_IS_ZOMBIE(taskStatus) (taskStatus & OS_TASK_STATUS_ZOMBIE) 122 #define OS_TASK_IS_ALREADY_JOIN(taskCB) (taskCB->joinner != NULL) 123 #define OS_TASK_IS_JOINING(taskCB) (taskCB->joined != NULL) 124 #else 125 #define OS_TASK_IS_JOINING(taskCB) FALSE 126 #define OS_TASK_IS_JOINABLE(taskCB) FALSE 127 #define OS_TASK_IS_DETACHED(taskCB) TRUE 128 #define OS_TASK_IS_ZOMBIE(taskStatus) FALSE 129 #endif 130 131 typedef struct tagTaskCB { 132 VOID *stackPointer; /* Task stack pointer */ 133 #ifdef LOSCFG_SCHED_MQ 134 volatile UINT16 taskStatus; /* The task status is not protected by the g_taskSpin, 135 * so it needs to be read again before each use. */ 136 #else 137 UINT16 taskStatus; /* Task status */ 138 #endif 139 UINT16 priority; /* Task priority */ 140 #ifdef LOSCFG_SCHED_MQ 141 volatile UINT32 taskExtStatus; /* Task extend status:used to protect concurrent access to the same task */ 142 #endif 143 UINT32 taskFlags : 31; /* Task extend flags: taskFlags uses 8 bits now. 23 bits left */ 144 UINT32 usrStack : 1; /* Usr Stack uses the last bit */ 145 UINT32 stackSize; /* Task stack size */ 146 UINTPTR topOfStack; /* Task stack top */ 147 UINT32 taskId; /* Task ID */ 148 TSK_ENTRY_FUNC taskEntry; /* Task entrance function */ 149 VOID *taskSem; /* Task-held semaphore */ 150 #ifdef LOSCFG_TASK_JOINABLE 151 struct tagTaskCB *joinner; /* Pointer to the task that 152 * invokes LOS_TaskJoin func to wait for cur task */ 153 struct tagTaskCB *joined; /* Point to the task waiting 154 * by the cur task invoking LOS_TaskJoin func */ 155 VOID *threadJoinRetval; /* Task join exit status */ 156 #endif 157 VOID *taskMux; /* Task-held mutex */ 158 #ifdef LOSCFG_OBSOLETE_API 159 UINTPTR args[4]; /* Parameter, of which the maximum number is 4 */ 160 #else 161 VOID *args; /* Parameter, of which the type is void * */ 162 #endif 163 CHAR *taskName; /* Task name */ 164 LOS_DL_LIST pendList; /* Task pend node */ 165 SortLinkList sortList; /* Task sortlink node */ 166 #ifdef LOSCFG_BASE_IPC_EVENT 167 EVENT_CB_S event; 168 UINT32 eventMask; /* Event mask */ 169 UINT32 eventMode; /* Event mode */ 170 #endif 171 VOID *msg; /* Memory allocated to queues */ 172 UINT32 priBitMap; /* BitMap for recording the change of task priority, 173 the priority can not be greater than 31 */ 174 #ifdef LOSCFG_KERNEL_SMP 175 UINT32 mpSignal; /* Mp Task signal */ 176 #endif 177 #ifdef LOSCFG_BASE_CORE_TIMESLICE 178 UINT16 timeSlice; /* Remaining time slice */ 179 #endif 180 #ifdef LOSCFG_BASE_IPC_RWSEM 181 UINT16 waitType; /* The type of the rwsem that the task waits for */ 182 #endif 183 #ifdef LOSCFG_KERNEL_SMP 184 volatile UINT16 currCpu; /* CPU core number of this task is running on */ 185 volatile UINT16 lastCpu; /* CPU core number of this task is running on last time */ 186 UINT32 timerCpu; /* CPU core number of this task is delayed or pended */ 187 UINT16 cpuAffiMask; /* CPU affinity mask, support up to 16 cores */ 188 #ifdef LOSCFG_KERNEL_SMP_TASK_SYNC 189 UINT32 syncSignal; /* Synchronization for signal handling */ 190 #endif 191 #endif 192 #ifdef LOSCFG_DEBUG_SCHED_STATISTICS 193 SchedStat schedStat; /* Schedule statistics */ 194 #endif 195 #ifdef LOSCFG_KERNEL_LOCKDEP 196 LockDep lockDep[LOCK_TYPE_MAX]; 197 #endif 198 #ifdef LOSCFG_KERNEL_PERF 199 UINTPTR pc; 200 UINTPTR fp; 201 #endif 202 #ifdef LOSCFG_TRUSTZONE 203 void *secureContextSP; 204 #endif 205 } LosTaskCB; 206 207 /* scheduler lock */ 208 extern SPIN_LOCK_S g_taskSpin; 209 #define SCHEDULER_LOCK(state) LOS_SpinLockSave(&g_taskSpin, &(state)) 210 #define SCHEDULER_UNLOCK(state) LOS_SpinUnlockRestore(&g_taskSpin, (state)) 211 212 #ifdef __cplusplus 213 } 214 #endif /* __cplusplus */ 215 216 #endif /* _LOS_TASK_BASE_H */ 217