Skip to content

Commit

Permalink
Merge #251
Browse files Browse the repository at this point in the history
251: Note on transformation of static variables by attribute exception r=therealprof a=hyperslv

Hi All
The "// `COUNT` has type `&mut u32` and it's safe to use" comment confused me since I did not expect that transformation of the source code so deep comparing with the C language.
Finally I found how it (works)[https://docs.rs/cortex-m-rt-macros/0.1.5/src/cortex_m_rt_macros/lib.rs.html#447].
This PR is to provide a note about the transformation.
Thanks, Slava

Co-authored-by: hyperslv <hyper.slv@gmail.com>
Co-authored-by: hyperslv <57301860+hyperslv@users.noreply.github.com>
  • Loading branch information
3 people committed Jul 5, 2020
2 parents 616962a + 5ba9989 commit 94d9ea8
Showing 1 changed file with 7 additions and 1 deletion.
8 changes: 7 additions & 1 deletion src/start/exceptions.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ This behavior is pretty much intended and it's required to provide a feature:
fn SysTick() {
static mut COUNT: u32 = 0;
// `COUNT` has type `&mut u32` and it's safe to use
// `COUNT` has transformed to type `&mut u32` and it's safe to use
*COUNT += 1;
}
```
Expand All @@ -47,6 +47,12 @@ use `static mut` variables. How is this possible? This is possible because
`exception` handlers can *not* be called by software thus reentrancy is not
possible.

> Note that the `exception` attribute transforms definitions of static variables
> inside the function by wrapping them into `unsafe` blocks and providing us
> with new appropriate variables of type `&mut` of the same name.
> Thus we can derefence the reference via `*` to access the values of the variables without
> needing to wrap them in an `unsafe` block.
## A complete example

Here's an example that uses the system timer to raise a `SysTick` exception
Expand Down

0 comments on commit 94d9ea8

Please sign in to comment.