Am Thu, 07 May 2015 16:04:55 +0000 schrieb "Jens Bauer" <doc...@who.no>:
> I'm sorry for opening such a topic; I've heard it's not liked a > lot, but I think it might be necessary. > > I'm not asking for a 'volatile' keyword, but rather to find out > what the right thing to use is. > After reading a few different threads related to > microcontrollers, I started wondering how to program the > following in D: > > 1: System level drivers, which writes directly to hardware > registers (any architecture, PC/i386 [Linux, Windows, others], > Atari ST, IBM BladeCenter, etc.) > 2: Interrupts that needs to share variables. > > 1) is what we basically need on microcontrollers. If it's > possible to write a driver in D, which has no problems with > accessing hardware, then it should be possible to do it for any > microcontroller as well. > > 2) shared variables could be used for interrupts, but what > happens if the TLS is disabled; will shared variables work ? > -Interrupts are not threads. > > Regarding (1), because marking a variable 'shared' is not enough > (it allows instructions to be moved around), Johannes already > made a volatileLoad and volatileStore, which will be usable for > microcontrollers, though for convenience, it requires writing > additional code. 1) Actually these were proposed for DMD some time before I've implemented the GDC part ;-) Shared should be avoided for volatile data for now. It might make sense if you want to synchronize accesses from threads to a volatile memory location, but then things might get complicated. 2) This can be done with volatileLoad/Store as well. Just access a global variable using volatileLoad/Store. This can also be used nicely in a wrapper. If you want to enforce atomic access to volatile variables (bigger than the word size) you might need shared(Volatile!T). Again things might get complicated ;-) > If variable 'alice' and variable 'bob' are both shared, and > reading from 'bob', then writing to 'alice'; would instructions > be moved around, so reading from 'bob' could actually occur after > writing to 'alice' ? Not sure about shared (I don't think anybody knows what exactly shared is supposed to do) but if you use volatileLoad/Store these instructions won't be moved around.