rtic-rs / rtic

Real-Time Interrupt-driven Concurrency (RTIC) framework for ARM Cortex-M microcontrollers

Home Page:https://rtic.rs

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

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

smmoosavi opened this issue · 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.

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!

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

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.

Did you figure this out by using the Arbiter?

@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)

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.