Coldfire (MCF5235) Interrupt Problem

Andrei Chichak groups at chichak.ca
Fri May 11 18:05:21 UTC 2012


On 2012-May-11, at 11:45 AM, Claus, Ric wrote:

> BTW, what is the purpose of the "do {...} while(0)" construct?  I've seen it used several times in RTEMS code and don't understand its purpose.  Is there some side-effect I'm not aware of?  Otherwise, why not use "{...}" by itself to introduce a new scope block?  Please educate me.
> 
> Ric


The do while(0) in a macro is a rare but useful construct;

Consider:

#define TPUInterruptSetHandler(a,b,c) \
rtems_interrupt_disable(a); \
rtems_interrupt_enable(a)

Then:

if (cond) 
	TPUInterruptSetHandler( 42, 17, "Hike");

You get something that doesn't quite do what you want. Only the first line of the macro will be conditional.

Wrap the define in a do/while(0) and it executes exactly once, but it becomes essentially a single statement and can handle being put in an ill formed if. This is probably why the MISRA guidelines say to always put curlies on ifs even though they are one liners.

Andrei








More information about the users mailing list