From: | Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us> |
---|---|
To: | Bruce Momjian <pgman(at)candle(dot)pha(dot)pa(dot)us> |
Cc: | PostgreSQL-development <pgsql-hackers(at)postgreSQL(dot)org> |
Subject: | Re: psql use of 'volatile' |
Date: | 2000-06-30 01:19:58 |
Message-ID: | 2925.962327998@sss.pgh.pa.us |
Views: | Raw Message | Whole Thread | Download mbox | Resend email |
Thread: | |
Lists: | pgsql-hackers |
Bruce Momjian <pgman(at)candle(dot)pha(dot)pa(dot)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
From | Date | Subject | |
---|---|---|---|
Next Message | Tom Lane | 2000-06-30 01:26:20 | Re: Installation layout |
Previous Message | The Hermit Hacker | 2000-06-30 01:11:25 | Re: Changes to handling version numbers internally |