Skip to content

Commit b9ed877

Browse files
committed
Adds MtxLock
1 parent 41b8001 commit b9ed877

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

src/lock/mod.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,24 @@ pub trait LockObject: Sized {}
77
pub trait Mtx<K: Kernel>: Sized {
88
fn lock_mut(&mut self) -> &mut K::LockObject;
99
}
10+
11+
/// RAII struct to unlock a mutex when dropped.
12+
pub struct MtxLock<K: Kernel> {
13+
kern: K,
14+
mtx: *mut K::Mtx,
15+
}
16+
17+
impl<K: Kernel> MtxLock<K> {
18+
/// # Safety
19+
/// `mtx` cannot be null.
20+
pub unsafe fn new(kern: K, mtx: *mut K::Mtx) -> Self {
21+
kern.mtx_lock_flags(mtx, 0, c"".as_ptr(), 0);
22+
Self { kern, mtx }
23+
}
24+
}
25+
26+
impl<K: Kernel> Drop for MtxLock<K> {
27+
fn drop(&mut self) {
28+
unsafe { self.kern.mtx_unlock_flags(self.mtx, 0, c"".as_ptr(), 0) };
29+
}
30+
}

0 commit comments

Comments
 (0)