更新日期: 2022/06/01 来源: https://gitee.com/weharmony/kernel_liteos_a_note
Task
Task 的协作图:

结构体

struct  OsTaskRobin
 
struct  UserTaskParam
 
struct  tagTskInitParam
 
struct  tagTskInfo
 

类型定义

typedef VOID *(* TSK_ENTRY_FUNC) (UINTPTR param1, UINTPTR param2, UINTPTR param3, UINTPTR param4)
 Define the type of a task entrance function. 更多...
 
typedef struct tagTskInitParam TSK_INIT_PARAM_S
 
typedef struct tagTskInfo TSK_INFO_S
 

函数

UINT32 OsStackWaterLineGet (const UINTPTR *stackBottom, const UINTPTR *stackTop, UINT32 *peakUsed)
 Get stack waterline. 更多...
 
UINT32 LOS_TaskCreateOnly (UINT32 *taskID, TSK_INIT_PARAM_S *initParam)
 Create a task and suspend. 更多...
 
UINT32 LOS_TaskCreate (UINT32 *taskID, TSK_INIT_PARAM_S *initParam)
 Create a task. 更多...
 
UINT32 LOS_TaskResume (UINT32 taskID)
 Resume a task. 更多...
 
UINT32 LOS_TaskSuspend (UINT32 taskID)
 Suspend a task. 更多...
 
UINT32 LOS_TaskDelete (UINT32 taskID)
 Delete a task. 更多...
 
UINT32 LOS_TaskDelay (UINT32 tick)
 Delay a task. 更多...
 
VOID LOS_TaskLock (VOID)
 Lock the task scheduling. 更多...
 
VOID LOS_TaskUnlock (VOID)
 Unlock the task scheduling. 更多...
 
UINT32 LOS_TaskPriSet (UINT32 taskID, UINT16 taskPrio)
 Set a task priority. 更多...
 
UINT32 LOS_CurTaskPriSet (UINT16 taskPrio)
 Set the priority of the current running task to a specified priority. 更多...
 
UINT32 LOS_TaskYield (VOID)
 Change the scheduling sequence of tasks with the same priority. 更多...
 
UINT16 LOS_TaskPriGet (UINT32 taskID)
 Obtain a task priority. 更多...
 
UINT32 LOS_CurTaskIDGet (VOID)
 Obtain current running task ID. 更多...
 
UINT32 LOS_GetSystemTaskMaximum (VOID)
 Gets the maximum number of threads supported by the system. 更多...
 
UINT32 LOS_TaskInfoGet (UINT32 taskID, TSK_INFO_S *taskInfo)
 Obtain a task information structure. 更多...
 
UINT32 LOS_TaskCpuAffiSet (UINT32 uwTaskID, UINT16 usCpuAffiMask)
 Set the affinity mask of the task scheduling cpu. 更多...
 
UINT16 LOS_TaskCpuAffiGet (UINT32 taskID)
 Get the affinity mask of the task scheduling cpu. 更多...
 
INT32 LOS_GetTaskScheduler (INT32 taskID)
 Get the scheduling policy for the task. 更多...
 
INT32 LOS_SetTaskScheduler (INT32 taskID, UINT16 policy, UINT16 priority)
 Set the scheduling policy and priority for the task. 更多...
 
VOID LOS_Schedule (VOID)
 Trigger active task scheduling. 更多...
 
UINT32 LOS_TaskJoin (UINT32 taskID, UINTPTR *retval)
 Wait for the specified task to finish and reclaim its resources. 更多...
 
UINT32 LOS_TaskDetach (UINT32 taskID)
 Change the joinable attribute of the task to detach. 更多...
 

变量

UINT32 g_taskMaxNum
 任务最大数量 默认128个 更多...
 
LosTaskCBg_taskCBArray
 外部变量 任务池 默认128个 更多...
 

详细描述

类型定义说明

◆ TSK_ENTRY_FUNC

typedef VOID *(* TSK_ENTRY_FUNC) (UINTPTR param1, UINTPTR param2, UINTPTR param3, UINTPTR param4)

Define the type of a task entrance function.

Description:
This API is used to define the type of a task entrance function and call it after a task is created and triggered.
注意
None.
参数
param1[IN] Type UINTPTR The first parameter passed to the task handling function.
param2[IN] Type UINTPTR The second parameter passed to the task handling function.
param3[IN] Type UINTPTR The third parameter passed to the task handling function.
param4[IN] Type UINTPTR The fourth parameter passed to the task handling function.
返回值
None.
Dependency:
  • los_task.h: the header file that contains the API declaration.
参见

在文件 los_task.h480 行定义.

◆ TSK_INFO_S

typedef struct tagTskInfo TSK_INFO_S

Task information structure.

◆ TSK_INIT_PARAM_S

Define the structure of the parameters used for task creation.

Information of specified parameters passed in during task creation.

函数说明

◆ LOS_CurTaskIDGet()

UINT32 LOS_CurTaskIDGet ( VOID  )

Obtain current running task ID.

Description:
This API is used to obtain the ID of current running task.
注意
  • This interface should not be called before system initialized.
返回值
#LOS_ERRNO_TSK_ID_INVALIDInvalid Task ID.
UINT32Task ID.
Dependency:
  • los_task.h: the header file that contains the API declaration.
参见

在文件 los_task.c331 行定义.

332{
333 LosTaskCB *runTask = OsCurrTaskGet();
334
335 if (runTask == NULL) {
336 return LOS_ERRNO_TSK_ID_INVALID;
337 }
338 return runTask->taskID;
339}
STATIC INLINE LosTaskCB * OsCurrTaskGet(VOID)
UINT32 taskID
函数调用图:
这是这个函数的调用关系图:

◆ LOS_CurTaskPriSet()

UINT32 LOS_CurTaskPriSet ( UINT16  taskPrio)

Set the priority of the current running task to a specified priority.

Description:
This API is used to set the priority of the current running task to a specified priority.
注意
  • Changing the priority of the current running task probably causes task scheduling.
  • Using the interface to change the priority of software timer task and idle task is not allowed.
  • Using the interface in the interrupt is not allowed.
参数
taskPrio[IN] Type UINT16 Task priority.
返回值
#LOS_ERRNO_TSK_PRIOR_ERRORIncorrect task priority.Re-configure the task priority
#LOS_ERRNO_TSK_OPERATE_IDLECheck the task ID and do not operate on the idle task.
#LOS_ERRNO_TSK_ID_INVALIDInvalid Task ID
#LOS_ERRNO_TSK_NOT_CREATEDThe task is not created.
#LOS_OKThe priority of the current running task is successfully set to a specified priority.
Dependency:
  • los_task.h: the header file that contains the API declaration.
参见
LOS_TaskPriGet

Set the priority of the current running task to a specified priority.

在文件 los_task.c1109 行定义.

1110{
1111 return LOS_TaskPriSet(OsCurrTaskGet()->taskID, taskPrio);
1112}
LITE_OS_SEC_TEXT_MINOR UINT32 LOS_TaskPriSet(UINT32 taskID, UINT16 taskPrio)
设置指定任务的优先级
Definition: los_task.c:1071
函数调用图:

◆ LOS_GetSystemTaskMaximum()

UINT32 LOS_GetSystemTaskMaximum ( VOID  )

Gets the maximum number of threads supported by the system.

Description:
This API is used to gets the maximum number of threads supported by the system.
注意
  • This interface should not be called before system initialized.
返回值
None.
Dependency:
  • los_task.h: the header file that contains the API declaration.
参见

在文件 los_task.c1637 行定义.

1638{
1639 return g_taskMaxNum;
1640}
LITE_OS_SEC_BSS UINT32 g_taskMaxNum
任务最大数量 默认128个
Definition: los_task.c:150

◆ LOS_GetTaskScheduler()

INT32 LOS_GetTaskScheduler ( INT32  taskID)

Get the scheduling policy for the task.

Description:
This API is used to get the scheduling policy for the task.
注意
None.
参数
taskID[IN] Type UINT32 Task ID. The task id value is obtained from task creation.
返回值
#-LOS_ESRCHInvalid task id.
#-LOS_EPERMThe process is not currently running.
INT32the scheduling policy.
Dependency:
  • los_task.h: the header file that contains the API declaration.
参见
LOS_SetTaskScheduler

Get the scheduling policy for the task.

在文件 los_task.c1467 行定义.

1468{
1469 UINT32 intSave;
1470 INT32 policy;
1471 SchedParam param = { 0 };
1472
1473 if (OS_TID_CHECK_INVALID(taskID)) {
1474 return -LOS_EINVAL;
1475 }
1476
1477 LosTaskCB *taskCB = OS_TCB_FROM_TID(taskID);
1478 SCHEDULER_LOCK(intSave);
1479 if (taskCB->taskStatus & OS_TASK_STATUS_UNUSED) {//状态不能是没有在使用
1480 policy = -LOS_EINVAL;
1481 OS_GOTO_ERREND();
1482 }
1483
1484 taskCB->ops->schedParamGet(taskCB, &param);
1485 policy = (INT32)param.policy;
1486
1487LOS_ERREND:
1488 SCHEDULER_UNLOCK(intSave);
1489 return policy;
1490}
signed int INT32
Definition: los_typedef.h:60
unsigned int UINT32
Definition: los_typedef.h:57
UINT32(* schedParamGet)(const LosTaskCB *taskCB, SchedParam *param)
获取调度参数
UINT16 policy
const SchedOps * ops
UINT16 taskStatus

◆ LOS_Schedule()

VOID LOS_Schedule ( VOID  )

Trigger active task scheduling.

Description:
This API is used to trigger active task scheduling.
注意
None.
参数
None
返回值
Nobe
Dependency:
  • los_task.h: the header file that contains the API declaration.

在文件 los_sched.c469 行定义.

470{
471 UINT32 intSave;
472 LosTaskCB *runTask = OsCurrTaskGet();
474
475 if (OS_INT_ACTIVE) {
477 return;
478 }
479
480 if (!OsPreemptable()) {
481 return;
482 }
483
484 /*
485 * trigger schedule in task will also do the slice check
486 * if necessary, it will give up the timeslice more in time.
487 * otherwise, there's no other side effects.
488 */
489 SCHEDULER_LOCK(intSave);
490
491 runTask->ops->timeSliceUpdate(rq, runTask, OsGetCurrSchedTimeCycle());
492
493 /* add run task back to ready queue */
494 runTask->ops->enqueue(rq, runTask);
495
496 /* reschedule to new thread */
498
499 SCHEDULER_UNLOCK(intSave);
500}
VOID OsSchedResched(VOID)
Definition: los_sched.c:449
STATIC INLINE UINT64 OsGetCurrSchedTimeCycle(VOID)
Definition: los_sched_pri.h:75
STATIC INLINE BOOL OsPreemptable(VOID)
STATIC INLINE SchedRunqueue * OsSchedRunqueue(VOID)
STATIC INLINE VOID OsSchedRunqueuePendingSet(VOID)
VOID(* enqueue)(SchedRunqueue *rq, LosTaskCB *taskCB)
入队列
VOID(* timeSliceUpdate)(SchedRunqueue *rq, LosTaskCB *taskCB, UINT64 currTime)
更新时间片
函数调用图:
这是这个函数的调用关系图:

◆ LOS_SetTaskScheduler()

INT32 LOS_SetTaskScheduler ( INT32  taskID,
UINT16  policy,
UINT16  priority 
)

Set the scheduling policy and priority for the task.

Description:
This API is used to set the scheduling policy and priority for the task.
注意
None.
参数
taskID[IN] Type UINT32 Task ID. The task id value is obtained from task creation.
policy[IN] Type UINT16 Task scheduling policy.
priority[IN] Type UINT16 Task scheduling priority.
返回值
-LOS_ESRCHInvalid task id.
-LOS_EOPNOTSUPPUnsupported fields.
-LOS_EPERMThe process is not currently running.
#0Set up the success.
Dependency:
  • los_task.h: the header file that contains the API declaration.
参见
LOS_GetTaskScheduler

在文件 los_task.c1493 行定义.

1494{
1495 SchedParam param = { 0 };
1496 UINT32 intSave;
1497
1498 if (OS_TID_CHECK_INVALID(taskID)) {
1499 return LOS_ESRCH;
1500 }
1501
1502 if (priority > OS_TASK_PRIORITY_LOWEST) {
1503 return LOS_EINVAL;
1504 }
1505
1506 if ((policy != LOS_SCHED_FIFO) && (policy != LOS_SCHED_RR)) {
1507 return LOS_EINVAL;
1508 }
1509
1510 LosTaskCB *taskCB = OS_TCB_FROM_TID(taskID);
1511 if (taskCB->taskStatus & OS_TASK_FLAG_SYSTEM_TASK) {
1512 return LOS_EPERM;
1513 }
1514
1515 SCHEDULER_LOCK(intSave);
1516 if (taskCB->taskStatus & OS_TASK_STATUS_UNUSED) {
1517 SCHEDULER_UNLOCK(intSave);
1518 return LOS_EINVAL;
1519 }
1520
1521 taskCB->ops->schedParamGet(taskCB, &param);
1522 param.policy = policy;
1523 param.priority = priority;
1524 BOOL needSched = taskCB->ops->schedParamModify(taskCB, &param);
1525 SCHEDULER_UNLOCK(intSave);
1526
1527 LOS_MpSchedule(OS_MP_CPU_ALL);
1528 if (needSched && OS_SCHEDULER_ACTIVE) {
1529 LOS_Schedule();
1530 }
1531
1532 return LOS_OK;
1533}
VOID LOS_Schedule(VOID)
Trigger active task scheduling.
Definition: los_sched.c:469
VOID LOS_MpSchedule(UINT32 target)
Definition: los_mp.c:76
size_t BOOL
Definition: los_typedef.h:88
BOOL(* schedParamModify)(LosTaskCB *taskCB, const SchedParam *param)
修改调度参数
UINT16 priority
函数调用图:
这是这个函数的调用关系图:

◆ LOS_TaskCpuAffiGet()

UINT16 LOS_TaskCpuAffiGet ( UINT32  taskID)

Get the affinity mask of the task scheduling cpu.

Description:
This API is used to get the affinity mask of the task scheduling cpu.
注意
None.
参数
taskID[IN] Type UINT32 Task ID. The task id value is obtained from task creation.
返回值
#0The cpu affinity mask fails to be obtained.
UINT16The scheduling cpu mask. The low to high bit of the mask corresponds to the cpu number.
Dependency:
  • los_task.h: the header file that contains the API declaration.
参见
LOS_TaskCpuAffiSet

Get the affinity mask of the task scheduling cpu.

在文件 los_task.c1263 行定义.

1264{
1265#ifdef LOSCFG_KERNEL_SMP
1266#define INVALID_CPU_AFFI_MASK 0
1267 UINT16 cpuAffiMask;
1268 UINT32 intSave;
1269
1270 if (OS_TID_CHECK_INVALID(taskID)) {
1271 return INVALID_CPU_AFFI_MASK;
1272 }
1273
1274 LosTaskCB *taskCB = OS_TCB_FROM_TID(taskID);
1275 SCHEDULER_LOCK(intSave);
1276 if (taskCB->taskStatus & OS_TASK_STATUS_UNUSED) { //任务必须在使用
1277 SCHEDULER_UNLOCK(intSave);
1278 return INVALID_CPU_AFFI_MASK;
1279 }
1280
1281 cpuAffiMask = taskCB->cpuAffiMask; //获取亲和力掩码
1282 SCHEDULER_UNLOCK(intSave);
1283
1284 return cpuAffiMask;
1285#else
1286 (VOID)taskID;
1287 return 1;//单核情况直接返回1 ,0号cpu对应0x01
1288#endif
1289}
unsigned short UINT16
Definition: los_typedef.h:56
UINT16 cpuAffiMask
这是这个函数的调用关系图:

