comp.lang.ada
 help / color / mirror / Atom feed
From: minyard@acm.org
Subject: Re: Pragma Volatile
Date: Sat, 29 Sep 2001 18:22:27 GMT
Date: 2001-09-29T18:22:27+00:00	[thread overview]
Message-ID: <m3ofnt26xq.fsf@wf-rch.cirr.com> (raw)
In-Reply-To: 3BB60733.4A80708A@avercom.net

Tucker Taft <stt@avercom.net> writes:

> There shouldn't be any problem if you are using this in a mono-processor.
  (talking about volatiles not being important on uniprocessors).

Note that this isn't true at all.  Multiprocessors require special
instructions for syncronization, but volatile is just as important on
a uniprocessor as a multiprocessor.  You use it when talking to device
registers (which are changed asyncronously or have side effects) or in
variables shared between tasks (because you can't necessarily
guarantee when tasks switch).  You are telling the compiler that the
variable has side-effects that it doesn't know about.  I assume
Mr. Taft know this but was trying to make another point.

Note that the rest of what was said was true; I am just addressing
this one point.

Another way to think about volatiles is that they guarantee that any
operation done on the variable is actually done.  For instance:

  A : Integer;
  .
  .
  .
  A := 1;
  while (A /= 0) loop
     delay 1.0;
  end loop;

A compiler would look at this and say "Hey, A doesn't change in the
loop, and A is set right before, so this just really just an infinite
loop", even if some other task changes A.  Setting A volatile
guarantees that every time you reference A and compare, it actually
get's referenced and compared.

Also, consider a register to reset a device, where you write a 0 then
a 1 to reset it.

  B : Register_Access;

  B.all := 0;
  B.all := 1;

Again, a compiler can say, "Hey, we can just ignore the first
operation, because it doesn't have any effect", from the compiler's
point of view.  But setting B volatile guarantees that both operations
get done.  Actually, I'm not sure of the syntax here, because what B
references is volatile, not B itself.  So I'm guessing you would have

  type Register_Access is access all Integer;
  pragma Volatile(Register_Access);

Instead of making B volatile.  This is just like the "C" language,
where the location of the "volatile" keyword is very critical to what
gets set volatile.

-Corey



  reply	other threads:[~2001-09-29 18:22 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2001-09-25 14:07 Pragma Volatile Jon R. Harshaw
2001-09-25 14:23 ` David C. Hoos
2001-09-25 14:38 ` Marin David Condic
2001-09-25 23:03 ` Mark Johnson
2001-09-29 17:38   ` Tucker Taft
2001-09-29 18:22     ` minyard [this message]
2001-09-29 22:28       ` Jeffrey Carter
2001-09-30 13:10         ` Robert Dewar
2001-09-30 21:19           ` Jeffrey Carter
2001-10-01  2:58             ` minyard
2001-10-02  9:38           ` AG
2001-10-02 10:59             ` Jeff Creem
2001-09-30  2:03       ` DuckE
2001-09-30 13:01       ` Robert Dewar
2001-09-30 20:12         ` minyard
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox