[dmd-internals] Regarding deprecation of volatile statements

Alex Rønne Petersen xtzgzorex at gmail.com
Mon Jul 23 15:46:44 PDT 2012


On Tue, Jul 24, 2012 at 12:34 AM, Jonathan M Davis <jmdavisProg at gmx.com> wrote:
> On Monday, July 23, 2012 23:28:05 Alex Rønne Petersen wrote:
>> Hi,
>>
>> I'm a bit confused.
>>
>> First of all: From what I understand, volatile is merely a compiler
>> reordering fence. It has nothing to do with atomicity, nor
>> synchronization. Is this correct?
>>
>> Assuming my understanding is correct: Why does DMD suggest using
>> synchronized to replace volatile statements? It doesn't even remotely
>> do the same thing, is much heavier, calls into the runtime, etc.
>>
>> And further: How are people *really* supposed to prevent compiler
>> reordering in modern D2 programs (without using atomics; they are
>> expensive and wasteful for this)?
>
> If D's volatile is/was anything like C's, it's my understanding the whole
> point of it is to tell the compiler that the object could be accessed by
> multiple threads simultaneously, so it shouldn't do optimizations or
> instruction reordering which screws with that. That being the case,
> I thought that shared replaced volatile, not synchronized.
>
> - Jonathan M Davis
> _______________________________________________
> dmd-internals mailing list
> dmd-internals at puremagic.com
> http://lists.puremagic.com/mailman/listinfo/dmd-internals

C's volatile guarantees nothing about threading; it's merely intended
to tell the compiler that repeated loads/stores to a volatile location
are intentional and should not be optimized out or reordered.

D's shared seems to want to tackle concurrency in addition to the
above. Not only is this wasteful for low-level programming where all
you want are the aforementioned guarantees, it is also highly
unportable (as I have ranted about countless times on the NG).

But either way, shared is not implemented. Replacing a language
feature with something that's incomplete and doesn't do the same thing
is not acceptable in any way. This direction worries me a lot since I
was considering trying out some kernel-level development with D.

BTW, the compiler actually advises you to use synchronized if you use
volatile without passing -d to the compiler.

Regards,
Alex


More information about the dmd-internals mailing list