Also, on a somewhat related note, I think there may be a bug in the
<avr/atomic.h> header, specifically, in the “__iRestore” function which is
called to restore the interrupt state after the atomic block is finished.
There’s currently a memory barrier after SREG is restored, but I think the
barrier should be before in order to ensure that any pending stores are
completed _before_ restoring the interrupt state. If the barrier is after,
I’m not sure if there’s anything preventing operations on non-volatile
variables from being reordered with respect to the restoring of SREG.
It’s likely that this problem has never been encountered in the field
because the common practice is to declare all shared globals as “volatile”,
which therefore prevents accesses to them from being reordered with respect
to access of SREG, but it seems like a possible issue to me.
If you agree, I can submit a bug report or even make the fix myself once I
figure out the process of sending in a patch. This is actually my first
time participating in any type of collaborative open source project!