FlashBurn wrote:So, problems solved. You have to write the following and then it works:
Code: Select all
static struct smpMsg_t * volatile freeList
So maybe someone could say where should the volatile keyword stay? I always though that "volatile uint32t foo" is alright, isn´t it. Or is this only not right for pointers?
Volatiles are really subtle in C, and as seen by the ACM
paper posted by dosfan above, many compilers get them wrong. For above coding case, I prefer the Linux way of handling the issue, namely
minimizing 'volatile' as far as possible, and depending on compiler and/or cpu
barriers as needed.
In above case, you can simply avoid volatile altogether, and use the gcc barrier "memory". As said by its
manual, the memory barrier "will cause GCC to not keep memory values cached in registers across the assembler instruction and not optimize stores or loads to that memory." So in above code, you can use:
Code: Select all
while(likely(freeList == 0))
asm volatile("pause": : :"memory");
and have peace.
In the risk of mimicking the linux kernel too much, you can create a cpu_relax() method, which abstracts the 'asm volatile("pause": :"memory")' part and use it in all your busy-loops. For further info check:
1- Linux kernel
Documentation/volatiles-considered-harmful.txt
2- Linus's posts on volatile.
Original patch,
reply 1, and
reply 2.