-
-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(maitake-sync):
mutex-traits
integration (#482)
Currently, `maitake-sync`'s blocking `Mutex` and `RwLock` use spinlocks to wait for the lock to become available. This works on (almost) all platforms,[^cas] but there are a number of reasons it might be less than desirable. For example: - You might want an IRQ-safe mutex so that data protected by the mutex can also be shared with interrupt handlers. In that case, the mutex must also disable IRQs when locked, and re-enable them when unlocked. - You might want to use an alternative mechanism for waiting, such as hardware lock elision, an OS mutex or something when not on `#![no_std]`, or an existing library RTOS mutex or similar. - You might be on a strictly single-threaded platform where any "waiting" is inherently a deadlock, and you would prefer to make any attempt to lock an already-locked mutex into a panic instead. Therefore, @jamesmunns and I have written the [`mutex-traits`] crate, to abstract over mutex implementations. This trait is similar to [`lock_api`], but with a `ScopedRawMutex` trait to support mutexen based on [`critical-section`], which cannot be locked and unlocked at arbitrary times and instead requires a closure. This branch updates `maitake-sync` to use [`mutex-traits`]. In particular: - The blocking `Mutex` type is generic over a `Lock` type parameter, which may implement `mutex_traits::RawMutex` or `mutex_traits::ScopedRawMutex`. - `Mutex` has scoped lock methods when `Lock: ScopedRawMutex`, and guard methods require `Lock: RawMutex`. - All async primitives that use a blocking `Mutex` are now generic over a `Lock` type for the underlying mutex. Everything except for `Semaphore` and `RwLock` only requires `ScopedRawMutex`, but `Semaphore` and `RwLock` require `RawMutex`. - By default, `Mutex`es are constructed with a `DefaultMutex` as the `ScopedRawMutex` impl, which tries to pick the right behavior based on features/target config. This only implements `ScopedRawMutex`. [`mutex-traits`]: https://crates.io/crates/mutex-traits [`lock_api`]: https://crates.io/crates/lock-api [`critical-section`]: https://crates.io/crates/critical-section [^cas]: Provided they have atomic CAS... BREAKING CHANGE: Renamed `spin::Mutex` and `spin::RwLock` to `blocking::Mutex` and `blocking::RwLock`. `blocking::Mutex::new` now constructs a `blocking::Mutex<T, DefaultMutex>`, which only provides scoped lock methods. To construct a mutex that also has RAII guard lock methods, use `blocking::Mutex::new_with_raw_mutex` to provide a `RawMutex` implementation.
- Loading branch information
Showing
44 changed files
with
2,855 additions
and
1,134 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.