◆ LOS_TaskCpuAffiSet()

UINT32 LOS_TaskCpuAffiSet ( UINT32  uwTaskID,
UINT16  usCpuAffiMask 
)

Set the affinity mask of the task scheduling cpu.

Description:
This API is used to set the affinity mask of the task scheduling cpu.
注意
  • If any low LOSCFG_KERNEL_CORE_NUM bit of the mask is not setted, an error is reported.
参数
uwTaskID[IN] Type UINT32 Task ID. The task id value is obtained from task creation.
usCpuAffiMask[IN] Type UINT32 The scheduling cpu mask.The low to high bit of the mask corresponds to the cpu number, the high bit that exceeding the CPU number is ignored.
返回值
#LOS_ERRNO_TSK_ID_INVALIDInvalid task ID.
#LOS_ERRNO_TSK_NOT_CREATEDThe task is not created.
#LOS_ERRNO_TSK_CPU_AFFINITY_MASK_ERRThe task cpu affinity mask is incorrect.
#LOS_OKThe task cpu affinity mask is successfully setted.
Dependency:
  • los_task.h: the header file that contains the API declaration.
参见
LOS_TaskCpuAffiGet

在文件 los_task.c1231 行定义.

1232{
1233 BOOL needSched = FALSE;
1234 UINT32 intSave;
1235 UINT16 currCpuMask;
1236
1237 if (OS_TID_CHECK_INVALID(taskID)) {//检测taskid是否有效,task由task池分配,鸿蒙默认128个任务 ID范围[0:127]
1238 return LOS_ERRNO_TSK_ID_INVALID;
1239 }
1240
1241 if (!(cpuAffiMask & LOSCFG_KERNEL_CPU_MASK)) {//检测cpu亲和力
1242 return LOS_ERRNO_TSK_CPU_AFFINITY_MASK_ERR;
1243 }
1244
1245 LosTaskCB *taskCB = OS_TCB_FROM_TID(taskID);
1246 SCHEDULER_LOCK(intSave);
1247 if (taskCB->taskStatus & OS_TASK_STATUS_UNUSED) {//贴有未使用标签的处理
1248 SCHEDULER_UNLOCK(intSave);
1249 return LOS_ERRNO_TSK_NOT_CREATED;
1250 }
1251 needSched = OsTaskCpuAffiSetUnsafe(taskID, cpuAffiMask, &currCpuMask);
1252
1253 SCHEDULER_UNLOCK(intSave);
1254
1255 if (needSched && OS_SCHEDULER_ACTIVE) {
1256 LOS_MpSchedule(currCpuMask);//发送信号调度信号给目标CPU
1257 LOS_Schedule();//申请调度
1258 }
1259
1260 return LOS_OK;
1261}
LITE_OS_SEC_TEXT BOOL OsTaskCpuAffiSetUnsafe(UINT32 taskID, UINT16 newCpuAffiMask, UINT16 *oldCpuAffiMask)
CPU亲和性(affinity)将任务绑在指定CPU上,用于多核CPU情况,(该函数仅在SMP模式下支持)
Definition: los_task.c:1212
函数调用图:
这是这个函数的调用关系图:

◆ LOS_TaskCreate()

UINT32 LOS_TaskCreate ( UINT32 taskID,
TSK_INIT_PARAM_S initParam 
)

Create a task.

Description:
This API is used to create a task. If the priority of the task created after system initialized is higher than the current task and task scheduling is not locked, it is scheduled for running. If not, the created task is added to the queue of ready tasks.
注意
  • During task creation, the task control block and task stack of the task that is previously automatically deleted are deallocated.
  • The task name is a pointer and is not allocated memory.
  • If the size of the task stack of the task to be created is 0, configure #LOSCFG_BASE_CORE_TSK_DEFAULT_STACK_SIZE to specify the default task stack size.
  • The task stack size must be aligned on the boundary of 8 bytes. The size is determined by whether it is big enough to avoid task stack overflow.
  • Less parameter value indicates higher task priority.
  • The task name cannot be null.
  • The pointer to the task executing function cannot be null.
  • The two parameters of this interface is pointer, it should be a correct value, otherwise, the system may be abnormal.
参数
taskID[OUT] Type UINT32 * Task ID.
initParam[IN] Type TSK_INIT_PARAM_S * Parameter for task creation.
返回值
#LOS_ERRNO_TSK_ID_INVALIDInvalid Task ID, param taskID is NULL.
#LOS_ERRNO_TSK_PTR_NULLParam initParam is NULL.
#LOS_ERRNO_TSK_NAME_EMPTYThe task name is NULL.
#LOS_ERRNO_TSK_ENTRY_NULLThe task entrance is NULL.
#LOS_ERRNO_TSK_PRIOR_ERRORIncorrect task priority.
#LOS_ERRNO_TSK_STKSZ_TOO_LARGEThe task stack size is too large.
#LOS_ERRNO_TSK_STKSZ_TOO_SMALLThe task stack size is too small.
#LOS_ERRNO_TSK_TCB_UNAVAILABLENo free task control block is available.
#LOS_ERRNO_TSK_NO_MEMORYInsufficient memory for task creation.
#LOS_OKThe task is successfully created.
Dependency:
  • los_task.h: the header file that contains the API declaration.
  • los_config.h: the header file that contains system configuration items.
参见
LOS_TaskDelete

Create a task.

在文件 los_task.c718 行定义.

719{
720 UINT32 ret;
721 UINT32 intSave;
722
723 if (initParam == NULL) {
724 return LOS_ERRNO_TSK_PTR_NULL;
725 }
726
727 if (OS_INT_ACTIVE) {
728 return LOS_ERRNO_TSK_YIELD_IN_INT;
729 }
730
731 if (OsProcessIsUserMode(OsCurrProcessGet())) { //当前进程为用户进程
732 initParam->processID = OsGetKernelInitProcessID();//@note_thinking 为什么进程ID变成了内核态根进程
733 } else {
734 initParam->processID = OsCurrProcessGet()->processID;
735 }
736
737 ret = LOS_TaskCreateOnly(taskID, initParam);
738 if (ret != LOS_OK) {
739 return ret;
740 }
741
742 LosTaskCB *taskCB = OS_TCB_FROM_TID(*taskID);
743
744 SCHEDULER_LOCK(intSave);
745 taskCB->ops->enqueue(OsSchedRunqueue(), taskCB);
746 SCHEDULER_UNLOCK(intSave);
747
748 /* in case created task not running on this core,
749 schedule or not depends on other schedulers status. */
750 LOS_MpSchedule(OS_MP_CPU_ALL);
751 if (OS_SCHEDULER_ACTIVE) {
752 LOS_Schedule();
753 }
754
755 return LOS_OK;
756}
LITE_OS_SEC_TEXT_INIT UINT32 LOS_TaskCreateOnly(UINT32 *taskID, TSK_INIT_PARAM_S *initParam)
LOS_TaskCreateOnly 创建任务,并使该任务进入suspend状态,不对该任务进行调度。如果需要调度,可以调用LOS_TaskResume使该任务进入ready状态
Definition: los_task.c:663
LITE_OS_SEC_TEXT UINT32 OsGetKernelInitProcessID(VOID)
获取内核态根进程
Definition: los_process.c:2249
STATIC INLINE BOOL OsProcessIsUserMode(const LosProcessCB *processCB)
STATIC INLINE LosProcessCB * OsCurrProcessGet(VOID)
UINT32 processID
UINT32 processID
进程ID
Definition: los_task.h:516
函数调用图:
这是这个函数的调用关系图:

◆ LOS_TaskCreateOnly()

UINT32 LOS_TaskCreateOnly ( UINT32 taskID,
TSK_INIT_PARAM_S initParam 
)

Create a task and suspend.

Description:
This API is used to create a task and suspend it. This task will not be added to the queue of ready tasks before resume it.
注意
  • During task creation, the task control block and task stack of the task that is previously automatically deleted are deallocated.
  • The task name is a pointer and is not allocated memory.
  • If the size of the task stack of the task to be created is 0, configure #LOSCFG_BASE_CORE_TSK_DEFAULT_STACK_SIZE to specify the default task stack size. The stack size should be a reasonable value, if the size is too large, may cause memory exhaustion.
  • The task stack size must be aligned on the boundary of 8 bytes. The size is determined by whether it is big enough to avoid task stack overflow.
  • Less parameter value indicates higher task priority.
  • The task name cannot be null.
  • The pointer to the task executing function cannot be null.
  • The two parameters of this interface is pointer, it should be a correct value, otherwise, the system may be abnormal.
参数
taskID[OUT] Type UINT32 * Task ID.
initParam[IN] Type TSK_INIT_PARAM_S * Parameter for task creation.
返回值
#LOS_ERRNO_TSK_ID_INVALIDInvalid Task ID, param taskID is NULL.
#LOS_ERRNO_TSK_PTR_NULLParam initParam is NULL.
#LOS_ERRNO_TSK_NAME_EMPTYThe task name is NULL.
#LOS_ERRNO_TSK_ENTRY_NULLThe task entrance is NULL.
#LOS_ERRNO_TSK_PRIOR_ERRORIncorrect task priority.
#LOS_ERRNO_TSK_STKSZ_TOO_LARGEThe task stack size is too large.
#LOS_ERRNO_TSK_STKSZ_TOO_SMALLThe task stack size is too small.
#LOS_ERRNO_TSK_TCB_UNAVAILABLENo free task control block is available.
#LOS_ERRNO_TSK_NO_MEMORYInsufficient memory for task creation.
#LOS_OKThe task is successfully created.
Dependency:
  • los_task.h: the header file that contains the API declaration.
  • los_config.h: the header file that contains system configuration items.
参见
LOS_TaskDelete

Create a task and suspend.

参数
initParam
taskID
返回
参见

在文件 los_task.c663 行定义.

664{
665 UINT32 intSave, errRet;
666 VOID *topStack = NULL;
667 VOID *pool = NULL;
668
669 errRet = OsTaskCreateParamCheck(taskID, initParam, &pool);//参数检查,获取内存池 *pool = (VOID *)m_aucSysMem1;
670 if (errRet != LOS_OK) {
671 return errRet;
672 }
673
674 LosTaskCB *taskCB = OsGetFreeTaskCB();//从g_losFreeTask中获取,还记得吗任务池中最多默认128个
675 if (taskCB == NULL) {
676 errRet = LOS_ERRNO_TSK_TCB_UNAVAILABLE;
677 goto LOS_ERREND;
678 }
679
680 errRet = OsTaskSyncCreate(taskCB);//SMP cpu多核间负载均衡相关
681 if (errRet != LOS_OK) {
682 goto LOS_ERREND_REWIND_TCB;
683 }
684 //OsTaskStackAlloc 只在LOS_TaskCreateOnly中被调用,此处是分配任务在内核态栈空间
685 OsTaskStackAlloc(&topStack, initParam->uwStackSize, pool);//为任务分配内核栈空间,注意此内存来自系统内核空间
686 if (topStack == NULL) {
687 errRet = LOS_ERRNO_TSK_NO_MEMORY;
688 goto LOS_ERREND_REWIND_SYNC;
689 }
690
691 VOID *stackPtr = OsTaskStackInit(taskCB->taskID, initParam->uwStackSize, topStack, TRUE);//初始化内核态任务栈,返回栈SP位置
692 errRet = OsTaskCBInit(taskCB, initParam, stackPtr, topStack);//初始化TCB,包括绑定进程等操作
693 if (errRet != LOS_OK) {
694 goto LOS_ERREND_TCB_INIT;
695 }
696 if (OsConsoleIDSetHook != NULL) {//每个任务都可以有属于自己的控制台
697 OsConsoleIDSetHook(taskCB->taskID, OsCurrTaskGet()->taskID);//设置控制台ID
698 }
699
700 *taskID = taskCB->taskID;
701 OsHookCall(LOS_HOOK_TYPE_TASK_CREATE, taskCB);
702 return LOS_OK;
703
704LOS_ERREND_TCB_INIT:
705 (VOID)LOS_MemFree(pool, topStack);
706LOS_ERREND_REWIND_SYNC:
707#ifdef LOSCFG_KERNEL_SMP_TASK_SYNC
709#endif
710LOS_ERREND_REWIND_TCB:
711 SCHEDULER_LOCK(intSave);
712 OsInsertTCBToFreeList(taskCB);//归还freetask
713 SCHEDULER_UNLOCK(intSave);
714LOS_ERREND:
715 return errRet;
716}
UINT32 LOS_MemFree(VOID *pool, VOID *ptr)
释放从指定动态内存中申请的内存
Definition: los_memory.c:1369
VOID * OsTaskStackInit(UINT32 taskID, UINT32 stackSize, VOID *topStack, BOOL initFlag)
内核态任务运行栈初始化
Definition: los_hw.c:73
STATIC VOID OsConsoleIDSetHook(UINT32 param1, UINT32 param2) __attribute__((weakref("OsSetConsoleID")))
STATIC INLINE VOID OsInsertTCBToFreeList(LosTaskCB *taskCB)
Definition: los_task.c:407
LITE_OS_SEC_TEXT_INIT STATIC UINT32 OsTaskCreateParamCheck(const UINT32 *taskID, TSK_INIT_PARAM_S *initParam, VOID **pool)
任务创建参数检查
Definition: los_task.c:515
STATIC INLINE VOID OsTaskSyncDestroy(UINT32 syncSignal)
销毁指定任务同步信号量
Definition: los_task.c:354
STATIC INLINE UINT32 OsTaskSyncCreate(LosTaskCB *taskCB)
创建指定任务同步信号量
Definition: los_task.c:341
STATIC UINT32 OsTaskCBInit(LosTaskCB *taskCB, const TSK_INIT_PARAM_S *initParam, const VOID *stackPtr, const VOID *topStack)
任务初始化
Definition: los_task.c:594
LITE_OS_SEC_TEXT LosTaskCB * OsGetFreeTaskCB(VOID)
获取一个空闲TCB
Definition: los_task.c:636
LITE_OS_SEC_TEXT_INIT STATIC VOID OsTaskStackAlloc(VOID **topStack, UINT32 stackSize, VOID *pool)
任务栈(内核态)内存分配,由内核态进程空间提供,即 KProcess 的进程空间
Definition: los_task.c:560
UINT32 syncSignal
UINT32 uwStackSize
Definition: los_task.h:508
函数调用图:
这是这个函数的调用关系图:

◆ LOS_TaskDelay()

UINT32 LOS_TaskDelay ( UINT32  tick)

Delay a task.

Description:
This API is used to delay the execution of the current task. The task is able to be scheduled after it is delayed for a specified number of Ticks.
注意
  • The task fails to be delayed if it is being delayed during interrupt processing or it is locked.
  • If 0 is passed in and the task scheduling is not locked, execute the next task in the queue of tasks with the same priority of the current task. If no ready task with the priority of the current task is available, the task scheduling will not occur, and the current task continues to be executed.
  • Using the interface before system initialized is not allowed.
  • DO NOT call this API in software timer callback.
参数
tick[IN] Type UINT32 Number of Ticks for which the task is delayed.
返回值
#LOS_ERRNO_TSK_DELAY_IN_INTThe task delay occurs during an interrupt.
#LOS_ERRNO_TSK_DELAY_IN_LOCKThe task delay occurs when the task scheduling is locked.
#LOS_ERRNO_TSK_ID_INVALIDInvalid Task ID
#LOS_ERRNO_TSK_YIELD_NOT_ENOUGH_TASKNo tasks with the same priority is available for scheduling.
#LOS_OKThe task is successfully delayed.
Dependency:
  • los_task.h: the header file that contains the API declaration.
