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: Richard Urwin
Subject: Re: [avr-gcc-list] generic queue library for AVR GCC?
Date: Tue, 16 Nov 2004 20:35:16 +0000
User-agent: KMail/1.5.3

On Tuesday 16 Nov 2004 7:36 pm, Geoffrey Wossum wrote:
> On Tuesday 16 November 2004 11:53 am, Richard Urwin wrote:
> > 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;
> >  }
> > }
>
> This doesn't look like it would handle nested critical sections. 

Correct.

> You could probably fix it in the majority of 
> usages by adding a counter, though.  You only store SREG in s when
> the counter is 0, and you only restore SREG when counter is being
> decremented back to 0.

Which moves away from fully optimised code. 

Howabout:

typedef unsigned char critical_section;

static inline void begin_critical_section(critical_section * s) {
        *s = SREG;
        cli();
    }

static inline void end_critical_section(critical_section * s) {
        SREG = *s;
    }

Then you can put all your variable definitions away at the top of the 
function they are used in, and call begin and end symmetrically.

> you still have the problem of what if you put a return in statements.  
> You'd leave the critical section without restoring interrupts. 

That was the advantage of my previous solution that used #define to open 
and close braces around the section - but that constrains you more.

-- 
Richard Urwin


reply via email to

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