avr-gcc-list
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [avr-gcc-list] generic queue library for AVR GCC?


From: David Brown
Subject: Re: [avr-gcc-list] generic queue library for AVR GCC?
Date: Wed, 17 Nov 2004 10:33:03 +0100

> On Tuesday 16 Nov 2004 9:36 am, David Brown wrote:
> >     static inline unsigned char begin_critical_section(void) {
> >         unsigned char s = SREG;
> >         cli();
> >         return s;
> >     }
> >
>
> But you still have to declare a variable to hold the return value, and
> that was what broke the #define version.

What broke the #define version was the use of two statements in the one
macro without a covering "do { } while (0)" hack.

Somewhere, a variable has to be created to hold the old SREG value.  Using a
static variable won't work - it is not re-entrant safe, and if you have a
full overview over when your functions will be called, there is no need for
this protection - you can just use cli() and sli() as needed.

Some suggested macros have been putting it straight onto the stack - a risky
idea, IMHO.  Others (yours, Richard?  I've lost track of the thread...) have
used macros openning a new scope and explicitly defining a local variable.
Such macros have a definite advantage in that compilation will fail if you
don't match your open and close critical sections properly - however, they
would produce something horrible if you wanted to write "if (bSomeFlag)
begin_critical_section".

David


>
> Given an optimising compiler:
>
> typedef enum {cs_start, cs_end} cs_enum;
>
> static inline unsigned char critical_section(cs_enum dir)
> {
>         static unsigned char s;
>
>  switch(dir)
>  {
>  case cs_start:
>   s = SREG;
>          cli();
>   break;
>  case cs_end:
>   SREG = s;
>  }
> }
>
> Not much bug protection there though.
>
>
> --
> Richard Urwin
>
> _______________________________________________
> avr-gcc-list mailing list
> address@hidden
> http://www.avr1.org/mailman/listinfo/avr-gcc-list
>




reply via email to

[Prev in Thread] Current Thread [Next in Thread]