Upgrade threading to 3.13.11; sys.setprofile & impl more threading by youknowone · Pull Request #6691 · RustPython/RustPython
112-112: LGTM: Module attributes and Lock constructor updated correctly.
The module attribute changes to "_thread" align with Python's standard library naming, and the Lock constructor correctly initializes with RawMutex::INIT. The parameter rename to _vm follows Rust conventions for unused parameters.
Also applies to: 176-177, 190-190
402-434: LGTM: Thread management functions implemented correctly.
The new functions provide essential threading APIs:
daemon_threads_allowedand_is_main_interpreterreturn appropriate constants for RustPython's single-interpreter model_shutdownis correctly stubbed as cleanup happens via the Thread class_make_thread_handleproperly creates a handle for an existing thread_get_main_thread_identprovides access to the main thread identifier
436-443: LGTM: Main thread identifier storage implemented correctly.
The use of AtomicCell<u64> ensures thread-safe access to the main thread identifier. The initialization logic correctly captures the current thread's ID.
577-596: LGTM: Thread-local cleanup mechanism implemented correctly.
The LocalGuard with Weak reference ensures proper cleanup of thread-local data when threads terminate, while avoiding circular references that would prevent cleanup. The Drop implementation correctly checks if the LocalData still exists before attempting removal.
598-607: LGTM: LocalData structure is well-designed.
The structure uses parking_lot::Mutex to protect a HashMap mapping thread IDs to per-thread dictionaries, providing an efficient thread-local storage implementation.
610-665: Excellent implementation of thread-local storage with proper synchronization.
The Local::l_dict implementation uses an efficient double-checked locking pattern:
- Fast path checks for existing dict under lock
- Slow path allocates outside lock to reduce contention
- Uses
EntryAPI to handle races correctly - Explicitly drops lock before TLS access to avoid potential deadlocks
- Guards registered only for new entries
This is a well-thought-out design that minimizes lock contention while ensuring correctness.
710-736: LGTM: Thread handle state machine and internal structure are well-designed.
The ThreadHandleState enum defines a clear lifecycle (NotStarted → Starting → Running → Done), and ThreadHandleInner tracks all necessary state including the join handle and a joined flag for idempotent join operations.
738-772: LGTM: ThreadHandle accessors and constructors are correctly implemented.
The new(), ident(), is_done(), _set_done(), and slot_new() methods properly manage the thread handle state with appropriate locking.
Also applies to: 810-816
818-827: LGTM: StartJoinableThreadArgs structure is well-defined.
The arguments structure correctly captures the function to execute, an optional pre-created handle, and a daemon flag (currently unused but allowed for future use).
3-3: The init_main_thread_ident function is properly called during VM initialization at crates/vm/src/vm/mod.rs:310, before other stdlib modules are initialized. The initialization is correctly guarded by the threading feature flag and properly captures the main thread identifier.