This commit is contained in:
Igor Katson 2021-07-01 19:17:44 +01:00
parent a6981231c1
commit 5942e6a9d5
12 changed files with 186 additions and 148 deletions

View file

@ -19,9 +19,22 @@ pub fn spawn<N: Display + 'static + Send>(
});
}
pub fn spawn_block_in_place<F: FnOnce() -> R, R>(f: F) -> R {
// Have this wrapper so that it's easy to switch to just f() when
// using tokio's single-threaded runtime. Single-threaded runtime is
// easier to read with time profilers.
tokio::task::block_in_place(f)
#[derive(Clone, Copy, Debug)]
pub struct BlockingSpawner {
allow_tokio_block_in_place: bool,
}
impl BlockingSpawner {
pub fn new(allow_tokio_block_in_place: bool) -> Self {
Self {
allow_tokio_block_in_place,
}
}
pub fn spawn_block_in_place<F: FnOnce() -> R, R>(&self, f: F) -> R {
if self.allow_tokio_block_in_place {
return tokio::task::block_in_place(f);
}
return f();
}
}