参见

Delay a task.

在文件 los_task.c1020 行定义.

1021{
1022 UINT32 intSave;
1023
1024 if (OS_INT_ACTIVE) {
1025 PRINT_ERR("In interrupt not allow delay task!\n");
1026 return LOS_ERRNO_TSK_DELAY_IN_INT;
1027 }
1028
1029 LosTaskCB *runTask = OsCurrTaskGet();
1030 if (runTask->taskStatus & OS_TASK_FLAG_SYSTEM_TASK) {
1031 OsBackTrace();
1032 return LOS_ERRNO_TSK_OPERATE_SYSTEM_TASK;
1033 }
1034
1035 if (!OsPreemptable()) {
1036 return LOS_ERRNO_TSK_DELAY_IN_LOCK;
1037 }
1038 OsHookCall(LOS_HOOK_TYPE_TASK_DELAY, tick);
1039 if (tick == 0) {
1040 return LOS_TaskYield();
1041 }
1042
1043 SCHEDULER_LOCK(intSave);
1044 UINT32 ret = runTask->ops->delay(runTask, OS_SCHED_TICK_TO_CYCLE(tick));
1045 OsHookCall(LOS_HOOK_TYPE_MOVEDTASKTODELAYEDLIST, runTask);
1046 SCHEDULER_UNLOCK(intSave);
1047 return ret;
1048}
VOID OsBackTrace(VOID)
Kernel backtrace function.
Definition: los_exc.c:1025
LITE_OS_SEC_TEXT_MINOR UINT32 LOS_TaskYield(VOID)
Change the scheduling sequence of tasks with the same priority.
Definition: los_task.c:1115
UINT32(* delay)(LosTaskCB *taskCB, UINT64 waitTime)
延时执行
函数调用图:
这是这个函数的调用关系图:

◆ LOS_TaskDelete()

UINT32 LOS_TaskDelete ( UINT32  taskID)

Delete a task.

Description:
This API is used to delete a specified task and release the resources for its task stack and task control block.
注意
  • The idle task and swtmr task cannot be deleted.
  • If delete current task maybe cause unexpected error.
  • If a task get a mutex is deleted or automatically deleted before release this mutex, other tasks pended this mutex maybe never be shchduled.
参数
taskID[IN] Type UINT32 Task ID. The task id value is obtained from task creation.
返回值
#LOS_ERRNO_TSK_OPERATE_IDLECheck the task ID and do not operate on the idle task.
#LOS_ERRNO_TSK_SUSPEND_SWTMR_NOT_ALLOWEDCheck the task ID and do not operate on the swtmr task.
#LOS_ERRNO_TSK_ID_INVALIDInvalid Task ID
#LOS_ERRNO_TSK_NOT_CREATEDThe task is not created.
#LOS_ERRNO_TSK_DELETE_LOCKEDThe task being deleted is current task and task scheduling is locked.
#LOS_OKThe task is successfully deleted.
Dependency:
  • los_task.h: the header file that contains the API declaration.
参见
LOS_TaskCreate | LOS_TaskCreateOnly

在文件 los_task.c968 行定义.

969{
970 UINT32 intSave;
971 UINT32 ret = LOS_OK;
972
973 if (OS_TID_CHECK_INVALID(taskID)) {
974 return LOS_ERRNO_TSK_ID_INVALID;
975 }
976
977 if (OS_INT_ACTIVE) {
978 return LOS_ERRNO_TSK_YIELD_IN_INT;
979 }
980
981 LosTaskCB *taskCB = OS_TCB_FROM_TID(taskID);
982 if (taskCB == OsCurrTaskGet()) {
983 if (!OsPreemptable()) {
984 return LOS_ERRNO_TSK_DELETE_LOCKED;
985 }
986 OsRunningTaskToExit(taskCB, OS_PRO_EXIT_OK);
987 return LOS_NOK;
988 }
989
990 SCHEDULER_LOCK(intSave);
991 if (taskCB->taskStatus & (OS_TASK_STATUS_UNUSED | OS_TASK_FLAG_SYSTEM_TASK | OS_TASK_FLAG_NO_DELETE)) {
992 if (taskCB->taskStatus & OS_TASK_STATUS_UNUSED) {
993 ret = LOS_ERRNO_TSK_NOT_CREATED;
994 } else {
995 ret = LOS_ERRNO_TSK_OPERATE_SYSTEM_TASK;
996 }
997 OS_GOTO_ERREND();
998 }
999
1000#ifdef LOSCFG_KERNEL_SMP
1001 if (taskCB->taskStatus & OS_TASK_STATUS_RUNNING) {
1002 taskCB->signal = SIGNAL_KILL;
1003 LOS_MpSchedule(taskCB->currCpu);
1004 ret = OsTaskSyncWait(taskCB);
1005 OS_GOTO_ERREND();
1006 }
1007#endif
1008
1009 OsInactiveTaskDelete(taskCB);
1010 OsEventWriteUnsafe(&g_resourceEvent, OS_RESOURCE_EVENT_FREE, FALSE, NULL);
1011
1012LOS_ERREND:
1013 SCHEDULER_UNLOCK(intSave);
1014 if (ret == LOS_OK) {
1015 LOS_Schedule();
1016 }
1017 return ret;
1018}
VOID OsEventWriteUnsafe(PEVENT_CB_S eventCB, UINT32 events, BOOL once, UINT8 *exitFlag)
以不安全的方式写事件
Definition: los_event.c:247
STATIC INLINE UINT32 OsTaskSyncWait(const LosTaskCB *taskCB)
OsTaskSyncWait 任务同步等待,通过信号量保持同步
Definition: los_task.c:372
LITE_OS_SEC_TEXT VOID OsInactiveTaskDelete(LosTaskCB *taskCB)
Definition: los_task.c:947
LITE_OS_SEC_TEXT VOID OsRunningTaskToExit(LosTaskCB *runTask, UINT32 status)
Definition: los_task.c:908
LITE_OS_SEC_BSS EVENT_CB_S g_resourceEvent
Definition: los_task.c:152
UINT32 signal
UINT16 currCpu
函数调用图:
这是这个函数的调用关系图:

◆ LOS_TaskDetach()

UINT32 LOS_TaskDetach ( UINT32  taskID)

Change the joinable attribute of the task to detach.

Description:
This API is used to change the joinable attribute of the task to detach.
注意
None.
参数
taskID[IN] task ID.
返回值
LOS_OKsuccessful
LOS_EINVALInvalid parameter or invalid operation
LOS_EINTRDisallow calls in interrupt handlers
LOS_EPERMWaiting tasks and calling tasks do not belong to the same process
LOS_ESRCHCannot modify the Joinable attribute of a task that is waiting for completion.
Dependency:
  • los_task.h: the header file that contains the API declaration.

在文件 los_task.c1601 行定义.

1602{
1603 UINT32 intSave;
1604 LosTaskCB *runTask = OsCurrTaskGet();
1605 UINT32 errRet;
1606
1607 if (OS_TID_CHECK_INVALID(taskID)) {
1608 return LOS_EINVAL;
1609 }
1610
1611 if (OS_INT_ACTIVE) {
1612 return LOS_EINTR;
1613 }
1614
1615 LosTaskCB *taskCB = OS_TCB_FROM_TID(taskID);
1616 SCHEDULER_LOCK(intSave);
1617 if (taskCB->taskStatus & OS_TASK_STATUS_UNUSED) {
1618 SCHEDULER_UNLOCK(intSave);
1619 return LOS_EINVAL;
1620 }
1621
1622 if (runTask->processID != taskCB->processID) {
1623 SCHEDULER_UNLOCK(intSave);
1624 return LOS_EPERM;
1625 }
1626
1627 if (taskCB->taskStatus & OS_TASK_STATUS_EXIT) {
1628 SCHEDULER_UNLOCK(intSave);
1629 return LOS_TaskJoin(taskID, NULL);
1630 }
1631
1632 errRet = OsTaskSetDetachUnsafe(taskCB);
1633 SCHEDULER_UNLOCK(intSave);
1634 return errRet;
1635}
UINT32 LOS_TaskJoin(UINT32 taskID, UINTPTR *retval)
Wait for the specified task to finish and reclaim its resources.
Definition: los_task.c:1561
LITE_OS_SEC_TEXT UINT32 OsTaskSetDetachUnsafe(LosTaskCB *taskCB)
任务设置分离模式 Deatch和JOIN是一对有你没我的状态
Definition: los_task.c:243
UINT32 processID
函数调用图:

◆ LOS_TaskInfoGet()

UINT32 LOS_TaskInfoGet ( UINT32  taskID,
TSK_INFO_S taskInfo 
)

Obtain a task information structure.

Description:
This API is used to obtain a task information structure.
注意
  • One parameter of this interface is a pointer, it should be a correct value, otherwise, the system may be abnormal.
参数
taskID[IN] Type UINT32 Task ID. The task id value is obtained from task creation.
taskInfo[OUT] Type TSK_INFO_S* Pointer to the task information structure to be obtained.
返回值
#LOS_ERRNO_TSK_PTR_NULLNull parameter.
#LOS_ERRNO_TSK_ID_INVALIDInvalid task ID.
#LOS_ERRNO_TSK_NOT_CREATEDThe task is not created.
#LOS_OKThe task information structure is successfully obtained.
Dependency:
  • los_task.h: the header file that contains the API declaration.
参见

在文件 los_task.c1161 行定义.

1162{
1163 UINT32 intSave;
1164 SchedParam param = { 0 };
1165
1166 if (taskInfo == NULL) {
1167 return LOS_ERRNO_TSK_PTR_NULL;
1168 }
1169
1170 if (OS_TID_CHECK_INVALID(taskID)) {
1171 return LOS_ERRNO_TSK_ID_INVALID;
1172 }
1173
1174 LosTaskCB *taskCB = OS_TCB_FROM_TID(taskID);
1175 SCHEDULER_LOCK(intSave);
1176 if (taskCB->taskStatus & OS_TASK_STATUS_UNUSED) {
1177 SCHEDULER_UNLOCK(intSave);
1178 return LOS_ERRNO_TSK_NOT_CREATED;
1179 }
1180
1181 if (!(taskCB->taskStatus & OS_TASK_STATUS_RUNNING) || OS_INT_ACTIVE) {
1182 taskInfo->uwSP = (UINTPTR)taskCB->stackPointer;
1183 } else {
1184 taskInfo->uwSP = ArchSPGet();
1185 }
1186
1187 taskCB->ops->schedParamGet(taskCB, &param);
1188 taskInfo->usTaskStatus = taskCB->taskStatus;
1189 taskInfo->usTaskPrio = param.priority;
1190 taskInfo->uwStackSize = taskCB->stackSize; //内核态栈大小
1191 taskInfo->uwTopOfStack = taskCB->topOfStack;//内核态栈顶位置
1192 taskInfo->uwEventMask = taskCB->eventMask;
1193 taskInfo->taskEvent = taskCB->taskEvent;
1194 taskInfo->pTaskMux = taskCB->taskMux;
1195 taskInfo->uwTaskID = taskID;
1196
1197 if (strncpy_s(taskInfo->acName, LOS_TASK_NAMELEN, taskCB->taskName, LOS_TASK_NAMELEN - 1) != EOK) {
1198 PRINT_ERR("Task name copy failed!\n");
1199 }
1200 taskInfo->acName[LOS_TASK_NAMELEN - 1] = '\0';
1201
1202 taskInfo->uwBottomOfStack = TRUNCATE(((UINTPTR)taskCB->topOfStack + taskCB->stackSize),//这里可以看出栈底地址是高于栈顶
1203 OS_TASK_STACK_ADDR_ALIGN);
1204 taskInfo->uwCurrUsed = (UINT32)(taskInfo->uwBottomOfStack - taskInfo->uwSP);//当前任务栈已使用了多少
1205
1206 taskInfo->bOvf = OsStackWaterLineGet((const UINTPTR *)taskInfo->uwBottomOfStack,//获取栈的使用情况
1207 (const UINTPTR *)taskInfo->uwTopOfStack, &taskInfo->uwPeakUsed);
1208 SCHEDULER_UNLOCK(intSave);
1209 return LOS_OK;
1210}
UINT32 OsStackWaterLineGet(const UINTPTR *stackBottom, const UINTPTR *stackTop, UINT32 *peakUsed)
Get stack waterline.
Definition: los_stackinfo.c:70
STATIC INLINE UINT32 ArchSPGet(VOID)
Definition: los_hw_cpu.h:284
unsigned long UINTPTR
Definition: los_typedef.h:68
UINT32 stackSize
CHAR taskName[OS_TCB_NAME_LEN]
UINTPTR topOfStack
UINT32 eventMask
VOID * stackPointer
VOID * taskEvent
VOID * taskMux
UINT16 usTaskPrio
Definition: los_task.h:536
UINT16 usTaskStatus
Definition: los_task.h:535
UINT32 uwStackSize
Definition: los_task.h:541
UINTPTR uwBottomOfStack
Definition: los_task.h:543
VOID * pTaskMux
Definition: los_task.h:538
UINT32 uwPeakUsed
Definition: los_task.h:546
UINT32 uwTaskID
Definition: los_task.h:534
UINTPTR uwSP
Definition: los_task.h:544
UINT32 uwCurrUsed
Definition: los_task.h:545
BOOL bOvf
Definition: los_task.h:547
CHAR acName[LOS_TASK_NAMELEN]
Definition: los_task.h:533
UINTPTR uwTopOfStack
Definition: los_task.h:542
UINT32 uwEventMask
Definition: los_task.h:540
VOID * taskEvent
Definition: los_task.h:539
函数调用图:

◆ LOS_TaskJoin()

UINT32 LOS_TaskJoin ( UINT32  taskID,
UINTPTR retval 
)

Wait for the specified task to finish and reclaim its resources.

Description:
This API is used to wait for the specified task to finish and reclaim its resources.
注意
None.
参数
taskID[IN] task ID.
retval[OUT] wait for the return value of the task.
返回值
LOS_OKsuccessful
LOS_EINVALInvalid parameter or invalid operation
LOS_EINTRDisallow calls in interrupt handlers
LOS_EPERMWaiting tasks and calling tasks do not belong to the same process
LOS_EDEADLKThe waiting task is the same as the calling task
Dependency:
  • los_task.h: the header file that contains the API declaration.

在文件 los_task.c1561 行定义.

1562{
1563 UINT32 intSave;
1564 LosTaskCB *runTask = OsCurrTaskGet();
1565 UINT32 errRet;
1566
1567 errRet = OsTaskJoinCheck(taskID);
1568 if (errRet != LOS_OK) {
1569 return errRet;
1570 }
1571
1572 LosTaskCB *taskCB = OS_TCB_FROM_TID(taskID);
1573 SCHEDULER_LOCK(intSave);
1574 if (taskCB->taskStatus & OS_TASK_STATUS_UNUSED) {
1575 SCHEDULER_UNLOCK(intSave);
1576 return LOS_EINVAL;
1577 }
1578
1579 if (runTask->processID != taskCB->processID) {
1580 SCHEDULER_UNLOCK(intSave);
1581 return LOS_EPERM;
1582 }
1583
1584 errRet = OsTaskJoinPendUnsafe(taskCB);
1585 SCHEDULER_UNLOCK(intSave);
1586
1587 if (errRet == LOS_OK) {
1588 LOS_Schedule();
1589
1590 if (retval != NULL) {
1591 *retval = (UINTPTR)taskCB->joinRetval;
1592 }
1593
1594 (VOID)LOS_TaskDelete(taskID);
1595 return LOS_OK;
1596 }
1597
1598 return errRet;
1599}
LITE_OS_SEC_TEXT_INIT UINT32 LOS_TaskDelete(UINT32 taskID)
Delete a task.
Definition: los_task.c:968
LITE_OS_SEC_TEXT UINT32 OsTaskJoinPendUnsafe(LosTaskCB *taskCB)
挂起任务,任务进入等待链表,Join代表是支持通过一个任务去唤醒其他的任务
Definition: los_task.c:224
STATIC UINT32 OsTaskJoinCheck(UINT32 taskID)
Definition: los_task.c:1535
VOID * joinRetval
函数调用图:
这是这个函数的调用关系图:

