Rahix / shared-bus

Crate for sharing buses between multiple devices
Apache License 2.0
129 stars 34 forks source link

What kind of shared bus should I use? #51

Closed za-songguo closed 1 year ago

za-songguo commented 1 year ago

I have already wrapped my peripherals with Arc and Mutex, like this:

pub struct AppPeripherals<'a> {
    pub sht31: Arc<Mutex<...>>,
    pub max17048: Arc<Mutex<...>>,
}

(The peripherals will be shared across multiple threads.) What kind of shared bus should I use? BusManagerSimple or BusMutex?

Rahix commented 1 year ago

Hi, sorry for the late response.

When every peripheral lives in its own mutex, you still need one additional mutex guarding the bus itself to ensure mutually exclusive access to it. As it seems you are using the Mutex from std for your peripherals, the BusManagerStd would be most appropriate.

However, you can also consider going a different route: You could put all peripherals into a single big mutex and then ensure exclusive access using the BusManagerAtomicCheck. This is particularly useful for frameworks like RTIC. Check the linked documentation for more details.

za-songguo commented 1 year ago

Thanks for your reply. It is useful for me.