Re: Strange code in 2.0.pre35 - apricot.c

Mike (ford@omnicron.com)
Sun, 12 Jul 98 01:09:19 -0700


> register int hold;
> do {
> hold = lp->scb.status;
> } while (lp->scb.command);
> The 'hold' variable is needed in both cases to prevent the optimizer
> from dropping the first clause, because some compilers don't [ ... ]

No, it's not. The assignment to hold is not any stronger of a reference
to scb.status than is an unsaved reference, and the above code is not
reliable, since the compiler is free to completely optimize away both
the reference to scb.status and the hold variable itself.

scb.status must be declared volatile if reading it has desirable side
effects, as is presumably the case in the original "strange" code. The
volatile keywoard does guarantee that references are not optimized away
even if the compiler can't see what purpose they have.

GCC's optimizer does in fact have the ability to optimize away the
"hold" example above, and does properly respect the volatile keyword as
required by the C standard. volatile is the right thing to use.

Side note: I don't actually see anywhere in apricot.c where it attempts
to declare this (or anything else) as volatile, so the code does seem
dubious.
-=] Ford [=-

"Look over there!... A dry ice (In Real Life: Mike Ditto)
factory -- a good place to get ford@omnicron.com
some thinking done." http://www.omnicron.com/~ford/ford.html
- Talking Heads, "Cities"

-
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@vger.rutgers.edu
Please read the FAQ at http://www.altern.org/andrebalsa/doc/lkml-faq.html