Odin icon indicating copy to clipboard operation
Odin copied to clipboard

core:thread get_name/set_name

Open peperronii opened this issue 5 months ago • 10 comments

feature request #5314

tested on arm_darwin

peperronii avatar Jul 14 '25 10:07 peperronii

With macos not supporting setting the name of another thread, should we just not support it anywhere, so we have a consistent API?

I'm wondering whether adding a possibility to thread.create_*/thread.start to add a name for the newly created thread would be an idea, instead of adding a thread.set_name proc?

Similar to how Java does it with new Thread(Runnable, name), this way thread creation/starting could set the name upfront, as this name is merely a "debugging" thing and I don't see a reason why someone would arbitrarily change one.

FourteenBrush avatar Jul 14 '25 19:07 FourteenBrush

With macos not supporting setting the name of another thread, should we just not support it anywhere, so we have a consistent API?

I'm wondering whether adding a possibility to thread.create_*/thread.start to add a name for the newly created thread would be an idea, instead of adding a thread.set_name proc?

Similar to how Java does it with new Thread(Runnable, name), this way thread creation/starting could set the name upfront, as this name is merely a "debugging" thing and I don't see a reason why someone would arbitrarily change one.

Good idea!

laytan avatar Jul 14 '25 19:07 laytan

With macos not supporting setting the name of another thread, should we just not support it anywhere, so we have a consistent API?

Yeah, I was also hesitant about this but It feels weird to shun others capability just to make this one feels consistent. I think the solution to have all the OS refer to the calling thread is pretty neat though. but, I would gladly comply if it’s somehow idiomatic.

I'm wondering whether adding a possibility to thread.create_*/thread.start to add a name for the newly created thread would be an idea, instead of adding a thread.set_name proc?

Similar to how Java does it with new Thread(Runnable, name), this way thread creation/starting could set the name upfront, as this name is merely a "debugging" thing and I don't see a reason why someone would arbitrarily change one.

I also think this is a good idea. but, I’m not sure about not adding thread.set_name at all though. even though I don’t personally use it.

@GloriousPtr thoughts?

peperronii avatar Jul 15 '25 02:07 peperronii

I would still get rid of the set_name, and instead move this name to thread.create, where something like __unix_thread_entry_proc and similar would set the name on the running thread itself

FourteenBrush avatar Jul 20 '25 11:07 FourteenBrush

@laytan I just looked at how Zig does it, and it simply returns an error.Unsupported, perhaps we should just also do that? :slightly_smiling_face:

https://ziglang.org/documentation/master/std/#std.Thread.setName

Seems like a reasonable tradeoff right?, additionally still keep the idea of an initial thread name param.

FourteenBrush avatar Jul 20 '25 15:07 FourteenBrush

If we’re going down the thread.create_with_name* route, I think we don’t need to have a get_name. we have to make a struct field (thread.Thread) for the name to make it callable from inside the thread, so if we want to retrieve the name, we can just t.name or something like that.

peperronii avatar Jul 21 '25 08:07 peperronii

I think we don’t need to have a get_name. we have to make a struct field (thread.Thread) for the name to make it callable from inside the thread

I disagree, I don't see a reason to add this field to a Thread, the kernel already represents the single point of truth for this value. This isn't a hot spot either so in my opinion nothing wrong with performing a syscall here to obtain the value.

My idea was to do something like this:

thread.create :: proc(procedure: Thread_Proc, priority: Thread_Priority = Thread_Priority.Normal, name: Maybe(string) = nil) -> ^Thread

(Similarly for all those other variants) Which then calls the respective _set_name proc in the threads __entry_proc, as this would get rid of the issue that darwin does not support changing arbitrary thread's names

FourteenBrush avatar Jul 21 '25 20:07 FourteenBrush

I disagree, I don't see a reason to add this field to a Thread, the kernel already represents the single point of truth for this value. This isn't a hot spot either so in my opinion nothing wrong with performing a syscall here to obtain the value.

yeah, I suppose you’re right on not getting rid of get_thread. But we do need to add a field in thread.Thread in order to pass it inside __unix_thread_entry_proc and __windows_thread_entry_proc though… right?

peperronii avatar Jul 22 '25 01:07 peperronii

But we do need to add a field in thread.Thread in order to pass it inside __unix_thread_entry_proc and __windows_thread_entry_proc though… right?

Looks like it, can't use context._internal here because the windows thread entry proc may not be called immediately

FourteenBrush avatar Jul 22 '25 18:07 FourteenBrush

Looks fine to me, is it tested ~~on all platforms~~?

FourteenBrush avatar Aug 24 '25 23:08 FourteenBrush