Skip to content

Commit 334bb98

Browse files
committed
no static for asyncgen
1 parent b4e4b66 commit 334bb98

File tree

4 files changed

+20
-15
lines changed

4 files changed

+20
-15
lines changed

crates/vm/src/builtins/asyncgenerator.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,14 +57,14 @@ impl PyAsyncGen {
5757

5858
zelf.ag_hooks_inited.store(true);
5959

60-
// Get and store finalizer from thread-local storage
61-
let finalizer = crate::vm::thread::ASYNC_GEN_FINALIZER.with_borrow(|f| f.as_ref().cloned());
60+
// Get and store finalizer from VM
61+
let finalizer = vm.async_gen_finalizer.borrow().clone();
6262
if let Some(finalizer) = finalizer {
6363
*zelf.ag_finalizer.lock() = Some(finalizer);
6464
}
6565

6666
// Call firstiter hook
67-
let firstiter = crate::vm::thread::ASYNC_GEN_FIRSTITER.with_borrow(|f| f.as_ref().cloned());
67+
let firstiter = vm.async_gen_firstiter.borrow().clone();
6868
if let Some(firstiter) = firstiter {
6969
let obj: PyObjectRef = zelf.to_owned().into();
7070
firstiter.call((obj,), vm)?;

crates/vm/src/stdlib/sys.rs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1229,10 +1229,10 @@ mod sys {
12291229
}
12301230

12311231
if let Some(finalizer) = args.finalizer.into_option() {
1232-
crate::vm::thread::ASYNC_GEN_FINALIZER.set(finalizer);
1232+
*vm.async_gen_finalizer.borrow_mut() = finalizer;
12331233
}
12341234
if let Some(firstiter) = args.firstiter.into_option() {
1235-
crate::vm::thread::ASYNC_GEN_FIRSTITER.set(firstiter);
1235+
*vm.async_gen_firstiter.borrow_mut() = firstiter;
12361236
}
12371237

12381238
Ok(())
@@ -1254,12 +1254,8 @@ mod sys {
12541254
#[pyfunction]
12551255
fn get_asyncgen_hooks(vm: &VirtualMachine) -> AsyncgenHooksData {
12561256
AsyncgenHooksData {
1257-
firstiter: crate::vm::thread::ASYNC_GEN_FIRSTITER
1258-
.with_borrow(Clone::clone)
1259-
.to_pyobject(vm),
1260-
finalizer: crate::vm::thread::ASYNC_GEN_FINALIZER
1261-
.with_borrow(Clone::clone)
1262-
.to_pyobject(vm),
1257+
firstiter: vm.async_gen_firstiter.borrow().clone().to_pyobject(vm),
1258+
finalizer: vm.async_gen_finalizer.borrow().clone().to_pyobject(vm),
12631259
}
12641260
}
12651261

crates/vm/src/vm/mod.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,10 @@ pub struct VirtualMachine {
8282
pub state: PyRc<PyGlobalState>,
8383
pub initialized: bool,
8484
recursion_depth: Cell<usize>,
85+
/// Async generator firstiter hook (per-thread, set via sys.set_asyncgen_hooks)
86+
pub async_gen_firstiter: RefCell<Option<PyObjectRef>>,
87+
/// Async generator finalizer hook (per-thread, set via sys.set_asyncgen_hooks)
88+
pub async_gen_finalizer: RefCell<Option<PyObjectRef>>,
8589
}
8690

8791
#[derive(Debug, Default)]
@@ -220,6 +224,8 @@ impl VirtualMachine {
220224
}),
221225
initialized: false,
222226
recursion_depth: Cell::new(0),
227+
async_gen_firstiter: RefCell::new(None),
228+
async_gen_finalizer: RefCell::new(None),
223229
};
224230

225231
if vm.state.hash_secret.hash_str("")

crates/vm/src/vm/thread.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
use crate::{AsObject, PyObject, PyObjectRef, VirtualMachine};
21
#[cfg(feature = "threading")]
32
use crate::frame::FrameRef;
3+
use crate::{AsObject, PyObject, VirtualMachine};
44
use core::{
55
cell::{Cell, RefCell},
66
ptr::NonNull,
@@ -18,8 +18,6 @@ thread_local! {
1818
pub(super) static VM_STACK: RefCell<Vec<NonNull<VirtualMachine>>> = Vec::with_capacity(1).into();
1919

2020
pub(crate) static COROUTINE_ORIGIN_TRACKING_DEPTH: Cell<u32> = const { Cell::new(0) };
21-
pub(crate) static ASYNC_GEN_FINALIZER: RefCell<Option<PyObjectRef>> = const { RefCell::new(None) };
22-
pub(crate) static ASYNC_GEN_FIRSTITER: RefCell<Option<PyObjectRef>> = const { RefCell::new(None) };
2321

2422
/// Current thread's frame slot for sys._current_frames()
2523
#[cfg(feature = "threading")]
@@ -56,7 +54,10 @@ fn init_frame_slot_if_needed(vm: &VirtualMachine) {
5654
if slot.borrow().is_none() {
5755
let thread_id = crate::stdlib::thread::get_ident();
5856
let new_slot = Arc::new(parking_lot::Mutex::new(None));
59-
vm.state.thread_frames.lock().insert(thread_id, new_slot.clone());
57+
vm.state
58+
.thread_frames
59+
.lock()
60+
.insert(thread_id, new_slot.clone());
6061
*slot.borrow_mut() = Some(new_slot);
6162
}
6263
});
@@ -233,6 +234,8 @@ impl VirtualMachine {
233234
state: self.state.clone(),
234235
initialized: self.initialized,
235236
recursion_depth: Cell::new(0),
237+
async_gen_firstiter: RefCell::new(None),
238+
async_gen_finalizer: RefCell::new(None),
236239
};
237240
ThreadedVirtualMachine { vm }
238241
}

0 commit comments

Comments
 (0)