@@ -9,7 +9,7 @@ pub(crate) use _thread::{
99pub ( crate ) mod _thread {
1010 use crate :: {
1111 AsObject , Py , PyPayload , PyRef , PyResult , VirtualMachine ,
12- builtins:: { PyDictRef , PyStr , PyTupleRef , PyType , PyTypeRef } ,
12+ builtins:: { PyDictRef , PyStr , PyStrRef , PyTupleRef , PyType , PyTypeRef } ,
1313 frame:: FrameRef ,
1414 function:: { ArgCallable , Either , FuncArgs , KwArgs , OptionalArg , PySetterValue } ,
1515 types:: { Constructor , GetAttr , Representable , SetAttr } ,
@@ -260,6 +260,11 @@ pub(crate) mod _thread {
260260 Ok ( ( ) )
261261 }
262262
263+ #[ pymethod]
264+ fn locked ( & self ) -> bool {
265+ self . mu . is_locked ( )
266+ }
267+
263268 #[ pymethod]
264269 fn _is_owned ( & self ) -> bool {
265270 self . mu . is_owned_by_current_thread ( )
@@ -293,6 +298,45 @@ pub(crate) mod _thread {
293298 current_thread_id ( )
294299 }
295300
301+ /// Set the name of the current thread
302+ #[ pyfunction]
303+ fn set_name ( name : PyStrRef ) {
304+ #[ cfg( target_os = "linux" ) ]
305+ {
306+ use std:: ffi:: CString ;
307+ if let Ok ( c_name) = CString :: new ( name. as_str ( ) ) {
308+ // pthread_setname_np on Linux has a 16-byte limit including null terminator
309+ let truncated = if c_name. as_bytes ( ) . len ( ) > 15 {
310+ CString :: new ( & c_name. as_bytes ( ) [ ..15 ] ) . unwrap_or ( c_name)
311+ } else {
312+ c_name
313+ } ;
314+ unsafe {
315+ libc:: pthread_setname_np ( libc:: pthread_self ( ) , truncated. as_ptr ( ) ) ;
316+ }
317+ }
318+ }
319+ #[ cfg( target_os = "macos" ) ]
320+ {
321+ use std:: ffi:: CString ;
322+ if let Ok ( c_name) = CString :: new ( name. as_str ( ) ) {
323+ unsafe {
324+ libc:: pthread_setname_np ( c_name. as_ptr ( ) ) ;
325+ }
326+ }
327+ }
328+ #[ cfg( windows) ]
329+ {
330+ // Windows doesn't have a simple pthread_setname_np equivalent
331+ // SetThreadDescription requires Windows 10+
332+ let _ = name;
333+ }
334+ #[ cfg( not( any( target_os = "linux" , target_os = "macos" , windows) ) ) ]
335+ {
336+ let _ = name;
337+ }
338+ }
339+
296340 /// Get OS-level thread ID (pthread_self on Unix)
297341 /// This is important for fork compatibility - the ID must remain stable after fork
298342 #[ cfg( unix) ]
0 commit comments