更新日期: 2022/06/01 来源: https://gitee.com/weharmony/kernel_liteos_a_note
los_task_pri.h 文件参考

浏览源代码.

结构体

struct  LosTask
 
struct  ProcessSignalInfo
 
struct  OsTaskRobin
 

类型定义

typedef int(* ForEachTaskCB) (LosTaskCB *tcb, void *arg)
 回调任务函数,例如:进程被kill 9 时,通知所有任务善后处理 更多...
 

函数

STATIC INLINE LosTaskCBOsGetTaskCB (UINT32 taskID)
 通过任务ID获取任务实体,task由任务池分配,本质是个数组,彼此都挨在一块 更多...
 
STATIC INLINE BOOL OsTaskIsUnused (const LosTaskCB *taskCB)
 任务是否在使用 更多...
 
STATIC INLINE BOOL OsTaskIsKilled (const LosTaskCB *taskCB)
 
STATIC INLINE BOOL OsTaskIsUserMode (const LosTaskCB *taskCB)
 
STATIC INLINE VOID OsTaskWaitSetPendMask (UINT16 mask, UINTPTR lockID, UINT32 timeout)
 设置事件阻塞掩码,即设置任务的等待事件. 更多...
 
STATIC INLINE VOID OsTaskWakeClearPendMask (LosTaskCB *resumeTask)
 清除事件阻塞掩码,即任务不再等待任何事件. 更多...
 
UINT32 OsTaskSetDetachUnsafe (LosTaskCB *taskCB)
 任务设置分离模式 Deatch和JOIN是一对有你没我的状态 更多...
 
VOID OsTaskJoinPostUnsafe (LosTaskCB *taskCB)
 OsTaskJoinPostUnsafe 查找task 通过 OS_TCB_FROM_PENDLIST 来完成,相当于由LOS_DL_LIST找到LosTaskCB, 将那些和参数任务绑在一起的task唤醒. 更多...
 
UINT32 OsTaskJoinPendUnsafe (LosTaskCB *taskCB)
 挂起任务,任务进入等待链表,Join代表是支持通过一个任务去唤醒其他的任务 更多...
 
BOOL OsTaskCpuAffiSetUnsafe (UINT32 taskID, UINT16 newCpuAffiMask, UINT16 *oldCpuAffiMask)
 CPU亲和性(affinity)将任务绑在指定CPU上,用于多核CPU情况,(该函数仅在SMP模式下支持) 更多...
 
VOID OsTaskSchedule (LosTaskCB *, LosTaskCB *)
 
VOID OsTaskContextLoad (LosTaskCB *newTask)
 
VOID OsIdleTask (VOID)
 空闲任务,每个CPU都有自己的空闲任务 更多...
 
UINT32 OsIdleTaskCreate (VOID)
 创建一个空闲任务 更多...
 
UINT32 OsTaskInit (VOID)
 
UINT32 OsShellCmdDumpTask (INT32 argc, const CHAR **argv)
 
UINT32 OsShellCmdTskInfoGet (UINT32 taskID, VOID *seqfile, UINT16 flag)
 
LosTaskCBOsGetMainTask (VOID)
 
VOID OsSetMainTask (VOID)
 
UINT32 OsGetIdleTaskId (VOID)
 获取IdletaskId,每个CPU核都对Task进行了内部管理,做到真正的并行处理 更多...
 
VOID OsTaskEntry (UINT32 taskID)
 
VOID OsTaskProcSignal (VOID)
 
UINT32 OsCreateUserTask (UINT32 processID, TSK_INIT_PARAM_S *initParam)
 创建一个用户态任务 更多...
 
INT32 OsSetTaskName (LosTaskCB *taskCB, const CHAR *name, BOOL setPName)
 
VOID OsTaskCBRecycleToFree (VOID)
 
VOID OsRunningTaskToExit (LosTaskCB *runTask, UINT32 status)
 
INT32 OsUserTaskOperatePermissionsCheck (const LosTaskCB *taskCB)
 
INT32 OsUserProcessOperatePermissionsCheck (const LosTaskCB *taskCB, UINT32 processID)
 
INT32 OsTcbDispatch (LosTaskCB *stcb, siginfo_t *info)
 给任务(线程)发送一个信号 更多...
 
VOID OsWriteResourceEvent (UINT32 events)
 
VOID OsWriteResourceEventUnsafe (UINT32 events)
 
UINT32 OsResourceFreeTaskCreate (VOID)
 创建一个回收资源的任务 更多...
 
VOID OsTaskInsertToRecycleList (LosTaskCB *taskCB)
 
VOID OsInactiveTaskDelete (LosTaskCB *taskCB)
 

变量

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

类型定义说明

◆ ForEachTaskCB

typedef int(* ForEachTaskCB) (LosTaskCB *tcb, void *arg)

回调任务函数,例如:进程被kill 9 时,通知所有任务善后处理

在文件 los_task_pri.h222 行定义.

函数说明

◆ OsCreateUserTask()

UINT32 OsCreateUserTask ( UINT32  processID,
TSK_INIT_PARAM_S initParam 
)

创建一个用户态任务

在文件 los_task.c1434 行定义.

1435{
1436 UINT32 taskID;
1437 UINT32 ret;
1438 UINT32 intSave;
1439
1440 ret = OsCreateUserTaskParamCheck(processID, initParam);//检查参数,堆栈,入口地址必须在用户空间
1441 if (ret != LOS_OK) {
1442 return ret;
1443 }
1444 //这里可看出一个任务有两个栈,内核态栈(内核指定栈大小)和用户态栈(用户指定栈大小)
1445 initParam->uwStackSize = OS_USER_TASK_SYSCALL_STACK_SIZE;
1446 initParam->usTaskPrio = OS_TASK_PRIORITY_LOWEST;//设置最低优先级 31级
1447 initParam->policy = LOS_SCHED_RR;//调度方式为抢占式,注意鸿蒙不仅仅只支持抢占式调度方式
1448 if (processID == OS_INVALID_VALUE) {//外面没指定进程ID的处理
1449 SCHEDULER_LOCK(intSave);
1450 LosProcessCB *processCB = OsCurrProcessGet();
1451 initParam->processID = processCB->processID;//进程ID赋值
1452 initParam->consoleID = processCB->consoleID;//任务控制台ID归属
1453 SCHEDULER_UNLOCK(intSave);
1454 } else {//进程已经创建
1455 initParam->processID = processID;//进程ID赋值
1456 initParam->consoleID = 0;//默认0号控制台
1457 }
1458
1459 ret = LOS_TaskCreateOnly(&taskID, initParam);//只创建task实体,不申请调度
1460 if (ret != LOS_OK) {
1461 return OS_INVALID_VALUE;
1462 }
1463
1464 return taskID;
1465}
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
STATIC INLINE LosProcessCB * OsCurrProcessGet(VOID)
LITE_OS_SEC_TEXT_INIT STATIC UINT32 OsCreateUserTaskParamCheck(UINT32 processID, TSK_INIT_PARAM_S *param)
创建任务之前,检查用户态任务栈的参数,是否地址在用户空间
Definition: los_task.c:1406
unsigned int UINT32
Definition: los_typedef.h:57
UINT32 processID
UINT16 consoleID
UINT16 policy
Definition: los_task.h:506
UINT16 usTaskPrio
Definition: los_task.h:505
UINT32 uwStackSize
Definition: los_task.h:508
UINT32 processID
进程ID
Definition: los_task.h:516
UINT16 consoleID
Definition: los_task.h:515
函数调用图:
这是这个函数的调用关系图:

◆ OsGetIdleTaskId()

UINT32 OsGetIdleTaskId ( VOID  )

获取IdletaskId,每个CPU核都对Task进行了内部管理,做到真正的并行处理

在文件 los_task.c295 行定义.

296{
297 return OsSchedRunqueueIdleGet();
298}
STATIC INLINE UINT32 OsSchedRunqueueIdleGet(VOID)
函数调用图:
这是这个函数的调用关系图:

◆ OsGetMainTask()

LosTaskCB * OsGetMainTask ( VOID  )

◆ OsGetTaskCB()

STATIC INLINE LosTaskCB * OsGetTaskCB ( UINT32  taskID)

通过任务ID获取任务实体,task由任务池分配,本质是个数组,彼此都挨在一块

在文件 los_task_pri.h250 行定义.

251{
252 return OS_TCB_FROM_TID(taskID);
253}
这是这个函数的调用关系图:

◆ OsIdleTask()

VOID OsIdleTask ( VOID  )

空闲任务,每个CPU都有自己的空闲任务

在文件 los_task.c191 行定义.

192{
193 while (1) {//只有一个死循环
194 WFI;//WFI指令:arm core 立即进入low-power standby state,进入休眠模式,等待中断。
195 }
196}
这是这个函数的调用关系图:

◆ OsIdleTaskCreate()

UINT32 OsIdleTaskCreate ( VOID  )

创建一个空闲任务

在文件 los_task.c300 行定义.

301{
302 UINT32 ret;
303 TSK_INIT_PARAM_S taskInitParam;
304 UINT32 idleTaskID;
305
306 (VOID)memset_s((VOID *)(&taskInitParam), sizeof(TSK_INIT_PARAM_S), 0, sizeof(TSK_INIT_PARAM_S));//任务初始参数清0
307 taskInitParam.pfnTaskEntry = (TSK_ENTRY_FUNC)OsIdleTask;//入口函数
308 taskInitParam.uwStackSize = LOSCFG_BASE_CORE_TSK_IDLE_STACK_SIZE;//任务栈大小 2K
309 taskInitParam.pcName = "Idle";//任务名称 叫pcName有点怪怪的,不能换个撒
310 taskInitParam.policy = LOS_SCHED_IDLE;
311 taskInitParam.usTaskPrio = OS_TASK_PRIORITY_LOWEST;//默认最低优先级 31
312 taskInitParam.processID = OsGetIdleProcessID();//任务的进程ID绑定为空闲进程
313#ifdef LOSCFG_KERNEL_SMP
314 taskInitParam.usCpuAffiMask = CPUID_TO_AFFI_MASK(ArchCurrCpuid());//每个idle任务只在单独的cpu上运行
315#endif
316 ret = LOS_TaskCreateOnly(&idleTaskID, &taskInitParam);
317 if (ret != LOS_OK) {
318 return ret;
319 }
320 LosTaskCB *idleTask = OS_TCB_FROM_TID(idleTaskID);
321 idleTask->taskStatus |= OS_TASK_FLAG_SYSTEM_TASK; //标记为系统任务,idle任务是给CPU休息用的,当然是个系统任务
322 OsSchedRunqueueIdleInit(idleTaskID);
323
324 return LOS_TaskResume(idleTaskID);
325}
LITE_OS_SEC_TEXT_INIT UINT32 LOS_TaskResume(UINT32 taskID)
恢复挂起的任务,使该任务进入ready状态
Definition: los_task.c:758
struct tagTskInitParam TSK_INIT_PARAM_S
VOID *(* TSK_ENTRY_FUNC)(UINTPTR param1, UINTPTR param2, UINTPTR param3, UINTPTR param4)
Define the type of a task entrance function.
Definition: los_task.h:480
STATIC INLINE UINT32 ArchCurrCpuid(VOID)
Definition: los_hw_cpu.h:168
LITE_OS_SEC_TEXT UINT32 OsGetIdleProcessID(VOID)
获取内核态空闲进程
Definition: los_process.c:2254
VOID OsSchedRunqueueIdleInit(UINT32 idleTaskID)
Definition: los_sched.c:207
LITE_OS_SEC_TEXT WEAK VOID OsIdleTask(VOID)
空闲任务,每个CPU都有自己的空闲任务
Definition: los_task.c:191
UINT16 taskStatus
UINT16 usCpuAffiMask
Definition: los_task.h:511
CHAR * pcName
Definition: los_task.h:509
TSK_ENTRY_FUNC pfnTaskEntry
Definition: los_task.h:504
函数调用图:
这是这个函数的调用关系图:

