forked from tokio-rs/tokio
-
Notifications
You must be signed in to change notification settings - Fork 0
7921: runtime: add worker_index() getter
#70
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
Open
martin-augment
wants to merge
3
commits into
master
Choose a base branch
from
pr-7921-2026-03-03-12-39-33
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,94 @@ | ||
| #![warn(rust_2018_idioms)] | ||
| #![cfg(all( | ||
| feature = "full", | ||
| tokio_unstable, | ||
| not(target_os = "wasi"), | ||
| target_has_atomic = "64" | ||
| ))] | ||
|
|
||
| use tokio::runtime::{self, Runtime}; | ||
|
|
||
| #[test] | ||
| fn worker_index_multi_thread() { | ||
| let rt = Runtime::new().unwrap(); | ||
| rt.block_on(async { | ||
| let index = tokio::task::spawn(async { runtime::worker_index() }) | ||
| .await | ||
| .unwrap(); | ||
| let num_workers = rt.metrics().num_workers(); | ||
| let index = index.expect("should be Some on worker thread"); | ||
| assert!( | ||
| index < num_workers, | ||
| "worker_index {index} >= num_workers {num_workers}" | ||
| ); | ||
| }); | ||
| } | ||
|
|
||
| #[test] | ||
| fn worker_index_current_thread() { | ||
| let rt = runtime::Builder::new_current_thread() | ||
| .enable_all() | ||
| .build() | ||
| .unwrap(); | ||
| rt.block_on(async { | ||
| let index = runtime::worker_index(); | ||
| assert_eq!(index, Some(0)); | ||
| }); | ||
| } | ||
|
|
||
| #[test] | ||
| fn worker_index_outside_runtime() { | ||
| assert_eq!(runtime::worker_index(), None); | ||
| } | ||
|
|
||
| #[test] | ||
| fn worker_index_matches_metrics_worker_thread_id() { | ||
| let rt = runtime::Builder::new_multi_thread() | ||
| .worker_threads(4) | ||
| .enable_all() | ||
| .build() | ||
| .unwrap(); | ||
| let metrics = rt.metrics(); | ||
|
|
||
| rt.block_on(async { | ||
| // Spawn a task and verify the worker_index matches the metrics index | ||
| tokio::task::spawn(async move { | ||
| let index = runtime::worker_index().expect("should be on worker thread"); | ||
| let current_thread = std::thread::current().id(); | ||
| let metrics_thread = metrics.worker_thread_id(index); | ||
| assert_eq!( | ||
| metrics_thread, | ||
| Some(current_thread), | ||
| "worker_index() returned {index} but metrics.worker_thread_id({index}) \ | ||
| does not match current thread" | ||
| ); | ||
| }) | ||
| .await | ||
| .unwrap(); | ||
| }); | ||
| } | ||
|
|
||
| #[test] | ||
| fn worker_index_from_spawn_blocking() { | ||
| let rt = Runtime::new().unwrap(); | ||
| rt.block_on(async { | ||
| let index = tokio::task::spawn_blocking(runtime::worker_index) | ||
| .await | ||
| .unwrap(); | ||
| assert_eq!( | ||
| index, None, | ||
| "spawn_blocking should not be on a worker thread" | ||
| ); | ||
| }); | ||
| } | ||
|
|
||
| #[test] | ||
| fn worker_index_block_on_multi_thread() { | ||
| let rt = Runtime::new().unwrap(); | ||
| // block_on runs on the calling thread, not a worker thread | ||
| let index = rt.block_on(async { runtime::worker_index() }); | ||
| assert_eq!( | ||
| index, None, | ||
| "block_on thread is not a worker thread on multi-thread runtime" | ||
| ); | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
The closure
|c| c.worker_index()can be simplified by passing the method as a function pointer directly toand_then. This is more idiomatic and concise.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.
value:good-but-wont-fix; category:bug; feedback: The Gemini AI reviewer is correct! The function pointer may be used instead of the closure. But this is a matter of taste and the Tokio project already uses many closures in such use cases, so this one will stay for consistency