- Sponsor
-
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
add a Handle::current() for getting access to TLS Runtime #2040
Conversation
tokio/src/runtime/blocking/mod.rs
Outdated
@@ -60,6 +60,8 @@ cfg_not_blocking_impl! { | |||
where | |||
F: FnOnce() -> R, | |||
{ | |||
let ctx = crate::runtime::context::ThreadContext::clone_current(); |
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 don't see why this isn't necessary and wasn't here before...
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.
Currently, since BlockingPool::enter
only sets the context for the blocking pool spawner, the no-op version doesn't need to do anything. This fn is only here to make feature flag code easier.
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.
Ok. Would you like me to remove this?
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 removing it would be best.
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.
Removed.
} | ||
} | ||
let ctx = crate::runtime::context::ThreadContext::clone_current(); | ||
let _e = ctx.with_blocking_spawner(self.clone()).enter(); |
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'm confused about why the Cell with a raw pointer was necessary here, when Spawner has an internal Arc.
So I simplified this to focus on just using that fact. I'm sure I'm missing something. The Drop for ThreadContext along with enter performs the same revert that was happening with the Reset before.
tokio/src/runtime/context.rs
Outdated
feature = "io-std", | ||
feature = "rt-threaded", | ||
))] | ||
pub(crate) fn with_blocking_spawner( |
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 this can be replaced w/ the cfg_blocking_impl
macro (here). It cannot go around fn defs, so you would split it out to another impl block:
cfg_blocking_impl! {
impl ThreadContext {
pub(crate) fn with_blocking_spawner(....) { ... }
}
}
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.
Ok, done.
tokio/src/runtime/context.rs
Outdated
pub(crate) fn io_handle() -> crate::runtime::io::Handle { | ||
#[cfg(any(not(feature = "io-driver"), loom))] |
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.
Could you elaborate on why this change was made?
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.
Oh. I think it was failing CI with a unused warning? Though I can try reverting it.
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.
Should the fn io_handle
be scoped w/ a cfg
then? The fns below have had their cfg
bits removed. I'd be interested in seeing the error.
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.
So the way that it's written right now, the field is not optional, it gets swapped out for ()
when these features aren't enabled (apparently). Here's the error with a default cargo check with those changed reverted::
error[E0308]: match arms have incompatible types
--> tokio/src/runtime/context.rs:105:21
|
103 | CONTEXT.with(|ctx| match *ctx.borrow() {
| ____________________________-
104 | | Some(ref ctx) => ctx.io_handle.clone(),
| | --------------------- this is found to be of type `()`
105 | | None => None,
| | ^^^^ expected (), found enum `std::option::Option`
106 | | })
| |_________- `match` arms have incompatible types
|
= note: expected type `()`
found type `std::option::Option<_>`
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.
Ah! What if you sub None
for Default::default()
?
That said, it is curious that io_handle()
is required even when the io-driver
feature isn't enabled.
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.
This stems from the way the Handle::Current works, which asks for all the components of ThreadContext, where Handle has all of these, so cfging off the io_handle function will spider out for a lot of other changes. Maybe Default::default() will work...
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.
Ok, that works.
This looks good to me. I added some minor inline comments. |
ok, @carllerche , I think I cleaned up what could be. Let me know if you want me to take care of anything else. |
I merged master & resolved the conflict. |
Thanks! I didn't notice we ended up with a conflict. |
I've been merging a lot of PRs |
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.
Thanks for pushing this through 👍
No problem. It's really benefiting from all the work that was done on ThreadContext. Thanks for managing all of this! |
Motivation
Get a Runtime::Handle to the current thread local storage Runtime components.
Solution
This adds a blocking::Spawner reference to ThreadContext. With that we then have access to all the components needed for a Handle::current. This will panic if components haven't been registered that are required.