Skip to content

Generic Mutex Trait #119

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
Rahix opened this issue Jan 1, 2019 · 5 comments
Open

Generic Mutex Trait #119

Rahix opened this issue Jan 1, 2019 · 5 comments

Comments

@Rahix
Copy link

Rahix commented Jan 1, 2019

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

@eldruin
Copy link
Member

eldruin commented Jan 1, 2019

I would also like to use this type for synchronization of the accesses to the I/O pins in the pcf847x I/O expander crate when using the "virtual" pins after a split() and probably similarly in the xca9548a I2C switch crate.
What do you guys think about the Bus in the name?

@Rahix
Copy link
Author

Rahix commented Jan 1, 2019

Oh sorry, the name should probably be just Mutex, BusMutex is what it is called in shared-bus, I forgot to change it ...

@therealprof
Copy link
Contributor

@Rahix seems like a good idea. shared-bus goes a long way towards solving #35 but this really should be more ubiquitous to apply to all applications and architectures and more readily available.

Is the idea of putting BusMutex in the hal traits that we can add additional traits which allow us to directly share busses without having to add extra crates and jump through hoops?

@Rahix
Copy link
Author

Rahix commented Jan 3, 2019

Well, first of all I would just suggest to add a Mutex trait to embedded-hal that is implemented by each bsp. This allows drivers to have a generic synchronization primitive available.

As soon as that is the case, using shared-bus and the like would be much easier as you no longer have to specify which mutex you want to use via a feature flag. And as @eldruin mentioned, this is also of benefit for other drivers that need to synchronize peripheral accesses.

allow us to directly share busses

No, you will still need shared-bus for the manager and proxy types. We could think about moving them into the hal as well, but I am not sure if that is a good idea ... In my opinion it doesn't hurt to have a different crate for that. The reason it hurts right now is because the mutex type is not in hal.

@jonas-schievink
Copy link
Contributor

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants