Task control block
Task control block is a data structure used to save the state of a task in CooCox CoOS. Once a task has been created, CooCox CoOS will assign a task control block to describe it (as shown in code 3). This ensure the task can execute accurately when it obtains the CPU runtime again.
Task control block always accompanies with the task as a description snapshot. It will not be recovered by the system until the task being deleted.
Code 3 Task control block
typedef struct TCB
{
OS_STK *stkPtr; /*!< The current point of task. */
U8 prio; /*!< Task priority. */
U8 state; /*!< TaSk status. */
OS_TID taskID; /*!< Task ID. */
#if CFG_MUTEX_EN > 0
OS_MutexID mutexID; /*!< Mutex ID. */
#endif
#if CFG_EVENT_EN > 0
OS_EventID eventID; /*!< Event ID. */
#endif
#if CFG_ROBIN_EN >0
U16 timeSlice; /*!< Task time slice */
#endif
#if CFG_STK_CHECKOUT_EN >0
OS_STK *stack; /*!< The top point of task. */
#endif
#if CFG_EVENT_EN > 0
void* pmail; /*!< Mail to task. */
struct TCB *waitNext; /*!< Point to next TCB in the Event waitting list.*/
struct TCB *waitPrev; /*!< Point to prev TCB in the Event waitting list.*/
#endif
#if CFG_TASK_SCHEDULE_EN == 0
FUNCPtr taskFuc;
OS_STK *taskStk;
#endif
#if CFG_FLAG_EN > 0
void* pnode; /*!< Pointer to node of event flag. */
#endif
#if CFG_TASK_WAITTING_EN >0
U32 delayTick; /*!< The number of ticks which delay. */
#endif
struct TCB *TCBnext; /*!< The pointer to next TCB. */
struct TCB *TCBprev; /*!< The pointer to prev TCB. */
}
OSTCB,*P_OSTCB;
| stkPtr: |
A pointer to the current task's top of stack. CooCox CoOS allows each task to have its own stack of any size. During every task switches, CoOS saves the current CPU running state through the stack that stkPtr pointed to so that the task can come back to the previous running state when it gets the CPU runtime again. Since Cortex-M3 has 16 32-bit general-purpose registers to describe the CPU states, the minimum size of stack for a task is 68 bytes(other 4 bytes are used to checking stack overflow) |
| prio: |
The task priority that you assigned. Multiple tasks can share the same priority in CooCox CoOS. |
| state |
The state of the task |
| taskID |
The task ID that system assigned. Since multiple tasks can share the same priority, the priority can not be used as the unique identifier. We use task ID to distinguish different tasks in CooCox CoOS |
| mutexID |
The mutex ID that the task waiting for |
| eventID |
The event ID that the task waiting for. |
| timeSlice |
The time slice of the task |
| Stack |
A pointer to the bottom of a stack. It can be used to check the stack overflow |
| Pmail |
The message pointer sent to the task |
| waitNext |
The TCB of the next task in the event waiting list. |
| waitPrev |
The TCB of the previous task in the event waiting list |
| taskFuc |
Task function pointer, to active task |
| taskStk |
A pointer to the current task's top of stack, to active task |
| Pnode |
The pointer to the node of the event flag |
| delayTick |
The time difference between the previous delayed event and the task when it is in the delayed state |
| TCBnext |
The next TCB when a task is in the ready list / delayed list / mutex waiting list. Which list the task belongs to is determined by the task state and the item of the TCB. If the current task is in the ready state, it is in the ready list. If it is in the waiting state, then judged by the mutexID and delayTick: If mutexID is not 0xFFFFFFFF, it is in the mutex waiting list; else if delayTick is not 0xFFFFFFFF, it is in the delayed list. |
| TCBprev |
The previous TCB when a task is in the ready list /delayed list /mutex waiting list. Which list the task belongs to is determined by the task state and the relevant event flag |
System will assign a block to the current task from the current free TCB list while creating a task. The free TCB pointer is designated by FreeTCB in CooCox CoOS. If the FreeTCB is NULL, there is no TCB to assign and the task will fail to create.
While system initializing, CoOS will sort all the assignable TCB resources and then reflect the current state of the TCB through the forms of lists, as follows:
Every time you create a task successfully, the FreeTCB will be assigned to this task and its next item will be the new FreeTCB until it equals NULL. When a task is deleted or exited, the system will recover the TCB which had been assigned to this task when it was created and then assign it as the FreeTCB of the next time so as to reuse the resources of the deleted task.
|
|