In article <FL********************@comcast.com>,
Ian Pilcher <i.*******@comcast.net> wrote:
It's pretty common to see declarations such as:
static volatile sig_atomic_t caught_signal = 0;
C99 defines sig_atomic_t as a "... (possibly volatile-qualified) integer
type of an object that can be accessed as an atomic entity, even in the
presence of asynchronous interrupts."
Does this mean that the use of "volatile" in the above declaration is
redundant? (It sure sounds that way to me.)
volatile tells the compiler that the value may have changed
between references, so that, for example,
a = (int) caught_signal;
b = (int) caught_signal;
is not necessarily going to be the same as
a = b = (int) caught_signal;
because there might have been a signal between the two.
sig_atomic_t is something that can be read by the processor in one
go, even in the presence of interrupts. For example, if you had
volatile double var;
then on -some- processors there is the possibility that the processor
might read the first 4 bytes of the double, get interrupted and
suspend the read half-way through, and pick up the read again later,
after the signal has overwritten the double... leaving you with
half the old value and half the new value.
This topic gets into processor dependant matters about whether or
what operations get restarted under what conditions, which can be
influenced by the POSIX sigaction(), and can be influenced by
very OS dependant routines such as SGI's IRIX handle_sigfpes(). But
no matter how the processor handles such things, sig_atomic_t is
something that you can be sure will not end up with corrupted bytes
due to interrupts, cache coherency matters and so on.
What gets even more interesting is if you have an element
similar to sig_atomic_t in which one is guaranteed that basic
arithmetic operations such as "increment by one" or "decrement
by one" will be done without interruption, even if it requires
a special processor instruction to do so. If you can be sure
of being able to do var++ and var-- without interrupts, then you can
impliment semaphores without having to drop into a driver routine
that raises high-level masks or priorities to guard the transaction.
Notice what you quoted said "accessed": to do semaphores, you
have to go beyond just read/writes into at least one read-modify-write
atomic instruction.
--
Oh, to be a Blobel!