[Bug 126] Add support for attribute to mark data as volatile.

Johannes Pfau via D.gnu d.gnu at puremagic.com
Tue Jun 3 00:09:22 PDT 2014


Am Mon, 02 Jun 2014 17:51:39 +0000
schrieb "Marc Schütz" <schuetzm at gmx.net>:

> On Monday, 2 June 2014 at 17:27:52 UTC, Johannes Pfau wrote:
> > And of course without a type qualifier there can't be 
> > transitivity. The
> > programmer always has to be careful to access struct members, 
> > array members,
> > and other types 'connected' via indirection with peek/poke.
> 
> I too think that a) volatile is necessary, and b) that it should 
> apply to variables, not operations. However, I'm not convinced of 
> transitivity. It makes sense to treat members of a volatile 
> struct as volatile, too, but I don't see why this needs to be the 
> case for pointers. Are there even cases of volatile pointers at 
> all? Usually, hardware registers don't contain pointers, and when 
> they do (DMA-like things maybe, but those typically use physical 
> addresses, not (virtual) pointers), what they point to would 
> probably be normal memory, wouldn't it?

I'm on the fence with transitivity as well cause it's really rarely
used with volatile. For DMA, strictly speaking the memory locations are
volatile cause the processor can modify the destination memory at any
time and if you have cached some value there you get problems. But in
practice you likely won't access the destination memory until the
transfer completed so it's usually not a problem.

More important points are:
* consistency with shared/const/immutable. Less special cases is always
  good.
* The main argument for shared to be transitive was 'if another thread
  can access ptr, it can also access *ptr so that location is shared as
  well'. This also applies for volatile, it's just very rare that
  indirection occurs. The main point for transitivity are interrupt
  handlers that use 'normal' volatile memory to pass information. Here
  volatile should be transitive.

These two points are not very strong and if you can give some examples
where transitivity hurts I'd be glad to change this. But I couldn't
think of an example where transitivity actually is a problem, mainly
because indirections are uncommon. (The DMA example is one example
where transitivity can be slightly annoying, but it's also one example
where you can use a cast easily to avoid these problems.)



More information about the D.gnu mailing list