PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.c > question related to compiler optimization
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
question related to compiler optimization

Réponse
 
LinkBack Outils de la discussion
Vieux 28/11/2007, 06h43   #1
junky_fellow@yahoo.co.in
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut question related to compiler optimization

Guys,

Please have a look at the following piece of code:

int gcount = 0;
int gdelay = 0;

/* both gcount and gdelay are changed outside this function by an
interrupt handler code */

somefunc()
{
int count = gcount;
while (count == gcount || gdelay != 0) {
/*some handling */
}

}

I am quite sure that "gcount" should be declared volatile so that
compiler doesn't optimise the condition "count == gcount".
However, I am not sure if "gdelay" should also be declared to be
volatile ?
  Réponse avec citation
Vieux 28/11/2007, 07h01   #2
Ian Collins
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: question related to compiler optimization

junky_fellow@yahoo.co.in wrote:
> Guys,
>
> Please have a look at the following piece of code:
>
> int gcount = 0;
> int gdelay = 0;
>
> /* both gcount and gdelay are changed outside this function by an
> interrupt handler code */
>
> somefunc()
> {
> int count = gcount;
> while (count == gcount || gdelay != 0) {
> /*some handling */
> }
>
> }
>
> I am quite sure that "gcount" should be declared volatile so that
> compiler doesn't optimise the condition "count == gcount".
> However, I am not sure if "gdelay" should also be declared to be
> volatile ?


Any variable that can be changed in another context should be declared
extern. The compiler could see that gdelay is initialised to 0 and
optimise away the expression...

--
Ian Collins.
  Réponse avec citation
Vieux 28/11/2007, 07h11   #3
Podi
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: question related to compiler optimization

On Nov 27, 10:43 pm, "junky_fel...@yahoo.co.in"
<junky_fel...@yahoo.co.in> wrote:
> Guys,
>
> Please have a look at the following piece of code:
>
> int gcount = 0;
> int gdelay = 0;
>
> /* both gcount and gdelay are changed outside this function by an
> interrupt handler code */
>
> somefunc()
> {
> int count = gcount;
> while (count == gcount || gdelay != 0) {
> /*some handling */
> }
>
> }
>
> I am quite sure that "gcount" should be declared volatile so that
> compiler doesn't optimise the condition "count == gcount".
> However, I am not sure if "gdelay" should also be declared to be
> volatile ?


It does not hurt to declare both as volatile.
  Réponse avec citation
Vieux 28/11/2007, 07h24   #4
Ian Collins
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: question related to compiler optimization

Podi wrote:
> On Nov 27, 10:43 pm, "junky_fel...@yahoo.co.in"
> <junky_fel...@yahoo.co.in> wrote:
>> Guys,
>>
>> Please have a look at the following piece of code:
>>
>> int gcount = 0;
>> int gdelay = 0;
>>
>> /* both gcount and gdelay are changed outside this function by an
>> interrupt handler code */
>>
>> somefunc()
>> {
>> int count = gcount;
>> while (count == gcount || gdelay != 0) {
>> /*some handling */
>> }
>>
>> }
>>
>> I am quite sure that "gcount" should be declared volatile so that
>> compiler doesn't optimise the condition "count == gcount".
>> However, I am not sure if "gdelay" should also be declared to be
>> volatile ?

>
> It does not hurt to declare both as volatile.


No, it hurts if you don't!

By way of an example from the OP:

void somefunc()
{
int count = gcount;
while (count == gcount || gdelay != 0) {
count = 42;
}
}

When gcount and gdelay are not volatile, this is one compiler's assembly
output for somefunc:

somefunc:
cmpl $42,gcount
jne .CG3.15

--
Ian Collins.
  Réponse avec citation
Vieux 28/11/2007, 09h38   #5
Charlie Gordon
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: question related to compiler optimization

"Ian Collins" <ian-news@hotmail.com> a écrit dans le message de news:
5r4i2kF12scp7U9@mid.individual.net...
> junky_fellow@yahoo.co.in wrote:
>> Guys,
>>
>> Please have a look at the following piece of code:
>>
>> int gcount = 0;
>> int gdelay = 0;
>>
>> /* both gcount and gdelay are changed outside this function by an
>> interrupt handler code */
>>
>> somefunc()
>> {
>> int count = gcount;
>> while (count == gcount || gdelay != 0) {
>> /*some handling */
>> }
>>
>> }
>>
>> I am quite sure that "gcount" should be declared volatile so that
>> compiler doesn't optimise the condition "count == gcount".
>> However, I am not sure if "gdelay" should also be declared to be
>> volatile ?

>
> Any variable that can be changed in another context should be declared
> extern. The compiler could see that gdelay is initialised to 0 and
> optimise away the expression...


nonsense! As long as they are not static, variables defined at global scope
*have* extern linkage.
Furthermore, the interrupt code could be in the same module, for instance in
the form of a signal handler, and these global variables could be made
static then.

Both gcount and gdelay should be declared volatile to account for what the
OP is describing. Incidentally, it may not be sufficient if accessing them
is not guaranteed to be an atomic operation. See sections 7.14 on signal
handling for a discussion of type sig_atomic_t. 7.18.3p3 only guarantees
that type sig_atomic_t support at least 255 different values.

To make matters worse, if your "interrupt" code modifies both gcount and
gdelay, you need further non trivial code to ascertain that the values used
in somefunc() be consistent, because the "interrupt" could occur between the
access to one and the other, or even in the middle of such an access (from
the previous paragraph).

Interrupt handling, and thread programming, is *very* tricky to do right.
Your code may seem to work for a long time before conditions occur that make
it fail. It better not be at the heart of an anti-lock braking system, an
air-bag trigger, or any kind of critical device control unit... This group
is not the best place to discuss these issues, but I recommend that you use
proven techniques to deal with your "interrupt" handling issues.

--
Chqrlie.


  Réponse avec citation
Vieux 28/11/2007, 09h54   #6
Ian Collins
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: question related to compiler optimization

Charlie Gordon wrote:
> "Ian Collins" <ian-news@hotmail.com> a écrit dans le message de news:
> 5r4i2kF12scp7U9@mid.individual.net...
>> junky_fellow@yahoo.co.in wrote:
>>> Guys,
>>>
>>> Please have a look at the following piece of code:
>>>
>>> int gcount = 0;
>>> int gdelay = 0;
>>>
>>> /* both gcount and gdelay are changed outside this function by an
>>> interrupt handler code */
>>>
>>> somefunc()
>>> {
>>> int count = gcount;
>>> while (count == gcount || gdelay != 0) {
>>> /*some handling */
>>> }
>>>
>>> }
>>>
>>> I am quite sure that "gcount" should be declared volatile so that
>>> compiler doesn't optimise the condition "count == gcount".
>>> However, I am not sure if "gdelay" should also be declared to be
>>> volatile ?

>> Any variable that can be changed in another context should be declared
>> extern. The compiler could see that gdelay is initialised to 0 and
>> optimise away the expression...

>
> nonsense! As long as they are not static, variables defined at global scope
> *have* extern linkage.


Indeed it was, I thought volatile and typed extern....

--
Ian Collins.
  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 12h35.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,20130 seconds with 14 queries