Skip site navigation (1)Skip section navigation (2)
Date:      Mon, 17 Jan 2000 07:17:41 +0100 (CET)
From:      Oliver Fromme <olli@dorifer.heim3.tu-clausthal.de>
To:        freebsd-questions@FreeBSD.ORG
Subject:   Re: Volatile variables
Message-ID:  <200001170617.HAA95305@dorifer.heim3.tu-clausthal.de>
In-Reply-To: <85uao9$224k$1@atlantis.rz.tu-clausthal.de>

next in thread | previous in thread | raw e-mail | index | archive | help
Mikhail Evstiounin <evstiounin@adelphia.net> wrote in list.freebsd-questions:
 > From: Oliver Fromme <olli@dorifer.heim3.tu-clausthal.de>
 >>Delivery of a signal is, in essence, cause by a hardware
 >>interrupt, directly or indirectly.  It is what you call
 >>"totally async".
 > 
 > This is one part, but you missed another - value of a register
 > could be changed completely in the middle of an assembler command.

No.  Except if your processor is broken.  :-)

 >>No, the sighandler is part of the process.  When a signal
 >>handler gets called, this is (simply speaking) like an
 >>asynchronous and non-foreseeable subroutine call.
 > 
 > If you are talking about process as something in UNIX, I agree
 > here, but I was talking about process as some essence in sense
 > of unit of concurrency.

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.

 >>No, it is not.  In particular, the compiler is unable to take
 >>it into consideration during register optimization.
 > 
 > Again, you missed my point - you as a programmer, not as
 > a compiler. You can mask signals.

Yes, but that's completely unrelated to "volatile".
You have to mask signals to ensure proper synchronisation and
avoid race-conditions.  You're right about this.  This has
nothing to do with "volatile" and register optimizations.

But setting singal masks does not prevent those register
optimizations.  You _must_ use "volatile" for this.

 >>In theory, if the processor hardware has enough registers,
 >>_all_ variables of the whole program could be held in registers
 >>during the _whole_ lifetime of the process.  There would be
 >>never a variable stored in memory.  This is perfectly legal,
 >>and a programmer whould have no chance to access them from
 >>within a signal handler.
 > 
 > You are wrong here again.

Then please explain why you think so.
What I said is correct.

 > I was a member of team that wrote compiler
 > allocating global variables in registers. And it worked.

Was it a C compiler?  In that case I hope it supported
"volatile" correctly, otherwise it would be in violation
of the standard.

 > except some special address scheme and speed there is no big difference
 > between memory and registers from the point of view of a compiler.

Except that register optimizations are forbidden when a
variable has been declared as "volatile".

 >>"BS book"?  How about quoting from an authoritative reference?
 > 
 > OK, Bjarne Stroustrap is not authorative, even it's a base for standard
 > or created based on standard drafts?

I think we're talking about C, not C++.  I'm not familiar with
usage of "volatile" in C++ (and this was not the question).

 >>   movl   i,%eax
 >>   movl   i,%edx
 >>   imull  %edx,%eax
 >>   movl   %eax,j
 >>   movl   j,%eax
 >>   movl   i,%edx
 >>   addl   %edx,%eax
 >>   movl   %eax,j
 >>
 >>As you can see, now the contents of i and j are always
 >>consistent.  This code works fine, even when an asynchronous
 >>mechanism (signal handler, hardware, etc.) tries to access
 >>them at some point.
 >>
 > 
 > For Chrises sake tell me what is going to happen if you got
 > interrupted after first movl?

If the signal handler changes the value of "i" at that point,
then the second usage of "i" will get a different value than
the first usage, which is the _correct_ behaviour.

Without "volatile", the gcc code will always use the old value,
which is _not_ correct.  Even worse, the behaviour depends on
the compiler's optimizer -- it _might_ use the old value, or it
might not.

 > eax and completely differend on edx. Vilotile does help
 > you to avoid synonyms, but doesn't help you to solve your problem.

Excuse me?  It solves the problem.  The code does exactly what
it's supposed to do.

 > there is no warranty that j will always have value of
 > square of i.

Correct.  There is no such warranty, no matter if "i" is
declared as "volatile" or not.

If the signal handler changes the value of "i", the programmer
should know what he is doing.  If he wants to calculate the
square of "i", he should use a different way (i.e. using signal
masks).

 > Vilotile tells to compiler (and to programmer)
 > that j can have a very strange result.

That's completely wrong.

 > Oliver, I don't want to tell anybody - don't use vilotile.

Then you cannot use global variables within signal handlers
correctly.  I'm under the impression that you misunderstand
the purpose of "volatile".

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




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