Skip to main content

DriverMutex

Struct DriverMutex 

Source
pub struct DriverMutex<T: 'static> { /* private fields */ }
Expand description

Specialized mutex used to guard HIL driver implementations

DriverMutex is a container type which wraps and provides mutual exclusion semantics for a HIL implementation within the Tock kernel. Even though Tock is fundamentally single-threaded, it is nevertheless highly concurrent and therefore susceptible to race conditions within its event loop when multiple kernel components are sharing the same underlying HIL driver.

§Queuing

DriverMutex contains an integrated request queue. Multiple clients can request access to the underlying resource, and they will be given access on a first-come first-served basis (as opposed to a traditional mutex, which would just return some “busy” error code).

The length of the internal queue is fixed, meaning it can only support a limited number of clients. Queue length is specified at construction time and may vary from instance to instance as needed.

§RAII

Access to the underlying resource is mediated via DriverMutexAny and DriverMutexRef. These are RAII guards which “release” the mutex upon being dropped. This provides some level of assurance at compile time that the underlying resource can only be accessed while the mutex is held.

Each acquisition provides one guard. When that guard is dropped, the mutex is released. If another client is queued, a new RAII guard is passed to its ready() callback. This process is carried out within a [deferred call][DeferredCall] to avoid doing too much work directly within the RAII drop() method, which could otherwise lead to reentrancy hazards, poor performance, or overly deep call stacks.

A client may have either one queued request or one active guard per mutex. It must drop its guard before requesting that mutex again. Different mutexes may be acquired independently.

Implementations§

Source§

impl<T> DriverMutex<T>

Source

pub fn new( resource: &'static T, clients: &'static [OptionalCell<&'static dyn DriverMutexClient>], queue_buffer: &'static mut [MaybeUninit<usize>], ) -> Self

Creates a new DriverMutex.

Source

pub fn add_client( &'static self, client: &'static dyn DriverMutexClient, ) -> Option<DriverMutexHandle>

Registers a client with this DriverMutex.

If the client was added successfully, a corresponding DriverMutexHandle is returned which can be used later when calling DriverMutex::request().

To support more complex scenarios, the same client may be added multiple times. In this case, a new slot is not consumed, and this function returns a copy of the handle that was originally returned when the client was first added.

DriverMutex supports only a fixed number of clients. If there is no more room for another client, this method returns None.

Source

pub fn request(&self, handle: DriverMutexHandle) -> Result<(), ErrorCode>

Requests access to the underlying resource.

Client is added to the internal queue. When the resource becomes available, it will be passed to the client’s ready() callback. To avoid reentrancy issues, this is guaranteed to happen in a separate Deferred Call.

§Errors

Returns [ErrorCode::ALREADY] if the client is already waiting in the queue or holds the active guard.

Returns [ErrorCode::INVAL] if passed a handle created from a different DriverMutex instance.

Returns [ErrorCode::FAIL] if the internal queue or state is already borrowed.

Trait Implementations§

Source§

impl<T> DeferredCallClient for DriverMutex<T>

Source§

fn handle_deferred_call(&'static self)

Software interrupt function that is called when the deferred call is triggered.
Source§

fn register(&'static self)

Auto Trait Implementations§

§

impl<T> !Freeze for DriverMutex<T>

§

impl<T> !RefUnwindSafe for DriverMutex<T>

§

impl<T> !Send for DriverMutex<T>

§

impl<T> !Sync for DriverMutex<T>

§

impl<T> !UnwindSafe for DriverMutex<T>

§

impl<T> Unpin for DriverMutex<T>

§

impl<T> UnsafeUnpin for DriverMutex<T>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.