Can someone explain why 'volatile' is used in psql/mainloop.c?  Seems
volatile is only for hardware-specific variables that can not be
optimized.


-- 
  Bruce Momjian                        |  http://www.op.net/~candle
  pgman@candle.pha.pa.us               |  (610) 853-3000
  +  If your life is a hard drive,     |  830 Blythe Avenue
  +  Christ can be your backup.        |  Drexel Hill, Pennsylvania 19026

Thu, 29 Jun 2000 12:17:19 -0400 (EDT)
Bruce Momjian <pgman@candle.pha.pa.us>
psql use of 'volatile'
On Thu, 29 Jun 2000, Bruce Momjian wrote:

> Can someone explain why 'volatile' is used in psql/mainloop.c?

If you remove them then you get tons of warnings about variables possibly
getting clobbered. The reason is the longjmp business that's going on when
you press Control-C. (The fact that the variables would get clobbered is
not critical since they're reinitialized immediately anyway, but who am I
to argue with the compiler.)

-- 
Peter Eisentraut                  Sernanders vï¿½g 10:115
peter_e@gmx.net                   75262 Uppsala
http://yi.org/peter-e/            Sweden


Thu, 29 Jun 2000 18:36:48 +0200 (MET DST)
Peter Eisentraut <e99re41@DoCS.UU.SE>
Re: psql use of 'volatile'
[ Charset ISO-8859-1 unsupported, converting... ]
> On Thu, 29 Jun 2000, Bruce Momjian wrote:
> 
> > Can someone explain why 'volatile' is used in psql/mainloop.c?
> 
> If you remove them then you get tons of warnings about variables possibly
> getting clobbered. The reason is the longjmp business that's going on when
> you press Control-C. (The fact that the variables would get clobbered is
> not critical since they're reinitialized immediately anyway, but who am I
> to argue with the compiler.)

Agreed.


-- 
  Bruce Momjian                        |  http://www.op.net/~candle
  pgman@candle.pha.pa.us               |  (610) 853-3000
  +  If your life is a hard drive,     |  830 Blythe Avenue
  +  Christ can be your backup.        |  Drexel Hill, Pennsylvania 19026

Thu, 29 Jun 2000 12:37:21 -0400 (EDT)
Bruce Momjian <pgman@candle.pha.pa.us>
Re: psql use of 'volatile'
Bruce Momjian <pgman@candle.pha.pa.us> writes:
> Can someone explain why 'volatile' is used in psql/mainloop.c?  Seems
> volatile is only for hardware-specific variables that can not be
> optimized.

Not at all.  The ANSI C spec specifically says that you must declare
as volatile any local variables that you need to change after a
setjmp() call.  This prevents the compiler from putting them in
registers, which would cause their post-longjmp values to be
uncertain.  The exact statement is that after a longjmp,

       [#3] All accessible objects  have  values  as  of  the  time
       longjmp  was  called,  except  that the values of objects of
       automatic storage duration that are local  to  the  function
       containing  the invocation of the corresponding setjmp macro
       that do not  have  volatile-qualified  type  and  have  been
       changed  between  the setjmp invocation and longjmp call are
       indeterminate.

The reason they're "indeterminate" is that longjmp restores all the
machine registers to the values saved by setjmp.  So, if your local
variable foo was allocated in a register, then foo reverts to its
value as of the time of setjmp; but if it's in memory, it doesn't
revert.  The behavior is really perfectly "determinate", but you
can't safely rely on which will happen.  Unless you force foo to be
allocated in memory, which you do by labeling it "volatile".

This is sort of an abuse of the intuitive meaning of "volatile",
but it falls right out of what the qualifier actually means to a C
compiler, which is "keep this in memory, not in a register, and
fetch it afresh from memory every single time the program references
it.  No common-subexpression elimination here!"

			regards, tom lane

Thu, 29 Jun 2000 21:19:58 -0400
Tom Lane <tgl@sss.pgh.pa.us>
Re: psql use of 'volatile' 