• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* ----------------------------------------------------------------------------
2  * Copyright (c) Huawei Technologies Co., Ltd. 2013-2015. All rights reserved.
3  * Description: LiteOS Task Module Implementation
4  * Redistribution and use in source and binary forms, with or without modification,
5  * are permitted provided that the following conditions are met:
6  * 1. Redistributions of source code must retain the above copyright notice, this list of
7  * conditions and the following disclaimer.
8  * 2. Redistributions in binary form must reproduce the above copyright notice, this list
9  * of conditions and the following disclaimer in the documentation and/or other materials
10  * provided with the distribution.
11  * 3. Neither the name of the copyright holder nor the names of its contributors may be used
12  * to endorse or promote products derived from this software without specific prior written
13  * permission.
14  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
15  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
16  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
18  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
19  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
21  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
22  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
23  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
24  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  * --------------------------------------------------------------------------- */
26 
27 #ifndef _LOS_TASK_PRI_H
28 #define _LOS_TASK_PRI_H
29 
30 #include "los_task.h"
31 
32 #ifdef __cplusplus
33 #if __cplusplus
34 extern "C" {
35 #endif /* __cplusplus */
36 #endif /* __cplusplus */
37 
38 /**
39  * @ingroup los_task
40  * Null task ID
41  *
42  */
43 #define OS_TASK_ERRORID                             0xFFFFFFFF
44 
45 /**
46  * @ingroup los_task
47  * Define a usable task priority.
48  *
49  * Highest task priority.
50  */
51 #define OS_TASK_PRIORITY_HIGHEST                    0
52 
53 /**
54  * @ingroup los_task
55  * Define a usable task priority.
56  *
57  * Lowest task priority.
58  */
59 #define OS_TASK_PRIORITY_LOWEST                     31
60 
61 /**
62  * @ingroup los_task
63  * Flag that indicates the task or task control block status.
64  *
65  * The task control block is unused.
66  */
67 #define OS_TASK_STATUS_UNUSED                       0x0001
68 
69 /**
70  * @ingroup los_task
71  * Flag that indicates the task or task control block status.
72  *
73  * The task is suspended.
74  */
75 #define OS_TASK_STATUS_SUSPEND                      0x0002
76 
77 /**
78  * @ingroup los_task
79  * Flag that indicates the task or task control block status.
80  *
81  * The task is ready.
82  */
83 #define OS_TASK_STATUS_READY                        0x0004
84 
85 /**
86  * @ingroup los_task
87  * Flag that indicates the task or task control block status.
88  *
89  * The task is blocked.
90  */
91 #define OS_TASK_STATUS_PEND                         0x0008
92 
93 /**
94  * @ingroup los_task
95  * Flag that indicates the task or task control block status.
96  *
97  * The task is running.
98  */
99 #define OS_TASK_STATUS_RUNNING                      0x0010
100 
101 /**
102  * @ingroup los_task
103  * Flag that indicates the task or task control block status.
104  *
105  * The task is delayed.
106  */
107 #define OS_TASK_STATUS_DELAY                        0x0020
108 
109 /**
110  * @ingroup los_task
111  * Flag that indicates the task or task control block status.
112  *
113  * The time for waiting for an event to occur expires.
114  */
115 #define OS_TASK_STATUS_TIMEOUT                      0x0040
116 
117 /**
118  * @ingroup los_task
119  * Flag that indicates the task or task control block status.
120  *
121  * The task is waiting for an event to occur.
122  */
123 #define OS_TASK_STATUS_EVENT                        0x0400
124 
125 /**
126  * @ingroup los_task
127  * Flag that indicates the task or task control block status.
128  *
129  * The task is reading an event.
130  */
131 #define OS_TASK_STATUS_EVENT_READ                   0x0800
132 
133 /**
134  * @ingroup los_task
135  * Flag that indicates the task or task control block status.
136  *
137  * A software timer is waiting for an event to occur.
138  */
139 #define OS_TASK_STATUS_SWTMR_WAIT                   0x1000
140 
141 /**
142  * @ingroup los_task
143  * Flag that indicates the task or task control block status.
144  *
145  * The task is blocked on a queue.
146  */
147 #define OS_TASK_STATUS_PEND_QUEUE                   0x2000
148 
149 /**
150  * @ingroup los_task
151  * Flag that indicates the task or task control block status.
152  *
153  * The task is blocked on a mutex.
154  */
155 #define OS_TASK_STATUS_PEND_MUT                     0x4000
156 
157 /**
158  * @ingroup los_task
159  * Flag that indicates the task or task control block status.
160  *
161  * The task is blocked on a semaphore.
162  */
163 #define OS_TASK_STATUS_PEND_SEM                     0x8000
164 
165 /**
166  * @ingroup los_task
167  * Boundary on which the stack size is aligned.
168  *
169  */
170 #define OS_TASK_STACK_SIZE_ALIGN                    16
171 
172 /**
173  * @ingroup los_task
174  * Boundary on which the stack address is aligned.
175  *
176  */
177 #define OS_TASK_STACK_ADDR_ALIGN                    8
178 
179 /**
180  * @ingroup los_task
181  * Task stack top magic number.
182  *
183  */
184 #define OS_TASK_MAGIC_WORD                          0xCCCCCCCC
185 
186 /**
187  * @ingroup los_task
188  * Initial task stack value.
189  *
190  */
191 #define OS_TASK_STACK_INIT                          0xCACACACA
192 
193 /**
194  * @ingroup los_task
195  * Number of usable task priorities.
196  */
197 #define OS_TSK_PRINUM                               ((OS_TASK_PRIORITY_LOWEST - OS_TASK_PRIORITY_HIGHEST) + 1)
198 
199 /**
200  * @ingroup los_task
201  * @brief the num of delayed tasks bucket
202  */
203 #define OS_TSK_SORTLINK_LEN                         32
204 
205 /**
206  * @ingroup los_task
207  * @brief the bit width occupied by the delayed ticks of task
208  */
209 #define OS_TSK_SORTLINK_LOGLEN                      5
210 
211 /**
212  * @ingroup los_task
213  * @brief the mask of delayed tasks bucket id.
214  */
215 #define OS_TSK_SORTLINK_MASK                        (OS_TSK_SORTLINK_LEN - 1)
216 
217 /**
218  * @ingroup los_task
219  * @brief the max task count for switch.
220  */
221 #define OS_TASK_SWITCH_INFO_COUNT                   0xA
222 
223 /**
224  * @ingroup  los_task
225  * @brief Check whether a task ID is valid.
226  *
227  * @par Description:
228  * This API is used to check whether a task ID, excluding the idle task ID, is valid.
229  * @attention None.
230  *
231  * @param  taskID [IN] Task ID.
232  *
233  * @retval 0 or 1. One indicates that the task ID is invalid, whereas zero indicates that the task ID is valid.
234  * @par Dependency:
235  * <ul><li>los_task_pri.h: the header file that contains the API declaration.</li></ul>
236  * @see
237  */
238 #define OS_TSK_GET_INDEX(taskID)                      (taskID)
239 
240 /**
241  * @ingroup  los_task
242  * @brief Obtain the pointer to a task control block.
243  *
244  * @par Description:
245  * This API is used to obtain the pointer to a task control block using a corresponding parameter.
246  * @attention None.
247  *
248  * @param  ptr [IN] Parameter used for obtaining the task control block.
249  *
250  * @retval Pointer to the task control block.
251  * @par Dependency:
252  * <ul><li>los_task_pri.h: the header file that contains the API declaration.</li></ul>
253  * @see
254  */
255 #define OS_TCB_FROM_PENDLIST(ptr)                       LOS_DL_LIST_ENTRY(ptr, LosTaskCB, pendList)
256 
257 /**
258  * @ingroup  los_task
259  * @brief Obtain the pointer to a task control block.
260  *
261  * @par Description:
262  * This API is used to obtain the pointer to a task control block that has a specified task ID.
263  * @attention None.
264  *
265  * @param  taskID [IN] task ID.
266  *
267  * @retval Pointer to the task control block.
268  * @par Dependency:
269  * <ul><li>los_task_pri.h: the header file that contains the API declaration.</li></ul>
270  * @see
271  */
272 #define OS_TCB_FROM_TID(taskID)                       (((LosTaskCB *)g_taskCBArray) + (taskID))
273 #define OS_IDLE_TASK_ENTRY                            ((TSK_ENTRY_FUNC)OsIdleTask)
274 
275 /**
276  * @ingroup los_task
277  * Define the task control block structure.
278  */
279 typedef struct {
280     VOID                        *stackPointer;            /**< Task stack pointer */
281     UINT16                      taskStatus;
282     UINT16                      priority;
283     UINT32                      stackSize;                /**< Task stack size */
284     UINT32                      topOfStack;               /**< Task stack top */
285     UINT32                      taskID;                   /**< Task ID */
286     TSK_ENTRY_FUNC              taskEntry;                /**< Task entrance function */
287     VOID                        *taskSem;                 /**< Task-held semaphore */
288     VOID                        *taskMux;                 /**< Task-held mutex */
289     UINT32                      args[LOS_TASK_ARG_NUM];   /**< Parameter */
290     CHAR                        *taskName;                /**< Task name */
291     LOS_DL_LIST                 pendList;
292     LOS_DL_LIST                 timerList;
293     UINT32                      idxRollNum;
294     EVENT_CB_S                  event;
295     UINT32                      eventMask;                /**< Event mask */
296     UINT32                      eventMode;                /**< Event mode */
297     VOID                        *msg;                     /**< Memory allocated to queues */
298     UINT32                      priBitMap;                /**< BitMap for recording the change of task priority, */
299 	                                                      /**< the priority can not be greater than 31 */
300 } LosTaskCB;
301 
302 typedef struct {
303     LosTaskCB   *runTask;
304     LosTaskCB   *newTask;
305 } LosTask;
306 
307 typedef struct {
308     LOS_DL_LIST     *sortLink;
309     UINT16          cursor;
310     UINT16          unused;
311 } TaskSortLinkAttr;
312 
313 /**
314  * @ingroup los_task
315  * Time slice structure.
316  */
317 typedef struct TaskTimeSlice {
318     LosTaskCB               *task;                        /**< Current running task */
319     UINT16                  time;                         /**< Expiration time point */
320     UINT16                  tout;                         /**< Expiration duration */
321 } OsTaskRobin;
322 
323 typedef struct {
324     UINT8 maxCnt : 7;   // bits [6:0] store count of task switch info
325     UINT8 isFull : 1;   // bit [7] store isfull status
326 } TaskCountInfo;
327 
328 /**
329  * @ingroup los_task
330  * Task switch information structure.
331  *
332  */
333 typedef struct {
334     UINT8 idx;
335     TaskCountInfo cntInfo;
336     UINT16 pid[OS_TASK_SWITCH_INFO_COUNT];
337     CHAR   name[OS_TASK_SWITCH_INFO_COUNT][LOS_TASK_NAMELEN];
338 } TaskSwitchInfo;
339 
340 extern LosTask              g_losTask;
341 
342 /**
343  * @ingroup los_task
344  * Task lock flag.
345  *
346  */
347 extern UINT16               g_losTaskLock;
348 
349 /**
350  * @ingroup los_task
351  * Maximum number of tasks.
352  *
353  */
354 extern UINT32               g_taskMaxNum;
355 
356 /**
357  * @ingroup los_task
358  * Idle task ID.
359  *
360  */
361 extern UINT32               g_idleTaskID;
362 
363 /**
364  * @ingroup los_task
365  * Software timer task ID.
366  *
367  */
368 extern UINT32               g_swtmrTaskID;
369 
370 /**
371  * @ingroup los_task
372  * Starting address of a task.
373  *
374  */
375 extern LosTaskCB            *g_taskCBArray;
376 
377 /**
378  * @ingroup los_task
379  * Delayed task linked list.
380  *
381  */
382 extern LOS_DL_LIST          g_taskTimerList;
383 
384 /**
385  * @ingroup los_task
386  * Free task linked list.
387  *
388  */
389 extern LOS_DL_LIST          g_losFreeTask;
390 
391 /**
392  * @ingroup los_task
393  * Circular linked list that stores tasks that are deleted automatically.
394  *
395  */
396 extern LOS_DL_LIST          g_taskRecyleList;
397 
398 /**
399  * @ingroup los_task
400  * task Sort linked list.
401  *
402  */
403 extern TaskSortLinkAttr            g_taskSortLink;
404 
405 extern  BOOL                        g_taskScheduled;
406 
407 extern  UINT32                      g_taskUsedCount;
408 
409 /**
410  * @ingroup los_task
411  * @brief the block status of task
412  */
413 extern VOID OsTaskSchedule(VOID);
414 
415 /**
416  * @ingroup  los_task
417  * @brief Modify the priority of task.
418  *
419  * @par Description:
420  * This API is used to modify the priority of task.
421  *
422  * @attention
423  * <ul>
424  * <li>The taskCB should be a correct pointer to task control block structure.</li>
425  * <li>the priority should be in [0, OS_TASK_PRIORITY_LOWEST].</li>
426  * </ul>
427  *
428  * @param  taskCB    [IN] Type #LosTaskCB * pointer to task control block structure.
429  * @param  priority  [IN] Type #UINT16 the priority of task.
430  *
431  * @retval  None.
432  * @par Dependency:
433  * <ul><li>los_task_pri.h: the header file that contains the API declaration.</li></ul>
434  * @see
435  */
436 extern VOID OsTaskPriModify(LosTaskCB *taskCB, UINT16 priority);
437 
438 /**
439  * @ingroup  los_task
440  * @brief Scan a task.
441  *
442  * @par Description:
443  * This API is used to scan a task.
444  *
445  * @attention
446  * <ul>
447  * <li>None.</li>
448  * </ul>
449  *
450  * @param  None.
451  *
452  * @retval  None.
453  * @par Dependency:
454  * <ul><li>los_task_pri.h: the header file that contains the API declaration.</li></ul>
455  * @see
456  */
457 extern VOID OsTaskScan(VOID);
458 
459 /**
460  * @ingroup  los_task
461  * @brief Initialization a task.
462  *
463  * @par Description:
464  * This API is used to initialization a task.
465  *
466  * @attention
467  * <ul>
468  * <li>None.</li>
469  * </ul>
470  *
471  * @param  None.
472  *
473  * @retval  UINT32    Initialization result.
474  * @par Dependency:
475  * <ul><li>los_task_pri.h: the header file that contains the API declaration.</li></ul>
476  * @see
477  */
478 extern UINT32 OsTaskInit(VOID *taskArray);
479 
480 /**
481  * @ingroup  los_task
482  * @brief Create idle task.
483  *
484  * @par Description:
485  * This API is used to create idle task.
486  *
487  * @attention
488  * <ul>
489  * <li>None.</li>
490  * </ul>
491  *
492  * @param  None.
493  *
494  * @retval  UINT32   Create result.
495  * @par Dependency:
496  * <ul><li>los_task_pri.h: the header file that contains the API declaration.</li></ul>
497  * @see
498  */
499 extern UINT32 OsIdleTaskCreate(VOID);
500 
501 /**
502  * @ingroup  los_task
503  * @brief Check task switch.
504  *
505  * @par Description:
506  * This API is used to check task switch.
507  *
508  * @attention
509  * <ul>
510  * <li>None.</li>
511  * </ul>
512  *
513  * @param  None.
514  *
515  * @retval  None.
516  * @par Dependency:
517  * <ul><li>los_task_pri.h: the header file that contains the API declaration.</li></ul>
518  * @see
519  */
520 extern VOID OsTaskSwitchCheck(VOID);
521 
522 /**
523  * @ingroup  los_task
524  * @brief TaskMonInit.
525  *
526  * @par Description:
527  * This API is used to taskMonInit.
528  *
529  * @attention
530  * <ul>
531  * <li>None.</li>
532  * </ul>
533  *
534  * @param  None.
535  *
536  * @retval  None.
537  * @par Dependency:
538  * <ul><li>los_task_pri.h: the header file that contains the API declaration.</li></ul>
539  * @see
540  */
541 extern VOID OsTaskMonInit(VOID);
542 
543 /**
544  * @ingroup  los_task
545  * @brief Task entry.
546  *
547  * @par Description:
548  * This API is used to task entry.
549  *
550  * @attention
551  * <ul>
552  * <li>None.</li>
553  * </ul>
554  *
555  * @param  taskID  [IN] Type #UINT32   task id.
556  *
557  * @retval  None.
558  * @par Dependency:
559  * <ul><li>los_task_pri.h: the header file that contains the API declaration.</li></ul>
560  * @see
561  */
562 extern VOID OsTaskEntry(UINT32 taskID);
563 
564 /**
565  * @ingroup  los_task
566  * @brief pend running task to pendlist
567  *
568  * @par Description:
569  * This API is used to pend task to  pendlist and add to sorted delay list.
570  *
571  * @attention
572  * <ul>
573  * <li>The pstList should be a vaild pointer to pendlist.</li>
574  * </ul>
575  *
576  * @param  list       [IN] Type #LOS_DL_LIST * pointer to list which running task will be pended.
577  * @param  taskStatus [IN] Type #UINT32  Task Status.
578  * @param  timeOut    [IN] Type #UINT32  Expiry time. The value range is [0,LOS_WAIT_FOREVER].
579  *
580  * @retval  LOS_OK       wait success
581  * @retval  LOS_NOK      pend out
582  * @par Dependency:
583  * <ul><li>los_task_pri.h: the header file that contains the API declaration.</li></ul>
584  * @see OsTaskWake
585  */
586 extern VOID OsTaskWait(LOS_DL_LIST *list, UINT32 taskStatus, UINT32 timeOut);
587 
588 /**
589  * @ingroup  los_task
590  * @brief delete task from pendlist.
591  *
592  * @par Description:
593  * This API is used to delete task from pendlist and also add to the priqueue.
594  *
595  * @attention
596  * <ul>
597  * <li>The pstList should be a vaild pointer to pend list.</li>
598  * </ul>
599  *
600  * @param  resumedTask [IN] Type #LosTaskCB * pointer to the task which will be add to priqueue.
601  * @param  taskStatus  [IN] Type #UINT32  Task Status.
602  *
603  * @retval  None.
604  * @par Dependency:
605  * <ul><li>los_task_pri.h: the header file that contains the API declaration.</li></ul>
606  * @see OsTaskWait
607  */
608 extern VOID OsTaskWake(LosTaskCB *resumedTask, UINT32 taskStatus);
609 
610 /**
611  * @ingroup  los_task
612  * @brief Get the task water line.
613  *
614  * @par Description:
615  * This API is used to get the task water line.
616  *
617  * @attention
618  * <ul>
619  * <li>None.</li>
620  * </ul>
621  *
622  * @param  taskID [IN] Type #UINT32 task id.
623  *
624  * @retval  UINT32  Task water line.
625  * @par Dependency:
626  * <ul><li>los_task_pri.h: the header file that contains the API declaration.</li></ul>
627  * @see None.
628  */
629 extern UINT32 OsGetTaskWaterLine(UINT32 taskID);
630 
631 /**
632  * @ingroup  los_task
633  * @brief Convert task status to string.
634  *
635  * @par Description:
636  * This API is used to convert task status to string.
637  *
638  * @attention
639  * <ul>
640  * <li>None.</li>
641  * </ul>
642  *
643  * @param  taskStatus [IN] Type #UINT16 task status.
644  *
645  * @retval  UINT8 *  String.
646  * @par Dependency:
647  * <ul><li>los_task_pri.h: the header file that contains the API declaration.</li></ul>
648  * @see None.
649  */
650 extern UINT8 *OsConvertTskStatus(UINT16 taskStatus);
651 
652 /**
653  * @ingroup  los_task
654  * @brief Add task to sorted delay list.
655  *
656  * @par Description:
657  * This API is used to add task to sorted delay list.
658  *
659  * @attention
660  * <ul>
661  * <li>The taskCB should be a correct pointer to task control block structure.</li>
662  * </ul>
663  *
664  * @param  taskCB     [IN] Type #LosTaskCB * pointer to task control block structure.
665  * @param  timeout    [IN] Type #UINT32 wait time, ticks.
666  *
667  * @retval  None.
668  * @par Dependency:
669  * <ul><li>los_task_pri.h: the header file that contains the API declaration.</li></ul>
670  * @see OsTimerListDelete
671  */
672 extern VOID OsTaskAdd2TimerList(LosTaskCB *taskCB, UINT32 timeout);
673 
674 /**
675  * @ingroup  los_task
676  * @brief delete task from sorted delay list.
677  *
678  * @par Description:
679  * This API is used to delete task from sorted delay list.
680  *
681  * @attention
682  * <ul>
683  * <li>The taskCB should be a correct pointer to task control block structure.</li>
684  * </ul>
685  *
686  * @param  taskCB [IN] Type #LosTaskCB * pointer to task control block structure.
687  *
688  * @retval  None.
689  * @par Dependency:
690  * <ul><li>los_task_pri.h: the header file that contains the API declaration.</li></ul>
691  * @see OsTaskAdd2TimerList
692  */
693 extern VOID OsTimerListDelete(LosTaskCB *taskCB);
694 
695 extern BOOL OsRunningTaskIsSwtmr(VOID);
696 
697 #ifdef __cplusplus
698 #if __cplusplus
699 }
700 #endif /* __cplusplus */
701 #endif /* __cplusplus */
702 
703 #endif /* _LOS_TASK_PRI_H */
704