8000 reactivate · RustPython/RustPython@2d02abf · GitHub
[go: up one dir, main page]

Skip to content

Commit 2d02abf

Browse files
committed
reactivate
1 parent 7e8d069 commit 2d02abf

File tree

6 files changed

+32
-6
lines changed

6 files changed

+32
-6
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ winresource = "0.1"
3333
rustpython-compiler = { workspace = true }
3434
rustpython-pylib = { workspace = true, optional = true }
3535
rustpython-stdlib = { workspace = true, optional = true, features = ["compiler"] }
36-
rustpython-vm = { workspace = true, features = ["compiler"] }
36+
rustpython-vm = { workspace = true, features = ["compiler", "gc"] }
3737
ruff_python_parser = { workspace = true }
3838

3939
cfg-if = { workspace = true }

crates/vm/Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ repository.workspace = true
1010
license.workspace = true
1111

1212
[features]
13-
default = ["compiler", "wasmbind", "stdio"]
13+
default = ["compiler", "wasmbind", "stdio", "gc"]
1414
stdio = []
1515
importlib = []
1616
encodings = ["importlib"]
@@ -19,6 +19,7 @@ flame-it = ["flame", "flamer"]
1919
freeze-stdlib = ["encodings"]
2020
jit = ["rustpython-jit"]
2121
threading = ["rustpython-common/threading"]
22+
gc = []
2223
compiler = ["parser", "codegen", "rustpython-compiler"]
2324
ast = ["ruff_python_ast", "ruff_text_size"]
2425
codegen = ["rustpython-codegen", "ast"]

crates/vm/src/object/core.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ pub(super) unsafe fn debug_obj<T: PyPayload + core::fmt::Debug>(
9393
}
9494

9595
/// Call `try_trace` on payload
96-
pub(super) unsafe fn try_trace_obj<T: PyPayload>(x: &PyObject, tracer_fn: &mut TraverseFn<'_>) {
96+
pub(super) unsafe fn try_traverse_obj<T: PyPayload>(x: &PyObject, tracer_fn: &mut TraverseFn<'_>) {
9797
let x = unsafe { &*(x as *const PyObject as *const PyInner<T>) };
9898
let payload = &x.payload;
9999
payload.try_traverse(tracer_fn)

crates/vm/src/object/traverse_object.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use crate::{
55
PyObject, PyObjectRef,
66
object::{
77
Erased, InstanceDict, MaybeTraverse, PyInner, PyObjectPayload, debug_obj, drop_dealloc_obj,
8-
try_clear_obj, try_trace_obj,
8+
try_clear_obj, try_traverse_obj,
99
},
1010
};
1111

@@ -29,7 +29,7 @@ impl PyObjVTable {
2929
debug: debug_obj::<T>,
3030
trace: const {
3131
if T::HAS_TRAVERSE {
32-
Some(try_trace_obj::<T>)
32+
Some(try_traverse_obj::<T>)
3333
} else {
3434
None
3535
}

crates/vm/src/signal.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,34 @@ static ANY_TRIGGERED: AtomicBool = AtomicBool::new(false);
1111
const ATOMIC_FALSE: AtomicBool = AtomicBool::new(false);
1212
pub(crate) static TRIGGERS: [AtomicBool; NSIG] = [ATOMIC_FALSE; NSIG];
1313

14+
// Reactivate EBR guard every N instructions to prevent epoch starvation.
15+
// This allows GC to advance epochs even during long-running operations.
16+
// 65536 instructions ≈ 1ms, much faster than CPython's 5ms GIL timeout
17+
#[cfg(all(feature = "threading", feature = "gc"))]
18+
const REACTIVATE_INTERVAL: u32 = 65536;
19+
20+
#[cfg(all(feature = "threading", feature = "gc"))]
21+
thread_local! {
22+
static INSTRUCTION_COUNTER: std::cell::Cell<u32> = const { std::cell::Cell::new(0) };
23+
}
24+
1425
#[cfg_attr(feature = "flame-it", flame)]
1526
#[inline(always)]
1627
pub fn check_signals(vm: &VirtualMachine) -> PyResult<()> {
28+
// Periodic EBR guard reactivation to prevent epoch starvation
29+
#[cfg(all(feature = "threading", feature = "gc"))]
30+
{
31+
INSTRUCTION_COUNTER.with(|counter| {
32+
let count = counter.get();
33+
if count >= REACTIVATE_INTERVAL {
34+
crate::vm::thread::reactivate_guard();
35+
counter.set(0);
36+
} else {
37+
counter.set(count + 1);
38+
}
39+
});
40+
}
41+
1742
if vm.signal_handlers.is_none() {
1843
return Ok(());
1944
}

crates/vm/src/stdlib/sys.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -866,7 +866,7 @@ mod sys {
866866

867867
for (thread_id, frame) in frames {
868868
let key = vm.ctx.new_int(thread_id);
869-
dict.set_item(key.as_object(), frame.as_object().to_owned(), vm)?;
869+
dict.set_item(key.as_object(), frame.into(), vm)?;
870870
}
871871

872872
Ok(dict)

0 commit comments

Comments
 (0)
0