当前位置:网站首页>Protection of RT thread critical section

Protection of RT thread critical section

2022-06-22 07:05:00 1ShyJn25

Protection of critical section

What is critical section

Critical section , In one sentence, it is a code segment that cannot be interrupted during execution . stay RT-Thread in , The most common scenario for critical segments is the operation of global variables , Global variables are like a target , Anyone can shoot him , But one shot , No one else can shoot , Otherwise, I don't know who hit the target .
So when will the critical section be interrupted ? One is system scheduling , There is also an external interrupt . stay RT-Thread in , Finally, system scheduling is also generated PendSV interrupt , stay PendSV Handler Implementation of thread switching in , So it can be reduced to interruption . In this case ,RT-Thread The protection of critical sections is simply handled , Turn off the interrupt directly , but NMI FAULT And hard FAULT With the exception of .

Cortex-M Kernel fast shutdown interrupt instruction
In order to switch off the interrupt quickly ,Cortex-M The kernel has a special CPS Instructions , Yes 4 Usage .
Code list CPS Instruction usage

CPSID I ;PRIMASK=1   ; Close the interrupt 
CPSIE I ;PRIMASK=0   ; Open the interrupt 
CPSID F ;FAULTMASK=1  ; Turn off the abnormality 
CPSIE F ;FAULTMASK=0  ; Open abnormally 

In the code listing PRIMASK and FAULTMAST yes Cortex-M The kernel 3 In interrupt mask registers 2 individual , The other one is BASEPRI, About this 3 The detailed usage of registers is shown in the table .

 Insert picture description here

Close the interrupt

RT-Thread The function that closes the interrupt is contex_rvds.s In the definition of , stay rthw.h In a statement , See the code list for the specific implementation 4-2.
Close the interrupt

/* ; * rt_base_t rt_hw_interrupt_disable(); ; */
rt_hw_interrupt_disable  PROC          
  EXPORT rt_hw_interrupt_disable        
  MRS   r0, PRIMASK              
  CPSID  I                   
  BX   LR                  
  ENDP    

1): keyword PROC Indicates that the assembly subroutine starts .
2): Use EXPORT Keyword export label rt_hw_interrupt_disable, Make it global , After the external header file declaration ( stay rthw.h In a statement ), You can go to C Call in file .
3): adopt MRS The instruction will special register PRIMASK The value of the register is stored in a general-purpose register r0. When in C When the subroutine called in the assembly returns , Will r0 As the return value of the function . So in C Call in rt_hw_interrupt_disable() when , You need to declare a variable in advance to store rt_hw_interrupt_disable() The return value of , namely r0 Register value , That is to say PRIMASK Value .
4): Turn off interrupt , That is to use CPS Instructions will be PRIMASK Set the value of the register 1.
5): Subprogram return .
6):ENDP Indicates the end of the assembly subroutine , And PROC Use in pairs .

Open the interrupt

RT-Thread The interrupt function is in the contex_rvds.s In the definition of , stay rthw.h In a statement .
Open the interrupt

/* * void rt_hw_interrupt_enable(rt_base_t level); */
rt_hw_interrupt_enable  PROC            
  EXPORT rt_hw_interrupt_enable          
  MSR   PRIMASK, r0               
  BX   LR                    
  ENDP       

1): keyword PROC Indicates that the assembly subroutine starts .
2): Use EXPORT Keyword export label rt_hw_interrupt_enable, Make it global , After the external header file declaration ( stay rthw.h In a statement ), You can go to C Call in file .
3): adopt MSR The instruction will the general purpose register r0 The value of is stored in a special register PRIMASK.
4): Subprogram return .
5):ENDP Indicates the end of the assembly subroutine , And PROC Use in pairs .

Application of critical segment code

Before entering the critical section , We will turn off the interrupt first , When exiting the critical section, open the interrupt , and Cortex-M The kernel has set the fast shutdown interrupt CPS Instructions , The implementation of switch interrupt function and the protection of critical segment code .

 The implementation of switch interrupt function and the protection of critical segment code 
 Implementation of switch interrupt function 
/* * void rt_hw_interrupt_disable(); */
rt_hw_interrupt_disable  PROC
 EXPORT rt_hw_interrupt_disable
  CPSID  I    
  BX   LR
  ENDP

;/* ; * void rt_hw_interrupt_enable(void); ; */
rt_hw_interrupt_enable  PROC
  EXPORT rt_hw_interrupt_enable
  CPSIE  I    
  BX   LR
  ENDP
PRIMASK = 0;           /* PRIMASK The initial value is 0, It means no interruption  */    

/*  Critical segment code protection  */
{
    
  /*  The critical section begins  */
  rt_hw_interrupt_disable();   /*  Close the interrupt ,PRIMASK = 1 */    
  {
    
    /*  Execute critical code , Non interruptible  */    
  }
  /*  The critical section ends  */
    rt_hw_interrupt_enable();   /*  Open the interrupt ,PRIMASK = 0 */   
}
原网站

版权声明
本文为[1ShyJn25]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/173/202206220702018920.html