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

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

Re: [avr-libc-dev] [bug #14616] definition of sei() in interrupts.h is f


From: Russell Shaw
Subject: Re: [avr-libc-dev] [bug #14616] definition of sei() in interrupts.h is faulty
Date: Tue, 27 Sep 2005 10:43:34 +1000
User-agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.7.11) Gecko/20050914 Debian/1.7.11-1

anonymous wrote:
Follow-up Comment #5, bug #14616 (project avr-libc):

The unfortunate thing is, that using volatile is not enough. Even if it works
for your extremely simple test case: I am having a more complex test case
example in my application code, where it does *not* work because gcc reorders
the statements. And so as I understand the specification of gcc's inline asm
statement, gcc has completely the right to reorder the statements as long as
we do not communicate the possible side effects.

I am convinced that You as well will be unhappy with the present
implementation if you discover one day that code segments that used to work
perfectly do no longer work because you had inserted a completely unrelated
line of code somewhere in the application:  A line of code that makes gcc
consider it to be useful to reorder the code!

FYI: I had started with code like

{
  uint8_t temp = SREG;
  asm volatile ("cli" : :);
  volatile_declared_uint16_t_var_in_memory = value;
  SREG = temp;
}

Here gcc had reordered it such that it would read

{
  uint8_t temp = SREG;
  asm volatile ("cli" : :);
  SREG = temp;
  volatile_declared_uint16_t_var_in_memory = value;
}

I then tried to use brute force by doing
{
  asm volatile ("cli" : :);
  volatile_declared_uint16_t_var_in_memory = value;
  asm volatile ("sei" : :);
}
and also this ended up at reordered like
{
  asm volatile ("cli" : :);
  asm volatile ("sei" : :);
  volatile_declared_uint16_t_var_in_memory = value;
}
. Only when adding the "memory" clobbers to the spec. I obtained true
protection. And IMO adding the clobbers *is* correct since indeed the sei
instruction *has* possible unpredictable side effects on the memory, like in
my case where the IRQ did write the variable!
All this is what draws me to the conclusion that the present macros are *not*
safe. IMO one must not make the mistake to derive from the fact that simple
test cases work that it works all the time :-(.

Bjoern.

Linux code has rmb() and wmb() for "read memory barrier" and "write memory 
barrier",
to solve that reordering problem. Something might be learned from finding how 
those
macros are defined.

http://www.xml.com/ldd/chapter/book/ch08.html




reply via email to

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