lkml.org 
[lkml]   [2008]   [Mar]   [23]   [last100]   RSS Feed
Views: [wrap][no wrap]   [headers]  [forward] 
 
Messages in this thread
/
Date
From
SubjectRe: Question about C language.
Chris Snook wrote:
> Francis Moreau wrote:
>> Hello,
>>
>> I know it's a bit out of topic but this is something I need to clarify
>> for
>> writing a Linux driver... hope you don't mind.
>>
>> In my driver I have a global variable that controls a loop such as:
>>
>> int my_condition;
>>
>> void change_my_condition(int new)
>> {
>> my_condition = new;
>> }
>>
>> int foo(void)
>> {
>> /* irqs are disabled */
>> my_condition = 1;
>> do {
>> ....
>> local_irq_enable();
>> cpu_sleep();
>> local_irq_disable();
>>
>> } while (my_condition);
>>
>> }
>>
>> This variable is modified by an interrupt handler define in another file
>> by using 'change_my_condition' function.
>>
>> By reading the ISO C99 specification, I _think_ that I needn't any
>> kind of barrier
>> or even use the volatile type qualifier for my_condition variable to
>> make a true
>> access to 'my_condition' in the controlling expression of the while,
>> but I'm not
>> sure.
>>
>> Coud anybody confirm ?
>>
>> Thanks,
>
> Even volatile may be insufficient with some architecture/compiler
> combinations. You should use explicit barriers wherever you need them,
> or Bad Things will happen.
>
> -- Chris
>

Oops, forgot to mention, you should use atomic_t, to avoid aliasing
problems, and ALSO use explicit barriers wherever you need them.

-- Chris


\
 
 \ /
  Last update: 2008-03-23 08:35    [W:0.042 / U:2.312 seconds]
©2003-2020 Jasper Spaans|hosted at Digital Ocean and TransIP|Read the blog|Advertise on this site