Skip site navigation (1)Skip section navigation (2)
Date:      Thu, 13 Jan 2000 16:52:04 +0200
From:      Giorgos Keramidas <charon@hades.hell.gr>
To:        Mikhail Evstiounin <evstiounin@adelphia.net>
Cc:        freebsd-questions@freebsd.org
Subject:   Re: Volatile variables
Message-ID:  <20000113165204.E2590@hades.hell.gr>
In-Reply-To: <00b101bf5d72$a95c0be0$fc353018@evstiouninadelphia.net.pit.adelphia.net>
References:  <00b101bf5d72$a95c0be0$fc353018@evstiouninadelphia.net.pit.adelphia.net>

next in thread | previous in thread | raw e-mail | index | archive | help
On Wed, Jan 12, 2000 at 10:02:43PM -0500, Mikhail Evstiounin wrote:
> 
> > From: Oliver Fromme <olli@dorifer.heim3.tu-clausthal.de>
>
> Your example still leaves variable in a "stale" mode.

It was not Oliver's example; it was mine.  Any blame for not using
aggressive optimization should also be credited back to me, and not
Oliver, thank you.  In fact, I did use -O2 of gcc-2.7.2.3 which comes
with my FreeBSD 3.4-STABLE.  That was supposed to provide for some form
of optimization.

Anyway, what volatile means, as far as I have understood so far, is
that "between" C statements the compiler can not assume that the
variable has not changed.  Therefore, it's value must be refetched from
it's memory location, instead of relying on a register which `might'
contain the value of the variable.

If a signal handler changes the value of a variable, the context change
that happens when you enter the signal handler is not know to the
compiler.  Two statements like:

	int k = 0;
	k = 1;

might be interrupted for handling a signal.  If the compiler can assume
that the value of a register holds the true value of `k' and a signal
handler changes it in the middle of these two statements, then weird
things can happen.  That's what `volatile' is supposed to protect us
from.

> The same is with vilotile - if there is a way to provide an "atomic"
> way for some part of code then compiler can use some pretty aggresive
> optimizations.

There are ways to provide atomic modifications to memory areas under
Unix too, but that would require the compiler to know a lot more about
the execution environment (Unix in this case).

Semaphores spring into my mind, but that would require the compiler to
know whether you have SYSV_SEM in your kernel options, which you might
prefer not to have.  It's a mess, it seems...

Ciao.

-- 
Giorgos Keramidas, < keramida @ ceid . upatras . gr >
"What we have to learn to do, we learn by doing." [Aristotle]


To Unsubscribe: send mail to majordomo@FreeBSD.org
with "unsubscribe freebsd-questions" in the body of the message




Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20000113165204.E2590>