-
Notifications
You must be signed in to change notification settings - Fork 242
Spawn local without attribute #1116
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
Open
usbalbin
wants to merge
8
commits into
rtic-rs:master
Choose a base branch
from
Dectron-AB:spawn-local-no-attrib
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
578bcbf
Allow spawning non Send/Sync tasks from the same prio using local spa…
usbalbin 964642b
Add example
usbalbin c2f5a60
Add tests
usbalbin d241893
Update changelog
usbalbin 92137f2
Revert moved context
usbalbin e0d4627
Ensure local_spawner is added as a field to Context
usbalbin 82c9d75
Cleanup
usbalbin ee5f773
Rename Dummy to TaskArg
usbalbin File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| Hello from task1! | ||
| Hello from task2! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| #![no_main] | ||
| #![no_std] | ||
|
|
||
| use panic_semihosting as _; | ||
|
|
||
| #[rtic::app(device = lm3s6965, dispatchers = [SSI0])] | ||
| mod app { | ||
| use cortex_m_semihosting::{debug, hprintln}; | ||
| use super::*; | ||
|
|
||
| #[shared] | ||
| struct Shared {} | ||
|
|
||
| #[local] | ||
| struct Local {} | ||
|
|
||
| #[init] | ||
| fn init(_cx: init::Context) -> (Shared, Local) { | ||
| task1::spawn().unwrap(); | ||
| //task2::spawn(Default::default()).ok(); <--- This is rejected since not all args are Send and Sync | ||
| (Shared {}, Local {}) | ||
| } | ||
|
|
||
| #[task(priority = 1)] | ||
| async fn task1(cx: task1::Context) { | ||
| hprintln!("Hello from task1!"); | ||
| cx.local_spawner.task2(Default::default()).unwrap(); | ||
| } | ||
|
|
||
| // Task where some args are !Send/!Sync | ||
| #[task(priority = 1)] | ||
| async fn task2(_cx: task2::Context, _nsns: NotSendNotSync) { | ||
| hprintln!("Hello from task2!"); | ||
| debug::exit(debug::EXIT_SUCCESS); // Exit QEMU simulator | ||
| } | ||
| } | ||
|
|
||
| #[derive(Default, Debug)] | ||
| struct NotSendNotSync(core::marker::PhantomData<*mut u8>); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| /// A trait for types that can be passed as arguments when spawning tasks | ||
| /// | ||
| /// This trait will only ever be implemented where type `Self::T` is `Self` | ||
| /// | ||
| /// The global `my_task::spawn` requires its args to be `Send`. This trait has to | ||
| /// be used because we can not have a function with a where clause which | ||
| /// requires a concrete type to be `Send` if that type is not `Send`. The compiler | ||
| /// will error out on us. However hiding that behind a dummy trait which is | ||
| /// only implemented for that same type enables us to defer the error to when | ||
| /// the user erroneously tries to call the function. | ||
| pub trait TaskArg { | ||
| /// This should always be same as `Self` | ||
| type T; | ||
| fn to(self) -> Self::T; | ||
| } | ||
|
|
||
| impl<T> TaskArg for T { | ||
| type T = T; | ||
| fn to(self) -> T { | ||
| self | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| #![no_main] | ||
|
|
||
| #[rtic::app(device = lm3s6965, dispatchers = [SSI0, GPIOA])] | ||
| mod app { | ||
| use super::*; | ||
|
|
||
| #[shared] | ||
| struct Shared {} | ||
|
|
||
| #[local] | ||
| struct Local {} | ||
|
|
||
| #[init] | ||
| fn init(_cx: init::Context) -> (Shared, Local) { | ||
| (Shared {}, Local {}) | ||
| } | ||
|
|
||
| #[task(priority = 1)] | ||
| async fn foo(_cx: foo::Context, _nsns: NotSendNotSync) {} | ||
|
|
||
| #[task(priority = 2)] | ||
| async fn bar(cx: bar::Context) { | ||
| cx.local_spawner.foo(Default::default()).ok(); | ||
| } | ||
| } | ||
|
|
||
| #[derive(Default, Debug)] | ||
| struct NotSendNotSync(core::marker::PhantomData<*mut u8>); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| error[E0599]: no method named `foo` found for struct `__rtic_internal_bar_LocalSpawner` in the current scope | ||
| --> ui/spawn-non-send-different-exec.rs:23:26 | ||
| | | ||
| 3 | #[rtic::app(device = lm3s6965, dispatchers = [SSI0, GPIOA])] | ||
| | ------------------------------------------------------------ method `foo` not found for this struct | ||
| ... | ||
| 23 | cx.local_spawner.foo(Default::default()).ok(); | ||
| | ^^^ method not found in `__rtic_internal_bar_LocalSpawner` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| #![no_main] | ||
|
|
||
| #[rtic::app(device = lm3s6965, dispatchers = [SSI0])] | ||
| mod app { | ||
| use super::*; | ||
|
|
||
| #[shared] | ||
| struct Shared {} | ||
|
|
||
| #[local] | ||
| struct Local {} | ||
|
|
||
| #[init] | ||
| fn init(_cx: init::Context) -> (Shared, Local) { | ||
| foo::spawn(NotSendNotSync::default()).ok(); | ||
| (Shared {}, Local {}) | ||
| } | ||
|
|
||
| #[task(priority = 1)] | ||
| async fn foo(_cx: foo::Context, _nsns: NotSendNotSync) {} | ||
| } | ||
|
|
||
| #[derive(Default, Debug)] | ||
| struct NotSendNotSync(core::marker::PhantomData<*mut u8>); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| error[E0277]: `*mut u8` cannot be sent between threads safely | ||
| --> ui/spawn-non-send-from-init.rs:15:20 | ||
| | | ||
| 15 | foo::spawn(NotSendNotSync::default()).ok(); | ||
| | ---------- ^^^^^^^^^^^^^^^^^^^^^^^^^ `*mut u8` cannot be sent between threads safely | ||
| | | | ||
| | required by a bound introduced by this call | ||
| | | ||
| = help: within `NotSendNotSync`, the trait `Send` is not implemented for `*mut u8` | ||
| note: required because it appears within the type `PhantomData<*mut u8>` | ||
| --> $RUST/core/src/marker.rs | ||
| | | ||
| | pub struct PhantomData<T: PointeeSized>; | ||
| | ^^^^^^^^^^^ | ||
| note: required because it appears within the type `NotSendNotSync` | ||
| --> ui/spawn-non-send-from-init.rs:24:8 | ||
| | | ||
| 24 | struct NotSendNotSync(core::marker::PhantomData<*mut u8>); | ||
| | ^^^^^^^^^^^^^^ | ||
| note: required by a bound in `__rtic_internal_foo_spawn` | ||
| --> ui/spawn-non-send-from-init.rs:3:1 | ||
| | | ||
| 3 | #[rtic::app(device = lm3s6965, dispatchers = [SSI0])] | ||
| | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ required by this bound in `__rtic_internal_foo_spawn` | ||
| = note: this error originates in the attribute macro `rtic::app` (in Nightly builds, run with -Z macro-backtrace for more info) | ||
|
|
||
| error[E0277]: `*mut u8` cannot be shared between threads safely | ||
| --> ui/spawn-non-send-from-init.rs:15:20 | ||
| | | ||
| 15 | foo::spawn(NotSendNotSync::default()).ok(); | ||
| | ---------- ^^^^^^^^^^^^^^^^^^^^^^^^^ `*mut u8` cannot be shared between threads safely | ||
| | | | ||
| | required by a bound introduced by this call | ||
| | | ||
| = help: within `NotSendNotSync`, the trait `Sync` is not implemented for `*mut u8` | ||
| note: required because it appears within the type `PhantomData<*mut u8>` | ||
| --> $RUST/core/src/marker.rs | ||
| | | ||
| | pub struct PhantomData<T: PointeeSized>; | ||
| | ^^^^^^^^^^^ | ||
| note: required because it appears within the type `NotSendNotSync` | ||
| --> ui/spawn-non-send-from-init.rs:24:8 | ||
| | | ||
| 24 | struct NotSendNotSync(core::marker::PhantomData<*mut u8>); | ||
| | ^^^^^^^^^^^^^^ | ||
| note: required by a bound in `__rtic_internal_foo_spawn` | ||
| --> ui/spawn-non-send-from-init.rs:3:1 | ||
| | | ||
| 3 | #[rtic::app(device = lm3s6965, dispatchers = [SSI0])] | ||
| | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ required by this bound in `__rtic_internal_foo_spawn` | ||
| = note: this error originates in the attribute macro `rtic::app` (in Nightly builds, run with -Z macro-backtrace for more info) |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a breaking change. For some reason the compiler can not auto demote
&mut Tto&Twhen generics are involved (not sure of the rules here).Without the above fix, we get
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Copying AfoHT's link on matrix: https://doc.rust-lang.org/reference/type-coercions.html#r-coerce.types.mut-reborrow