Add support for LazyLock

This commit is contained in:
2025-01-17 22:31:40 +01:00
parent 40f835afd2
commit 25ae542ade
6 changed files with 152 additions and 13 deletions

View File

@@ -193,6 +193,14 @@ impl MutexId {
unreachable!("Tried to drop lock for mutex {:?} but it wasn't held", self)
});
}
/// Execute the given closure while the guard is held.
pub fn with_held<T>(&self, f: impl FnOnce() -> T) -> T {
// Note: we MUST construct the RAII guard, we cannot simply mark held + mark released, as
// f() may panic and corrupt our state.
let _guard = self.get_borrowed();
f()
}
}
impl Default for MutexId {