◆ LOS_TaskLock()

VOID LOS_TaskLock ( VOID  )

Lock the task scheduling.

Description:
This API is used to lock the task scheduling. Task switching will not occur if the task scheduling is locked.
注意
  • If the task scheduling is locked, but interrupts are not disabled, tasks are still able to be interrupted.
  • One is added to the number of task scheduling locks if this API is called. The number of locks is decreased by one if the task scheduling is unlocked. Therefore, this API should be used together with LOS_TaskUnlock.
参数
None.
返回值
None.
Dependency:
  • los_task.h: the header file that contains the API declaration.
参见
LOS_TaskUnlock

在文件 los_task.c1139 行定义.

1140{
1141 UINT32 intSave;
1142
1143 intSave = LOS_IntLock();
1144 OsSchedLock();
1145 LOS_IntRestore(intSave);
1146}
STATIC INLINE VOID LOS_IntRestore(UINT32 intSave)
Restore interrupts. | 恢复到使用LOS_IntLock关闭所有中断之前的状态
Definition: los_hwi.h:337
STATIC INLINE UINT32 LOS_IntLock(VOID)
Disable all interrupts. | 关闭当前处理器所有中断响应
Definition: los_hwi.h:286
STATIC INLINE VOID OsSchedLock(VOID)
函数调用图:
这是这个函数的调用关系图:

◆ LOS_TaskPriGet()

UINT16 LOS_TaskPriGet ( UINT32  taskID)

Obtain a task priority.

Description:
This API is used to obtain the priority of a specified task.
注意
None.
参数
taskID[IN] Type UINT32 Task ID. The task id value is obtained from task creation.
返回值
#OS_INVALIDThe task priority fails to be obtained.
UINT16The task priority.
Dependency:
  • los_task.h: the header file that contains the API declaration.
参见
LOS_TaskPriSet

Obtain a task priority.

在文件 los_task.c1050 行定义.

1051{
1052 UINT32 intSave;
1053 SchedParam param = { 0 };
1054
1055 if (OS_TID_CHECK_INVALID(taskID)) {
1056 return (UINT16)OS_INVALID;
1057 }
1058
1059 LosTaskCB *taskCB = OS_TCB_FROM_TID(taskID);
1060 SCHEDULER_LOCK(intSave);
1061 if (taskCB->taskStatus & OS_TASK_STATUS_UNUSED) {//就这么一句话也要来个自旋锁,内核代码自旋锁真是无处不在啊
1062 SCHEDULER_UNLOCK(intSave);
1063 return (UINT16)OS_INVALID;
1064 }
1065
1066 taskCB->ops->schedParamGet(taskCB, &param);
1067 SCHEDULER_UNLOCK(intSave);
1068 return param.priority;
1069}
这是这个函数的调用关系图:

◆ LOS_TaskPriSet()

UINT32 LOS_TaskPriSet ( UINT32  taskID,
UINT16  taskPrio 
)

Set a task priority.

Description:
This API is used to set the priority of a specified task.
注意
  • If the set priority is higher than the priority of the current running task, task scheduling probably occurs.
  • Changing the priority of the current running task also probably causes task scheduling.
  • Using the interface to change the priority of software timer task and idle task is not allowed.
  • Using the interface in the interrupt is not allowed.
参数
taskID[IN] Type UINT32 Task ID. The task id value is obtained from task creation.
taskPrio[IN] Type UINT16 Task priority.
返回值
#LOS_ERRNO_TSK_PRIOR_ERRORIncorrect task priority.Re-configure the task priority
#LOS_ERRNO_TSK_OPERATE_IDLECheck the task ID and do not operate on the idle task.
#LOS_ERRNO_TSK_ID_INVALIDInvalid Task ID
#LOS_ERRNO_TSK_NOT_CREATEDThe task is not created.
#LOS_OKThe task priority is successfully set to a specified priority.
Dependency:
  • los_task.h: the header file that contains the API declaration.
参见
LOS_TaskPriSet

Set a task priority.

在文件 los_task.c1071 行定义.

1072{
1073 UINT32 intSave;
1074 SchedParam param = { 0 };
1075
1076 if (taskPrio > OS_TASK_PRIORITY_LOWEST) {
1077 return LOS_ERRNO_TSK_PRIOR_ERROR;
1078 }
1079
1080 if (OS_TID_CHECK_INVALID(taskID)) {
1081 return LOS_ERRNO_TSK_ID_INVALID;
1082 }
1083
1084 LosTaskCB *taskCB = OS_TCB_FROM_TID(taskID);
1085 if (taskCB->taskStatus & OS_TASK_FLAG_SYSTEM_TASK) {
1086 return LOS_ERRNO_TSK_OPERATE_SYSTEM_TASK;
1087 }
1088
1089 SCHEDULER_LOCK(intSave);
1090 if (taskCB->taskStatus & OS_TASK_STATUS_UNUSED) {
1091 SCHEDULER_UNLOCK(intSave);
1092 return LOS_ERRNO_TSK_NOT_CREATED;
1093 }
1094
1095 taskCB->ops->schedParamGet(taskCB, &param);
1096
1097 param.priority = taskPrio;
1098
1099 BOOL needSched = taskCB->ops->schedParamModify(taskCB, &param);
1100 SCHEDULER_UNLOCK(intSave);
1101
1102 LOS_MpSchedule(OS_MP_CPU_ALL);
1103 if (needSched && OS_SCHEDULER_ACTIVE) {
1104 LOS_Schedule();
1105 }
1106 return LOS_OK;
1107}
函数调用图:
这是这个函数的调用关系图:

◆ LOS_TaskResume()

UINT32 LOS_TaskResume ( UINT32  taskID)

Resume a task.

Description:
This API is used to resume a suspended task.
注意
  • If the task is delayed or blocked, resume the task without adding it to the queue of ready tasks.
  • If the priority of the task resumed after system initialized is higher than the current task and task scheduling is not locked, it is scheduled for running.
参数
taskID[IN] Type UINT32 Task ID. The task id value is obtained from task creation.
返回值
#LOS_ERRNO_TSK_ID_INVALIDInvalid Task ID
#LOS_ERRNO_TSK_NOT_CREATEDThe task is not created.
#LOS_ERRNO_TSK_NOT_SUSPENDEDThe task is not suspended.
#LOS_OKThe task is successfully resumed.
Dependency:
  • los_task.h: the header file that contains the API declaration.
参见
LOS_TaskSuspend

Resume a task.

在文件 los_task.c758 行定义.

759{
760 UINT32 intSave;
761 UINT32 errRet;
762 BOOL needSched = FALSE;
763
764 if (OS_TID_CHECK_INVALID(taskID)) {
765 return LOS_ERRNO_TSK_ID_INVALID;
766 }
767
768 LosTaskCB *taskCB = OS_TCB_FROM_TID(taskID);
769 SCHEDULER_LOCK(intSave);
770
771 /* clear pending signal */
772 taskCB->signal &= ~SIGNAL_SUSPEND;//清楚挂起信号
773
774 if (taskCB->taskStatus & OS_TASK_STATUS_UNUSED) {
775 errRet = LOS_ERRNO_TSK_NOT_CREATED;
776 OS_GOTO_ERREND();
777 } else if (!(taskCB->taskStatus & OS_TASK_STATUS_SUSPENDED)) {
778 errRet = LOS_ERRNO_TSK_NOT_SUSPENDED;
779 OS_GOTO_ERREND();
780 }
781
782 errRet = taskCB->ops->resume(taskCB, &needSched);
783 SCHEDULER_UNLOCK(intSave);
784
785 LOS_MpSchedule(OS_MP_CPU_ALL);
786 if (OS_SCHEDULER_ACTIVE && needSched) {
787 LOS_Schedule();
788 }
789
790 return errRet;
791
792LOS_ERREND:
793 SCHEDULER_UNLOCK(intSave);
794 return errRet;
795}
UINT32(* resume)(LosTaskCB *taskCB, BOOL *needSched)
恢复任务
函数调用图:
这是这个函数的调用关系图:

◆ LOS_TaskSuspend()

UINT32 LOS_TaskSuspend ( UINT32  taskID)

Suspend a task.

Description:
This API is used to suspend a specified task, and the task will be removed from the queue of ready tasks.
注意
  • The task that is running and locked cannot be suspended.
  • The idle task and swtmr task cannot be suspended.
参数
taskID[IN] Type UINT32 Task ID. The task id value is obtained from task creation.
返回值
#LOS_ERRNO_TSK_OPERATE_IDLECheck the task ID and do not operate on the idle task.
#LOS_ERRNO_TSK_SUSPEND_SWTMR_NOT_ALLOWEDCheck the task ID and do not operate on the swtmr task.
#LOS_ERRNO_TSK_ID_INVALIDInvalid Task ID
#LOS_ERRNO_TSK_NOT_CREATEDThe task is not created.
#LOS_ERRNO_TSK_ALREADY_SUSPENDEDThe task is already suspended.
#LOS_ERRNO_TSK_SUSPEND_LOCKEDThe task being suspended is current task and task scheduling is locked.
#LOS_OKThe task is successfully suspended.
Dependency:
  • los_task.h: the header file that contains the API declaration.
参见
LOS_TaskResume

Suspend a task.

在文件 los_task.c855 行定义.

856{
857 UINT32 intSave;
858 UINT32 errRet;
859
860 if (OS_TID_CHECK_INVALID(taskID)) {
861 return LOS_ERRNO_TSK_ID_INVALID;
862 }
863
864 LosTaskCB *taskCB = OS_TCB_FROM_TID(taskID);
865 if (taskCB->taskStatus & OS_TASK_FLAG_SYSTEM_TASK) {
866 return LOS_ERRNO_TSK_OPERATE_SYSTEM_TASK;
867 }
868
869 SCHEDULER_LOCK(intSave);
870 errRet = OsTaskSuspend(taskCB);
871 SCHEDULER_UNLOCK(intSave);
872 return errRet;
873}
LITE_OS_SEC_TEXT STATIC UINT32 OsTaskSuspend(LosTaskCB *taskCB)
任务暂停,参数可以不是当前任务,也就是说 A任务可以让B任务处于阻塞状态,挂起指定的任务,然后切换任务
Definition: los_task.c:835
函数调用图:
这是这个函数的调用关系图:

◆ LOS_TaskUnlock()

VOID LOS_TaskUnlock ( VOID  )

Unlock the task scheduling.

Description:
This API is used to unlock the task scheduling. Calling this API will decrease the number of task locks by one. If a task is locked more than once, the task scheduling will be unlocked only when the number of locks becomes zero.
注意
  • The number of locks is decreased by one if this API is called. One is added to the number of task scheduling locks if the task scheduling is locked. Therefore, this API should be used together with LOS_TaskLock.
参数
None.
返回值
None.
Dependency:
  • los_task.h: the header file that contains the API declaration.
参见
LOS_TaskLock

在文件 los_task.c1148 行定义.

1149{
1150 UINT32 intSave;
1151
1152 intSave = LOS_IntLock();
1153 BOOL needSched = OsSchedUnlockResch();
1154 LOS_IntRestore(intSave);
1155
1156 if (needSched) {
1157 LOS_Schedule();
1158 }
1159}
STATIC INLINE BOOL OsSchedUnlockResch(VOID)
函数调用图:
这是这个函数的调用关系图:

◆ LOS_TaskYield()

UINT32 LOS_TaskYield ( VOID  )

Change the scheduling sequence of tasks with the same priority.

Description:
This API is used to move current task in a queue of tasks with the same priority to the tail of the queue of ready tasks.
注意
  • At least two ready tasks need to be included in the queue of ready tasks with the same priority. If the less than two ready tasks are included in the queue, an error is reported.
参数
None.
返回值
#LOS_ERRNO_TSK_ID_INVALIDInvalid Task ID
#LOS_ERRNO_TSK_YIELD_NOT_ENOUGH_TASKNo tasks with the same priority is available for scheduling.
#LOS_OKThe scheduling sequence of tasks with same priority is successfully changed.
Dependency:
  • los_task.h: the header file that contains the API declaration.
参见

在文件 los_task.c1115 行定义.

1116{
1117 UINT32 intSave;
1118
1119 if (OS_INT_ACTIVE) {
1120 return LOS_ERRNO_TSK_YIELD_IN_INT;
1121 }
1122
1123 if (!OsPreemptable()) {
1124 return LOS_ERRNO_TSK_YIELD_IN_LOCK;
1125 }
1126
1127 LosTaskCB *runTask = OsCurrTaskGet();
1128 if (OS_TID_CHECK_INVALID(runTask->taskID)) {
1129 return LOS_ERRNO_TSK_ID_INVALID;
1130 }
1131
1132 SCHEDULER_LOCK(intSave);
1133 /* reset timeslice of yielded task */
1134 runTask->ops->yield(runTask);
1135 SCHEDULER_UNLOCK(intSave);
1136 return LOS_OK;
1137}
VOID(* yield)(LosTaskCB *taskCB)
让出控制权
函数调用图:
这是这个函数的调用关系图:

◆ OsStackWaterLineGet()

UINT32 OsStackWaterLineGet ( const UINTPTR stackBottom,
const UINTPTR stackTop,
UINT32 peakUsed 
)

Get stack waterline.

Description:
This API is used to get stack waterline size and check stack whether overflow.
注意
None
参数
stackBottom[IN] Type #const UINTPTR * pointer to stack bottom.
stackTop[IN] Type #const UINTPTR * pointer to stack top.
peakUsed[OUT] Type UINT32 * stack waterline.
返回值
#LOS_NOKstack overflow
#LOS_OKstack is normal, not overflow
Dependency:
参见

Get stack waterline.

在文件 los_stackinfo.c70 行定义.

71{
72 UINT32 size;
73 const UINTPTR *tmp = NULL;
74 if (*stackTop == OS_STACK_MAGIC_WORD) {//栈顶值是否等于 magic 0xCCCCCCCC
75 tmp = stackTop + 1;
76 while ((tmp < stackBottom) && (*tmp == OS_STACK_INIT)) {//记录从栈顶到栈低有多少个连续的 0xCACACACA
77 tmp++;
78 }
79 size = (UINT32)((UINTPTR)stackBottom - (UINTPTR)tmp);//剩余多少非0xCACACACA的栈空间
80 *peakUsed = (size == 0) ? size : (size + sizeof(CHAR *));//得出高峰用值,还剩多少可用
81 return LOS_OK;
82 } else {
83 *peakUsed = OS_INVALID_WATERLINE;//栈溢出了
84 return LOS_NOK;
85 }
86}
macro EXC_SP_SET stackBottom
Definition: asm.h:50
char CHAR
Definition: los_typedef.h:63
这是这个函数的调用关系图:

变量说明

◆ g_taskCBArray

LosTaskCB* g_taskCBArray
extern

外部变量 任务池 默认128个

Starting address of a task.

在文件 los_task.c147 行定义.

◆ g_taskMaxNum

UINT32 g_taskMaxNum
extern

任务最大数量 默认128个

Maximum number of tasks.

在文件 los_task.c150 行定义.