GCC and SMP problems

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Re: GCC and SMP problems

Post by Solar »

gravaera wrote:I had always assumed that keywords and storage class keywords applied to the entire declaration/definition...
Funny... it was one of the first things in the books I read, the differences between

Code: Select all

int * i1;

const int * i2;

int const * i3;

int * const i4;

int const * const i5;
For anyone not following the article links: i2 and i3 are identical, pointers to constant integers. In i4, you have a constant pointer to a non-const integer (you can change the integer but not the pointer, so you better initialize it right away), and in i5 you have a constant pointer to a constant integer (you can change neither).

Same goes for volatile.

gravaera, what you probably remembered meant are static, extern and auto, which indeed work for the whole declaraction.

PS: And if you take a minute looking at the code lines above, you can figure out why I always write it as with i3, and never as with i2 (if I can help it - I went with the standard declarations in PDCLib).
Every good solution is obvious once you've found it.
User avatar
gravaera
Member
Member
Posts: 737
Joined: Tue Jun 02, 2009 4:35 pm
Location: Supporting the cause: Use \tabs to indent code. NOT \x20 spaces.

Re: GCC and SMP problems

Post by gravaera »

@Solar: Thanks :)

I'm always glad to learn something new, or improve something where I was faltering. I suppose a bit more reading is in order, but you've laid it out rather clearly for me.

--All the best
gravaera.
17:56 < sortie> Paging is called paging because you need to draw it on pages in your notebook to succeed at it.
User avatar
Darwish
Posts: 21
Joined: Sat Oct 17, 2009 4:32 am

"Volatile-considered harmful" (was:Re: GCC and SMP problems)

Post by Darwish »

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.
For latest news, please check my homepage and my blog.
—Darwish
earlz
Member
Member
Posts: 1546
Joined: Thu Jul 07, 2005 11:00 pm
Contact:

Re: "Volatile-considered harmful" (was:Re: GCC and SMP problems)

Post by earlz »

Darwish wrote:
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.

I would assume your not going to have any hope of compiling your kernel with anything but gcc then?
User avatar
Darwish
Posts: 21
Joined: Sat Oct 17, 2009 4:32 am

Re: "Volatile-considered harmful" (was:Re: GCC and SMP problems)

Post by Darwish »

earlz wrote:I would assume your not going to have any hope of compiling your kernel with anything but gcc then?
Why? If the other compiler provides a compile-time memory barrier (and if it does aggressive reordering, it should), you can abstract this difference away in a macro inside cpu_relax() above.
For latest news, please check my homepage and my blog.
—Darwish
Post Reply