* whether the placement of "volatile" makes a difference
@ 2006-11-01 10:19 Robert P. J. Day
0 siblings, 0 replies; only message in thread
From: Robert P. J. Day @ 2006-11-01 10:19 UTC (permalink / raw)
To: C programming list
from the definition of an "atomic" variable in the linux kernel,
there are numerous arch-specific header files that define an atomic
variable in one of two ways:
1) typedef struct { int counter; } atomic_t;
2) typedef struct { volatile int counter; } atomic_t;
the purported need for the "volatile" type qualifier is to guarantee
that the (gcc) compiler doesn't try to do anything clever with code
generation, of course, which makes sense.
the problem is that some architectures add the "volatile" qualifier,
while others don't. but whether it's necessary isn't just related to
the architecture, is it? wouldn't it be related to just the
properties of the compiler itself?
second, even if an architecture didn't strictly *need* that
qualifier, would it hurt to have it there? surely having an
unnecessary "volatile" qualifier couldn't break code that would
otherwise work, could it? it would only prevent some possible
optimization, no?
and, finally, in some of those header files, even if the typedef
doesn't use "volatile", some of the function definitions do:
static inline int atomic_add_return (int i, volatile atomic_t *v) {
^^^^^^^^
i find this curious. in this particular header file, while the
typedef doesn't use the type qualifier "volatile", every routine that
refers to that typedef *does*. would that be considered equivalent?
that is, would the compiler treat identically the cases where either
1) the typedef itself has a volatile member, or 2) every reference is
to a such a typedef which is declared "volatile". (and on a related
note, would that mean that having "volatile" in both places would be
utterly redundant? and it would make more sense to just centralize
that qualifier in a single place, in the typedef?)
thanks.
rday
^ permalink raw reply [flat|nested] only message in thread
only message in thread, other threads:[~2006-11-01 10:19 UTC | newest]
Thread overview: (only message) (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2006-11-01 10:19 whether the placement of "volatile" makes a difference Robert P. J. Day
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).