public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: Chris Snook <csnook@redhat.com>
To: Francis Moreau <francis.moro@gmail.com>
Cc: linux-kernel@vger.kernel.org
Subject: Re: Question about C language.
Date: Sun, 23 Mar 2008 03:32:03 -0400	[thread overview]
Message-ID: <47E60773.2040409@redhat.com> (raw)
In-Reply-To: <47E60702.30302@redhat.com>

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

  reply	other threads:[~2008-03-23  7:32 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2008-03-21 15:46 Question about C language Francis Moreau
2008-03-23  7:30 ` Chris Snook
2008-03-23  7:32   ` Chris Snook [this message]
2008-03-24  8:25     ` Francis Moreau
2008-03-24  8:24   ` Francis Moreau

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=47E60773.2040409@redhat.com \
    --to=csnook@redhat.com \
    --cc=francis.moro@gmail.com \
    --cc=linux-kernel@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox