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