Skip to main content

forge/os/
lock.rs

1use crate::os::mutex::Mutex;
2
3/// An RAII guard that locks a [`Mutex`] on creation and unlocks it on drop.
4pub struct ScopedLock<'a> {
5    mtx: &'a Mutex,
6}
7
8impl<'a> ScopedLock<'a> {
9    /// Locks `mtx` and returns a guard that unlocks it when dropped.
10    pub fn new(mtx: &'a Mutex) -> Self {
11        mtx.lock();
12        Self { mtx }
13    }
14}
15
16impl<'a> Drop for ScopedLock<'a> {
17    fn drop(&mut self) {
18        self.mtx.unlock();
19    }
20}