digitalmars.D.learn - How to interface to a C struct with volatile variables?
- Charles Hixson (5/5) Feb 15 2013 Does D have an equivalent to the C marking of volatile?
- Ben Davis (11/16) Feb 16 2013 In your case, I think you can declare opaque structs in D:
- Jacob Carlborg (8/25) Feb 16 2013 If you're interfacing with C you should just remove the volatile
- Ben Davis (10/24) Feb 16 2013 That's only telling you how to declare a C *function*, not a C struct
- Charles Hixson (13/40) Feb 17 2013 Thank you. Since "volatile", IIUC, is suppose to mean "This think can
- Lee Braiden (11/24) Feb 23 2013 It ALSO means, "This can be read by other processes / devices at any
Does D have an equivalent to the C marking of volatile? Currently I'm just going to try removing all variables from the struct, as it's never declared in D or accessed from within D, and I just want to avoid cast(void*)s, but I may need to access something like this sometime in the future.
Feb 15 2013
On 16/02/2013 03:54, Charles Hixson wrote:Does D have an equivalent to the C marking of volatile? Currently I'm just going to try removing all variables from the struct, as it's never declared in D or accessed from within D, and I just want to avoid cast(void*)s, but I may need to access something like this sometime in the future.In your case, I think you can declare opaque structs in D: struct S; //Opaque struct S { ... } //Not opaque 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 :)
Feb 16 2013
On 2013-02-16 15:13, Ben Davis wrote:On 16/02/2013 03:54, Charles Hixson wrote: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 -- /Jacob CarlborgDoes D have an equivalent to the C marking of volatile? Currently I'm just going to try removing all variables from the struct, as it's never declared in D or accessed from within D, and I just want to avoid cast(void*)s, but I may need to access something like this sometime in the future.In your case, I think you can declare opaque structs in D: struct S; //Opaque struct S { ... } //Not opaque 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 :)
Feb 16 2013
On 16/02/2013 15:19, Jacob Carlborg wrote:On 2013-02-16 15:13, Ben Davis wrote: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).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
Feb 16 2013
On 02/16/2013 08:16 AM, Ben Davis wrote:On 16/02/2013 15:19, Jacob Carlborg wrote:Thank you. Since "volatile", IIUC, is suppose to mean "This think can change without warning, and for no reason that you'll be able to see.", I think perhaps I should just use a normal variable of the correct size, and comment it as "/**< DON'T USE THIS VARIABLE*/". My only qualm about this was that the compiler might write the current value of the structure back over the structure parameter. (Well, actually since I don't intend to use ANY of the variables in the struct, but just pass it to another C routine in the same library, the opaque pointer solution works well in THIS case. But I was worried about what to do if I needed to change one of the variables.) Perhaps this is worrying unreasonably...it's definitely worrying about things I don't yet need to deal with.On 2013-02-16 15:13, Ben Davis wrote: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).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
Feb 17 2013
On Sun, 17 Feb 2013 10:58:57 -0800, Charles Hixson wrote:Thank you. Since "volatile", IIUC, is suppose to mean "This think can change without warning, and for no reason that you'll be able to see.",It ALSO means, "This can be read by other processes / devices at any time, so don't assume you can ignore whatever I put in there, just because I don't use it later myself." I think it was Walter who said that D just doesn't do this kind of volatility awareness, and the only way to achieve it in D is to write your code with inline assembly. That seems like a shame, to me.I think perhaps I should just use a normal variable of the correct size, and comment it as "/**< DON'T USE THIS VARIABLE*/". My only qualm about this was that the compiler might write the current value of the structure back over the structure parameter. (Well, actually since I don't intend to use ANY of the variables in the struct, but just pass it to another C routine in the same library, the opaque pointer solution works well in THIS case. But I was worried about what to do if I needed to change one of the variables.) Perhaps this is worrying unreasonably...it's definitely worrying about things I don't yet need to deal with.These sound like valid concerns. Maybe D's contracts will help you to guarantee that nothing's being changed, though? -- Lee
Feb 23 2013