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.
Code 3 Task control block
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.
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. */
#if CFG_EVENT_EN > 0
OS_EventID eventID; /*!< Event ID. */
#if CFG_ROBIN_EN >0
U16 timeSlice; /*!< Task time slice */
#if CFG_STK_CHECKOUT_EN >0
OS_STK *stack; /*!< The top point of task. */
#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.*/
#if CFG_TASK_SCHEDULE_EN == 0
#if CFG_FLAG_EN > 0
void* pnode; /*!< Pointer to node of event flag. */
#if CFG_TASK_WAITTING_EN >0
U32 delayTick; /*!< The number of ticks which delay. */
struct TCB *TCBnext; /*!< The pointer to next TCB. */
struct TCB *TCBprev; /*!< The pointer to prev TCB. */
||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)
||The task priority that you assigned. Multiple tasks can share the same priority in CooCox CoOS.
||The state of the task
||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
||The mutex ID that the task waiting for
||The event ID that the task waiting for.
||The time slice of the task
|| A pointer to the bottom of a stack. It can be used to check the stack overflow
||The message pointer sent to the task
||The TCB of the next task in the event waiting list.
||The TCB of the previous task in the event waiting list
||Task function pointer, to active task
||A pointer to the current task's top of stack, to active task
||The pointer to the node of the event flag
||The time difference between the previous delayed event and the task when it is in the delayed state
||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.
||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.