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 Thread.setName and Thread.getName #8570
Conversation
API-wise I think it makes sense to add an extra struct parameter to the thread creation function: struct CreateThreadOptions = struct {
thread_name: ?[:0]const u8 = null,
} There's little to no point to setting the name after the thread is created. |
It can be useful to name worker threads after what they are currently doing. This also has implications in schedulers where workloads are not locked to specific threads. |
4ef68ab
to
24b03c2
Compare
I've implemented linux with or without pthreads, macOS and netbsd/freebsd/openbsd. I've only been able to test linux at the moment, I'll be able to test freebsd and probably netbsd. I'm still working on the windows implementation. I'll make the API change last. |
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 return type for functions for OpenBSD should be void
. It will make it different from .freebsd
for setting the name, and different from others for getting it.
I've addressed most of the comments in the latest commit (minus those still in discussion). I successfully tested this on FreeBSD, however I can't test on NetBSD or OpenBSD because of compilation errors somewhere else in the stdlib. |
I've added the windows implementation. One thing I'm not sure about is the utf8 -> utf16 conversion, right now I'm doing this:
The utf8 |
8c9c7ee
to
1fedd74
Compare
22cd76d
to
d91180b
Compare
This is correct. A string of 'n' UTF16 code units can always hold 'n' UTF8 code units. The normal issue is only the other way around, where one UTF16 code unit might require multiple UTF8 code units of storage. For ASCII code points, this isn't relevant. But for larger code points (eg. Emoji), this can be important. I suspect that Windows supports a maximum number of UTF16 code units. Since you allocate to implement getName(), this works fine. However, limiting maxNameLen to 31 UTF8 code units artificially limits the number of code points you can give to setName() (say, a string of Emoji). But using Emoji in thread names is probably a bad idea anyway since debuggers might not support it well :-) |
Thanks. I actually tested emojis in the thread name, Visual Studio 2019 supports them at least. |
437c367
to
70afb6f
Compare
6c2ff78
to
7a57207
Compare
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 your patience; I know it took a long time to get a review from me.
I have a couple requests and then I think this looks good to merge.
Can you please rebase against master branch and fix the conflicts in addition to the requested changes?
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 the fixups! Found 2 more things - sorry for not finding them last time, but at least now I am paying close attention, and will give this a prompt merge :)
Ok, addressed your last feedback. I had to change some logic for musl since the type returned by |
Pushed the last fixes for macos, freebsd/netbsd. |
This PR adds
Thread.setName
andThread.getName
.It's not complete, I only tested this on linux. I spent a bit of time researching what other platforms do and from what I can tell setting/getting a thread's name is supported on linux, windows (since Windows 10), macos, openbsd, freebsd, netbsd.
Annoyingly since it's non standard there are some slight differences between implementations of
pthread_setname_np
.Before implementing everything I'd like to get some early feedback on the API.