-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
rt: add Handle::block_on #3549
rt: add Handle::block_on #3549
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -201,6 +201,18 @@ impl Handle { | |
let _ = self.blocking_spawner.spawn(task, &self); | ||
handle | ||
} | ||
|
||
/// TODO: write docs if this is a good direction | ||
pub fn block_on<F: Future>(&self, future: F) -> F::Output { | ||
// Enter the **runtime** context. This configures spawning, the current I/O driver, ... | ||
let _rt_enter = self.enter(); | ||
|
||
// Enter a **blocking** context. This prevents blocking from a runtime. | ||
let mut _blocking_enter = crate::runtime::enter(true); | ||
|
||
// Block on the future | ||
_blocking_enter.block_on(future).expect("failed to park thread") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Then this should be a block on that runs the task and can wake up on runtime shutdown. We likely wanna use the same notify trick? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The future isn't a task per se. When the runtime is shutdown, in theory all resources should transition to a "shutdown" state and in-flight operations canceled. This would result in this block_on waking up. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Even if the task is doing nothing? E.g. if I just call There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hmm, I guess my gut is that calling |
||
} | ||
} | ||
|
||
/// Error returned by `try_current` when no Runtime has been started | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can enter check if the runtime is still alive?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think you opened a different issue, but that is someone of an orthogonal concern :)