parking_lot/util.rs
1use std::time::{Duration, Instant};
2
3/// Runs `f`, aborting the process if it unwinds.
4///
5/// This is used after publishing a raw lock ownership transition which cannot
6/// be rolled back safely.
7#[inline]
8pub(crate) fn abort_on_panic<T>(f: impl FnOnce() -> T) -> T {
9 struct AbortOnDrop;
10
11 impl Drop for AbortOnDrop {
12 fn drop(&mut self) {
13 panic!("aborting due to panic while changing lock ownership");
14 }
15 }
16
17 let guard = AbortOnDrop;
18 let result = f();
19 core::mem::forget(guard);
20 result
21}
22
23#[inline]
24pub fn to_deadline(timeout: Duration) -> Option<Instant> {
25 Instant::now().checked_add(timeout)
26}