Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

concurrency chapter #48

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open

concurrency chapter #48

wants to merge 5 commits into from

Conversation

japaric
Copy link
Member

@japaric japaric commented Apr 8, 2019

This PR adds a chapter that documents the most common (memory) safe uses of unsafe code in concurrent bare metal (no_std) programs. The patterns described here are used to implement the safe concurrency abstractions provided by the cortex-m, cortex-m-rt and cortex-m-rtfm crates.

Rendered

@RalfJung would it be possible to get the UCG WG to (at some point) review / audit these patterns? I'm particularly concerned about LLVM attributes / semantics not matching the intended semantics.

cc @jamesmunns this is relevant to your recent work on shared-rs

@japaric japaric requested a review from a team as a code owner April 8, 2019 21:23
@rust-highfive
Copy link

r? @korken89

(rust_highfive has picked a reviewer for you, use r? to override)

@rust-highfive rust-highfive added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-resources labels Apr 8, 2019
{{#include ../ci/concurrency/examples/systick.rs}}```

If you are not familiar with embedded / Cortex-M programs the most important
thing to point note here is that the function marked with the `entry` attribute
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should probably either be point out or note.

src/concurrency.md Outdated Show resolved Hide resolved
@therealprof
Copy link
Contributor

Other than those two nits LGTM

@andre-richter
Copy link
Member

andre-richter commented Apr 9, 2019

EDIT ----------------------
I just realized we are in fact showing a spinlock under the "Atomic" chapter. Sorry for the confusion.

In this case, it is probably needed to explain a bit more about the terms

  • Atomics
  • Spinlock
  • Mutex

and the unfortunate case that due to the naming in the spin crate, this example

use spin::Mutex;

static COUNTER: Mutex<u64> = Mutex::new(0);

actually shows a spinlock which happens to be named Mutex.

I think readers new to these terms will be left very confused (same as me :D)
EDIT END ----------------------

I think the end of the document is missing a subchapter to conclude the multi-core story and therefore the whole document nicely.

First, we tell that Mutex as implemented in the current form is not Sync. Then we introduce Atomics as an alternative, and only casually mention they can be used to build spinlocks too.

IMO, we should showcase exactly that after the Atomics chapter. We can probably cite a minimal implementation, as for example it can be found in the spin crate.

Other than that, I like 👍 :)

@korken89
Copy link
Contributor

korken89 commented Apr 9, 2019

I'm happy with this, but lets include @therealprof comments. After that I will merge.

@korken89
Copy link
Contributor

korken89 commented Apr 9, 2019

Something seems amiss with the asm format, can you have a look @japaric ?

@RalfJung
Copy link

RalfJung commented Apr 9, 2019

would it be possible to get the UCG WG to (at some point) review / audit these patterns? I'm particularly concerned about LLVM attributes / semantics not matching the intended semantics.

I guess? If you have concrete questions, I suggest to open an issue at https://github.com/rust-rfcs/unsafe-code-guidelines/. However, in this PR I see 1700 lines of code and I am not sure what to look at.^^

therealprof and others added 4 commits April 13, 2019 17:17
also note the bounds and multi-core requirements of the cooperative handlers
pattern
@japaric
Copy link
Member Author

japaric commented Apr 13, 2019

@therealprof thanks for the review; I have addressed your comments

@andre-richter I have added some notes; let me know if you think something else needs to be clarified

@korken89 if you mean the CI failure; it should have been fixed by #47

@RalfJung great, I'll open a few issues over there

@Disasm
Copy link
Member

Disasm commented Apr 14, 2019

@japaric In Priorities section I would suggest using some different interrupts as an example:

  • Both SysTick and SVCall have fixed (and different) priorities.
  • They are both exceptions, not interrupts. That's a bit misleading.

@jounathaen
Copy link

I'd like to bump this PR, because the chapter contains a lot of useful information and it would be great to have them in the "documentation"!

@jamesmunns
Copy link
Member

Note: will probably need to be rebased on #66

Copy link

@jonas-schievink jonas-schievink left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great writeup!

[this repository]: https://github.com/rust-embedded/embedonomicon

``` rust
{{#include ../ci/concurrency/examples/systick.rs}}```

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The backticks on the same line break with the current mdbook version (there's multiple instances of this).

Suggested change
{{#include ../ci/concurrency/examples/systick.rs}}```
{{#include ../ci/concurrency/examples/systick.rs}}
```


``` rust
#[link_name = "SysTick"] // places this function in the vector table
fn randomly_generated_identifier() {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is no longer random

@@ -0,0 +1,65 @@
// source: examples/init.rs

#![feature(maybe_uninit)]

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is stable now

Comment on lines +16 to +17
// TODO does T require a Sync / Send bound?
unsafe impl<T> Sync for Mutex<T> {}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// TODO does T require a Sync / Send bound?
unsafe impl<T> Sync for Mutex<T> {}
unsafe impl<T: Send> Sync for Mutex<T> {}

contexts* that may preempt each other.

- `Send`: types that can be transferred between *execution contexts* that may
preempt each other.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It might make sense to update this wrt. the RFC to restrict Send/Sync reasoning to single cores

The problem with `Mutex` is not the critical section that uses; it's the fact
that it can be stored in a `static` variable making accessible to all cores.
Thus in multi-core context the `Mutex` abstraction should not implement the
`Sync` trait.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh yeah this is outdated with the RFC now

@TDHolmes
Copy link

TDHolmes commented Jul 3, 2021

Any chance this will be going in at some point? Sounds very useful. I'm coming here from the "under the hood" section of the RTIC documentation


#[exception]
fn SysTick() {
X.store(true, Ordering::Relaxed);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using relaxed ordering is probably fine for this toy example since there's no other important memory accesses that could be reordered across the load or store, but if someone copies this example and starts making the wrong assumptions, it would be quite easy for their code to become unsound/incorrect. It might be better to default to sequentially consistent atomics for the examples.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-resources
Projects
None yet
Development

Successfully merging this pull request may close these issues.