From owner-freebsd-questions Thu Jan 13 14:36: 6 2000 Delivered-To: freebsd-questions@freebsd.org Received: from mx2.x-treme.gr (mx2.x-treme.gr [212.120.192.15]) by hub.freebsd.org (Postfix) with ESMTP id 2682C14BCE for ; Thu, 13 Jan 2000 14:35:55 -0800 (PST) (envelope-from keramida@diogenis.ceid.upatras.gr) Received: from hades.hell.gr (pat34.x-treme.gr [212.120.197.226]) by mx2.x-treme.gr (8.9.3/8.9.3/IPNG-ADV-ANTISPAM-0.1) with ESMTP id AAA09695; Fri, 14 Jan 2000 00:35:50 +0200 Received: (from charon@localhost) by hades.hell.gr (8.9.3/8.9.3) id QAA03601; Thu, 13 Jan 2000 16:52:04 +0200 (EET) (envelope-from keramida@diogenis.ceid.upatras.gr) Date: Thu, 13 Jan 2000 16:52:04 +0200 From: Giorgos Keramidas To: Mikhail Evstiounin Cc: freebsd-questions@freebsd.org Subject: Re: Volatile variables Message-ID: <20000113165204.E2590@hades.hell.gr> Reply-To: keramida@ceid.upatras.gr References: <00b101bf5d72$a95c0be0$fc353018@evstiouninadelphia.net.pit.adelphia.net> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Mailer: Mutt 1.0pre3i In-Reply-To: <00b101bf5d72$a95c0be0$fc353018@evstiouninadelphia.net.pit.adelphia.net> Sender: owner-freebsd-questions@FreeBSD.ORG Precedence: bulk X-Loop: FreeBSD.ORG On Wed, Jan 12, 2000 at 10:02:43PM -0500, Mikhail Evstiounin wrote: > > > From: Oliver Fromme > > 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