Critical sections enter and exit

取消
显示结果 
显示  仅  | 搜索替代 
您的意思是: 

Critical sections enter and exit

1,609 次查看
kai_liu
Senior Contributor I

I am porting some drivers to KL25Z, which has ring buffer management. The original code has CRITICAL_SECTION_ENTER and CRITICAL_SECTION_EXIT macros, which  basically disable interrupt and enable interrupt.

But should we keep some critical register(s) before entering critical sections, and restore after exiting? And which registers should be saved? And any reference code? because I am not good at assembly.

Allan K Liu

标记 (1)
0 项奖励
回复
1 回复

1,106 次查看
bobpaddock
Senior Contributor III

If a simple single writer, single reader, ring buffer is turning IRQs on and off it is not implemented correctly.

Here is some C code that you can use:

WebSVN - Projects - Rev 230 - /common/CBUF.h

I cove what to watch out for in my Blog related to Queue code:

Software Safety: Even "Design Patterns" still have bugs. A common Queue bug.

Main point is to be aware of what the atomic size of an increment will be.  If multi-byte resisters  get split your code will have hard to reproduce bugs.