Skip to content

Commit e325efd

Browse files
committed
thread_inherit_context
1 parent 6ff5979 commit e325efd

File tree

3 files changed

+52
-1
lines changed

3 files changed

+52
-1
lines changed

crates/vm/src/stdlib/sys.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1301,6 +1301,8 @@ mod sys {
13011301
safe_path: bool,
13021302
/// -X warn_default_encoding, PYTHONWARNDEFAULTENCODING
13031303
warn_default_encoding: u8,
1304+
/// -X thread_inherit_context, whether new threads inherit context from parent
1305+
thread_inherit_context: bool,
13041306
}
13051307

13061308
impl FlagsData {
@@ -1324,6 +1326,7 @@ mod sys {
13241326
int_max_str_digits: settings.int_max_str_digits,
13251327
safe_path: settings.safe_path,
13261328
warn_default_encoding: settings.warn_default_encoding as u8,
1329+
thread_inherit_context: settings.thread_inherit_context,
13271330
}
13281331
}
13291332
}

crates/vm/src/stdlib/thread.rs

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ pub(crate) use _thread::{
99
pub(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)]

crates/vm/src/vm/setting.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,9 @@ pub struct Settings {
8888
/// -X warn_default_encoding, PYTHONWARNDEFAULTENCODING
8989
pub warn_default_encoding: bool,
9090

91+
/// -X thread_inherit_context, whether new threads inherit context from parent
92+
pub thread_inherit_context: bool,
93+
9194
/// -i
9295
pub inspect: bool,
9396

@@ -190,6 +193,7 @@ impl Default for Settings {
190193
isolated: false,
191194
dev_mode: false,
192195
warn_default_encoding: false,
196+
thread_inherit_context: false,
193197
warnoptions: vec![],
194198
path_list: vec![],
195199
argv: vec![],

0 commit comments

Comments
 (0)