On 7 May 2015 at 18:04, Jens Bauer via Digitalmars-d <digitalmars-d@puremagic.com> wrote: > 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. > -But this solution ... I do not know if it would work, when writing a driver > for Debian running on a i586 platform or PowerMac G3 for instance. > > 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' ? >
Yes. Take this example: --- shared int alice, bob; void foo() { alice = bob + 1; bob = 0; } --- gdc without optimisations produces: --- ; load bob into %eax movl _D7reorder3bobOi(%rip), %eax ; + 1 addl $1, %eax ; store to alice movl %eax, _D7reorder5aliceOi(%rip) ; bob = 0 movl $0, _D7reorder3bobOi(%rip) --- gdc with optimisations produces: --- ; load bob into eax movl _D7reorder3bobOi(%rip), %eax ; bob = 0 movl $0, _D7reorder3bobOi(%rip) ; + 1 addl $1, %eax ; store to alice movl %eax, _D7reorder5aliceOi(%rip) --- Now, this does not change the behaviour of the program if we consider that bob and alice do not have any side effects. However on a micro-controller dealing with direct memory I/O ... Iain.