rtic icon indicating copy to clipboard operation
rtic copied to clipboard

Question: `Send` shared resource container for `thumbv6m-none-eabi` target

Open smmoosavi opened this issue 1 year ago • 6 comments

Hi,

I want to define a shareable mutable container like Rc<RefCell> and store that in the #[shared] struct. But Rc<RefCell> is not Send.

Types of #[shared] resources have to be Send.

Then I tried to use the Arbiter struct. It's not Clone so I have to wrap it in Arc. Arc<Arbiter> is working as expected. but Arc is not Implemented for the thumbv6m-none-eabi target.

use alloc::sync::Arc;
//         ^^^^ could not find `sync` in `alloc`

I also tried &Arbiter, but it forced me to have a lifetime for my struct. which cause lifetime error when used by Mutex:

assert_eq!(mtx.lock(|option| option.wait_for_value()).await, 1);
//                   ------- ^^^^^^^^^^^^^^^^^^^^^^^ returning this value requires that `'1` must outlive `'2`
//                   |     |
//                   |     return type of closure is async_option::wait_for_value_future::WaitForValueFuture<'2, i32>
//                   has type `&'1 mut async_option::AsyncOption<i32>`

What is the best alternative for the Rc<RefCell> for the thumbv6m-none-eabi target?

I created a sample repo with multiple implementations.

smmoosavi avatar Sep 11 '23 13:09 smmoosavi

Hi,

The only way to have non-Send is in a resource in an priority = 0 task. If you want to store something (where the inner value is Send), Arbiter is a good choice. I'm not sure I understand your usecase good enough to give any better advice.

Hope that helps!

korken89 avatar Sep 13 '23 19:09 korken89

I'm not sure I understand your usecase good enough to give any better advice.

We need access to the same Arbiter from multiple tasks. The Arc struct was the perfect solution for us, But it does not exist in thumbv6m-none-eabi

smmoosavi avatar Sep 18 '23 14:09 smmoosavi

Hi,

The rtic_sync::arbiter::Arbiter should work from multiple tasks, it is Sync. So either share it via a static or an immutable resource would be my recommendation. Arc is not needed for Arbiter.

korken89 avatar Sep 20 '23 18:09 korken89

Did you figure this out by using the Arbiter?

AfoHT avatar Oct 04 '23 18:10 AfoHT

@AfoHT

No, @korken89's recommendation is more about how to avoid the problem. Sometimes I have a non-static struct that needs one simple field and one shared field, so it can't be either static or immutable

I add the ValueContainer (rc version) example to the repo (arbiter version which have same life-time error)

smmoosavi avatar Oct 07 '23 13:10 smmoosavi

I found a portable-atomic-util crate that provides the Arc type for thumbv6m-none-eabi. It is part of portable atomic and uses portable-atomic crate internally. rtic use atomic-polyfill which is deprecated and encourages the use portable-atomic instead.

smmoosavi avatar Oct 29 '23 15:10 smmoosavi