1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
//! # Thread Utilities
//!
//! This module provides cross-platform utilities for thread management and debugging
//! in the Tessera UI framework.
//!
//! ## Overview
//!
//! Thread naming is essential for debugging and profiling multi-threaded applications.
//! This module provides a unified interface for setting thread names across different
//! operating systems, abstracting away the platform-specific implementation details.
//!
//! ## Platform Support
//!
//! - **Unix-like systems** (Linux, macOS, etc.): Uses `pthread_setname_np`
//! - **Windows**: Uses `SetThreadDescription` API
//! - **Other platforms**: No-op (function exists but does nothing)
//!
//! ## Usage
//!
//! ```rust,ignore
//! use crate::thread_utils::set_thread_name;
//!
//! // Set the current thread's name for debugging
//! set_thread_name("tessera-renderer");
//!
//! // In a spawned thread
//! std::thread::spawn(|| {
//! set_thread_name("tessera-worker");
//! // ... thread work
//! });
//! ```
//!
//! ## Thread Name Limitations
//!
//! Different platforms have different limitations on thread names:
//! - **Linux**: Maximum 15 characters (excluding null terminator)
//! - **macOS**: Maximum 63 characters
//! - **Windows**: No specific length limit, but shorter names are recommended
//!
//! Names longer than the platform limit will be truncated or may cause the
//! operation to fail silently.
/// Sets the name of the current thread for debugging and profiling purposes.
///
/// This function provides a cross-platform way to set thread names, which is
/// invaluable for debugging multi-threaded applications. Thread names appear
/// in debuggers, profilers, and system monitoring tools.
///
/// # Arguments
///
/// * `name` - The name to assign to the current thread. Should be descriptive
/// and follow platform-specific length limitations.
///
/// # Platform Behavior
///
/// - **Unix-like systems**: Uses `pthread_setname_np()` to set the thread name
/// - **Windows**: Uses `SetThreadDescription()` API
/// - **Other platforms**: No operation is performed
///
/// # Examples
///
/// ```rust,ignore
/// use tessera_ui::thread_utils::set_thread_name;
///
/// // Set a descriptive name for the current thread
/// set_thread_name("ui-renderer");
///
/// // In a worker thread
/// std::thread::spawn(|| {
/// set_thread_name("background-task");
/// // ... perform background work
/// });
/// ```
///
/// # Panics
///
/// This function may panic if:
/// - The thread name contains null bytes (Unix systems)
/// - Memory allocation fails during string conversion
///
/// # Performance
///
/// This is a lightweight operation that should have minimal performance impact.
/// However, it involves system calls, so avoid calling it frequently in
/// performance-critical code paths.
/// Sets the thread name on Unix-like systems using pthread APIs.
///
/// This function uses the POSIX `pthread_setname_np` function to set the
/// thread name. The name is converted to a C-compatible null-terminated
/// string before passing to the system API.
///
/// # Arguments
///
/// * `name` - The thread name to set
///
/// # Platform Notes
///
/// - **Linux**: Thread names are limited to 15 characters (plus null terminator)
/// - **macOS**: Thread names can be up to 63 characters
/// - Names exceeding the limit may be truncated or cause the operation to fail
///
/// # Panics
///
/// Panics if the name contains null bytes, as this would create an invalid
/// C string.
///
/// # Safety
///
/// This function uses unsafe code to call the `pthread_setname_np` system call.
/// The safety is ensured by:
/// - Using a valid C string created from the input
/// - Calling with the current thread handle (`pthread_self()`)
/// - The pthread API is designed to handle these parameters safely
/// Sets the thread name on Windows using the Win32 API.
///
/// This function uses the Windows `SetThreadDescription` API, which was
/// introduced in Windows 10 version 1607 and Windows Server 2016. The
/// thread name is converted to UTF-16 format as required by the Windows API.
///
/// # Arguments
///
/// * `name` - The thread name to set
///
/// # Platform Notes
///
/// - Available on Windows 10 version 1607 and later
/// - On older Windows versions, this function will fail silently
/// - Thread names have no specific length limit but shorter names are recommended
///
/// # Error Handling
///
/// Any errors from the Windows API are ignored (the result is discarded with `let _`).
/// This is intentional as thread naming is a debugging aid and should not cause
/// application failures.
///
/// # Safety
///
/// This function uses unsafe code to call Windows APIs. The safety is ensured by:
/// - Creating a valid UTF-16 null-terminated string
/// - Using the current thread handle from `GetCurrentThread()`
/// - Properly constructing the `PCWSTR` parameter