MCPcopy Index your code
hub / github.com/RustPython/RustPython / cleanup_current_thread_frames

Function cleanup_current_thread_frames

crates/vm/src/vm/thread.rs:426–466  ·  view source on GitHub ↗
(vm: &VirtualMachine)

Source from the content-addressed store, hash-verified

424/// Cleanup thread slot for the current thread. Called at thread exit.
425#[cfg(feature = "threading")]
426pub fn cleanup_current_thread_frames(vm: &VirtualMachine) {
427 let thread_id = crate::stdlib::_thread::get_ident();
428 let current_slot = CURRENT_THREAD_SLOT.with(|slot| slot.borrow().as_ref().cloned());
429
430 // A dying thread should not remain logically ATTACHED while its
431 // thread-state slot is being removed.
432 #[cfg(all(unix, feature = "threading"))]
433 if let Some(slot) = &current_slot {
434 let _ = slot.state.compare_exchange(
435 THREAD_ATTACHED,
436 THREAD_DETACHED,
437 Ordering::AcqRel,
438 Ordering::Acquire,
439 );
440 }
441
442 // Guard against OS thread-id reuse races: only remove the registry entry
443 // if it still points at this thread's own slot.
444 let removed = if let Some(slot) = &current_slot {
445 let mut registry = vm.state.thread_frames.lock();
446 match registry.get(&thread_id) {
447 Some(registered) if Arc::ptr_eq(registered, slot) => registry.remove(&thread_id),
448 _ => None,
449 }
450 } else {
451 None
452 };
453 #[cfg(all(unix, feature = "threading"))]
454 if let Some(slot) = &removed
455 && vm.state.stop_the_world.requested.load(Ordering::Acquire)
456 && thread_id != vm.state.stop_the_world.requester_ident()
457 && slot.state.load(Ordering::Relaxed) != THREAD_SUSPENDED
458 {
459 // A non-requester thread disappeared while stop-the-world is pending.
460 // Unblock requester countdown progress.
461 vm.state.stop_the_world.notify_thread_gone();
462 }
463 CURRENT_THREAD_SLOT.with(|s| {
464 *s.borrow_mut() = None;
465 });
466}
467
468/// Reinitialize thread slot after fork. Called in child process.
469/// Creates a fresh slot and registers it for the current thread,

Callers 1

run_threadFunction · 0.85

Calls 11

withMethod · 0.80
requester_identMethod · 0.80
notify_thread_goneMethod · 0.80
borrow_mutMethod · 0.80
get_identFunction · 0.50
as_refMethod · 0.45
borrowMethod · 0.45
lockMethod · 0.45
getMethod · 0.45
removeMethod · 0.45
loadMethod · 0.45

Tested by

no test coverage detected