Task Execution
(slides are based on Prof. Dr. Jian-Jia Chen and http://www.freertos.org)
Dr. Kuan-Hsun Chen
LS 12, TU Dortmund
December 5, 2019
Dr. Kuan-Hsun Chen (LS 12, TU Dortmund) 1 / 15
Task Creation (Recall)
• All the related information of a task is stored in a task control block (TCB) so
that the operating systems can use it for operating on a task.
Top of Stack pointer to the last item place on the stack for the task
Task State list item used to place the TCB in ready and blocked queues
Event List list item used to place the TCB in the event lists
Priority task priority (0=lowest)
Stack Start pointer to the start of the process stack
Others other information
Dr. Kuan-Hsun Chen (LS 12, TU Dortmund) 2 / 15
Task Execution Sequence
[http://www.freertos.org]
Dr. Kuan-Hsun Chen (LS 12, TU Dortmund) 3 / 15
Task Execution - Prior to the RTOS tick interrupt
[http://www.freertos.org]
Dr. Kuan-Hsun Chen (LS 12, TU Dortmund) 4 / 15
Task Execution - RTOS tick interrupt occurs
[http://www.freertos.org]
Dr. Kuan-Hsun Chen (LS 12, TU Dortmund) 5 / 15
Task Execution - RTOS tick interrupt is executed
[http://www.freertos.org]
Dr. Kuan-Hsun Chen (LS 12, TU Dortmund) 6 / 15
Incrementing the Tick Count
• vTaskIncrementTick() executes after the TaskA context has been saved.
• TaskB has higher priority than TaskA and is ready to run.
• vTaskSwitchContext() selects TaskB as the task to be given processing time when
the ISR completes.
Dr. Kuan-Hsun Chen (LS 12, TU Dortmund) 7 / 15
Task Execution - TaskB stack pointer is retrieved
[http://www.freertos.org]
Dr. Kuan-Hsun Chen (LS 12, TU Dortmund) 8 / 15
Task Execution - Restore the context of TaskB
[http://www.freertos.org]
Dr. Kuan-Hsun Chen (LS 12, TU Dortmund) 9 / 15
Task Execution - The RTOS tick exits
[http://www.freertos.org]
Dr. Kuan-Hsun Chen (LS 12, TU Dortmund) 10 / 15
Task Control Block and Task Execution
• Central Processing Unit (CPU)
• Program Counter (PC), Stack Pointer (SP), Registers (Reg)
• Task Control Block (TCB)
• Stack pointer, Waiting time (Tme), priority (Prio)
Dr. Kuan-Hsun Chen (LS 12, TU Dortmund) 11 / 15
Task Control Block in FreeRTOS - struct tskTaskControlBlock
1t y p e d e f s t r u c t t s k T a s k C o n t r o l B l o c k {
2 / ∗ < P o i n t s t o t h e l o c a t i o n o f t h e l a s t i t e m p l a c e d o n t h e s t a c k . ∗ /
3 v o l a t i l e p o r t S T A C K _ T Y P E ∗ p x T o p O f S t a c k ;
4 . . .
5 / ∗ < L i s t i t e m u s e d t o p l a c e t h e T C B i n r e a d y a n d b l o c k e d q u e u e s . ∗ /
6 x L i s t I t e m x G e n e r i c L i s t I t e m ;
7 / ∗ < L i s t i t e m u s e d t o p l a c e t h e T C B i n e v e n t l i s t s . ∗ /
8 x L i s t I t e m x E v e n t L i s t I t e m ;
9 / ∗ < T h e p r i o r i t y o f t h e t a s k w h e r e 0 i s t h e l o w e s t p r i o r i t y . ∗ /
10 u n s i g n e d p o r t B A S E _ T Y P E u x P r i o r i t y ; 11 / ∗ < P o i n t s t o t h e s t a r t o f t h e s t a c k . ∗ /
12 p o r t S T A C K _ T Y P E ∗ p x S t a c k ;
13 / ∗ < D e s c r i p t i v e n a m e g i v e n t o t h e t a s k w h e n c r e a t e d f o r d e b u g g i n g ∗ /
14 s i g n e d c h a r p c T a s k N a m e [ c o n f i g M A X _ T A S K _ N A M E _ L E N ] ; 15
16 . . . . . 17
18 } tskTCB ;
Dr. Kuan-Hsun Chen (LS 12, TU Dortmund) 12 / 15
xTaskGenericCreate
1s i g n e d p o r t B A S E _ T Y P E x T a s k G e n e r i c C r e a t e ( . . . ) {
2 . . .
3 t s k T C B ∗ p x N e w T C B ;
4 c o n f i g A S S E R T ( p x T a s k C o d e ) ;
5 c o n f i g A S S E R T ( ( ( u x P r i o r i t y & ( ˜ p o r t P R I V I L E G E _ B I T ) ) < ←- c o n f i g M A X _ P R I O R I T I E S ) ) ;
6 / ∗ A l l o c a t e t h e m e m o r y r e q u i r e d b y t h e T C B a n d s t a c k f o r t h e n e w t a s k , ←- c h e c k i n g t h a t t h e a l l o c a t i o n w a s s u c c e s s f u l . ∗ /
7 p x N e w T C B = p r v A l l o c a t e T C B A n d S t a c k ( u s S t a c k D e p t h , p u x S t a c k B u f f e r ) ;
8 if( p x N e w T C B != NULL ) {
9 . . . .
10 / ∗ S e t u p t h e n e w l y a l l o c a t e d T C B w i t h t h e i n i t i a l s t a t e o f t h e t a s k . ←-
∗ /
11 p r v I n i t i a l i s e T C B V a r i a b l e s ( p x N e w T C B , pcName , u x P r i o r i t y , x R e g i o n s , ←- u s S t a c k D e p t h ) ;
12 . . . .
Dr. Kuan-Hsun Chen (LS 12, TU Dortmund) 13 / 15
xTaskGenericCreate (cont.)
1 / ∗ We a r e g o i n g t o m a n i p u l a t e t h e t a s k q u e u e s t o a d d t h i s t a s k t o a ←- r e a d y l i s t , s o m u s t m a k e s u r e n o i n t e r r u p t s o c c u r . ∗ /
2 t a s k E N T E R _ C R I T I C A L ( ) ; { 3 u x C u r r e n t N u m b e r O f T a s k s ++;
4 if( p x C u r r e n t T C B == NULL ) {
5 / ∗ T h e r e a r e n o o t h e r t a s k s , o r a l l t h e o t h e r t a s k s a r e i n t h e ←-
s u s p e n d e d s t a t e − m a k e t h i s t h e c u r r e n t t a s k . ∗ /
6 p x C u r r e n t T C B = p x N e w T C B ;
7 . . . . }
8 e l s e{
9 / ∗ I f t h e s c h e d u l e r i s n o t a l r e a d y r u n n i n g , m a k e t h i s t a s k t h e ←- c u r r e n t t a s k i f i t i s t h e h i g h e s t p r i o r i t y t a s k t o b e ←- c r e a t e d s o f a r . ∗ /
10 if( x S c h e d u l e r R u n n i n g == p d F A L S E ) {
11 if( pxCurrentTCB−>u x P r i o r i t y <= u x P r i o r i t y ) {
12 p x C u r r e n t T C B = p x N e w T C B ; }
13 }
14 }
15 . . . .
Dr. Kuan-Hsun Chen (LS 12, TU Dortmund) 14 / 15
xTaskGenericCreate (cont.)
1 p r v A d d T a s k T o R e a d y Q u e u e ( p x N e w T C B ) ;
2 x R e t u r n = p d P A S S ;
3 . . . }
4 t a s k E X I T _ C R I T I C A L ( ) ;
5 }
6 . . .
7 if( x R e t u r n == pdPASS ) {
8 if( x S c h e d u l e r R u n n i n g != p d F A L S E ) {
9 / ∗ I f t h e c r e a t e d t a s k i s o f a h i g h e r p r i o r i t y t h a n t h e c u r r e n t ←-
t a s k t h e n i t s h o u l d r u n n o w . ∗ /
10 if( pxCurrentTCB−>u x P r i o r i t y < u x P r i o r i t y ) { 11 p o r t Y I E L D _ W I T H I N _ A P I ( ) ;
12 }
13 }
14 }
15 r e t u r n x R e t u r n ; 16 }
Let’s read the source code for a while.
Dr. Kuan-Hsun Chen (LS 12, TU Dortmund) 15 / 15