Feat: sys.thread_info

This commit is contained in:
Hyunmin Shin
2022-08-11 15:55:48 +09:00
parent 53f3fe3dca
commit ec5fd4a596
2 changed files with 51 additions and 7 deletions

View File

@@ -4,23 +4,22 @@ pub(crate) use sys::{UnraisableHookArgs, MAXSIZE, MULTIARCH};
#[pymodule]
mod sys {
use crate::common::{
ascii,
hash::{PyHash, PyUHash},
};
use crate::{
builtins::{PyDictRef, PyNamespace, PyStr, PyStrRef, PyTupleRef, PyTypeRef},
common::{
ascii,
hash::{PyHash, PyUHash},
},
frame::FrameRef,
function::{FuncArgs, OptionalArg, PosArgs},
stdlib::builtins,
stdlib::{self, builtins},
types::PyStructSequence,
version,
vm::{Settings, VirtualMachine},
AsObject, PyObjectRef, PyRef, PyRefExact, PyResult,
};
use num_traits::ToPrimitive;
use std::sync::atomic::Ordering;
use std::{env, mem, path};
use std::{env, mem, path, sync::atomic::Ordering};
// not the same as CPython (e.g. rust's x86_x64-unknown-linux-gnu is just x86_64-linux-gnu)
// but hopefully that's just an implementation detail? TODO: copy CPython's multiarch exactly,
@@ -562,6 +561,12 @@ mod sys {
update_use_tracing(vm);
}
#[cfg(feature = "threading")]
#[pyattr]
fn thread_info(vm: &VirtualMachine) -> PyTupleRef {
PyThreadInfo::INFO.into_struct_sequence(vm)
}
#[pyattr]
fn version_info(vm: &VirtualMachine) -> PyTupleRef {
VersionInfo::VERSION.into_struct_sequence(vm)
@@ -643,6 +648,27 @@ mod sys {
}
}
#[cfg(feature = "threading")]
#[pyclass(noattr, name = "thread_info")]
#[derive(PyStructSequence)]
pub(super) struct PyThreadInfo {
name: Option<&'static str>,
lock: Option<&'static str>,
version: Option<&'static str>,
}
#[cfg(feature = "threading")]
#[pyclass(with(PyStructSequence))]
impl PyThreadInfo {
const INFO: Self = PyThreadInfo {
name: stdlib::thread::_thread::PYTHREAD_NAME,
/// As I know, there's only way to use lock as "Mutex" in Rust
/// with satisfying python document spec.
lock: Some("mutex+cond"),
version: None,
};
}
#[pyclass(noattr, name = "float_info")]
#[derive(PyStructSequence)]
pub(super) struct PyFloatInfo {
@@ -658,6 +684,7 @@ mod sys {
radix: u32,
rounds: i32,
}
#[pyclass(with(PyStructSequence))]
impl PyFloatInfo {
const INFO: Self = PyFloatInfo {
@@ -713,6 +740,7 @@ mod sys {
bits_per_digit: usize,
sizeof_digit: usize,
}
#[pyclass(with(PyStructSequence))]
impl PyIntInfo {
const INFO: Self = PyIntInfo {
@@ -765,6 +793,7 @@ mod sys {
product_type: u8,
platform_version: (u32, u32, u32),
}
#[cfg(windows)]
#[pyclass(with(PyStructSequence))]
impl WindowsVersion {}

View File

@@ -18,6 +18,21 @@ pub(crate) mod _thread {
use std::{cell::RefCell, fmt, thread, time::Duration};
use thread_local::ThreadLocal;
// PYTHREAD_NAME: show current thread name
pub const PYTHREAD_NAME: Option<&str> = {
cfg_if::cfg_if! {
if #[cfg(windows)] {
Some("nt")
} else if #[cfg(unix)] {
Some("pthread")
} else if #[cfg(any(target_os = "solaris", target_os = "illumos"))] {
Some("solaris")
} else {
None
}
}
};
// TIMEOUT_MAX_IN_MICROSECONDS is a value in microseconds
#[cfg(not(target_os = "windows"))]
const TIMEOUT_MAX_IN_MICROSECONDS: i64 = i64::MAX / 1_000;