Trait shared_bus::mutex::BusMutex
source · pub trait BusMutex<T> {
fn create(v: T) -> Self;
fn lock<R, F: FnOnce(&T) -> R>(&self, f: F) -> R;
}
Expand description
An abstraction over a mutex lock.
Any type that can implement this trait can be used as a mutex for sharing a bus.
If the std
feature is enabled, BusMutex
is implemented for std::sync::Mutex
.
If the cortexm
feature is enabled, BusMutex
is implemented for [cortex_m::interrupt::Mutex
].
If there is no feature available for your Mutex type, you have to write one yourself. It should
look something like this (for cortex_m
as an example):
extern crate cortex_m;
// You need a newtype because you can't implement foreign traits on
// foreign types.
struct MyMutex<T>(cortex_m::interrupt::Mutex<T>);
impl<T> shared_bus::BusMutex<T> for MyMutex<T> {
fn create(v: T) -> MyMutex<T> {
MyMutex(cortex_m::interrupt::Mutex::new(v))
}
fn lock<R, F: FnOnce(&T) -> R>(&self, f: F) -> R {
cortex_m::interrupt::free(|cs| {
let v = self.0.borrow(cs);
f(v)
})
}
}
type MyBusManager<L, P> = shared_bus::BusManager<MyMutex<L>, P>;