Ucos ii Task Management 3: delete a task
With the previous experience, mcu21 decided to comprehensively learn about task management. In this way, project programming can be implemented with confidence ".
After creating a task in ucos ii, you can perform the following operations on the task:
1. Delete the task ostaskdel ()
2. Suspend the task ostasksuspend ()
3. Restore task ostaskresume ()
4. Change the task priority ostaskchangeprio ()
5. Extract task information ostaskquery ()
Let's learn how to delete a task today.
Deleting a task means that the task is returned and in sleep state, and the task code is no longer called by μC/OS-ⅱ.
Deleting a task is not the code for deleting a task.
At first glance, deleting a task is similar to suspending a task. In fact, there are essential differences. Basically, I think the biggest difference is that deleting a task has operations on the task control block. We know that when creating a task, we need to assign a task control block to each task, which stores important information about the task. It plays a vital role in switching between tasks.
If a task is suspended, the task control block is not moved to the control block of the task, but the task control block is removed from the ostcblist linked list to the ostcbfreelist.
The following describes how to delete a task.
|
Int8u ostaskdel (int8u PRIO) |
|
{ |
|
OS _tcb * ptcb; |
|
OS _event * pevent; |
|
|
|
If (PRIO = OS _idle_prio) {(1) |
|
Return (OS _task_del_idle ); |
|
} |
|
If (PRIO> = OS _lowest_prio & PRIO! = OS _prio_self) {(2) |
|
Return (OS _prio_invalid ); |
|
} |
|
OS _enter_critical (); |
|
If (osintnesting> 0) {(3) |
|
OS _exit_critical (); |
|
Return (OS _task_del_isr ); |
|
} |
|
If (PRIO = OS _prio_self) {(4) |
|
Prio = ostcbcur-> ostcbprio; |
|
} |
|
If (ptcb = ostcbpriotbl [PRIO])! = (OS _tcb *) 0) {(5) |
|
If (osrdytbl [ptcb-> ostcby] & = ~ Ptcb-> ostcbbitx) = 0) {(6) |
|
Osrdygrp & = ~ Ptcb-> ostcbbity; |
|
} |
|
If (pevent = ptcb-> ostcbeventptr )! = (OS _event *) 0) {(7) |
|
If (pevent-> oseventtbl [ptcb-> ostcby] & = ~ Ptcb-> ostcbbitx) = 0 ){ |
|
Pevent-> oseventgrp & = ~ Ptcb-> ostcbbity; |
|
} |
|
} |
|
Ptcb-> ostcbdly = 0; (8) |
|
Ptcb-> ostcbstat = OS _stat_rdy; (9) |
|
Oslocknesting ++; (10) |
|
OS _exit_critical (); (11) |
|
Osdummy (); (12) |
|
OS _enter_critical (); |
|
Oslocknesting --; (13) |
|
Ostaskdelhook (ptcb); (14) |
|
Ostaskctr --; |
|
Ostcbpriotbl [PRIO] = (OS _tcb *) 0; (15) |
|
If (ptcb-> ostcbprev = (OS _tcb *) 0) {(16) |
|
Ptcb-> ostcbnext-> ostcbprev = (OS _tcb *) 0; |
|
Ostcblist = ptcb-> ostcbnext; |
|
} Else { |
|
Ptcb-> ostcbprev-> ostcbnext = ptcb-> ostcbnext; |
|
Ptcb-> ostcbnext-> ostcbprev = ptcb-> ostcbprev; |
|
} |
|
Ptcb-> ostcbnext = ostcbfreelist; (17) |
|
Ostcbfreelist = ptcb; |
|
OS _exit_critical (); |
|
Ossched (); (18) |
|
Return (OS _no_err ); |
|
} Else { |
|
OS _exit_critical (); |
|
Return (OS _task_del_err ); |
|
} |
|
} |
After carefully reading the above program, we can see that (1) (2) (3) (4) (5) (6) (7) is basically the same as the function used to resume a task. It reflects the integrity of ucos ii code.
The process is roughly as follows:
1. Remove the ready table from the task to be deleted.
2. Remove the pending table from the task to be deleted.
3. prevent other tasks or ISR from calling ostaskresume ()
4. Stop task scheduling to prevent switching to other tasks during Task deletion.
The above are foreplays, and the next is the climax, you know.
5. Reduce the task counter of μCOS-ⅱ. Simply point the OS _tcb pointer to the deleted task to null to remove OS _tcb from the priority table.
6. Remove the OS _tcb of the deleted task from the OS _tcb bidirectional linked list.
After completing the preceding steps, you can delete a task.