From c4bad51deb75efc033431be513e264c3247b64cb Mon Sep 17 00:00:00 2001 From: Jorge Aparicio Date: Tue, 16 Apr 2019 16:39:51 +0200 Subject: [PATCH 1/3] note that late resources must be Send --- book/en/src/by-example/types-send-sync.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/book/en/src/by-example/types-send-sync.md b/book/en/src/by-example/types-send-sync.md index da53cf963f..8ec62e6569 100644 --- a/book/en/src/by-example/types-send-sync.md +++ b/book/en/src/by-example/types-send-sync.md @@ -39,6 +39,11 @@ The example below shows where a type that doesn't implement `Send` can be used. {{#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. + ## `Sync` Similarly, [`Sync`] is a marker trait for "types for which it is safe to share From e865cbb2e59a95962d7e4d45c31f9ff1e4ba6579 Mon Sep 17 00:00:00 2001 From: Jorge Aparicio Date: Tue, 16 Apr 2019 21:54:19 +0200 Subject: [PATCH 2/3] book: resources shared with init must also be `Send` --- book/en/src/by-example/types-send-sync.md | 8 +++++ ci/script.sh | 1 + examples/shared-with-init.rs | 38 +++++++++++++++++++++++ 3 files changed, 47 insertions(+) create mode 100644 examples/shared-with-init.rs diff --git a/book/en/src/by-example/types-send-sync.md b/book/en/src/by-example/types-send-sync.md index 8ec62e6569..632946b9ac 100644 --- a/book/en/src/by-example/types-send-sync.md +++ b/book/en/src/by-example/types-send-sync.md @@ -44,6 +44,14 @@ 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` Similarly, [`Sync`] is a marker trait for "types for which it is safe to share diff --git a/ci/script.sh b/ci/script.sh index 7cda1e5e66..0e350d1ffd 100644 --- a/ci/script.sh +++ b/ci/script.sh @@ -109,6 +109,7 @@ main() { types not-send not-sync + shared-with-init generics ramfunc diff --git a/examples/shared-with-init.rs b/examples/shared-with-init.rs new file mode 100644 index 0000000000..5ddd2cc3d9 --- /dev/null +++ b/examples/shared-with-init.rs @@ -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 = 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); + } + } +}; From 473a0e7bb58c1ec4b10dea505056ad76f9023871 Mon Sep 17 00:00:00 2001 From: Jorge Aparicio Date: Sun, 21 Apr 2019 18:05:24 +0200 Subject: [PATCH 3/3] add .run file --- ci/expected/shared-with-init.run | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 ci/expected/shared-with-init.run diff --git a/ci/expected/shared-with-init.run b/ci/expected/shared-with-init.run new file mode 100644 index 0000000000..e69de29bb2