INT8U OS_TCBInit (INT8U prio, OS_STK *ptos, OS_STK *pbos, INT16U id, INT32U stk_size, void *pext, INT16U opt)
{
OS_TCB *ptcb;
OS_ENTER_CRITICAL();
ptcb = OSTCBFreeList; /* Get a free TCB from the free TCB list */
if (ptcb != (OS_TCB *)0)
{
OSTCBFreeList = ptcb->OSTCBNext; /* Update pointer to free TCB list 空任务链表更新 指向TCB的双向链接的后链接 我认为是FreeList 指向了原本那个TCB块的后面一个TCB块,即移除一个TCB给OSTCBList*/
OS_EXIT_CRITICAL();
ptcb->OSTCBStkPtr = ptos; /* Load Stack pointer in TCB */
ptcb->OSTCBPrio = prio; /* Load task priority into TCB */
ptcb->OSTCBStat = OS_STAT_RDY; /* Task is ready to run */
ptcb->OSTCBStatPend = OS_STAT_PEND_OK; /* Clear pend status */
ptcb->OSTCBDly = 0; /* Task is not delayed */
pext = pext; /* Prevent compiler warning if not used */
stk_size = stk_size;
pbos = pbos;
opt = opt;
id = id;
#if OS_LOWEST_PRIO <= 63
ptcb->OSTCBY = (INT8U)(prio >> 3); /* Pre-compute X, Y, BitX and BitY */
ptcb->OSTCBX = (INT8U)(prio & 0x07);
ptcb->OSTCBBitY = (INT8U)(1 << ptcb->OSTCBY);
ptcb->OSTCBBitX = (INT8U)(1 << ptcb->OSTCBX);
#else
ptcb->OSTCBY = (INT8U)((prio >> 4) & 0xFF); /* Pre-compute X, Y, BitX and BitY */
ptcb->OSTCBX = (INT8U) (prio & 0x0F);
ptcb->OSTCBBitY = (INT16U)(1 << ptcb->OSTCBY);
ptcb->OSTCBBitX = (INT16U)(1 << ptcb->OSTCBX);
#endif
#if OS_TASK_PROFILE_EN > 0
ptcb->OSTCBCtxSwCtr = 0L; /* Initialize profiling variables */
ptcb->OSTCBCyclesStart = 0L;
ptcb->OSTCBCyclesTot = 0L;
ptcb->OSTCBStkBase = (OS_STK *)0;
ptcb->OSTCBStkUsed = 0L;
#endif
OS_ENTER_CRITICAL();
OSTCBPrioTbl[prio] = ptcb; ///* Table of pointers to created TCBs */
ptcb->OSTCBNext = OSTCBList; /* Link into TCB chain */将OSTCBList指向的那个TCB地址赋给ptcb->OSTCBNext(刚才从FreeList中移除的那个TCB的Next)
ptcb->OSTCBPrev = (OS_TCB *)0; //如图prev前指针是指向NULL
if (OSTCBList != (OS_TCB *)0)
{
OSTCBList->OSTCBPrev = ptcb; //如下图 假如原先OSTCBList指向OSTCBPrio=1的那个TCB,即OSTCBList != (OS_TCB *)0 让OSTCBList->OSTCBPrev(OSTCBPrio=1的那个TCB的OSTCBPrev)=刚才从FreeList中移除的那个TCB
}
OSTCBList = ptcb; //OSTCBList由原来指向OSTCBPrio=1的那个TCB改为指向OSTCBPrio=3的那个TCB
OSRdyGrp |= ptcb->OSTCBBitY; /* Make task ready to run */
OSRdyTbl[ptcb->OSTCBY] |= ptcb->OSTCBBitX;
OSTaskCtr++; /* Increment the #tasks counter */
OS_EXIT_CRITICAL();
return (OS_ERR_NONE);
}
OS_EXIT_CRITICAL();
return (OS_ERR_TASK_NO_MORE_TCB);
}