◆ OsInactiveTaskDelete()

VOID OsInactiveTaskDelete ( LosTaskCB taskCB)

在文件 los_task.c947 行定义.

948{
949 UINT16 taskStatus = taskCB->taskStatus;
950
951 OsTaskReleaseHoldLock(taskCB);
952
953 taskCB->ops->exit(taskCB);
954 if (taskStatus & OS_TASK_STATUS_PENDING) {
955 LosMux *mux = (LosMux *)taskCB->taskMux;
956 if (LOS_MuxIsValid(mux) == TRUE) {
957 OsMuxBitmapRestore(mux, NULL, taskCB);
958 }
959 }
960
961 OsTaskStatusUnusedSet(taskCB);
962
964
965 OsHookCall(LOS_HOOK_TYPE_TASK_DELETE, taskCB);
966}
LITE_OS_SEC_TEXT BOOL LOS_MuxIsValid(const LosMux *mutex)
互斥锁是否有效
Definition: los_mux.c:239
VOID OsMuxBitmapRestore(const LosMux *mutex, const LOS_DL_LIST *list, const LosTaskCB *runTask)
恢复互斥锁位图
Definition: los_mux.c:328
VOID OsDeleteTaskFromProcess(LosTaskCB *taskCB)
Definition: los_process.c:107
STATIC INLINE VOID OsTaskStatusUnusedSet(LosTaskCB *taskCB)
设置任务为不使用状态
Definition: los_task.c:875
STATIC VOID OsTaskReleaseHoldLock(LosTaskCB *taskCB)
Definition: los_task.c:883
unsigned short UINT16
Definition: los_typedef.h:56
Definition: los_mux.h:73
VOID(* exit)(LosTaskCB *taskCB)
任务退出
VOID * taskMux
const SchedOps * ops
if(tv==NULL)
Definition: time.c:430
函数调用图:
这是这个函数的调用关系图:

◆ OsResourceFreeTaskCreate()

UINT32 OsResourceFreeTaskCreate ( VOID  )

创建一个回收资源的任务

在文件 los_task.c1672 行定义.

1673{
1674 UINT32 ret;
1675 UINT32 taskID;
1676 TSK_INIT_PARAM_S taskInitParam;
1677
1678 ret = LOS_EventInit((PEVENT_CB_S)&g_resourceEvent);//初始化资源事件
1679 if (ret != LOS_OK) {
1680 return LOS_NOK;
1681 }
1682
1683 (VOID)memset_s((VOID *)(&taskInitParam), sizeof(TSK_INIT_PARAM_S), 0, sizeof(TSK_INIT_PARAM_S));
1684 taskInitParam.pfnTaskEntry = (TSK_ENTRY_FUNC)OsResourceRecoveryTask;//入口函数
1685 taskInitParam.uwStackSize = OS_TASK_RESOURCE_STATIC_SIZE;
1686 taskInitParam.pcName = "ResourcesTask";
1687 taskInitParam.usTaskPrio = OS_TASK_RESOURCE_FREE_PRIORITY;// 5 ,优先级很高
1688 ret = LOS_TaskCreate(&taskID, &taskInitParam);
1689 if (ret == LOS_OK) {
1690 OS_TCB_FROM_TID(taskID)->taskStatus |= OS_TASK_FLAG_NO_DELETE;
1691 }
1692 return ret;
1693}
LITE_OS_SEC_TEXT_INIT UINT32 LOS_EventInit(PEVENT_CB_S eventCB)
初始化一个事件控制块
Definition: los_event.c:95
LITE_OS_SEC_TEXT_INIT UINT32 LOS_TaskCreate(UINT32 *taskID, TSK_INIT_PARAM_S *initParam)
创建任务,并使该任务进入ready状态,如果就绪队列中没有更高优先级的任务,则运行该任务
Definition: los_task.c:718
STATIC VOID OsResourceRecoveryTask(VOID)
资源回收任务
Definition: los_task.c:1652
LITE_OS_SEC_BSS EVENT_CB_S g_resourceEvent
Definition: los_task.c:152
函数调用图:

◆ OsRunningTaskToExit()

VOID OsRunningTaskToExit ( LosTaskCB runTask,
UINT32  status 
)

在文件 los_task.c908 行定义.

909{
910 UINT32 intSave;
911
912 if (OsProcessThreadGroupIDGet(runTask) == runTask->taskID) {
914 }
915
916 OsHookCall(LOS_HOOK_TYPE_TASK_DELETE, runTask);
917
918 SCHEDULER_LOCK(intSave);
919 if (OsProcessThreadNumberGet(runTask) == 1) { /* 1: The last task of the process exits */
920 SCHEDULER_UNLOCK(intSave);
921
922 OsTaskResourcesToFree(runTask);
923 OsProcessResourcesToFree(OS_PCB_FROM_PID(runTask->processID));
924
925 SCHEDULER_LOCK(intSave);
926
927 OsProcessNaturalExit(OS_PCB_FROM_PID(runTask->processID), status);
928 OsTaskReleaseHoldLock(runTask);
929 OsTaskStatusUnusedSet(runTask);
930 } else if (runTask->taskStatus & OS_TASK_FLAG_PTHREAD_JOIN) {
931 OsTaskReleaseHoldLock(runTask);
932 } else {
933 SCHEDULER_UNLOCK(intSave);
934
935 OsTaskResourcesToFree(runTask);
936
937 SCHEDULER_LOCK(intSave);
938 OsInactiveTaskDelete(runTask);
939 OsEventWriteUnsafe(&g_resourceEvent, OS_RESOURCE_EVENT_FREE, FALSE, NULL);
940 }
941
943 SCHEDULER_UNLOCK(intSave);
944 return;
945}
VOID OsEventWriteUnsafe(PEVENT_CB_S eventCB, UINT32 events, BOOL once, UINT8 *exitFlag)
以不安全的方式写事件
Definition: los_event.c:247
VOID OsProcessNaturalExit(LosProcessCB *processCB, UINT32 status)
Definition: los_process.c:550
LITE_OS_SEC_TEXT VOID OsProcessThreadGroupDestroy(VOID)
Definition: los_process.c:2200
LITE_OS_SEC_TEXT VOID OsProcessResourcesToFree(LosProcessCB *processCB)
Definition: los_process.c:431
STATIC INLINE UINT32 OsProcessThreadGroupIDGet(const LosTaskCB *taskCB)
STATIC INLINE UINT32 OsProcessThreadNumberGet(const LosTaskCB *taskCB)
VOID OsSchedResched(VOID)
Definition: los_sched.c:449
LITE_OS_SEC_TEXT VOID OsInactiveTaskDelete(LosTaskCB *taskCB)
Definition: los_task.c:947
STATIC VOID OsTaskResourcesToFree(LosTaskCB *taskCB)
Definition: los_task.c:424
UINT32 taskID
UINT32 processID
函数调用图:
这是这个函数的调用关系图:

◆ OsSetMainTask()

VOID OsSetMainTask ( VOID  )

◆ OsSetTaskName()

INT32 OsSetTaskName ( LosTaskCB taskCB,
const CHAR name,
BOOL  setPName 
)

在文件 los_task.c1337 行定义.

1338{
1339 UINT32 intSave;
1340 errno_t err;
1341 const CHAR *namePtr = NULL;
1342 CHAR nameBuff[OS_TCB_NAME_LEN] = { 0 };
1343
1344 if ((taskCB == NULL) || (name == NULL)) {
1345 return EINVAL;
1346 }
1347
1348 if (LOS_IsUserAddress((VADDR_T)(UINTPTR)name)) {
1349 err = LOS_StrncpyFromUser(nameBuff, (const CHAR *)name, OS_TCB_NAME_LEN);
1350 if (err < 0) {
1351 return -err;
1352 }
1353 namePtr = nameBuff;
1354 } else {
1355 namePtr = name;
1356 }
1357
1358 SCHEDULER_LOCK(intSave);
1359
1360 err = strncpy_s(taskCB->taskName, OS_TCB_NAME_LEN, (VOID *)namePtr, OS_TCB_NAME_LEN - 1);
1361 if (err != EOK) {
1362 err = EINVAL;
1363 goto EXIT;
1364 }
1365
1366 err = LOS_OK;
1367 /* if thread is main thread, then set processName as taskName */
1368 if ((taskCB->taskID == OsProcessThreadGroupIDGet(taskCB)) && (setPName == TRUE)) {
1369 err = (INT32)OsSetProcessName(OS_PCB_FROM_PID(taskCB->processID), (const CHAR *)taskCB->taskName);
1370 if (err != LOS_OK) {
1371 err = EINVAL;
1372 }
1373 }
1374
1375EXIT:
1376 SCHEDULER_UNLOCK(intSave);
1377 return err;
1378}
UINT32 OsSetProcessName(LosProcessCB *processCB, const CHAR *name)
Definition: los_process.c:704
signed int INT32
Definition: los_typedef.h:60
unsigned long VADDR_T
Definition: los_typedef.h:208
unsigned long UINTPTR
Definition: los_typedef.h:68
char CHAR
Definition: los_typedef.h:63
STATIC INLINE BOOL LOS_IsUserAddress(VADDR_T vaddr)
虚拟地址是否在用户空间
Definition: los_vm_map.h:275
INT32 LOS_StrncpyFromUser(CHAR *dst, const CHAR *src, INT32 count)
CHAR taskName[OS_TCB_NAME_LEN]
函数调用图:
这是这个函数的调用关系图:

◆ OsShellCmdDumpTask()

UINT32 OsShellCmdDumpTask ( INT32  argc,
const CHAR **  argv 
)

在文件 task_shellcmd.c615 行定义.

616{
617 UINT32 flag = 0;
618#ifdef LOSCFG_KERNEL_VM
619 flag |= OS_PROCESS_MEM_INFO;
620#endif
621
622 if (argc >= 2) { /* 2: The task shell name restricts the parameters */
623 goto TASK_HELP;
624 }
625
626 if (argc == 1) {
627 if (strcmp("-a", argv[0]) == 0) {
628 flag |= OS_PROCESS_INFO_ALL;
629#ifdef LOSCFG_SCHED_DEBUG
630#ifdef LOSCFG_SCHED_TICK_DEBUG
631 } else if (strcmp("-i", argv[0]) == 0) {
633 return LOS_OK;
634 }
635 goto TASK_HELP;
636#endif
637 } else if (strcmp("-t", argv[0]) == 0) {
639 return LOS_OK;
640 }
641 goto TASK_HELP;
642#endif
643 } else {
644 goto TASK_HELP;
645 }
646 }
647
648 return OsShellCmdTskInfoGet(OS_ALL_TASK_MASK, NULL, flag);
649
650TASK_HELP:
651 PRINTK("Unknown option: %s\n", argv[0]);
652 PRINTK("Usage:\n");
653 PRINTK(" task --- Basic information about all created processes.\n");
654 PRINTK(" task -a --- Complete information about all created processes.\n");
655 return LOS_NOK;
656}
UINT32 OsShellShowTickResponse(VOID)
UINT32 OsShellShowSchedStatistics(VOID)
LITE_OS_SEC_TEXT_MINOR UINT32 OsShellCmdTskInfoGet(UINT32 taskID, VOID *seqBuf, UINT16 flag)
函数调用图:

◆ OsShellCmdTskInfoGet()

UINT32 OsShellCmdTskInfoGet ( UINT32  taskID,
VOID *  seqfile,
UINT16  flag 
)

在文件 task_shellcmd.c585 行定义.

