Re: Disabling interrupts to protect data
- From: Stefan Reuther <stefan.news@xxxxxxxx>
- Date: Fri, 30 Oct 2009 20:54:21 +0100
David Brown wrote:
Stefan Reuther wrote:
All this is totally simple when you can use CLI/STI:
void atomic_swap(node** a, node** b, node* c) {
cli(); *a = *b; *b = c; sti();
}
// prepend new node to a list (easier than appending...)
node* list;
node* newNode;
atomic_swap(&newNode->next, &list, newNode);
(some strategically-placed 'volatile' can be appropriate.)
Here you have missed one important point that many people fail to
understand about interrupt disabling (and the related topic of volatile
accesses) - memory barriers. Unless your cli() and sti() macros /
assembly functions specifically include memory barriers, then your
atomic_swap function is not safe.
Yep. The declaration should've been
void atomic_swap(node*volatile* a, node*volatile* b, node* c);
I was just too lazy to figure out where to place the 'volatile's :-)
That, together with cli/sti macros acting as memory barriers (honestly,
cli/sti which are not such barriers are useless) fixes the routine for a
single processor system. Multiprocessor is a little harder.
In the atomic_swap function above, for example, the compiler will
probably see the cli() and sti() as volatile accesses (depending on the
compiler and the definition of these macros). But the code in the
middle does not use volatile accesses - the compiler can therefore
freely move these operations before or after the cli() and sti()
operations.
Actually, our compiler moved the accesses even though I used 'volatile'.
Fortunately the vendor fixed it quickly, but in the meantime I've made
an asm version to fix it forever.
Stefan
.
- References:
- Disabling interrupts to protect data
- From: KIRAN
- Re: Disabling interrupts to protect data
- From: D Yuniskis
- Re: Disabling interrupts to protect data
- From: Vladimir Vassilevsky
- Re: Disabling interrupts to protect data
- From: D Yuniskis
- Re: Disabling interrupts to protect data
- From: Stefan Reuther
- Re: Disabling interrupts to protect data
- From: D Yuniskis
- Re: Disabling interrupts to protect data
- From: Stefan Reuther
- Re: Disabling interrupts to protect data
- From: David Brown
- Disabling interrupts to protect data
- Prev by Date: Re: Progress indicators
- Next by Date: Re: Small Linux board
- Previous by thread: Re: Disabling interrupts to protect data
- Next by thread: Re: Disabling interrupts to protect data
- Index(es):
Relevant Pages
|