上次是就绪表,这次是任务控制块,初始化程序如下
- /*
- *********************************************************************************************************
- * INITIALIZATION
- * INITIALIZE THE FREE LIST OF TASK CONTROL BLOCKS
- *
- * Description: This function is called by OSInit() to initialize the free list of OS_TCBs.
- *
- * Arguments : none
- *
- * Returns : none
- *********************************************************************************************************
- */
- static void OS_InitTCBList (void)
- {
- INT8U i;
- OS_TCB *ptcb1;
- OS_TCB *ptcb2;
- OS_MemClr((INT8U *)&OSTCBTbl[0], sizeof(OSTCBTbl)); /* Clear all the TCBs */
- OS_MemClr((INT8U *)&OSTCBPrioTbl[0], sizeof(OSTCBPrioTbl)); /* Clear the priority table */
- ptcb1 = &OSTCBTbl[0];
- ptcb2 = &OSTCBTbl[1];
- for (i = 0; i < (OS_MAX_TASKS + OS_N_SYS_TASKS - 1); i++) { /* Init. list of free TCBs */
- ptcb1->OSTCBNext = ptcb2;
- #if OS_TASK_NAME_SIZE > 1
- ptcb1->OSTCBTaskName[0] = '?'; /* Unknown name */
- ptcb1->OSTCBTaskName[1] = OS_ASCII_NUL;
- #endif
- ptcb1++;
- ptcb2++;
- }
- ptcb1->OSTCBNext = (OS_TCB *)0; /* Last OS_TCB */
- #if OS_TASK_NAME_SIZE > 1
- ptcb1->OSTCBTaskName[0] = '?'; /* Unknown name */
- ptcb1->OSTCBTaskName[1] = OS_ASCII_NUL;
- #endif
- OSTCBList = (OS_TCB *)0; /* TCB lists initializations */
- OSTCBFreeList = &OSTCBTbl[0];
- }
任务控制块是一个数据结构,其内容如下,这个结构在我理解相当于进程描述符的作用
- /*
- *********************************************************************************************************
- * TASK CONTROL BLOCK
- *********************************************************************************************************
- */
- typedef struct os_tcb {
- OS_STK *OSTCBStkPtr; /* Pointer to current top of stack */
- #if OS_TASK_CREATE_EXT_EN > 0
- void *OSTCBExtPtr; /* Pointer to user definable data for TCB extension */
- OS_STK *OSTCBStkBottom; /* Pointer to bottom of stack */
- INT32U OSTCBStkSize; /* Size of task stack (in number of stack elements) */
- INT16U OSTCBOpt; /* Task options as passed by OSTaskCreateExt() */
- INT16U OSTCBId; /* Task ID (0..65535) */
- #endif
- struct os_tcb *OSTCBNext; /* Pointer to next TCB in the TCB list */
- struct os_tcb *OSTCBPrev; /* Pointer to previous TCB in the TCB list */
- #if OS_EVENT_EN
- OS_EVENT *OSTCBEventPtr; /* Pointer to event control block */
- #endif
- #if ((OS_Q_EN > 0) && (OS_MAX_QS > 0)) || (OS_MBOX_EN > 0)
- void *OSTCBMsg; /* Message received from OSMboxPost() or OSQPost() */
- #endif
- #if (OS_VERSION >= 251) && (OS_FLAG_EN > 0) && (OS_MAX_FLAGS > 0)
- #if OS_TASK_DEL_EN > 0
- OS_FLAG_NODE *OSTCBFlagNode; /* Pointer to event flag node */
- #endif
- OS_FLAGS OSTCBFlagsRdy; /* Event flags that made task ready to run */
- #endif
- INT16U OSTCBDly; /* Nbr ticks to delay task or, timeout waiting for event */
- INT8U OSTCBStat; /* Task status */
- BOOLEAN OSTCBPendTO; /* Flag indicating PEND timed out (OS_TRUE == timed out) */
- INT8U OSTCBPrio; /* Task priority (0 == highest) */
- INT8U OSTCBX; /* Bit position in group corresponding to task priority */
- INT8U OSTCBY; /* Index into ready table corresponding to task priority */
- #if OS_LOWEST_PRIO <= 63
- INT8U OSTCBBitX; /* Bit mask to access bit position in ready table */
- INT8U OSTCBBitY; /* Bit mask to access bit position in ready group */
- #else
- INT16U OSTCBBitX; /* Bit mask to access bit position in ready table */
- INT16U OSTCBBitY; /* Bit mask to access bit position in ready group */
- #endif
- #if OS_TASK_DEL_EN > 0
- INT8U OSTCBDelReq; /* Indicates whether a task needs to delete itself */
- #endif
- #if OS_TASK_PROFILE_EN > 0
- INT32U OSTCBCtxSwCtr; /* Number of time the task was switched in */
- INT32U OSTCBCyclesTot; /* Total number of clock cycles the task has been running */
- INT32U OSTCBCyclesStart; /* Snapshot of cycle counter at start of task resumption */
- OS_STK *OSTCBStkBase; /* Pointer to the beginning of the task stack */
- INT32U OSTCBStkUsed; /* Number of bytes used from the stack */
- #endif
- #if OS_TASK_NAME_SIZE > 1
- INT8U OSTCBTaskName[OS_TASK_NAME_SIZE];
- #endif
- } OS_TCB;
OSTCBStkPtr 指向任务堆栈顶端的指针 栈的元素大小为32位宽
当允许使用OSTaskCreatExt()时即OS_TASK_CREATE_EXT_EN被置为1时可用:
void * OSTCBExtPtr 使用系统调用OSTaskCreatExt()创建的用户自定义数据段
OSTCBStkBottom 指向栈底的指针,系统调用OSTaskStkChk()来检测运行中堆栈的情况
OSTCBStkSize 存储栈所能容纳的指针元素,如果一个指针是32位,则有栈的大小为本数*4个字节
OSTCBOpt选择项用来告诉TaskCreatExt()在任务建立的时候任务栈检验功能得到了允许,
OSTCBNext OSTCBPrev 构成双链表的双向指针,指向上一个或者下一个TCB数据结构,构成了一个双向非循环链表
OSTCBEventPtr 指向事件控制数据块 用于任务间的通信和同步
OSTCBTaskName存任务名的数组,其中第一个数据[0]存入'?'
OSTCBList是一个指向TCB双链表表头的指针,初始化中赋值为NULL指针,之后创建task时加入TCB双链表
OSTCBFreeList空任务控制块指针,指向由空任务组成的链表,当一个任务建立时,将头指针指向的任务块交给该任务使用,当任务删除时,任务把任务块交还给空任务链表。
OSTCBTbl[]存放所有任务块的数组,不论是空任务块还是非空任务块,都在这个数组中,在不同的链表中组织结构不同
OSTCBPrioTbl[]指针数组 每一个元素都指向TCB
总结:这个初始化函数初始化了TCB数组和链表,TCB结构的初始化在建立新task时才会用到
阅读(3288) | 评论(0) | 转发(0) |