1 /*
2 * Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved.
3 * Copyright (c) 2020-2023 Huawei Device Co., Ltd. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without modification,
6 * are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice, this list of
9 * conditions and the following disclaimer.
10 *
11 * 2. Redistributions in binary form must reproduce the above copyright notice, this list
12 * of conditions and the following disclaimer in the documentation and/or other materials
13 * provided with the distribution.
14 *
15 * 3. Neither the name of the copyright holder nor the names of its contributors may be used
16 * to endorse or promote products derived from this software without specific prior written
17 * permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
21 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
26 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
27 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
28 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
29 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 #ifndef _LOS_TASK_PRI_H
33 #define _LOS_TASK_PRI_H
34
35 #include "los_task.h"
36 #include "los_sched_pri.h"
37
38 #ifdef __cplusplus
39 #if __cplusplus
40 extern "C" {
41 #endif /* __cplusplus */
42 #endif /* __cplusplus */
43
44 /**
45 * @ingroup los_task
46 * Define task signal types.
47 *
48 * Task signal types.
49 */
50 #define SIGNAL_NONE 0U
51 #define SIGNAL_KILL (1U << 0)
52 #define SIGNAL_SUSPEND (1U << 1)
53 #define SIGNAL_AFFI (1U << 2)
54
55 /* scheduler lock */
56 extern SPIN_LOCK_S g_taskSpin;
57 #define SCHEDULER_HELD() LOS_SpinHeld(&g_taskSpin)
58 #define SCHEDULER_LOCK(state) LOS_SpinLockSave(&g_taskSpin, &(state))
59 #define SCHEDULER_UNLOCK(state) LOS_SpinUnlockRestore(&g_taskSpin, state)
60
61 /* default and non-running task's ownership id */
62 #define OS_TASK_INVALID_CPUID 0xFFFF
63
64 /**
65 * @ingroup los_task
66 * Null task ID
67 *
68 */
69 #define OS_TASK_ERRORID 0xFFFFFFFF
70
71 /**
72 * @ingroup los_task
73 * Flag that indicates the task or task control block status.
74 *
75 * The task control block is unused.
76 */
77 #define OS_TASK_STATUS_UNUSED 0x0400U
78
79 /**
80 * @ingroup los_task
81 * Flag that indicates the task or task control block status.
82 *
83 * The task is joinable.
84 */
85 #define OS_TASK_FLAG_PTHREAD_JOIN 0x0800U
86
87 /**
88 * @ingroup los_task
89 * Flag that indicates the task or task control block status.
90 *
91 * The task is user mode task.
92 */
93 #define OS_TASK_FLAG_USER_MODE 0x1000U
94
95 /**
96 * @ingroup los_task
97 * Flag that indicates the task property.
98 *
99 * The task is system-level task, like idle, swtmr and etc.
100 */
101 #define OS_TASK_FLAG_SYSTEM_TASK 0x2000U
102
103 /**
104 * @ingroup los_task
105 * Flag that indicates the task property.
106 *
107 * The task is no-delete system task, like resourceTask.
108 */
109 #define OS_TASK_FLAG_NO_DELETE 0x4000U
110
111 /**
112 * @ingroup los_task
113 * Flag that indicates the task property.
114 *
115 * Kills the thread during process exit.
116 */
117 #define OS_TASK_FLAG_EXIT_KILL 0x8000U
118
119 /**
120 * @ingroup los_task
121 * Flag that indicates the task property.
122 *
123 * Specifies the process creation task.
124 */
125 #define OS_TASK_FLAG_SPECIFIES_PROCESS 0x0U
126
127 /**
128 * @ingroup los_task
129 * Boundary on which the stack size is aligned.
130 *
131 */
132 #define OS_TASK_STACK_SIZE_ALIGN 16U
133
134 /**
135 * @ingroup los_task
136 * Boundary on which the stack address is aligned.
137 *
138 */
139 #define OS_TASK_STACK_ADDR_ALIGN 8U
140
141 /**
142 * @ingroup los_task
143 * Number of usable task priorities.
144 */
145 #define OS_TSK_PRINUM (OS_TASK_PRIORITY_LOWEST - OS_TASK_PRIORITY_HIGHEST + 1)
146
147 /**
148 * @ingroup los_task
149 * @brief Check whether a task ID is valid.
150 *
151 * @par Description:
152 * This API is used to check whether a task ID, excluding the idle task ID, is valid.
153 * @attention None.
154 *
155 * @param taskID [IN] Task ID.
156 *
157 * @retval 0 or 1. One indicates that the task ID is invalid, whereas zero indicates that the task ID is valid.
158 * @par Dependency:
159 * <ul><li>los_task_pri.h: the header file that contains the API declaration.</li></ul>
160 * @see
161 */
162 #define OS_TSK_GET_INDEX(taskID) (taskID)
163
164 /**
165 * @ingroup los_task
166 * @brief Obtain the pointer to a task control block.
167 *
168 * @par Description:
169 * This API is used to obtain the pointer to a task control block using a corresponding parameter.
170 * @attention None.
171 *
172 * @param ptr [IN] Parameter used for obtaining the task control block.
173 *
174 * @retval Pointer to the task control block.
175 * @par Dependency:
176 * <ul><li>los_task_pri.h: the header file that contains the API declaration.</li></ul>
177 * @see
178 */
179 #define OS_TCB_FROM_PENDLIST(ptr) LOS_DL_LIST_ENTRY(ptr, LosTaskCB, pendList)
180
181 /**
182 * @ingroup los_task
183 * @brief Obtain the pointer to a task control block.
184 *
185 * @par Description:
186 * This API is used to obtain the pointer to a task control block that has a specified task ID.
187 * @attention None.
188 *
189 * @param TaskID [IN] Task ID.
190 *
191 * @retval Pointer to the task control block.
192 * @par Dependency:
193 * <ul><li>los_task_pri.h: the header file that contains the API declaration.</li></ul>
194 * @see
195 */
196 #define OS_TCB_FROM_RTID(taskID) (((LosTaskCB *)g_taskCBArray) + (taskID))
197 #ifdef LOSCFG_PID_CONTAINER
198 #define OS_TCB_FROM_TID(taskID) OsGetTCBFromVtid(taskID)
199 #else
200 #define OS_TCB_FROM_TID(taskID) OS_TCB_FROM_RTID(taskID)
201 #endif
202
203 #ifndef LOSCFG_STACK_POINT_ALIGN_SIZE
204 #define LOSCFG_STACK_POINT_ALIGN_SIZE (sizeof(UINTPTR) * 2)
205 #endif
206
207 #define OS_TASK_RESOURCE_STATIC_SIZE 0x1000
208 #define OS_TASK_RESOURCE_FREE_PRIORITY 5
209 #define OS_RESOURCE_EVENT_MASK 0xFF
210 #define OS_RESOURCE_EVENT_OOM 0x02
211 #define OS_RESOURCE_EVENT_FREE 0x04
212
213 typedef struct {
214 LosTaskCB *runTask;
215 LosTaskCB *newTask;
216 } LosTask;
217
218 struct ProcessSignalInfo {
219 siginfo_t *sigInfo; /**< Signal to be dispatched */
220 LosTaskCB *defaultTcb; /**< Default TCB */
221 LosTaskCB *unblockedTcb; /**< The signal unblock on this TCB*/
222 LosTaskCB *awakenedTcb; /**< This TCB was awakened */
223 LosTaskCB *receivedTcb; /**< This TCB received the signal */
224 };
225
226 typedef int (*ForEachTaskCB)(LosTaskCB *tcb, void *arg);
227
228 /**
229 * @ingroup los_task
230 * Maximum number of tasks.
231 *
232 */
233 extern UINT32 g_taskMaxNum;
234
235 /**
236 * @ingroup los_task
237 * Starting address of a task.
238 *
239 */
240 extern LosTaskCB *g_taskCBArray;
241
242 /**
243 * @ingroup los_task
244 * Time slice structure.
245 */
246 typedef struct {
247 LosTaskCB *task; /**< Current running task */
248 UINT16 time; /**< Expiration time point */
249 UINT16 timeout; /**< Expiration duration */
250 } OsTaskRobin;
251
OsGetTaskCB(UINT32 taskID)252 STATIC INLINE LosTaskCB *OsGetTaskCB(UINT32 taskID)
253 {
254 return OS_TCB_FROM_TID(taskID);
255 }
256
OsTaskIsUnused(const LosTaskCB * taskCB)257 STATIC INLINE BOOL OsTaskIsUnused(const LosTaskCB *taskCB)
258 {
259 return ((taskCB->taskStatus & OS_TASK_STATUS_UNUSED) != 0);
260 }
261
OsTaskIsKilled(const LosTaskCB * taskCB)262 STATIC INLINE BOOL OsTaskIsKilled(const LosTaskCB *taskCB)
263 {
264 return((taskCB->taskStatus & OS_TASK_FLAG_EXIT_KILL) != 0);
265 }
266
OsTaskIsNotDelete(const LosTaskCB * taskCB)267 STATIC INLINE BOOL OsTaskIsNotDelete(const LosTaskCB *taskCB)
268 {
269 return ((taskCB->taskStatus & (OS_TASK_STATUS_UNUSED | OS_TASK_FLAG_SYSTEM_TASK | OS_TASK_FLAG_NO_DELETE)) != 0);
270 }
271
OsTaskIsUserMode(const LosTaskCB * taskCB)272 STATIC INLINE BOOL OsTaskIsUserMode(const LosTaskCB *taskCB)
273 {
274 return ((taskCB->taskStatus & OS_TASK_FLAG_USER_MODE) != 0);
275 }
276
277 #define OS_TID_CHECK_INVALID(taskID) ((UINT32)(taskID) >= g_taskMaxNum)
278
279 /* get task info */
280 #define OS_ALL_TASK_MASK 0xFFFFFFFF
281
282 #define OS_TASK_WAIT_ANYPROCESS (1 << 0U)
283 #define OS_TASK_WAIT_PROCESS (1 << 1U)
284 #define OS_TASK_WAIT_GID (1 << 2U)
285 #define OS_TASK_WAIT_SEM (OS_TASK_WAIT_GID + 1)
286 #define OS_TASK_WAIT_QUEUE (OS_TASK_WAIT_SEM + 1)
287 #define OS_TASK_WAIT_JOIN (OS_TASK_WAIT_QUEUE + 1)
288 #define OS_TASK_WAIT_SIGNAL (OS_TASK_WAIT_JOIN + 1)
289 #define OS_TASK_WAIT_LITEIPC (OS_TASK_WAIT_SIGNAL + 1)
290 #define OS_TASK_WAIT_MUTEX (OS_TASK_WAIT_LITEIPC + 1)
291 #define OS_TASK_WAIT_FUTEX (OS_TASK_WAIT_MUTEX + 1)
292 #define OS_TASK_WAIT_EVENT (OS_TASK_WAIT_FUTEX + 1)
293 #define OS_TASK_WAIT_COMPLETE (OS_TASK_WAIT_EVENT + 1)
294
OsTaskWaitSetPendMask(UINT16 mask,UINTPTR lockID,UINT32 timeout)295 STATIC INLINE VOID OsTaskWaitSetPendMask(UINT16 mask, UINTPTR lockID, UINT32 timeout)
296 {
297 LosTaskCB *runTask = OsCurrTaskGet();
298 runTask->waitID = lockID;
299 runTask->waitFlag = mask;
300 (VOID)timeout;
301 }
302
OsTaskWakeClearPendMask(LosTaskCB * resumeTask)303 STATIC INLINE VOID OsTaskWakeClearPendMask(LosTaskCB *resumeTask)
304 {
305 resumeTask->waitID = 0;
306 resumeTask->waitFlag = 0;
307 }
308
309 extern UINT32 OsTaskSetDetachUnsafe(LosTaskCB *taskCB);
310 extern VOID OsTaskJoinPostUnsafe(LosTaskCB *taskCB);
311 extern UINT32 OsTaskJoinPendUnsafe(LosTaskCB *taskCB);
312 extern BOOL OsTaskCpuAffiSetUnsafe(UINT32 taskID, UINT16 newCpuAffiMask, UINT16 *oldCpuAffiMask);
313 extern VOID OsTaskSchedule(LosTaskCB *, LosTaskCB *);
314 extern VOID OsTaskContextLoad(LosTaskCB *newTask);
315 extern VOID OsIdleTask(VOID);
316 extern UINT32 OsIdleTaskCreate(UINTPTR processID);
317 extern UINT32 OsTaskInit(UINTPTR processCB);
318 extern UINT32 OsShellCmdDumpTask(INT32 argc, const CHAR **argv);
319 extern UINT32 OsShellCmdTskInfoGet(UINT32 taskID, VOID *seqfile, UINT16 flag);
320 extern LosTaskCB *OsGetMainTask(VOID);
321 extern VOID OsSetMainTask(VOID);
322 extern UINT32 OsGetIdleTaskId(VOID);
323 extern VOID OsTaskEntry(UINT32 taskID);
324 extern VOID OsTaskProcSignal(VOID);
325 extern UINT32 OsCreateUserTask(UINTPTR processID, TSK_INIT_PARAM_S *initParam);
326 extern INT32 OsSetTaskName(LosTaskCB *taskCB, const CHAR *name, BOOL setPName);
327 extern VOID OsTaskCBRecycleToFree(VOID);
328 extern VOID OsRunningTaskToExit(LosTaskCB *runTask, UINT32 status);
329 extern INT32 OsUserTaskOperatePermissionsCheck(const LosTaskCB *taskCB);
330 extern INT32 OsUserProcessOperatePermissionsCheck(const LosTaskCB *taskCB, UINTPTR processCB);
331 extern INT32 OsTcbDispatch(LosTaskCB *stcb, siginfo_t *info);
332 extern VOID OsWriteResourceEvent(UINT32 events);
333 extern VOID OsWriteResourceEventUnsafe(UINT32 events);
334 extern UINT32 OsResourceFreeTaskCreate(VOID);
335 extern VOID OsTaskInsertToRecycleList(LosTaskCB *taskCB);
336 extern VOID OsInactiveTaskDelete(LosTaskCB *taskCB);
337 extern VOID OsSetMainTaskProcess(UINTPTR processCB);
338 extern LosTaskCB *OsGetDefaultTaskCB(VOID);
339
340 #ifdef __cplusplus
341 #if __cplusplus
342 }
343 #endif /* __cplusplus */
344 #endif /* __cplusplus */
345
346 #endif /* _LOS_TASK_PRI_H */
347