@@ -1618,44 +1618,101 @@ impl VirtualMachine {
16181618
16191619 #[ cfg( feature = "threading" ) ]
16201620 pub ( super ) fn new_thread_vm ( & self ) -> Self {
1621- let global_trace = self . state . global_trace_func . lock ( ) . clone ( ) ;
1622- let global_profile = self . state . global_profile_func . lock ( ) . clone ( ) ;
1623- let use_tracing = global_trace. is_some ( ) || global_profile. is_some ( ) ;
1621+ InterpreterThreadRoots :: new ( self ) . into_vm (
1622+ self . state . clone ( ) ,
1623+ self . recursion_limit . get ( ) ,
1624+ self . thread_factory . clone ( ) ,
1625+ )
1626+ }
1627+ }
1628+
1629+ /// Shared roots needed to create a thread state, without sharing any mutable
1630+ /// thread-local VM fields. VMs own this factory; the runtime registry is weak.
1631+ #[ cfg( feature = "threading" ) ]
1632+ pub ( crate ) struct InterpreterThreadFactory {
1633+ roots : InterpreterThreadRoots ,
1634+ pub ( crate ) recursion_limit : crossbeam_utils:: atomic:: AtomicCell < usize > ,
1635+ }
16241636
1637+ #[ cfg( feature = "threading" ) ]
1638+ #[ derive( Clone ) ]
1639+ struct InterpreterThreadRoots {
1640+ builtins : crate :: PyRef < crate :: builtins:: PyModule > ,
1641+ sys_module : crate :: PyRef < crate :: builtins:: PyModule > ,
1642+ ctx : crate :: common:: rc:: PyRc < crate :: Context > ,
1643+ wasm_id : Option < String > ,
1644+ import_func : PyObjectRef ,
1645+ importlib : PyObjectRef ,
1646+ initialized : bool ,
1647+ import_bootstrap_complete : bool ,
1648+ callable_cache : super :: CallableCache ,
1649+ startup_error : Option < PyBaseExceptionRef > ,
1650+ }
1651+
1652+ #[ cfg( feature = "threading" ) ]
1653+ impl InterpreterThreadRoots {
1654+ fn new ( vm : & VirtualMachine ) -> Self {
16251655 Self {
1626- builtins : self . builtins . clone ( ) ,
1627- sys_module : self . sys_module . clone ( ) ,
1628- ctx : self . ctx . clone ( ) ,
1656+ builtins : vm. builtins . clone ( ) ,
1657+ sys_module : vm. sys_module . clone ( ) ,
1658+ ctx : vm. ctx . clone ( ) ,
1659+ wasm_id : vm. wasm_id . clone ( ) ,
1660+ import_func : vm. import_func . clone ( ) ,
1661+ importlib : vm. importlib . clone ( ) ,
1662+ initialized : vm. initialized ,
1663+ import_bootstrap_complete : vm. import_bootstrap_complete ,
1664+ callable_cache : vm. callable_cache . clone ( ) ,
1665+ startup_error : vm. startup_error . clone ( ) ,
1666+ }
1667+ }
1668+
1669+ fn into_vm (
1670+ self ,
1671+ state : crate :: common:: rc:: PyRc < PyGlobalState > ,
1672+ recursion_limit : usize ,
1673+ factory : Option < Arc < InterpreterThreadFactory > > ,
1674+ ) -> VirtualMachine {
1675+ let global_trace = state. global_trace_func . lock ( ) . clone ( ) ;
1676+ let global_profile = state. global_profile_func . lock ( ) . clone ( ) ;
1677+ let use_tracing = global_trace. is_some ( ) || global_profile. is_some ( ) ;
1678+ let profile_func = RefCell :: new ( global_profile. unwrap_or_else ( || self . ctx . none ( ) ) ) ;
1679+ let trace_func = RefCell :: new ( global_trace. unwrap_or_else ( || self . ctx . none ( ) ) ) ;
1680+
1681+ VirtualMachine {
1682+ thread_factory : factory,
1683+ builtins : self . builtins ,
1684+ sys_module : self . sys_module ,
1685+ ctx : self . ctx ,
16291686 datastack : core:: cell:: UnsafeCell :: new ( crate :: datastack:: DataStack :: new ( ) ) ,
1630- wasm_id : self . wasm_id . clone ( ) ,
1687+ wasm_id : self . wasm_id ,
16311688 exceptions : RefCell :: default ( ) ,
1632- import_func : self . import_func . clone ( ) ,
1633- importlib : self . importlib . clone ( ) ,
1689+ import_func : self . import_func ,
1690+ importlib : self . importlib ,
16341691 import_timing : Cell :: default ( ) ,
1635- profile_func : RefCell :: new ( global_profile . unwrap_or_else ( || self . ctx . none ( ) ) ) ,
1636- trace_func : RefCell :: new ( global_trace . unwrap_or_else ( || self . ctx . none ( ) ) ) ,
1692+ profile_func,
1693+ trace_func,
16371694 use_tracing : Cell :: new ( use_tracing) ,
16381695 what_event : Cell :: new ( None ) ,
16391696 tracing_depth : Cell :: new ( 0 ) ,
1640- recursion_limit : self . recursion_limit . clone ( ) ,
1697+ recursion_limit : Cell :: new ( recursion_limit ) ,
16411698 signal_handlers : core:: cell:: OnceCell :: new ( ) ,
16421699 signal_rx : None ,
16431700 repr_guards : RefCell :: default ( ) ,
16441701 lazy_imports_resolving : RefCell :: default ( ) ,
1645- state : self . state . clone ( ) ,
1702+ state,
16461703 initialized : self . initialized ,
16471704 import_bootstrap_complete : self . import_bootstrap_complete ,
1648- startup_error : self . startup_error . clone ( ) ,
1705+ startup_error : self . startup_error ,
16491706 recursion_depth : Cell :: new ( 0 ) ,
16501707 #[ cfg( any( miri, target_env = "musl" , target_arch = "wasm32" ) ) ]
16511708 native_recursion_depth : Cell :: new ( 0 ) ,
1652- c_stack_soft_limit : Cell :: new ( Self :: calculate_c_stack_soft_limit ( ) ) ,
1709+ c_stack_soft_limit : Cell :: new ( VirtualMachine :: calculate_c_stack_soft_limit ( ) ) ,
16531710 async_gen_firstiter : RefCell :: new ( None ) ,
16541711 async_gen_finalizer : RefCell :: new ( None ) ,
16551712 asyncio_running_loop : RefCell :: new ( None ) ,
16561713 asyncio_running_task : RefCell :: new ( None ) ,
16571714 context_stack : RefCell :: default ( ) ,
1658- callable_cache : self . callable_cache . clone ( ) ,
1715+ callable_cache : self . callable_cache ,
16591716 pending_tailcall_frame : Cell :: new ( None ) ,
16601717 pending_tailcall_owner : core:: cell:: UnsafeCell :: new ( None ) ,
16611718 pending_gen_resume : core:: cell:: UnsafeCell :: new ( None ) ,
@@ -1664,3 +1721,25 @@ impl VirtualMachine {
16641721 }
16651722 }
16661723}
1724+
1725+ #[ cfg( feature = "threading" ) ]
1726+ impl InterpreterThreadFactory {
1727+ pub ( super ) fn new ( vm : & VirtualMachine ) -> Self {
1728+ Self {
1729+ roots : InterpreterThreadRoots :: new ( vm) ,
1730+ recursion_limit : crossbeam_utils:: atomic:: AtomicCell :: new ( vm. recursion_limit . get ( ) ) ,
1731+ }
1732+ }
1733+
1734+ pub ( crate ) fn new_thread (
1735+ self : & Arc < Self > ,
1736+ state : crate :: common:: rc:: PyRc < PyGlobalState > ,
1737+ ) -> ThreadedVirtualMachine {
1738+ ThreadedVirtualMachine {
1739+ vm : self
1740+ . roots
1741+ . clone ( )
1742+ . into_vm ( state, self . recursion_limit . load ( ) , Some ( self . clone ( ) ) ) ,
1743+ }
1744+ }
1745+ }
0 commit comments