1 /*
2 * Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved.
3 * Copyright (c) 2020-2022 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_LOCK(state) LOS_SpinLockSave(&g_taskSpin, &(state))
58 #define SCHEDULER_UNLOCK(state) LOS_SpinUnlockRestore(&g_taskSpin, state)
59
60 /* default and non-running task's ownership id */
61 #define OS_TASK_INVALID_CPUID 0xFFFF
62
63 /**
64 * @ingroup los_task
65 * Null task ID
66 *
67 */
68 #define OS_TASK_ERRORID 0xFFFFFFFF
69
70 /**
71 * @ingroup los_task
72 * Flag that indicates the task or task control block status.
73 *
74 * The task control block is unused.
75 */
76 #define OS_TASK_STATUS_UNUSED 0x0400U
77
78 /**
79 * @ingroup los_task
80 * Flag that indicates the task or task control block status.
81 *
82 * The task is joinable.
83 */
84 #define OS_TASK_FLAG_PTHREAD_JOIN 0x0800U
85
86 /**
87 * @ingroup los_task
88 * Flag that indicates the task or task control block status.
89 *
90 * The task is user mode task.
91 */
92 #define OS_TASK_FLAG_USER_MODE 0x1000U
93
94 /**
95 * @ingroup los_task
96 * Flag that indicates the task property.
97 *
98 * The task is system-level task, like idle, swtmr and etc.
99 */
100 #define OS_TASK_FLAG_SYSTEM_TASK 0x2000U
101
102 /**
103 * @ingroup los_task
104 * Flag that indicates the task property.
105 *
106 * The task is no-delete system task, like resourceTask.
107 */
108 #define OS_TASK_FLAG_NO_DELETE 0x4000U
109
110 /**
111 * @ingroup los_task
112 * Flag that indicates the task property.
113 *
114 * Kills the thread during process exit.
115 */
116 #define OS_TASK_FLAG_EXIT_KILL 0x8000U
117
118 /**
119 * @ingroup los_task
120 * Flag that indicates the task property.
121 *
122 * Specifies the process creation task.
123 */
124 #define OS_TASK_FLAG_SPECIFIES_PROCESS 0x0U
125
126 /**
127 * @ingroup los_task
128 * Boundary on which the stack size is aligned.
129 *
130 */
131 #define OS_TASK_STACK_SIZE_ALIGN 16U
132
133 /**
134 * @ingroup los_task
135 * Boundary on which the stack address is aligned.
136 *
137 */
138 #define OS_TASK_STACK_ADDR_ALIGN 8U
139
140 /**
141 * @ingroup los_task
142 * Number of usable task priorities.
143 */
144 #define OS_TSK_PRINUM (OS_TASK_PRIORITY_LOWEST - OS_TASK_PRIORITY_HIGHEST + 1)
145
146 /**
147 * @ingroup los_task
148 * @brief Check whether a task ID is valid.
149 *
150 * @par Description:
151 * This API is used to check whether a task ID, excluding the idle task ID, is valid.
152 * @attention None.
153 *
154 * @param taskID [IN] Task ID.
155 *
156 * @retval 0 or 1. One indicates that the task ID is invalid, whereas zero indicates that the task ID is valid.
157 * @par Dependency:
158 * <ul><li>los_task_pri.h: the header file that contains the API declaration.</li></ul>
159 * @see
160 */
161 #define OS_TSK_GET_INDEX(taskID) (taskID)
162
163 /**
164 * @ingroup los_task
165 * @brief Obtain the pointer to a task control block.
166 *
167 * @par Description:
168 * This API is used to obtain the pointer to a task control block using a corresponding parameter.
169 * @attention None.
170 *
171 * @param ptr [IN] Parameter used for obtaining the task control block.
172 *
173 * @retval Pointer to the task control block.
174 * @par Dependency:
175 * <ul><li>los_task_pri.h: the header file that contains the API declaration.</li></ul>
176 * @see
177 */
178 #define OS_TCB_FROM_PENDLIST(ptr) LOS_DL_LIST_ENTRY(ptr, LosTaskCB, pendList)
179
180 /**
181 * @ingroup los_task
182 * @brief Obtain the pointer to a task control block.
183 *
184 * @par Description:
185 * This API is used to obtain the pointer to a task control block that has a specified task ID.
186 * @attention None.
187 *
188 * @param TaskID [IN] Task ID.
189 *
190 * @retval Pointer to the task control block.
191 * @par Dependency:
192 * <ul><li>los_task_pri.h: the header file that contains the API declaration.</li></ul>
193 * @see
194 */
195 #define OS_TCB_FROM_TID(taskID) (((LosTaskCB *)g_taskCBArray) + (taskID))
196
197 #ifndef LOSCFG_STACK_POINT_ALIGN_SIZE
198 #define LOSCFG_STACK_POINT_ALIGN_SIZE (sizeof(UINTPTR) * 2)
199 #endif
200
201 #define OS_TASK_RESOURCE_STATIC_SIZE 0x1000
202 #define OS_TASK_RESOURCE_FREE_PRIORITY 5
203 #define OS_RESOURCE_EVENT_MASK 0xFF
204 #define OS_RESOURCE_EVENT_OOM 0x02
205 #define OS_RESOURCE_EVENT_FREE 0x04
206
207 typedef struct {
208 LosTaskCB *runTask;
209 LosTaskCB *newTask;
210 } LosTask;
211
212 struct ProcessSignalInfo {
213 siginfo_t *sigInfo; /**< Signal to be dispatched */
214 LosTaskCB *defaultTcb; /**< Default TCB */
215 LosTaskCB *unblockedTcb; /**< The signal unblock on this TCB*/
216 LosTaskCB *awakenedTcb; /**< This TCB was awakened */
217 LosTaskCB *receivedTcb; /**< This TCB received the signal */
218 };
219
220 typedef int (*ForEachTaskCB)(LosTaskCB *tcb, void *arg);
221
222 /**
223 * @ingroup los_task
224 * Maximum number of tasks.
225 *
226 */
227 extern UINT32 g_taskMaxNum;
228
229 /**
230 * @ingroup los_task
231 * Starting address of a task.
232 *
233 */
234 extern LosTaskCB *g_taskCBArray;
235
236 /**
237 * @ingroup los_task
238 * Time slice structure.
239 */
240 typedef struct {
241 LosTaskCB *task; /**< Current running task */
242 UINT16 time; /**< Expiration time point */
243 UINT16 timeout; /**< Expiration duration */
244 } OsTaskRobin;
245
OsGetTaskCB(UINT32 taskID)246 STATIC INLINE LosTaskCB *OsGetTaskCB(UINT32 taskID)
247 {
248 return OS_TCB_FROM_TID(taskID);
249 }
250
OsTaskIsUnused(const LosTaskCB * taskCB)251 STATIC INLINE BOOL OsTaskIsUnused(const LosTaskCB *taskCB)
252 {
253 return ((taskCB->taskStatus & OS_TASK_STATUS_UNUSED) != 0);
254 }
255
OsTaskIsKilled(const LosTaskCB * taskCB)256 STATIC INLINE BOOL OsTaskIsKilled(const LosTaskCB *taskCB)
257 {
258 return ((taskCB->taskStatus & OS_TASK_FLAG_EXIT_KILL) != 0);
259 }
260
OsTaskIsUserMode(const LosTaskCB * taskCB)261 STATIC INLINE BOOL OsTaskIsUserMode(const LosTaskCB *taskCB)
262 {
263 return ((taskCB->taskStatus & OS_TASK_FLAG_USER_MODE) != 0);
264 }
265
266 #define OS_TID_CHECK_INVALID(taskID) ((UINT32)(taskID) >= g_taskMaxNum)
267
268 /* get task info */
269 #define OS_ALL_TASK_MASK 0xFFFFFFFF
270
271 #define OS_TASK_WAIT_ANYPROCESS (1 << 0U)
272 #define OS_TASK_WAIT_PROCESS (1 << 1U)
273 #define OS_TASK_WAIT_GID (1 << 2U)
274 #define OS_TASK_WAIT_SEM (OS_TASK_WAIT_GID + 1)
275 #define OS_TASK_WAIT_QUEUE (OS_TASK_WAIT_SEM + 1)
276 #define OS_TASK_WAIT_JOIN (OS_TASK_WAIT_QUEUE + 1)
277 #define OS_TASK_WAIT_SIGNAL (OS_TASK_WAIT_JOIN + 1)
278 #define OS_TASK_WAIT_LITEIPC (OS_TASK_WAIT_SIGNAL + 1)
279 #define OS_TASK_WAIT_MUTEX (OS_TASK_WAIT_LITEIPC + 1)
280 #define OS_TASK_WAIT_FUTEX (OS_TASK_WAIT_MUTEX + 1)
281 #define OS_TASK_WAIT_EVENT (OS_TASK_WAIT_FUTEX + 1)
282 #define OS_TASK_WAIT_COMPLETE (OS_TASK_WAIT_EVENT + 1)
283
OsTaskWaitSetPendMask(UINT16 mask,UINTPTR lockID,UINT32 timeout)284 STATIC INLINE VOID OsTaskWaitSetPendMask(UINT16 mask, UINTPTR lockID, UINT32 timeout)
285 {
286 LosTaskCB *runTask = OsCurrTaskGet();
287 runTask->waitID = lockID;
288 runTask->waitFlag = mask;
289 (VOID)timeout;
290 }
291
OsTaskWakeClearPendMask(LosTaskCB * resumeTask)292 STATIC INLINE VOID OsTaskWakeClearPendMask(LosTaskCB *resumeTask)
293 {
294 resumeTask->waitID = 0;
295 resumeTask->waitFlag = 0;
296 }
297
298 extern UINT32 OsTaskSetDetachUnsafe(LosTaskCB *taskCB);
299 extern VOID OsTaskJoinPostUnsafe(LosTaskCB *taskCB);
300 extern UINT32 OsTaskJoinPendUnsafe(LosTaskCB *taskCB);
301 extern BOOL OsTaskCpuAffiSetUnsafe(UINT32 taskID, UINT16 newCpuAffiMask, UINT16 *oldCpuAffiMask);
302 extern VOID OsTaskSchedule(LosTaskCB *, LosTaskCB *);
303 extern VOID OsTaskContextLoad(LosTaskCB *newTask);
304 extern VOID OsIdleTask(VOID);
305 extern UINT32 OsIdleTaskCreate(VOID);
306 extern UINT32 OsTaskInit(VOID);
307 extern UINT32 OsShellCmdDumpTask(INT32 argc, const CHAR **argv);
308 extern UINT32 OsShellCmdTskInfoGet(UINT32 taskID, VOID *seqfile, UINT16 flag);
309 extern LosTaskCB *OsGetMainTask(VOID);
310 extern VOID OsSetMainTask(VOID);
311 extern UINT32 OsGetIdleTaskId(VOID);
312 extern VOID OsTaskEntry(UINT32 taskID);
313 extern VOID OsTaskProcSignal(VOID);
314 extern UINT32 OsCreateUserTask(UINT32 processID, TSK_INIT_PARAM_S *initParam);
315 extern INT32 OsSetTaskName(LosTaskCB *taskCB, const CHAR *name, BOOL setPName);
316 extern VOID OsTaskCBRecycleToFree(VOID);
317 extern VOID OsRunningTaskToExit(LosTaskCB *runTask, UINT32 status);
318 extern INT32 OsUserTaskOperatePermissionsCheck(const LosTaskCB *taskCB);
319 extern INT32 OsUserProcessOperatePermissionsCheck(const LosTaskCB *taskCB, UINT32 processID);
320 extern INT32 OsTcbDispatch(LosTaskCB *stcb, siginfo_t *info);
321 extern VOID OsWriteResourceEvent(UINT32 events);
322 extern VOID OsWriteResourceEventUnsafe(UINT32 events);
323 extern UINT32 OsResourceFreeTaskCreate(VOID);
324 extern VOID OsTaskInsertToRecycleList(LosTaskCB *taskCB);
325 extern VOID OsInactiveTaskDelete(LosTaskCB *taskCB);
326
327 #ifdef __cplusplus
328 #if __cplusplus
329 }
330 #endif /* __cplusplus */
331 #endif /* __cplusplus */
332
333 #endif /* _LOS_TASK_PRI_H */
334