Chinaunix首页 | 论坛 | 博客
  • 博客访问: 431578
  • 博文数量: 103
  • 博客积分: 1455
  • 博客等级: 上尉
  • 技术积分: 1380
  • 用 户 组: 普通用户
  • 注册时间: 2012-09-15 22:17
文章分类

全部博文(103)

文章存档

2013年(4)

2012年(99)

我的朋友

分类: LINUX

2012-11-02 09:39:11

上次是就绪表,这次是任务控制块,初始化程序如下

点击(此处)折叠或打开

  1. /*
  2. *********************************************************************************************************
  3. * INITIALIZATION
  4. * INITIALIZE THE FREE LIST OF TASK CONTROL BLOCKS
  5. *
  6. * Description: This function is called by OSInit() to initialize the free list of OS_TCBs.
  7. *
  8. * Arguments : none
  9. *
  10. * Returns : none
  11. *********************************************************************************************************
  12. */

  13. static void OS_InitTCBList (void)
  14. {
  15.     INT8U i;
  16.     OS_TCB *ptcb1;
  17.     OS_TCB *ptcb2;


  18.     OS_MemClr((INT8U *)&OSTCBTbl[0], sizeof(OSTCBTbl)); /* Clear all the TCBs */
  19.     OS_MemClr((INT8U *)&OSTCBPrioTbl[0], sizeof(OSTCBPrioTbl)); /* Clear the priority table */
  20.     ptcb1 = &OSTCBTbl[0];
  21.     ptcb2 = &OSTCBTbl[1];
  22.     for (i = 0; i < (OS_MAX_TASKS + OS_N_SYS_TASKS - 1); i++) { /* Init. list of free TCBs */
  23.         ptcb1->OSTCBNext = ptcb2;
  24. #if OS_TASK_NAME_SIZE > 1
  25.         ptcb1->OSTCBTaskName[0] = '?'; /* Unknown name */
  26.         ptcb1->OSTCBTaskName[1] = OS_ASCII_NUL;
  27. #endif
  28.         ptcb1++;
  29.         ptcb2++;
  30.     }
  31.     ptcb1->OSTCBNext = (OS_TCB *)0; /* Last OS_TCB */
  32. #if OS_TASK_NAME_SIZE > 1
  33.     ptcb1->OSTCBTaskName[0] = '?'; /* Unknown name */
  34.     ptcb1->OSTCBTaskName[1] = OS_ASCII_NUL;
  35. #endif
  36.     OSTCBList = (OS_TCB *)0; /* TCB lists initializations */
  37.     OSTCBFreeList = &OSTCBTbl[0];
  38. }

任务控制块是一个数据结构,其内容如下,这个结构在我理解相当于进程描述符的作用

点击(此处)折叠或打开

  1. /*
  2. *********************************************************************************************************
  3. * TASK CONTROL BLOCK
  4. *********************************************************************************************************
  5. */

  6. typedef struct os_tcb {
  7.     OS_STK *OSTCBStkPtr; /* Pointer to current top of stack */

  8. #if OS_TASK_CREATE_EXT_EN > 0
  9.     void *OSTCBExtPtr; /* Pointer to user definable data for TCB extension */
  10.     OS_STK *OSTCBStkBottom; /* Pointer to bottom of stack */
  11.     INT32U OSTCBStkSize; /* Size of task stack (in number of stack elements) */
  12.     INT16U OSTCBOpt; /* Task options as passed by OSTaskCreateExt() */
  13.     INT16U OSTCBId; /* Task ID (0..65535) */
  14. #endif

  15.     struct os_tcb *OSTCBNext; /* Pointer to next TCB in the TCB list */
  16.     struct os_tcb *OSTCBPrev; /* Pointer to previous TCB in the TCB list */

  17. #if OS_EVENT_EN
  18.     OS_EVENT *OSTCBEventPtr; /* Pointer to event control block */
  19. #endif

  20. #if ((OS_Q_EN > 0) && (OS_MAX_QS > 0)) || (OS_MBOX_EN > 0)
  21.     void *OSTCBMsg; /* Message received from OSMboxPost() or OSQPost() */
  22. #endif

  23. #if (OS_VERSION >= 251) && (OS_FLAG_EN > 0) && (OS_MAX_FLAGS > 0)
  24. #if OS_TASK_DEL_EN > 0
  25.     OS_FLAG_NODE *OSTCBFlagNode; /* Pointer to event flag node */
  26. #endif
  27.     OS_FLAGS OSTCBFlagsRdy; /* Event flags that made task ready to run */
  28. #endif

  29.     INT16U OSTCBDly; /* Nbr ticks to delay task or, timeout waiting for event */
  30.     INT8U OSTCBStat; /* Task status */
  31.     BOOLEAN OSTCBPendTO; /* Flag indicating PEND timed out (OS_TRUE == timed out) */
  32.     INT8U OSTCBPrio; /* Task priority (0 == highest) */

  33.     INT8U OSTCBX; /* Bit position in group corresponding to task priority */
  34.     INT8U OSTCBY; /* Index into ready table corresponding to task priority */
  35. #if OS_LOWEST_PRIO <= 63
  36.     INT8U OSTCBBitX; /* Bit mask to access bit position in ready table */
  37.     INT8U OSTCBBitY; /* Bit mask to access bit position in ready group */
  38. #else
  39.     INT16U OSTCBBitX; /* Bit mask to access bit position in ready table */
  40.     INT16U OSTCBBitY; /* Bit mask to access bit position in ready group */
  41. #endif

  42. #if OS_TASK_DEL_EN > 0
  43.     INT8U OSTCBDelReq; /* Indicates whether a task needs to delete itself */
  44. #endif

  45. #if OS_TASK_PROFILE_EN > 0
  46.     INT32U OSTCBCtxSwCtr; /* Number of time the task was switched in */
  47.     INT32U OSTCBCyclesTot; /* Total number of clock cycles the task has been running */
  48.     INT32U OSTCBCyclesStart; /* Snapshot of cycle counter at start of task resumption */
  49.     OS_STK *OSTCBStkBase; /* Pointer to the beginning of the task stack */
  50.     INT32U OSTCBStkUsed; /* Number of bytes used from the stack */
  51. #endif

  52. #if OS_TASK_NAME_SIZE > 1
  53.     INT8U OSTCBTaskName[OS_TASK_NAME_SIZE];
  54. #endif
  55. } 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时才会用到
 
阅读(3276) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~