Skip to content
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

Handle/guard support for current thread pool #1097

Open
nazar-pc opened this issue Oct 27, 2023 · 1 comment
Open

Handle/guard support for current thread pool #1097

nazar-pc opened this issue Oct 27, 2023 · 1 comment

Comments

@nazar-pc
Copy link

I'm doing some really not present things in the code righ now just to make rayon-based parallel code running on a specific thread pool.

Specifically I have two different thread pools and a piece of code that depending on some conditions needs to run on one of those thread pools.

The only way do do it right now is to use thread_pool.install(), but something like this would have been much more convenient:

{
    let _guard = if foo { thread_pool_a.handle().enter() } else { thread_pool_b.handle().enter() };

    // Do compute here
}

Moreover, Handle::current() should also be possible to capture current thread pool's handle, regardless of whether custom or global thread pool is used.

This is similar in behavior to tokio and tracing that both have local guards that allow you to enter/exit runtime/context/scope/span/etc.

My educated guess is that something like this is already implicitly present, just not exposed to the user.

@cuviper
Copy link
Member

cuviper commented Oct 27, 2023

The implicit support is just our thread-local WorkerThread pointer -- so if the current thread is already part of a pool, it will use that, otherwise we use the global pool. Then install works by sending its closure as a job to execute in the pool, so whatever runs in that closure will also see that thread-local connected to the pool.

I guess for a guard, we would need a separate TLS which is checked before anything else. I fear that will add overhead to the critical path of what would normally be a local-only operation though.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants