[Info-vax] yet another sys$qiow question

Chris Scheers chris at applied-synergy.com
Wed Aug 19 15:38:46 EDT 2015


John Reagan wrote:
> On Tuesday, August 18, 2015 at 7:45:35 PM UTC-4, George Cornelius wrote:
> 
>> The whole volatile thing appears to have been a distraction.  As far as
>> I can tell, the C compiler never issues memory barrier instructions when
>> referencing volatile data, and, as John has pointed out, volatile is
>> not really needed as a hint to the compiler that the data might change
>> asynchronously as long as it already knows that the address of the
>> entity has been taken and passed to external code, because it
>> apparently has an internal implementation of a 'weakly volatile'
>> designation.  But if you do not synchronize, and it does change
>> between two successive calls to external code, the compiler is
>> free to remember, say, the value it read the first time in a
>> sequence of uninterrupted local code and not fetch it separately
>> each time thereafter.
> 
> You don't need memory barriers.  You do need volatile.  The IOSB is written behind your back.  volatile says we have to re-fetch.  
> 
> I think you misunderstood my prior posts.  Places that only need a 'weak volatile' are places where you took the address of something (for example, put the address of a variable into an item list descriptor) and then call some system service that would write that variable for you.  But C doesn't have a weak volatile.  You can get away without using volatile, but in general, I'd recommend it at least for documentation purposes.
> 
> You only need memory barriers when you need to define an order of reads & writes to DIFFERENT memory locations.  For example, if you have a buffer and a 'buffer-ready' flag, you want a memory barrier between the writes to the buffer and the write to the ready flag to make sure the buffer is indeed written before you set the flag.

I think that some of the confusion comes from an expectation that 
VOLATILE is something smarter than what it is.

 From what I understand, VOLATILE could be renamed "DO NOT OPTIMIZE".

This is mostly seen as always going back to memory for the value (do not 
save it in a register, on the stack, etc.) and not reordering accesses, 
including not moving accesses outside of loops.

The exact meaning depends on the optimizations done by the compiler, so 
the meaning is compiler/version/implementation/flags specific.

This is not a VMS or IOSB or AST problem.  Any multithreaded application 
on any platform that uses shared data also has these issues.

Lack of the proper usage of VOLATILE does not mean that your program 
will fail.  It may mean that the behavior of certain parts of the 
program may be indeterminate.

YMMV.

-- 
-----------------------------------------------------------------------
Chris Scheers, Applied Synergy, Inc.

Voice: 817-237-3360            Internet: chris at applied-synergy.com
   Fax: 817-237-3074



More information about the Info-vax mailing list