Skip to content

Commit 3ffcd2d

Browse files
committed
Explain how fences interact with volatile operations
This was the result of a discussion with Amanieu at RustWeek. It would be nice to add more details about the planned updates (some ticket for example), and I'm not sure if this is the best place to add it to the embedonomicon. But for now, I think mentioning it here is better than nothing.
1 parent 052fd35 commit 3ffcd2d

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

src/dma.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,27 @@ of `compiler_fence`. That should generate a DMB instruction on Cortex-M devices.
245245

246246
[`atomic::fence`]: https://doc.rust-lang.org/core/sync/atomic/fn.fence.html
247247

248+
## Don't we need atomics?
249+
250+
The documentation on fences states that they only work in combination with atomics:
251+
252+
> A fence ‘A’ which has (at least) Release ordering semantics, synchronizes with
253+
> a fence ‘B’ with (at least) Acquire semantics, if and only if there exist
254+
> operations X and Y, both operating on some atomic object ‘m’ such that A is
255+
> sequenced before X, Y is sequenced before B and Y observes the change to m.
256+
257+
The same is true for `compiler_fence`:
258+
259+
> Note that just like fence, synchronization still requires atomic operations
260+
> to be used in both threads – it is not possible to perform synchronization
261+
> entirely with fences and non-atomic operations.
262+
263+
So how does this work when not talking to another thread, but to
264+
some hardware like the DMA engine? The answer is that in the current
265+
implementation, volatiles happen to work just like relaxed atomic
266+
operations. There's work going on to actually guarantee this behavior
267+
for future versions of rust.
268+
248269
## Generic buffer
249270

250271
Our API is more restrictive that it needs to be. For example, the following

0 commit comments

Comments
 (0)