linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* volatile variable
@ 2003-08-01 10:57 Dinesh  Gandhewar
  2003-08-01 11:38 ` Richard B. Johnson
  0 siblings, 1 reply; 10+ messages in thread
From: Dinesh  Gandhewar @ 2003-08-01 10:57 UTC (permalink / raw)
  To: mlist-linux-kernel

Hello,

If a system call is having following code.

add current process into wait quque ;
while (1)
{  set task state INTERRUPTIBLE ;
    if (a > 0)
      break ;
    schedule() ;
}
set task state RUNNING ;
remove current from wait queue ;


If an interrupt service is having following code

set a = 512 ;

'a' is a global variable shared in ISR and system call

Do I need to define a as 'volatile int a ;' ? Why?

Thanks & Regards,
Dinesh




___________________________________________________
Download the hottest & happening ringtones here!
OR SMS: Top tone to 7333
Click here now: 
http://sms.rediff.com/cgi-bin/ringtone/ringhome.pl



^ permalink raw reply	[flat|nested] 10+ messages in thread
* Re: volatile variable
@ 2003-08-02 14:52 Harm Verhagen
  0 siblings, 0 replies; 10+ messages in thread
From: Harm Verhagen @ 2003-08-02 14:52 UTC (permalink / raw)
  To: lkml

>On Fri, 1 Aug 2003, Dinesh  Gandhewar wrote:
>
>> Hello,
>>
>> If a system call is having following code.
>>
>> add current process into wait quque ;
>> while (1)
>> {  set task state INTERRUPTIBLE ;
>>     if (a > 0)
>>       break ;
>>     schedule() ;
>> }
>> set task state RUNNING ;
>> remove current from wait queue ;
>>
>> 'a' is a global variable shared in ISR and system call
>

Dick Johnson wrote:

>In any event in your loop, variable 'a', has already been
>read by the code the compiler generates. There is nothing
>else in the loop that touches that variable. Therefore
>the compiler is free to (correctly) assume that whatever
>it was when it was first read is what it will continue to
>be. The compiler will therefore optimise it to be a single
>read and compare. So, the loop will continue forever if
>'a' started as zero because the compiler correctly knows
>that it cannot possibly change in the only execution
>path that it knows about.

This is incorrect.
If variable 'a' is a _global_ variable the compiler cannot (and will
not) assume it is
not changed in the loop. (The function call to schedule() might well
change the global, from the compiler point of view)
It will be reread every loop, even without beeing volatile.
When you have local variables that are/contain pointers to some data,
you need to mark those data fields volatie to make sure they get reread.

regards,
Harm Verhagen
-- 
Harm Verhagen <h.verhagen@chello.nl>


^ permalink raw reply	[flat|nested] 10+ messages in thread

end of thread, other threads:[~2003-08-11 17:08 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2003-08-01 10:57 volatile variable Dinesh  Gandhewar
2003-08-01 11:38 ` Richard B. Johnson
2003-08-11 13:33   ` David Woodhouse
2003-08-11 14:06     ` Richard B. Johnson
2003-08-11 14:37       ` Jakub Jelinek
2003-08-11 14:38       ` David Woodhouse
2003-08-11 14:40       ` David Howells
2003-08-11 14:49       ` Mike Galbraith
2003-08-11 17:07       ` Robert Love
2003-08-02 14:52 Harm Verhagen

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).