-
Notifications
You must be signed in to change notification settings - Fork 13.8k
First implementation of Thread::set_name(&str) #44258
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
Conversation
Hi, there's a couple of details of the whole thread implementation I can't review but I have a few comments. We need to remember that using |
src/libstd/thread/mod.rs
Outdated
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.
Mutating through a shared pointer like this is illegal, inside and outside of unsafe
blocks.
For the inner thread we have an Arc<Inner>
and no interior mutability, in particular no interior mutability around the cname field, so it's completely impossible to mutate that field, unless we get mutable access to it like with Arc::get_mut
, but that method is only useful when there is no other handle to the same Arc.
So to make it possible to change cname, interior mutability is needed. Usually this means Mutex
.
Unfortunately. The method pub fn name(&self) -> Option<&str>
just a bit up in the file will exclude any possibility of using interior mutability. This is where even more creativity needs to pour in to solve it. 😄
Thank you for the input :D |
What do you mean by that exactly? An |
@rust-lang/infra, how come this never got assigned by @rust-highfive? |
@dns2utf8 I don't see how to solve it, but that doesn't mean there isn't a solution. There is no simple fix, like marking the function differently -- see my previous comments about unsafe code. |
@dtolnay I wouldn't classify @rust-highfive as the most reliable piece of software |
Some ideas were floated with @dtolnay
One question, do the OSes all allow changing the name of a thread? |
I am. Thank you for the reminder |
7a71f84
to
14bb26b
Compare
They are rather loose ideas. It seems like there is agreement ther is no "perfect" solution, so it's interesting what's pragmatically possible here and if the compromises needed are acceptable. |
@dns2utf8 looks like there may be a missing |
Hm so reading over this and the discussion now I see how this is going to be a very difficult method to implement. @dns2utf8 you stated motivation in the OP, I wonder if it would perhaps work to configure the name on the thread builder? I think the implementation here can also run into problems though (in addition to the mutability concerns brought up by @bluss). A |
6b2ef79
to
e576ded
Compare
I am getting an error about |
You'll want to add that to the libc crate: https://github.com/rust-lang/libc. |
The PR will fail until rust-lang/libc#789 is merged |
@dns2utf8 Give the Mac OS X limitation and the mutability/race problems mentioned by others, I wouldn't recommend trying to set the thread name via the handle. The Mac OS X limitation won't be fixed anytime soon IMHO. Basically the options are:
I'd say the first approach should be fine when writing a threapool since you're probably going to need a way to communicate with your threads anyway, right? |
Thank you all for the comments. I am sorry, about never writing back.
My current plan is as follows:
I am currently thinking about wrapping the result from get into a Again, thank you all for the comments, they helped me 👍 |
@dns2utf8 oh I was thinking that for fetching the name we probably won't query the OS 100% of the time but would instead use what we have today with a stored value in |
At least on pthreads, the OS will truncate the thread name to ~16 bytes, so we'd want to store a copy locally instead of relying on the pthreads API. |
@sfackler Well, returning the truncated name would better reflect what the thread is actually named, no? I guess this boils down to whether the thread name should be useful for 'outside' interaction (ie. looking up a thread in a process list / manager, etc.), or to be useful for internal purposes of the program. edit: Also, a call to 3rd party and/or native API might change the thread name, in which case the cached name would no longer reflect the actual name. |
The thread name is used quite often inside of the program - for example, it's often attached to log output. There, you'd absolutely want to see the full name rather than just the first 16 bytes. Web server's commonly name the thread handling a request with e.g. |
@sfackler Ok, that makes sense, but then you either need access to the thread handle whenever you log something or store the thread name elsewhere, such as TLS (?). |
It's easy to get access to the handle for the thread you're on: |
From my point of view, the first implementation should query the OS every time.
|
r? @sfackler I hope you don't mind — the OS details are not my area. |
I'm not sure what you mean by this. There is both locking and synchronization being performed in |
er sorry, that was for @dns2utf8 |
Hi @dns2utf8, are you still working on this? 😃 |
Thanks for the PR, @dns2utf8, but we haven't heard from you in a few weeks. I am going to close this PR to keep things tidy. Please feel free to reopen it once you've addressed the most recent feedback! |
I would like to be able to rename threads in the threadpool crate without restarting the worker-threads.
This is my first PR with unsafe code, so I improvised a little to get it to compile.
Ideas for improvement are very appreciated.