Skip to content

Generic Mutex Trait #119

Description

@Rahix

While writing shared-bus I was hit with the lack of a standardized Mutex type. std solves this by implementing the mutex type differently for all architectures it supports and this is what I currently do in shared-bus as well. However, this is not feasible in the long run, because we want to support all architectures that implement embedded-hal. In my opinion, embedded-hal needs a standardized mutex trait and I would propose it to look similar to the one I have in shared-bus right now:

pub trait BusMutex<T> {
    /// Create a new instance of this mutex type containing the value `v`.
    fn create(v: T) -> Self;

    /// Lock the mutex for the duration of the closure `f`.
    fn lock<R, F: FnOnce(&T) -> R>(&self, f: F) -> R;
}

As explanation:

  • The create method is needed to allow drivers to transparently create mutex objects. I specifically chose not to name it new to avoid name conflicts.
  • In std, lock returns a lock-guard but this design is not feasible here. The reason is that the mutex type defined in bare-metal is to be used with a closure and a CriticalSection.

cc @jamesmunns, @therealprof

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions