avr-libc-dev
[Top][All Lists]
Advanced

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

Re: [avr-libc-dev] "cli" and "sei" should clobber memory


From: Joerg Wunsch
Subject: Re: [avr-libc-dev] "cli" and "sei" should clobber memory
Date: Mon, 12 Dec 2005 23:01:36 +0100
User-agent: Mutt/1.4.2.1i

As Paulo Marques wrote:

> I have a 32 bit variable that is updated on an interrupt. I don't
> want to declare it volatile as it is _not_ volatile most of the
> time.

Then use two variables: one marked `volatile' that is used to
communicate between the main application and the ISR, and a "shadow"
variable elsewhere.

> I want to explicitly state the places where I want to do an "atomic"
> access to it.

IMHO you cannot do that.  `volatile' is not about atomic accesses but
about preventing the compiler from making implicit assemptions whether
a variable could have been modified or not.

> The problem is that gcc reorders around the "sei" instruction,
> resulting in this output:

>       cli();
>  22e: f8 94           cli
>       ret = *ptr;
>       sei();
>  230: 78 94           sei
>  232: 60 81           ld      r22, Z
>  234: 71 81           ldd     r23, Z+1        ; 0x01
>  236: 82 81           ldd     r24, Z+2        ; 0x02
>  238: 93 81           ldd     r25, Z+3        ; 0x03
>  23a: 08 95           ret

Note that GCC didn't reorder the sei instruction but the assignment.

> Changing the "sei" and "cli" definitions in avr/interrupt.h to:
(memory clobber)
> fixes the problem.

Sure, the memory clobber causes something like a "global volatile"
effective for *all* memory variables at these sequence points.  IOW,
it's a huge pessimization, and (IMHO) *way* worse than declaring your
single variable to be volatile, and shadow its value where the
volatileness isn't needed.

The original author of that bug report has meanwhile analyzed the
behaviour he saw as a bug in his respective version of GCC, and IIRC
admitted that no memory clobber for sei/cli is really needed.  At
least, that's what I've gathered from the previous discussion (I only
kept in mind that the bug report could basically be closed now).

> This seems like the correct thing to do since we want to make sure
> that accesses to variables between "cli" and "sei" are not affect by
> interrupts.

No, please mark these variables as volatile.  Sure, you might need to
interrupt-protect them anyway, depending on your application.

-- 
cheers, J"org               .-.-.   --... ...--   -.. .  DL8DTL

http://www.sax.de/~joerg/                        NIC: JW11-RIPE
Never trust an operating system you don't have sources for. ;-)





reply via email to

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