diff --git a/core/thread/thread.odin b/core/thread/thread.odin index a81fb3fba..14e266b20 100644 --- a/core/thread/thread.odin +++ b/core/thread/thread.odin @@ -53,6 +53,7 @@ Thread :: struct { // it is started. data: rawptr, // Thread's Name/Description that will get set during thread creation + // it will be set using init_context's allocator to allocate and free a cstring buffer // for thread's creation only : do not refer to it, use thread.get_name instead name: Maybe(string), // User-supplied integer, that will be available to the thread once it is @@ -110,7 +111,7 @@ To start the thread, call `start()`. Also the `create_and_start()` procedure can be called to create and start the thread immediately. Optionally specify the thread's name/description. -the name/description will be truncated to 15 bytes on Unix and 127 bytes on Windows. +the name/description will be truncated to fit the OS's limit. */ create :: proc(procedure: Thread_Proc, priority := Thread_Priority.Normal, name: Maybe(string) = nil) -> ^Thread { @@ -188,7 +189,7 @@ specified by `init_context` will be used as the context in which `fn` is going to execute. The thread will have priority specified by the `priority` parameter. Optionally specify the thread's name/description. -the name/description will be truncated to 15 bytes on Unix and 127 bytes on Windows. +the name/description will be truncated to fit the OS's limit. **IMPORTANT**: If `init_context` is specified and the default temporary allocator is used, the thread procedure needs to call `runtime.default_temp_allocator_destroy()` @@ -206,7 +207,7 @@ specified by `init_context` will be used as the context in which `fn` is going to execute. The thread will have priority specified by the `priority` parameter. Optionally specify the thread's name/description. -the name/description will be truncated to 15 bytes on Unix and 127 bytes on Windows. +the name/description will be truncated to fit the OS's limit. **IMPORTANT**: If `init_context` is specified and the default temporary allocator is used, the thread procedure needs to call `runtime.default_temp_allocator_destroy()` @@ -224,7 +225,7 @@ specified by `init_context` will be used as the context in which `fn` is going to execute. The thread will have priority specified by the `priority` parameter. Optionally specify the thread's name/description. -the name/description will be truncated to 15 bytes on Unix and 127 bytes on Windows. +the name/description will be truncated to fit the OS's limit. **IMPORTANT**: If `init_context` is specified and the default temporary allocator is used, the thread procedure needs to call `runtime.default_temp_allocator_destroy()` @@ -243,7 +244,7 @@ specified by `init_context` will be used as the context in which `fn` is going to execute. The thread will have priority specified by the `priority` parameter. Optionally specify the thread's name/description. -the name/description will be truncated to 15 bytes on Unix and 127 bytes on Windows. +the name/description will be truncated to fit the OS's limit. **IMPORTANT**: If `init_context` is specified and the default temporary allocator is used, the thread procedure needs to call `runtime.default_temp_allocator_destroy()` @@ -262,7 +263,7 @@ specified by `init_context` will be used as the context in which `fn` is going to execute. The thread will have priority specified by the `priority` parameter. Optionally specify the thread's name/description. -the name/description will be truncated to 15 bytes on Unix and 127 bytes on Windows. +the name/description will be truncated to fit the OS's limit. **IMPORTANT**: If `init_context` is specified and the default temporary allocator is used, the thread procedure needs to call `runtime.default_temp_allocator_destroy()` @@ -281,7 +282,7 @@ specified by `init_context` will be used as the context in which `fn` is going to execute. The thread will have priority specified by the `priority` parameter. Optionally specify the thread's name/description. -the name/description will be truncated to 15 bytes on Unix and 127 bytes on Windows. +the name/description will be truncated to fit the OS's limit. **IMPORTANT**: If `init_context` is specified and the default temporary allocator is used, the thread procedure needs to call `runtime.default_temp_allocator_destroy()` @@ -304,7 +305,7 @@ If `self_cleanup` is specified, after the thread finishes the execution of the automatically freed. Optionally specify the thread's name/description. -the name/description will be truncated to 15 bytes on Unix and 127 bytes on Windows. +the name/description will be truncated to fit the OS's limit. **Do not** dereference the `^Thread` pointer, if this flag is specified. That includes calling `join`, which needs to dereference ^Thread`. @@ -342,7 +343,7 @@ If `self_cleanup` is specified, after the thread finishes the execution of the automatically freed. Optionally specify the thread's name/description. -the name/description will be truncated to 15 bytes on Unix and 127 bytes on Windows. +the name/description will be truncated to fit the OS's limit. **Do not** dereference the `^Thread` pointer, if this flag is specified. That includes calling `join`, which needs to dereference ^Thread`. @@ -384,7 +385,7 @@ If `self_cleanup` is specified, after the thread finishes the execution of the automatically freed. Optionally specify the thread's name/description. -the name/description will be truncated to 15 bytes on Unix and 127 bytes on Windows. +the name/description will be truncated to fit the OS's limit. **Do not** dereference the `^Thread` pointer, if this flag is specified. That includes calling `join`, which needs to dereference ^Thread`. @@ -432,7 +433,7 @@ If `self_cleanup` is specified, after the thread finishes the execution of the automatically freed. Optionally specify the thread's name/description. -the name/description will be truncated to 15 bytes on Unix and 127 bytes on Windows. +the name/description will be truncated to fit the OS's limit. **Do not** dereference the `^Thread` pointer, if this flag is specified. That includes calling `join`, which needs to dereference ^Thread`. @@ -486,7 +487,7 @@ If `self_cleanup` is specified, after the thread finishes the execution of the automatically freed. Optionally specify the thread's name/description. -the name/description will be truncated to 15 bytes on Unix and 127 bytes on Windows. +the name/description will be truncated to fit the OS's limit. **Do not** dereference the `^Thread` pointer, if this flag is specified. That includes calling `join`, which needs to dereference ^Thread`. @@ -542,7 +543,7 @@ If `self_cleanup` is specified, after the thread finishes the execution of the automatically freed. Optionally specify the thread's name/description. -the name/description will be truncated to 15 bytes on Unix and 127 bytes on Windows. +the name/description will be truncated to fit the OS's limit. **Do not** dereference the `^Thread` pointer, if this flag is specified. That includes calling `join`, which needs to dereference ^Thread`. diff --git a/core/thread/thread_name_darwin.odin b/core/thread/thread_name_darwin.odin index 866f608a3..7a01e0371 100644 --- a/core/thread/thread_name_darwin.odin +++ b/core/thread/thread_name_darwin.odin @@ -5,6 +5,8 @@ package thread import "core:sys/posix" import "core:c" +_MAX_PTHREAD_NAME_LENGTH :: 64 + foreign import pthread "system:System.framework" foreign pthread { diff --git a/core/thread/thread_name_freebsd.odin b/core/thread/thread_name_freebsd.odin index 9a36a54d2..5b79728a2 100644 --- a/core/thread/thread_name_freebsd.odin +++ b/core/thread/thread_name_freebsd.odin @@ -5,6 +5,8 @@ package thread import "core:sys/posix" import "core:c" +_MAX_PTHREAD_NAME_LENGTH :: 20 + foreign import pthread "system:pthread" foreign pthread { diff --git a/core/thread/thread_name_linux.odin b/core/thread/thread_name_linux.odin index 67d72e5c9..b84bf999c 100644 --- a/core/thread/thread_name_linux.odin +++ b/core/thread/thread_name_linux.odin @@ -5,6 +5,8 @@ package thread import "core:sys/posix" import "core:c" +_MAX_PTHREAD_NAME_LENGTH :: 16 + foreign import pthread "system:pthread" foreign pthread { diff --git a/core/thread/thread_name_netbsd.odin b/core/thread/thread_name_netbsd.odin index 12f31f760..c62c8c8f4 100644 --- a/core/thread/thread_name_netbsd.odin +++ b/core/thread/thread_name_netbsd.odin @@ -5,6 +5,8 @@ package thread import "core:sys/posix" import "core:c" +_MAX_PTHREAD_NAME_LENGTH :: 32 + foreign import pthread "system:pthread" foreign pthread { diff --git a/core/thread/thread_name_openbsd.odin b/core/thread/thread_name_openbsd.odin index e91970649..b349c2566 100644 --- a/core/thread/thread_name_openbsd.odin +++ b/core/thread/thread_name_openbsd.odin @@ -7,6 +7,8 @@ import "core:c" foreign import pthread "system:pthread" +_MAX_PTHREAD_NAME_LENGTH :: 20 + foreign pthread { pthread_get_name_np :: proc(thread: posix.pthread_t, name: [^]u8, len: c.size_t) --- pthread_set_name_np :: proc(thread: posix.pthread_t, name: [^]u8) --- diff --git a/core/thread/thread_unix.odin b/core/thread/thread_unix.odin index 2720808fa..0ab54cea2 100644 --- a/core/thread/thread_unix.odin +++ b/core/thread/thread_unix.odin @@ -8,7 +8,6 @@ import "core:sys/posix" import "core:strings" _IS_SUPPORTED :: true -_MAX_PTHREAD_NAME_LENGTH :: 16 // NOTE(tetra): Aligned here because of core/unix/pthread_linux.odin/pthread_t. // Also see core/sys/darwin/mach_darwin.odin/semaphore_t. Thread_Os_Specific :: struct #align(16) { @@ -230,12 +229,12 @@ _set_name :: proc(thread: ^Thread) { tid := thread.unix_thread } - buf: [_MAX_PTHREAD_NAME_LENGTH]u8 - // _MAX_PTHREAD_NAME_LENGTH includes terminating null + buflen := len(name) + 1 < _MAX_PTHREAD_NAME_LENGTH ? len(name) + 1 : _MAX_PTHREAD_NAME_LENGTH + buf := make([]u8, buflen) + defer delete(buf) copy(buf[:len(buf) - 1], name) - when ODIN_OS == .Darwin { pthread_setname_np(raw_data(buf[:])) } else when ODIN_OS == .OpenBSD { @@ -245,5 +244,4 @@ _set_name :: proc(thread: ^Thread) { } else { pthread_setname_np(tid, raw_data(buf[:])) } - } diff --git a/core/thread/thread_windows.odin b/core/thread/thread_windows.odin index 0b5824f64..f13d550f7 100644 --- a/core/thread/thread_windows.odin +++ b/core/thread/thread_windows.odin @@ -9,8 +9,9 @@ import win32 "core:sys/windows" import "core:unicode/utf16" _IS_SUPPORTED :: true -//NOTE(peperronii): not sure about the exact length but there must be a limit -_THREAD_DESCRIPTION_LENGTH :: 128 + +//NOTE(peperronii): this is the system limit for windows api call, not specific to thread description +_THREAD_DESCRIPTION_LENGTH :: 32_767 Thread_Os_Specific :: struct { win32_thread: win32.HANDLE, @@ -185,8 +186,10 @@ _set_name :: proc(thread: ^Thread) { t_handle := thread.win32_thread - buf: [_THREAD_DESCRIPTION_LENGTH]u16 // _THREAD_DESCRIPTION_LENGTH includes terminating null + buflen := len(name) + 1 < _THREAD_DESCRIPTION_LENGTH ? len(name) + 1 : _THREAD_DESCRIPTION_LENGTH + buf := make([]u16, buflen) + defer delete(buf) utf16.encode_string(buf[:len(buf) - 1], name) win32.SetThreadDescription(t_handle, cstring16(raw_data(buf[:]))) }