[Info-vax] yet another sys$qiow question

Johnny Billquist bqt at softjar.se
Wed Aug 19 09:00:48 EDT 2015


On 2015-08-19 13:03, Stephen Hoffman wrote:
> On 2015-08-19 02:46:48 +0000, John Reagan said:
>
>> You don't need memory barriers.  You do need volatile.  The IOSB is
>> written behind your back.  volatile says we have to re-fetch.
>
> If that's the case, then there's a shedload of broken C code around
> (including all of the SYS$EXAMPLES: IOSB references I can find; all of
> those are not declared volatile, no volatile use in tcpip$examples:,
> etc), and the OpenVMS and the C documentation and the OpenVMS and C
> source code examples need updates (e.g.
> <http://h71000.www7.hp.com/commercial/c/docs/5492profile_016.html>), and
> a release note in the C compiler to flag all this stuff.

What John said is correct.
However, the code is not really totally broken. But it might be taking 
longer to notice changes to the IOSB than it needed to.
What happens is, as John points out, that the IOSB can change behind 
your back. The C compiler can optimize reads of the IOSB within the 
block to only read it once, and cache that value in a register (for 
example) as the C compiler knows that nothing in the code will possibly 
change the value of the IOSB in the code stream. As soon as you have a 
function call for example, the compiler cannot know anymore if the value 
of the IOSB changed, so after a function call, it have to re-read the 
IOSB. (The compiler cannot know what things a called function might modify.)

For things that are modified asynchronously, volatile is required. That 
is equally true for memory changed by the OS and by hardware. Which is 
why device registers and similar stuff always needs to be declared 
volatile as well. The C compiler are not allowed to optimize 
reads/writes to those registers/memory cells. Since each read/write 
might get values updated outside of the scope of the C code, and 
reads/write might also have side effects that the compiler is unaware of.

And yes, there is a shitload of code all over the world which fails to 
appreciate what volatile really is meant for, or when it should be 
used/is needed. Most of the time, people get away with it because there 
is sprinkles of function calls around as well, and you often have C 
compilers that do not optimize aggressively enough to cause the program 
to fail.

	Johnny




More information about the Info-vax mailing list