586{
587 UINT32 size;
588 LosProcessCB *pcbArray = NULL;
589 LosTaskCB *tcbArray = NULL;
590 SchedParam *schedParam = NULL;
591 UINTPTR cpupInfo = 0;
592
593 if (taskID == OS_ALL_TASK_MASK) {
594 if (flag & OS_PROCESS_MEM_INFO) {
595 size = OS_PROCESS_ALL_INFO_LEN + OS_PROCESS_MEM_ALL_INFO_LEN + OS_TASK_ALL_INFO_LEN;
596 } else {
597 size = OS_PROCESS_ALL_INFO_LEN + OS_TASK_ALL_INFO_LEN;
598 }
599 pcbArray = (LosProcessCB *)LOS_MemAlloc(m_aucSysMem1, size);
600 if (pcbArray == NULL) {
601 PRINT_ERR("Memory is not enough to save task info!\n");
602 return LOS_NOK;
603 }
604 (VOID)memset_s(pcbArray, size, 0, size);
605 ProcessAndTaskInfoGet(&pcbArray, &tcbArray, &schedParam, &cpupInfo, flag);
606 ProcessInfoShow(pcbArray, schedParam, cpupInfo, seqBuf, flag);
607 TaskInfoData(tcbArray, cpupInfo, seqBuf, flag);
608
609 (VOID)LOS_MemFree(m_aucSysMem1, pcbArray);
610 }
611
612 return LOS_OK;
613}
VOID * LOS_MemAlloc(VOID *pool, UINT32 size)
从指定内存池中申请size长度的内存,注意这可不是从内核堆空间中申请内存
Definition: los_memory.c:1123
UINT8 * m_aucSysMem1
系统动态内存池地址的起始地址 @note_thinking 能否不要用 0,1来命名核心变量 ???
Definition: los_memory.c:108
UINT32 LOS_MemFree(VOID *pool, VOID *ptr)
释放从指定动态内存中申请的内存
Definition: los_memory.c:1369
STATIC VOID ProcessAndTaskInfoGet(LosProcessCB **pcbArray, LosTaskCB **tcbArray, SchedParam **schedParam, UINTPTR *cpupInfo, UINT16 flag)
STATIC VOID ProcessInfoShow(const LosProcessCB *pcbArray, const SchedParam *param, UINTPTR cpupInfo, VOID *seqBuf, UINT16 flag)
STATIC VOID TaskInfoData(const LosTaskCB *allTaskArray, UINTPTR cpupInfo, VOID *seqBuf, UINT16 flag)
函数调用图:
这是这个函数的调用关系图:

◆ OsTaskCBRecycleToFree()

VOID OsTaskCBRecycleToFree ( VOID  )

◆ OsTaskContextLoad()

VOID OsTaskContextLoad ( LosTaskCB newTask)
这是这个函数的调用关系图:

◆ OsTaskCpuAffiSetUnsafe()

BOOL OsTaskCpuAffiSetUnsafe ( UINT32  taskID,
UINT16  newCpuAffiMask,
UINT16 oldCpuAffiMask 
)

CPU亲和性(affinity)将任务绑在指定CPU上,用于多核CPU情况,(该函数仅在SMP模式下支持)

在文件 los_task.c1212 行定义.

1213{
1214#ifdef LOSCFG_KERNEL_SMP
1215 LosTaskCB *taskCB = OS_TCB_FROM_TID(taskID);
1216
1217 taskCB->cpuAffiMask = newCpuAffiMask;
1218 *oldCpuAffiMask = CPUID_TO_AFFI_MASK(taskCB->currCpu);
1219 if (!((*oldCpuAffiMask) & newCpuAffiMask)) {
1220 taskCB->signal = SIGNAL_AFFI;
1221 return TRUE;
1222 }
1223#else
1224 (VOID)taskID;
1225 (VOID)newCpuAffiMask;
1226 (VOID)oldCpuAffiMask;
1227#endif /* LOSCFG_KERNEL_SMP */
1228 return FALSE;
1229}
UINT16 cpuAffiMask
UINT32 signal
UINT16 currCpu
这是这个函数的调用关系图:

◆ OsTaskEntry()

VOID OsTaskEntry ( UINT32  taskID)

在文件 los_task.c493 行定义.

494{
495 LOS_ASSERT(!OS_TID_CHECK_INVALID(taskID));
496
497 /*
498 * task scheduler needs to be protected throughout the whole process
499 * from interrupt and other cores. release task spinlock and enable
500 * interrupt in sequence at the task entry.
501 */
502 LOS_SpinUnlock(&g_taskSpin);//释放任务自旋锁
503 (VOID)LOS_IntUnLock();//恢复中断
504
505 LosTaskCB *taskCB = OS_TCB_FROM_TID(taskID);
506 taskCB->joinRetval = taskCB->taskEntry(taskCB->args[0], taskCB->args[1],//调用任务的入口函数
507 taskCB->args[2], taskCB->args[3]); /* 2 & 3: just for args array index */
508 if (!(taskCB->taskStatus & OS_TASK_FLAG_PTHREAD_JOIN)) {
509 taskCB->joinRetval = 0;//结合数为0
510 }
511
512 OsRunningTaskToExit(taskCB, 0);
513}
STATIC INLINE UINT32 LOS_IntUnLock(VOID)
Enable all interrupts. | 打开当前处理器所有中断响应
Definition: los_hwi.h:311
VOID LOS_SpinUnlock(SPIN_LOCK_S *lock)
Definition: los_spinlock.c:84
LITE_OS_SEC_TEXT VOID OsRunningTaskToExit(LosTaskCB *runTask, UINT32 status)
Definition: los_task.c:908
SPIN_LOCK_S g_taskSpin
TSK_ENTRY_FUNC taskEntry
VOID * joinRetval
UINTPTR args[4]
函数调用图:
这是这个函数的调用关系图:

◆ OsTaskInit()

UINT32 OsTaskInit ( VOID  )

在文件 los_task.c259 行定义.

