From owner-freebsd-questions Mon Jan 17 11:55:59 2000 Delivered-To: freebsd-questions@freebsd.org Received: from dorifer.heim3.tu-clausthal.de (dorifer.heim3.tu-clausthal.de [139.174.243.252]) by hub.freebsd.org (Postfix) with ESMTP id 6627E14A2B for ; Mon, 17 Jan 2000 11:55:52 -0800 (PST) (envelope-from olli@dorifer.heim3.tu-clausthal.de) Received: (from olli@localhost) by dorifer.heim3.tu-clausthal.de (8.9.3/8.9.3) id UAA88876; Mon, 17 Jan 2000 20:55:48 +0100 (CET) (envelope-from olli) Date: Mon, 17 Jan 2000 20:55:48 +0100 (CET) Message-Id: <200001171955.UAA88876@dorifer.heim3.tu-clausthal.de> From: Oliver Fromme To: freebsd-questions@FreeBSD.ORG Reply-To: freebsd-questions@FreeBSD.ORG Subject: Re: Volatile variables X-Newsgroups: list.freebsd-questions In-Reply-To: <85v96r$2jjd$1@atlantis.rz.tu-clausthal.de> User-Agent: tin/1.4.1-19991201 ("Polish") (UNIX) (FreeBSD/3.4-19991219-STABLE (i386)) MIME-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 8bit Sender: owner-freebsd-questions@FreeBSD.ORG Precedence: bulk X-Loop: FreeBSD.ORG Mikhail Evstiounin wrote in list.freebsd-questions: >>No. Except if your processor is broken. :-) > > Oliver, what about timer registers or serial interface registers? > what about mapped video memory or shared memory in multi > CPU system? It has nothing to do with the issue. You claimed that the processor could be interrupted in the middle of an assembler instruction, which is incorrect. At least for all processor types that I know about. >>There is no concurrency. Even if you have two processors, >>the signal handler will not run in parallel to the "main" >>part of the program. > > I agree completely, I was trying to stress a difference between sighandler > and threads and unix processes. Oh, then I misunderstood you, sorry. Of course, a sighandler is something completely cifferent. :-) >>But setting singal masks does not prevent those register >>optimizations. You _must_ use "volatile" for this. > > In a way, how you proposed - yes, but it's not enough. It depends on the program, and what it uses the variables for. That's a different issue, not subject to this discussion. This discussion is about using the "volatile" qualifier, which is necessary for global variables that are accessed from within signal handlers. You did not agree with this (do you still not agree?), and I'm trying to convince you that it _is_ indeed necessary. :-) >> > Vilotile tells to compiler (and to programmer) >> > that j can have a very strange result. >> >>That's completely wrong. > > So, if programmer wites j = i * i; (s)he doesn't expect to get a square? I > understand, that according to standard vilotile means it, but it's not > intuitive. That's why I told that vilotile tells to compiler and to > programmer - expect something strange. No, that's still wrong. Without volatile, you can get exactly the same "strange" result. You asked me to quote from the standard. This is not easy, because volatile is mentioned in a lot of places, and I don't have it in *.txt form, so I have to type everything manually from it. Here are a few sections which I think are pretty important: [...] an implementation might perform various optimizations within each translation unit, such that the actual semantics would agree with the abstract semantics only when making function calls across translation unit boundaries. [...] In this type of implementation, objects referred to by interrupt service routines activated by the signal function would require explicit specification of "volatile" storage [...] A "volatile" declaration may be used to describe an object corresponding to a memory-mapped input/output port or an object accessed by an asynchronously interrupting function. Actions on objects so declared shall not be "optimized out" by an implementation or reordered except as permitted by the rules for evaluating expressions. If the signal occurs [...], the behavior is undefined if the signal handler refers to any object with static storage duration other than by assigning a value to an object declared as "volatile sig_atomic_t" [...] It sounds like you cannot even read a global variable inside a signal handler, even if it's volatile. I didn't know this before (and I think it's strange), but that's what the standard says. Regards Oliver -- Oliver Fromme, Leibnizstr. 18/61, 38678 Clausthal, Germany (Info: finger userinfo:olli@dorifer.heim3.tu-clausthal.de) "In jedem Stück Kohle wartet ein Diamant auf seine Geburt" (Terry Pratchett) To Unsubscribe: send mail to majordomo@FreeBSD.org with "unsubscribe freebsd-questions" in the body of the message