Important changes to forums and questions
All forums and questions are now archived. To start a new conversation or read the latest updates go to forums.mbed.com.
10 years, 5 months ago.
No critical setion protection in ring buffer management?
Hi, EA has written many sample projects for LPC812, which teaches me a lot in C++ programming. Thanks.
I am looking for a general purpose application protocol over serial, you can find my post in forum
The xbee code is ideal for my reference. I have only question regarding this project.
There is a ring buffer inside xbee class. Normally access ring buffer requires critical section mutex. In regular C programming, we will add two MACROs, one is CRITICAL_SECTION_ENTER, the other is CRITICAL_SECTION_EXIT. Basically they disable/enable irq.
I usually uses AVRLib in programming on AVR.
It is all right to skip them in your code?Have you ever lost some data during transmission?
Additionally, since Cortex-M0 is complext than AVR/8051, these MACROs have to be atomic operations, otherwise it still have some problems.
In general, to be on the safe side you probably need critical sections since C/C++ really doesn't guarantee any atomic operations. If you look closer at the code you refer to you see that you basically have two parts which can access the buffer in parallel: the uart irq and the application calling the function process() (only one thread should call process).
The uart irq will interrupt the process() call, but the process() call will not interrupt the uart irq.
The uart irq is calling uartRxQPut() which is the only function modifying rxqIn. The process() call is calling uartRxQGet() which is the only function modifying rxqOut. So what could happen if the uart irq interrupts process().
- uartRxQGet is about to fetch data and increase rxqOut, but is interrupted before rxqOut is changed. The irq calls uartRxQPut which in this scenario sees a full buffer. Would this have been improved by disabling interrupts in uartRxQGet? Probably not since having a full buffer when receiving data in general means that you either have a too small buffer or an application not processing received data fast enough.
posted by EmbeddedArtists AB 25 Jun 2014Your explanation is very informative. I got it. Thank you very much.
posted by Kai Liu 25 Jun 2014