-
Notifications
You must be signed in to change notification settings - Fork 993
Q: help understanding NoopRawMutex vs. ThreadModeRawMutex #4034
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
Comments
Basically:
This means that Conversely, NoopRawMutex cannot be put in a static, and since you can't pass borrowed data to other tasks, it can't be used to share data across tasks. You can use NoopRawMutex locally within a task, shared across multiple futures potentially. The use case for NoopRawMutex would be if you are using something like a shared i2c/spi port within a single task, and you need a mutex for guarding access to the bus. The "Raw" part of the mutex is what guards the Mutex's state itself:
With ANY Raw mutex, the outer Mutex acts the same: If the resource is locked, we yield and await. If the resource is not, we take exclusive access and continue. The Raw Mutex guarantees only take place at lock/unlock, NOT the entire time the outer Mutex is locked. In general:
In general:
|
Ahhhh, that's an awesome explanation, thank you so, so much, for caring and for your time and effort writing it up! 😍 Honestly, I'd love if you would just copy & paste it verbatim into the rustdoc for future readers 😃 (including a future me 😂) To summarize in my words - please correct me if I got it wrong! - the difference between
Did I get it reasonably right? |
I am starting to try learning and using async features in embassy, and preparing to share data between tasks. Through the examples I found at: https://embassy.dev/book/#_sharing_peripherals_between_tasks, I understand, that when instantiating various syncing types (like Mutex, channels, etc.), I will need to make a choice of a "raw" mutex to specialize them with. In the
embassy_sync::blocking_mutex::raw
namespace, I see three types. TheCriticalSectionRawMutex
seems only necessary when sharing data between interrupts and non-interrupt tasks, this sounds clear from its description.However, I'm super confused as to when I should use the
NoopRawMutex
vs. theThreadModeRawMutex
. Based on the description in asyncMutex
, they both seem explained as for use when data is "shared between tasks running on the same executor". Ok, so I cannot use either when sharing between tasks on different executors, got it. But then, theThreadMode...
one seems to get some additional qualifiers - in one place, it is described as "...but you want a singleton", and in another one as "...only allows borrowing from thread mode". Both of those qualifiers don't seem to tell me much that I would manage to understand:NoopRawMutex
results in the guarded object being copied? how is it a mutex in such case? Or does this mean something completely else that I don't get?NoopRawMutex
? isn't the whole point of using any kind of mutex to share data between tasks? again, I'm finding myself making a blank stare with a feeling of not knowing what to grasp to get some firm ground...I'd be super grateful for some help trying to understand those! I tried googling up, I tried searching the issues, checking the FAQ, still haven't managed to find any other explanation...
edit: to add to my confusion, from what I recall some examples seemed to use the
NoopRawMutex
, while others theThreadModeRawMutex
- but I still wasn't able to decipher the criteria for when I should choose one over the other...TIA!
The text was updated successfully, but these errors were encountered: