Skip to content

Q: help understanding NoopRawMutex vs. ThreadModeRawMutex #4034

Closed
@akavel

Description

@akavel

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. The CriticalSectionRawMutex 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. the ThreadModeRawMutex. Based on the description in async Mutex, 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, the ThreadMode... 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:

  • as for "you want a singleton": isn't the whole point of mutexes in general to guard a "singleton" resource? I don't seem to be able to envision an alternative at the moment; does it mean that the 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?
  • as for "borrowing from thread mode": what is a "thread mode"? I don't seem to recall seeing this mentioned elsewhere in embassy docs; I only remember seeing "tasks" mentioned - what's their relation to "threads"? or is it some completely different concept? and again - how does this differ to 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 the ThreadModeRawMutex - but I still wasn't able to decipher the criteria for when I should choose one over the other...

TIA!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions