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>
impl<T> DriverMutex<T>
Sourcepub fn new(
resource: &'static T,
clients: &'static [OptionalCell<&'static dyn DriverMutexClient>],
queue_buffer: &'static mut [MaybeUninit<usize>],
) -> Self
pub fn new( resource: &'static T, clients: &'static [OptionalCell<&'static dyn DriverMutexClient>], queue_buffer: &'static mut [MaybeUninit<usize>], ) -> Self
Creates a new DriverMutex.
Sourcepub fn add_client(
&'static self,
client: &'static dyn DriverMutexClient,
) -> Option<DriverMutexHandle>
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.
Sourcepub fn request(&self, handle: DriverMutexHandle) -> Result<(), ErrorCode>
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.