260{
261 UINT32 index;
262 UINT32 size;
263 UINT32 ret;
264
265 g_taskMaxNum = LOSCFG_BASE_CORE_TSK_LIMIT;//任务池中最多默认128个,可谓铁打的任务池流水的线程
266 size = (g_taskMaxNum + 1) * sizeof(LosTaskCB);//计算需分配内存总大小
267 /*
268 * This memory is resident memory and is used to save the system resources
269 * of task control block and will not be freed.
270 */
271 g_taskCBArray = (LosTaskCB *)LOS_MemAlloc(m_aucSysMem0, size);//任务池常驻内存,不被释放
272 if (g_taskCBArray == NULL) {
273 ret = LOS_ERRNO_TSK_NO_MEMORY;
274 goto EXIT;
275 }
276 (VOID)memset_s(g_taskCBArray, size, 0, size);
277
278 LOS_ListInit(&g_losFreeTask);//初始化空闲任务链表
279 LOS_ListInit(&g_taskRecycleList);//初始化回收任务链表
280 for (index = 0; index < g_taskMaxNum; index++) {//任务挨个初始化
281 g_taskCBArray[index].taskStatus = OS_TASK_STATUS_UNUSED;//默认未使用,干净.
282 g_taskCBArray[index].taskID = index;//任务ID [0 ~ g_taskMaxNum - 1]
283 LOS_ListTailInsert(&g_losFreeTask, &g_taskCBArray[index].pendList);//通过pendList节点插入空闲任务列表
284 }//注意:这里挂的是pendList节点,所以取TCB也要通过 OS_TCB_FROM_PENDLIST 取.
285
286 ret = OsSchedInit();//调度器初始化
287
288EXIT:
289 if (ret != LOS_OK) {
290 PRINT_ERR("OsTaskInit error\n");
291 }
292 return ret;
293}
LITE_OS_SEC_ALW_INLINE STATIC INLINE VOID LOS_ListInit(LOS_DL_LIST *list)
Definition: los_list.h:104
LITE_OS_SEC_ALW_INLINE STATIC INLINE VOID LOS_ListTailInsert(LOS_DL_LIST *list, LOS_DL_LIST *node)
Insert a node to the tail of a doubly linked list.
Definition: los_list.h:244
UINT8 * m_aucSysMem0
异常交互动态内存池地址的起始地址,当不支持异常交互特性时,m_aucSysMem0等于m_aucSysMem1。
Definition: los_memory.c:107
LITE_OS_SEC_BSS UINT32 g_taskMaxNum
任务最大数量 默认128个
Definition: los_task.c:150
LITE_OS_SEC_BSS LosTaskCB * g_taskCBArray
外部变量 任务池 默认128个
Definition: los_task.c:147
UINT32 OsSchedInit(VOID)
Definition: los_sched.c:213
LITE_OS_SEC_BSS LOS_DL_LIST g_losFreeTask
Definition: los_task.c:148
LITE_OS_SEC_BSS LOS_DL_LIST g_taskRecycleList
Definition: los_task.c:149
函数调用图:
这是这个函数的调用关系图:

◆ OsTaskInsertToRecycleList()

VOID OsTaskInsertToRecycleList ( LosTaskCB taskCB)

在文件 los_task.c198 行定义.

199{
201}
LOS_DL_LIST pendList
函数调用图:
这是这个函数的调用关系图:

◆ OsTaskIsKilled()

STATIC INLINE BOOL OsTaskIsKilled ( const LosTaskCB taskCB)

在文件 los_task_pri.h260 行定义.

261{
262 return ((taskCB->taskStatus & OS_TASK_FLAG_EXIT_KILL) != 0);
263}
这是这个函数的调用关系图:

◆ OsTaskIsUnused()

STATIC INLINE BOOL OsTaskIsUnused ( const LosTaskCB taskCB)

任务是否在使用

在文件 los_task_pri.h255 行定义.

256{
257 return ((taskCB->taskStatus & OS_TASK_STATUS_UNUSED) != 0);
258}
这是这个函数的调用关系图:

◆ OsTaskIsUserMode()

STATIC INLINE BOOL OsTaskIsUserMode ( const LosTaskCB taskCB)

在文件 los_task_pri.h265 行定义.

266{
267 return ((taskCB->taskStatus & OS_TASK_FLAG_USER_MODE) != 0);
268}
这是这个函数的调用关系图:

◆ OsTaskJoinPendUnsafe()

UINT32 OsTaskJoinPendUnsafe ( LosTaskCB taskCB)

挂起任务,任务进入等待链表,Join代表是支持通过一个任务去唤醒其他的任务

在文件 los_task.c224 行定义.

225{
226 if (taskCB->taskStatus & OS_TASK_STATUS_INIT) {
227 return LOS_EINVAL;
228 }
229
230 if (taskCB->taskStatus & OS_TASK_STATUS_EXIT) {
231 return LOS_OK;
232 }
233
234 if ((taskCB->taskStatus & OS_TASK_FLAG_PTHREAD_JOIN) && LOS_ListEmpty(&taskCB->joinList)) {
235 OsTaskWaitSetPendMask(OS_TASK_WAIT_JOIN, taskCB->taskID, LOS_WAIT_FOREVER);//设置任务的等待标记
236 LosTaskCB *runTask = OsCurrTaskGet();
237 return runTask->ops->wait(runTask, &taskCB->joinList, LOS_WAIT_FOREVER);
238 }
239
240 return LOS_EINVAL;
241}
LITE_OS_SEC_ALW_INLINE STATIC INLINE BOOL LOS_ListEmpty(LOS_DL_LIST *list)
Identify whether a specified doubly linked list is empty. | 判断链表是否为空
Definition: los_list.h:321
STATIC INLINE LosTaskCB * OsCurrTaskGet(VOID)
STATIC INLINE VOID OsTaskWaitSetPendMask(UINT16 mask, UINTPTR lockID, UINT32 timeout)
设置事件阻塞掩码,即设置任务的等待事件.
Definition: los_task_pri.h:289
UINT32(* wait)(LosTaskCB *runTask, LOS_DL_LIST *list, UINT32 timeout)
任务等待
LOS_DL_LIST joinList
函数调用图:
这是这个函数的调用关系图:

◆ OsTaskJoinPostUnsafe()

