Skip to content

Commit ff128fb

Browse files
committed
Make it compatible with lock_api::RawMutex
1 parent a34dd06 commit ff128fb

File tree

5 files changed

+70
-1
lines changed

5 files changed

+70
-1
lines changed

source/mutex-traits/Cargo.toml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,16 @@ categories = [
1515
documentation = "https://docs.rs/mutex-traits/"
1616

1717
[features]
18+
default = ["compatible-with-lock_api-0_4"]
1819
std = []
20+
compatible-with-lock_api-0_4 = ["dep:lock_api-0_4"]
1921

2022
[package.metadata.docs.rs]
2123
all-features = true
2224
rustdoc-args = ["--cfg", "docsrs"]
25+
26+
[dependencies.lock_api-0_4]
27+
package = "lock_api"
28+
version = "0.4"
29+
default-features = false
30+
optional = true
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
use crate::{ConstInit, RawMutex};
2+
use lock_api_0_4 as lock_api;
3+
4+
impl<R: lock_api::RawMutex> ConstInit for R {
5+
const INIT: Self = R::INIT;
6+
}
7+
8+
unsafe impl<R: lock_api::RawMutex> RawMutex for R {
9+
type GuardMarker = R::GuardMarker;
10+
11+
#[inline]
12+
#[track_caller]
13+
fn try_lock(&self) -> bool {
14+
lock_api::RawMutex::try_lock(self)
15+
}
16+
17+
#[inline]
18+
#[track_caller]
19+
fn lock(&self) {
20+
lock_api::RawMutex::lock(self)
21+
}
22+
23+
#[inline]
24+
#[track_caller]
25+
unsafe fn unlock(&self) {
26+
lock_api::RawMutex::unlock(self)
27+
}
28+
29+
#[inline]
30+
#[track_caller]
31+
fn is_locked(&self) -> bool {
32+
lock_api::RawMutex::is_locked(self)
33+
}
34+
}

source/mutex-traits/src/lib.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
#![cfg_attr(not(feature = "std"), no_std)]
44
#![cfg_attr(docsrs, feature(doc_cfg, doc_auto_cfg))]
55

6+
#[cfg(feature = "compatible-with-lock_api-0_4")]
7+
mod impl_for_lock_api;
8+
69
/// Const Init Trait
710
///
811
/// This trait is intended for use when implementers of [`ScopedRawMutex`] that can
@@ -82,7 +85,7 @@ pub unsafe trait ScopedRawMutex {
8285
/// Some mutex implementations may not be able to implement the full `RawMutex`
8386
/// trait, and may only be able to implement the closure-based
8487
/// [`ScopedRawMutex`] subset. For example, implementations for the
85-
/// [`critical-section`] crate (in [`mutex::raw_impls::cs`][cs]) can only
88+
/// `critical-section` crate (in [`mutex::raw_impls::cs`][cs]) can only
8689
/// implement the `ScopedRawMutex` trait. However, in general, **mutex
8790
/// implementations that *can* implement the more general `RawMutex` trait
8891
/// should prefer to do so**, as they will be able to be used in code that

source/mutex/Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,17 @@ version = "0.4"
3131
default-features = false
3232
optional = true
3333

34+
[dev-dependencies]
35+
parking_lot = "0.12"
36+
3437
[features]
3538
default = [
3639
"impl-critical-section",
3740
]
3841
impl-critical-section = ["dep:critical-section"]
3942
impl-unsafe-cortex-m-single-core = []
4043
impl-lock_api-0_4 = ["dep:lock_api-0_4"]
44+
compatible-with-lock_api-0_4 = ["mutex-traits/compatible-with-lock_api-0_4"]
4145
# Enables `fmt::Debug` and `fmt::Display` implementations.
4246
#
4347
# These can be disabled when minimizing binary size is important.

source/mutex/src/lib.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -456,4 +456,24 @@ mod test {
456456
.unwrap();
457457
assert_eq!(Some(1), res);
458458
}
459+
460+
#[cfg(feature = "compatible-with-lock_api-0_4")]
461+
#[test]
462+
fn lock_api_compatibility() {
463+
let mutex = BlockingMutex::<parking_lot::RawMutex, u32>::new(0);
464+
let mut guard = mutex.lock();
465+
assert_eq!(*guard, 0);
466+
*guard = 1;
467+
drop(guard);
468+
469+
let mut guard = mutex.lock();
470+
assert_eq!(*guard, 1);
471+
*guard = 2;
472+
drop(guard);
473+
474+
mutex.with_lock(|data| {
475+
assert_eq!(*data, 2);
476+
*data = 3;
477+
});
478+
}
459479
}

0 commit comments

Comments
 (0)