Commit 820fd1d
authored
Safe DMA transfer API: ownership-based framebuffer locking (#31)
* Make DMA transfer API ownership-based to prevent write-after-submit races
The previous `start_dma_transfer(&self, framebuffer: &FrameBuf<...>)`
released the borrow immediately on return, leaving the caller free to
write to the buffer while DMA was still reading it — a data race the
compiler could not catch.
The new design follows the embedded-hal ecosystem pattern (stm32f4xx-hal,
nrf-hal, Embedonomicon): `start_dma_transfer` takes the framebuffer by
value and returns a `DmaTransfer` token. The buffer is locked inside the
token until `DmaTransfer::wait` returns it, making concurrent access a
compile error.
Changes:
- New `DmaTransfer` trait with `is_done(&self)` and `wait(self) -> Buffer`
- New `TransferError<FB>` carries the buffer back on failure so it is never lost
- `DisplayBackend::start_dma_transfer` now takes `FrameBuf` by value and
returns `Result<Self::Transfer, TransferError<FB>>`
- `wait_for_dma`, `is_dma_ready`, `present`, `present_region` removed from
the trait — waiting/polling moves to `DmaTransfer`, presenting stays in
`SwapChain`
- `SimulatorBackend` gains `CompletedTransfer<FB>` — an immediately-done token
- `SwapChain` restructured with a `FrontState<FB, Xfer>` enum: the front
buffer is either `Idle(FrameBuf)` or `InFlight(Transfer)`, preventing
any access path to the buffer while DMA is active
- `TripleSwapChain` follows the same pattern
- All call sites (`dma_rendering_demo.rs`, `present()` etc.) are unchanged
* Fix CI: add Debug impl for TransferError, fix height() method call in test
* Add cargo test to pre-commit hook1 parent 02f40f6 commit 820fd1d
3 files changed
Lines changed: 592 additions & 413 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
2 | 12 | | |
3 | 13 | | |
4 | 14 | | |
5 | | - | |
| 15 | + | |
6 | 16 | | |
7 | 17 | | |
8 | 18 | | |
9 | 19 | | |
10 | 20 | | |
11 | 21 | | |
| 22 | + | |
12 | 23 | | |
13 | 24 | | |
14 | 25 | | |
| |||
31 | 42 | | |
32 | 43 | | |
33 | 44 | | |
34 | | - | |
35 | | - | |
36 | | - | |
37 | | - | |
38 | | - | |
39 | | - | |
40 | | - | |
41 | | - | |
42 | | - | |
43 | | - | |
44 | | - | |
45 | | - | |
46 | | - | |
47 | | - | |
48 | | - | |
49 | | - | |
50 | | - | |
51 | | - | |
52 | | - | |
53 | | - | |
54 | | - | |
55 | | - | |
56 | | - | |
57 | | - | |
58 | | - | |
59 | | - | |
60 | | - | |
61 | | - | |
62 | | - | |
63 | | - | |
64 | | - | |
65 | | - | |
| 45 | + | |
| 46 | + | |
0 commit comments