Skip to main content

blktrace_rs/
async_api.rs

1//! Async wrappers around the blocking core, behind the `async` feature.
2//!
3//! The tracing core is inherently blocking (relay channel reads, ioctl
4//! waits, a deliberate flush wait). These wrappers push all of it onto the
5//! tokio blocking pool, so async callers never stall their executor.
6
7use std::path::Path;
8
9use tokio::task::spawn_blocking;
10
11use crate::error::BlktraceError;
12use crate::session::{BlkTrace, BlkTraceReport, BlkTraceSession};
13
14impl BlkTrace {
15    /// Async version of [`BlkTrace::monitor`].
16    pub async fn monitor_async(path: impl AsRef<Path>) -> Result<BlkTraceSession, BlktraceError> {
17        let path = path.as_ref().to_owned();
18        spawn_blocking(move || BlkTrace::monitor(path))
19            .await
20            .map_err(|_| BlktraceError::ThreadPanicked)?
21    }
22}
23
24impl BlkTraceSession {
25    /// Async version of [`BlkTraceSession::shutdown`].
26    pub async fn shutdown_async(self) -> Result<BlkTraceReport, BlktraceError> {
27        spawn_blocking(move || self.shutdown())
28            .await
29            .map_err(|_| BlktraceError::ThreadPanicked)?
30    }
31}