How to interface to a C struct with volatile variables?

Ben Davis entheh at cantab.net
Sat Feb 16 08:16:45 PST 2013


On 16/02/2013 15:19, Jacob Carlborg wrote:
> On 2013-02-16 15:13, Ben Davis wrote:
>> As for 'volatile', there's some info at
>> http://dlang.org/deprecate.html#volatile about how it used to be
>> available for marking statements as 'do not optimise field accesses'.
>> The corrective action listed there is to use 'synchronized' instead. So
>> probably your best bet is to use 'synchronized' blocks wherever you
>> access the variables that you know are meant to be volatile.
>>
>> Hope that helps :)
>
> If you're interfacing with C you should just remove the volatile
> statement/keyword.
>
> "There is no volatile type modifier in D. To declare a C function that
> uses volatile, just drop the keyword from the declaration."
>
> http://dlang.org/interfaceToC.html

That's only telling you how to declare a C *function*, not a C struct 
(or global). While you'll probably get away with it in practice, I think 
dropping it from a struct is technically unsafe (and may break as a 
result of future compiler optimisation improvements) unless you back it 
up with the 'synchronized' strategy.

I was going to suggest __gshared, but for struct members it implies 
static, so it's not what you want. There doesn't seem to be a way to 
make a struct member 'volatile' for the purposes of interacting with C 
(which seems to be a gap in the C support).


More information about the Digitalmars-d-learn mailing list