VOID OsTaskJoinPostUnsafe ( LosTaskCB taskCB)

OsTaskJoinPostUnsafe 查找task 通过 OS_TCB_FROM_PENDLIST 来完成,相当于由LOS_DL_LIST找到LosTaskCB, 将那些和参数任务绑在一起的task唤醒.

参数
taskCB
返回
参见

在文件 los_task.c212 行定义.

213{
214 if (taskCB->taskStatus & OS_TASK_FLAG_PTHREAD_JOIN) {//join任务处理
215 if (!LOS_ListEmpty(&taskCB->joinList)) {//注意到了这里 joinList中的节点身上都有阻塞标签
216 LosTaskCB *resumedTask = OS_TCB_FROM_PENDLIST(LOS_DL_LIST_FIRST(&(taskCB->joinList)));//通过贴有JOIN标签链表的第一个节点找到Task
217 OsTaskWakeClearPendMask(resumedTask);//清除任务的挂起标记
218 resumedTask->ops->wake(resumedTask);
219 }
220 }
221 taskCB->taskStatus |= OS_TASK_STATUS_EXIT;//贴上任务退出标签
222}
STATIC INLINE VOID OsTaskWakeClearPendMask(LosTaskCB *resumeTask)
清除事件阻塞掩码,即任务不再等待任何事件.
Definition: los_task_pri.h:298
VOID(* wake)(LosTaskCB *taskCB)
任务唤醒
函数调用图:
这是这个函数的调用关系图:

◆ OsTaskProcSignal()

VOID OsTaskProcSignal ( VOID  )

由其他CPU核触发阻塞进程的信号 函数由汇编代码调用 ..\arch\arm\arm\src\los_dispatch.S

在文件 los_task.c1298 行定义.

1299{
1300 UINT32 ret;
1301 //私有且不可中断,无需保护。这个任务在其他CPU核看到它时总是在运行,所以它在执行代码的同时也可以继续接收信号
1302 /*
1303 * private and uninterruptable, no protection needed.
1304 * while this task is always running when others cores see it,
1305 * so it keeps receiving signals while follow code executing.
1306 */
1307 LosTaskCB *runTask = OsCurrTaskGet();
1308 if (runTask->signal == SIGNAL_NONE) {
1309 return;
1310 }
1311
1312 if (runTask->signal & SIGNAL_KILL) {//意思是其他cpu发起了要干掉你的信号
1313 /*
1314 * clear the signal, and do the task deletion. if the signaled task has been
1315 * scheduled out, then this deletion will wait until next run.
1316 *///如果发出信号的任务已出调度就绪队列,则此删除将等待下次运行
1317 runTask->signal = SIGNAL_NONE;//清除信号,
1318 ret = LOS_TaskDelete(runTask->taskID);
1319 if (ret != LOS_OK) {
1320 PRINT_ERR("Task proc signal delete task(%u) failed err:0x%x\n", runTask->taskID, ret);
1321 }
1322 } else if (runTask->signal & SIGNAL_SUSPEND) {//意思是其他cpu发起了要挂起你的信号
1323 runTask->signal &= ~SIGNAL_SUSPEND;//任务贴上被其他CPU挂起的标签
1324
1325 /* suspend killed task may fail, ignore the result */
1326 (VOID)LOS_TaskSuspend(runTask->taskID);
1327#ifdef LOSCFG_KERNEL_SMP
1328 } else if (runTask->signal & SIGNAL_AFFI) {//意思是下次调度其他cpu要媾和你
1329 runTask->signal &= ~SIGNAL_AFFI;//任务贴上被其他CPU媾和的标签
1330
1331 /* pri-queue has updated, notify the target cpu */
1332 LOS_MpSchedule((UINT32)runTask->cpuAffiMask);//发生调度,此任务将移交给媾和CPU运行.
1333#endif
1334 }
1335}
LITE_OS_SEC_TEXT_INIT UINT32 LOS_TaskDelete(UINT32 taskID)
Delete a task.
Definition: los_task.c:968
LITE_OS_SEC_TEXT_INIT UINT32 LOS_TaskSuspend(UINT32 taskID)
外部接口,对OsTaskSuspend的封装
Definition: los_task.c:855
VOID LOS_MpSchedule(UINT32 target)
Definition: los_mp.c:76
函数调用图:

◆ OsTaskSchedule()

VOID OsTaskSchedule ( LosTaskCB ,
LosTaskCB  
)
这是这个函数的调用关系图:

◆ OsTaskSetDetachUnsafe()

UINT32 OsTaskSetDetachUnsafe ( LosTaskCB taskCB)

任务设置分离模式 Deatch和JOIN是一对有你没我的状态

在文件 los_task.c243 行定义.

244{
245 if (taskCB->taskStatus & OS_TASK_FLAG_PTHREAD_JOIN) {//join状态时
246 if (LOS_ListEmpty(&(taskCB->joinList))) {//joinlist中没有数据了
247 LOS_ListDelete(&(taskCB->joinList));//所谓删除就是自己指向自己
248 taskCB->taskStatus &= ~OS_TASK_FLAG_PTHREAD_JOIN;//去掉JOIN标签
249 return LOS_OK;
250 }
251 /* This error code has a special purpose and is not allowed to appear again on the interface */
252 return LOS_ESRCH;
253 }
254
255 return LOS_EINVAL;
256}
LITE_OS_SEC_ALW_INLINE STATIC INLINE VOID LOS_ListDelete(LOS_DL_LIST *node)
Definition: los_list.h:292
函数调用图:
这是这个函数的调用关系图:

◆ OsTaskWaitSetPendMask()

STATIC INLINE VOID OsTaskWaitSetPendMask ( UINT16  mask,
UINTPTR  lockID,
UINT32  timeout 
)

设置事件阻塞掩码,即设置任务的等待事件.

在文件 los_task_pri.h289 行定义.

290{
291 LosTaskCB *runTask = OsCurrTaskGet();
292 runTask->waitID = lockID; //
293 runTask->waitFlag = mask; //当前任务在等待什么东东到来 例如: OS_TASK_WAIT_LITEIPC
294 (VOID)timeout;
295}
UINTPTR waitID
UINT16 waitFlag
函数调用图:
这是这个函数的调用关系图:

◆ OsTaskWakeClearPendMask()

STATIC INLINE VOID OsTaskWakeClearPendMask ( LosTaskCB resumeTask)

清除事件阻塞掩码,即任务不再等待任何事件.

在文件 los_task_pri.h298 行定义.

299{
300 resumeTask->waitID = 0;
301 resumeTask->waitFlag = 0;
302}
这是这个函数的调用关系图:

◆ OsTcbDispatch()

INT32 OsTcbDispatch ( LosTaskCB stcb,
siginfo_t *  info 
)

给任务(线程)发送一个信号

在文件 los_signal.c182 行定义.

183{
184 bool masked = FALSE;
185 sig_cb *sigcb = &stcb->sig;
186
187 OS_RETURN_IF_NULL(sigcb);
188 /* If signo is 0, not send signal, just check process or pthread exist */
189 if (info->si_signo == 0) {//如果信号为0,则不发送信号,只是作为检查进程和线程是否还存在.
190 return 0;
191 }
192 masked = (bool)OsSigIsMember(&sigcb->sigprocmask, info->si_signo);//@note_thinking 这里还有 masked= -1的情况要处理!!!
193 if (masked) {//如果信号被屏蔽了,要看等待信号集,sigwaitmask
194 /* If signal is in wait list and mask list, need unblock it */ //如果信号在等待列表和掩码列表中,需要解除阻止
195 if (LOS_ListEmpty(&sigcb->waitList) ||
196 (!LOS_ListEmpty(&sigcb->waitList) && !OsSigIsMember(&sigcb->sigwaitmask, info->si_signo))) {
197 OsSigAddSet(&sigcb->sigPendFlag, info->si_signo);//将信号加入挂起/待办集
198 }
199 } else {//信号没有被屏蔽的处理
200 /* unmasked signal actions */
201 OsSigAddSet(&sigcb->sigFlag, info->si_signo);//不屏蔽的信号集
202 }
203
204 if (OsAddSigInfoToTmpList(sigcb, info) == LOS_NOK) {
205 return -ENOMEM;
206 }
207
208 return OsPendingTaskWake(stcb, info->si_signo);
209}
int OsSigAddSet(sigset_t *set, int signo)
向信号集中加入signo信号
Definition: los_signal.c:527
STATIC UINT32 OsPendingTaskWake(LosTaskCB *taskCB, INT32 signo)
Definition: los_signal.c:143
int OsSigIsMember(const sigset_t *set, int signo)
判定信号signo是否存在信号集中。如果信号集里已有该信号则返回1,否则返回0。如果有错误则返回-1
Definition: los_signal.c:62
STATIC INT32 OsAddSigInfoToTmpList(sig_cb *sigcb, siginfo_t *info)
Definition: los_signal.c:96
size_t bool
Definition: shell_list.h:44
sig_cb sig
信号控制块,用于异步通信,类似于 linux singal模块
信号控制块(描述符)
Definition: los_signal.h:220
sigset_t sigwaitmask
Definition: los_signal.h:226
sigset_t sigprocmask
Signals that are blocked | 任务屏蔽了哪些信号
Definition: los_signal.h:223
LOS_DL_LIST waitList
待链表,上面挂的是等待信号到来的任务, 请查找 OsTaskWait(&sigcb->waitList, timeout, TRUE) 理解
Definition: los_signal.h:225
sigset_t sigPendFlag
信号阻塞标签集,记录那些信号来过,任务依然阻塞的集合.即:这些信号不能唤醒任务
Definition: los_signal.h:222
sigset_t sigFlag
不屏蔽的信号集
Definition: los_signal.h:221
函数调用图:
这是这个函数的调用关系图:

◆ OsUserProcessOperatePermissionsCheck()

INT32 OsUserProcessOperatePermissionsCheck ( const LosTaskCB taskCB,
UINT32  processID 
)

在文件 los_task.c1385 行定义.

1386{
1387 if (taskCB == NULL) {
1388 return LOS_EINVAL;
1389 }
1390
1391 if (processID == OS_INVALID_VALUE) {
1392 return LOS_EINVAL;
1393 }
1394
1395 if (taskCB->taskStatus & OS_TASK_STATUS_UNUSED) {
1396 return LOS_EINVAL;
1397 }
1398
1399 if (processID != taskCB->processID) {
1400 return LOS_EPERM;
1401 }
1402
1403 return LOS_OK;
1404}
这是这个函数的调用关系图:

◆ OsUserTaskOperatePermissionsCheck()

INT32 OsUserTaskOperatePermissionsCheck ( const LosTaskCB taskCB)

在文件 los_task.c1380 行定义.

1381{
1382 return OsUserProcessOperatePermissionsCheck(taskCB, OsCurrProcessGet()->processID);
1383}
INT32 OsUserProcessOperatePermissionsCheck(const LosTaskCB *taskCB, UINT32 processID)
Definition: los_task.c:1385
函数调用图:
这是这个函数的调用关系图:

◆ OsWriteResourceEvent()

VOID OsWriteResourceEvent ( UINT32  events)

在文件 los_task.c1642 行定义.

1643{
1644 (VOID)LOS_EventWrite(&g_resourceEvent, events);
1645}
LITE_OS_SEC_TEXT UINT32 LOS_EventWrite(PEVENT_CB_S eventCB, UINT32 events)
写指定的事件类型
Definition: los_event.c:318
函数调用图:
这是这个函数的调用关系图:

◆ OsWriteResourceEventUnsafe()

VOID OsWriteResourceEventUnsafe ( UINT32  events)

在文件 los_task.c1647 行定义.

1648{
1649 (VOID)OsEventWriteUnsafe(&g_resourceEvent, events, FALSE, NULL);
1650}
函数调用图:

变量说明

◆ g_taskSpin

SPIN_LOCK_S g_taskSpin
extern