Merge pull request #169 from japaric/late-must-be-send

book: note that late resources must be Send
This commit is contained in:
Jorge Aparicio 2019-04-21 18:20:15 +02:00 committed by GitHub
commit b5a756bd7d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 52 additions and 0 deletions

View file

@ -39,6 +39,19 @@ The example below shows where a type that doesn't implement `Send` can be used.
{{#include ../../../../examples/not-send.rs}} {{#include ../../../../examples/not-send.rs}}
``` ```
It's important to note that late initialization of resources is effectively a
send operation where the initial value is sent from `idle`, which has the lowest
priority of `0`, to a task with will run with a priority greater than or equal
to `1`. Thus all late resources need to implement the `Send` trait.
Sharing a resource with `init` can be used to implement late initialization, see
example below. For that reason, resources shared with `init` must also implement
the `Send` trait.
``` rust
{{#include ../../../../examples/shared-with-init.rs}}
```
## `Sync` ## `Sync`
Similarly, [`Sync`] is a marker trait for "types for which it is safe to share Similarly, [`Sync`] is a marker trait for "types for which it is safe to share

View file

View file

@ -109,6 +109,7 @@ main() {
types types
not-send not-send
not-sync not-sync
shared-with-init
generics generics
ramfunc ramfunc

View file

@ -0,0 +1,38 @@
//! `examples/shared-with-init.rs`
#![deny(unsafe_code)]
#![deny(warnings)]
#![no_main]
#![no_std]
extern crate panic_halt;
use cortex_m_semihosting::debug;
use lm3s6965::Interrupt;
use rtfm::app;
pub struct MustBeSend;
#[app(device = lm3s6965)]
const APP: () = {
static mut SHARED: Option<MustBeSend> = None;
#[init(resources = [SHARED])]
fn init() {
// this `message` will be sent to task `UART0`
let message = MustBeSend;
*resources.SHARED = Some(message);
rtfm::pend(Interrupt::UART0);
}
#[interrupt(resources = [SHARED])]
fn UART0() {
if let Some(message) = resources.SHARED.take() {
// `message` has been received
drop(message);
debug::exit(debug::EXIT_SUCCESS);
}
}
};