summaryrefslogtreecommitdiffstats
path: root/Python/pystate.c
Commit message (Collapse)AuthorAgeFilesLines
* gh-116012: Preserve GetLastError() across calls to TlsGetValue on Windows ↵Steve Dower2024-02-281-9/+0
| | | | (GH-116014)
* gh-115168: Add pystats counter for invalidated executors (GH-115169)Michael Droettboom2024-02-261-1/+1
|
* gh-115103: Implement delayed free mechanism for free-threaded builds (#115367)Sam Gross2024-02-201-0/+6
| | | | | | This adds `_PyMem_FreeDelayed()` and supporting functions. The `_PyMem_FreeDelayed()` function frees memory with the same allocator as `PyMem_Free()`, but after some delay to ensure that concurrent lock-free readers have finished.
* gh-115491: Keep some fields valid across allocations (free-threading) (#115573)Sam Gross2024-02-201-0/+15
| | | | | This avoids filling the memory occupied by ob_tid, ob_ref_local, and ob_ref_shared with debug bytes (e.g., 0xDD) in mimalloc in the free-threaded build.
* gh-110850: Replace _PyTime_t with PyTime_t (#115719)Victor Stinner2024-02-201-1/+1
| | | | | Run command: sed -i -e 's!\<_PyTime_t\>!PyTime_t!g' $(find -name "*.c" -o -name "*.h")
* gh-112175: Add `eval_breaker` to `PyThreadState` (#115194)Brett Simmers2024-02-201-9/+7
| | | | | | | | | | | This change adds an `eval_breaker` field to `PyThreadState`. The primary motivation is for performance in free-threaded builds: with thread-local eval breakers, we can stop a specific thread (e.g., for an async exception) without interrupting other threads. The source of truth for the global instrumentation version is stored in the `instrumentation_version` field in PyInterpreterState. Threads usually read the version from their local `eval_breaker`, where it continues to be colocated with the eval breaker bits.
* GH-112354: Initial implementation of warm up on exits and trace-stitching ↵Mark Shannon2024-02-201-0/+2
| | | | (GH-114142)
* gh-115103: Implement delayed memory reclamation (QSBR) (#115180)Sam Gross2024-02-161-0/+30
| | | | | | This adds a safe memory reclamation scheme based on FreeBSD's "GUS" and quiescent state based reclamation (QSBR). The API provides a mechanism for callers to detect when it is safe to free memory that may be concurrently accessed by readers.
* gh-113743: Use per-interpreter locks for types (#115541)Dino Viehland2024-02-161-1/+1
| | | Move type-lock to per-interpreter lock to avoid heavy contention in interpreters test
* gh-113743: Make the MRO cache thread-safe in free-threaded builds (#113930)Dino Viehland2024-02-151-0/+3
| | | | | | | Makes _PyType_Lookup thread safe, including: Thread safety of the underlying cache. Make mutation of mro and type members thread safe Also _PyType_GetMRO and _PyType_GetBases are currently returning borrowed references which aren't safe.
* gh-115482: Assume the Main Interpreter is Always Running "main" (gh-115484)Eric Snow2024-02-141-1/+8
| | | | | This is a temporary fix to unblock embedders that do not call Py_Main(). _PyInterpreterState_IsRunningMain() will always return true for the main interpreter, even in corner cases where it technically should not. The (future) full solution will do the right thing in those corner cases.
* gh-111968: Rename freelist related struct names to Eric's suggestion (gh-115329)Donghee Na2024-02-141-2/+2
|
* GH-113710: Backedge counter improvements. (GH-115166)Mark Shannon2024-02-131-7/+3
|
* gh-111968: Refactor _PyXXX_Fini to integrate with _PyObject_ClearFreeLists ↵Donghee Na2024-02-101-17/+2
| | | | (gh-114899)
* gh-110481: Implement inter-thread queue for biased reference counting (#114824)Sam Gross2024-02-091-0/+11
| | | | | | | | | Biased reference counting maintains two refcount fields in each object: `ob_ref_local` and `ob_ref_shared`. The true refcount is the sum of these two fields. In some cases, when refcounting operations are split across threads, the ob_ref_shared field can be negative (although the total refcount must be at least zero). In this case, the thread that decremented the refcount requests that the owning thread give up ownership and merge the refcount fields.
* gh-115035: Mark ThreadHandles as non-joinable earlier after forking (#115042)Sam Gross2024-02-061-0/+2
| | | | | | This marks dead ThreadHandles as non-joinable earlier in `PyOS_AfterFork_Child()` before we execute any Python code. The handles are stored in a global linked list in `_PyRuntimeState` because `fork()` affects the entire process.
* gh-104530: Enable native Win32 condition variables by default (GH-104531)Andrew Rogers2024-02-021-1/+11
|
* gh-111968: Use per-thread freelists for dict in free-threading (gh-114323)Donghee Na2024-02-011-0/+3
|
* gh-103323: Remove current_fast_get() unused parameter (#114593)Victor Stinner2024-01-301-26/+24
| | | | The current_fast_get() static inline function doesn't use its 'runtime' parameter, so just remove it.
* gh-113055: Use pointer for interp->obmalloc state (gh-113412)Neil Schemenauer2024-01-271-8/+6
| | | | | | | | | For interpreters that share state with the main interpreter, this points to the same static memory structure. For interpreters with their own obmalloc state, it is heap allocated. Add free_obmalloc_arenas() which will free the obmalloc arenas and radix tree structures for interpreters with their own obmalloc state. Co-authored-by: Eric Snow <ericsnowcurrently@gmail.com>
* gh-112529: Implement GC for free-threaded builds (#114262)Sam Gross2024-01-251-1/+3
| | | | | | | * gh-112529: Implement GC for free-threaded builds This implements a mark and sweep GC for the free-threaded builds of CPython. The implementation relies on mimalloc to find GC tracked objects (i.e., "containers").
* gh-114312: Collect stats for unlikely events (GH-114493)Michael Droettboom2024-01-251-0/+1
|
* GH-113710: Add a tier 2 peephole optimization pass. (GH-114487)Mark Shannon2024-01-241-3/+7
| | | | | * Convert _LOAD_CONST to inline versions * Remove PEP 523 checks
* gh-111964: Implement stop-the-world pauses (gh-112471)Sam Gross2024-01-231-14/+255
| | | | | | | | | | | | | | | | | The `--disable-gil` builds occasionally need to pause all but one thread. Some examples include: * Cyclic garbage collection, where this is often called a "stop the world event" * Before calling `fork()`, to ensure a consistent state for internal data structures * During interpreter shutdown, to ensure that daemon threads aren't accessing Python objects This adds the following functions to implement global and per-interpreter pauses: * `_PyEval_StopTheWorldAll()` and `_PyEval_StartTheWorldAll()` (for the global runtime) * `_PyEval_StopTheWorld()` and `_PyEval_StartTheWorld()` (per-interpreter) (The function names may change.) These functions are no-ops outside of the `--disable-gil` build.
* gh-111968: Use per-thread freelists for generator in free-threading (gh-114189)Donghee Na2024-01-181-1/+2
|
* gh-111968: Use per-thread freelists for PyContext in free-threading (gh-114122)Donghee Na2024-01-161-0/+1
|
* gh-111968: Use per-thread slice_cache in free-threading (gh-113972)Donghee Na2024-01-151-0/+1
|
* gh-111968: Use per-thread freelists for tuple in free-threading (gh-113921)Donghee Na2024-01-111-0/+1
|
* gh-111968: Use per-thread freelists for float in free-threading (gh-113886)Donghee Na2024-01-101-0/+1
|
* gh-111968: Introduce _PyFreeListState and _PyFreeListState_GET API (gh-113584)Donghee Na2024-01-091-0/+11
|
* gh-112532: Tag mimalloc heaps and pages (#113742)Sam Gross2024-01-051-2/+2
| | | | | | | | | | | | | | | | | | | | | * gh-112532: Tag mimalloc heaps and pages Mimalloc pages are data structures that contain contiguous allocations of the same block size. Note that they are distinct from operating system pages. Mimalloc pages are contained in segments. When a thread exits, it abandons any segments and contained pages that have live allocations. These segments and pages may be later reclaimed by another thread. To support GC and certain thread-safety guarantees in free-threaded builds, we want pages to only be reclaimed by the corresponding heap in the claimant thread. For example, we want pages containing GC objects to only be claimed by GC heaps. This allows heaps and pages to be tagged with an integer tag that is used to ensure that abandoned pages are only claimed by heaps with the same tag. Heaps can be initialized with a tag (0-15); any page allocated by that heap copies the corresponding tag. * Fix conversion warning
* gh-112532: Isolate abandoned segments by interpreter (#113717)Sam Gross2024-01-041-0/+5
| | | | | | | | | | | | | | | * gh-112532: Isolate abandoned segments by interpreter Mimalloc segments are data structures that contain memory allocations along with metadata. Each segment is "owned" by a thread. When a thread exits, it abandons its segments to a global pool to be later reclaimed by other threads. This changes the pool to be per-interpreter instead of process-wide. This will be important for when we use mimalloc to find GC objects in the `--disable-gil` builds. We want heaps to only store Python objects from a single interpreter. Absent this change, the abandoning and reclaiming process could break this isolation. * Add missing '&_mi_abandoned_default' to 'tld_empty'
* gh-112532: Use separate mimalloc heaps for GC objects (gh-113263)Sam Gross2023-12-261-0/+55
| | | | | | | | | | | | | | | | | | | | | | | | | | * gh-112532: Use separate mimalloc heaps for GC objects In `--disable-gil` builds, we now use four separate heaps in anticipation of using mimalloc to find GC objects when the GIL is disabled. To support this, we also make a few changes to mimalloc: * `mi_heap_t` and `mi_tld_t` initialization is split from allocation. This allows us to have a `mi_tld_t` per-`PyThreadState`, which is important to keep interpreter isolation, since the same OS thread may run in multiple interpreters (using different PyThreadStates.) * Heap abandoning (mi_heap_collect_ex) can now be called from a different thread than the one that created the heap. This is necessary because we may clear and delete the containing PyThreadStates from a different thread during finalization and after fork(). * Use enum instead of defines and guard mimalloc includes. * The enum typedef will be convenient for future PRs that use the type. * Guarding the mimalloc includes allows us to unconditionally include pycore_mimalloc.h from other header files that rely on things like `struct _mimalloc_thread_state`. * Only define _mimalloc_thread_state in Py_GIL_DISABLED builds
* gh-112535: Implement fallback implementation of _Py_ThreadId() (gh-113185)Donghee Na2023-12-181-0/+14
| | | | | --------- Co-authored-by: Sam Gross <colesbury@gmail.com>
* gh-112723: Call `PyThreadState_Clear()` from the correct interpreter (#112776)Sam Gross2023-12-131-4/+3
| | | | | | | | | | | | | The `PyThreadState_Clear()` function must only be called with the GIL held and must be called from the same interpreter as the passed in thread state. Otherwise, any Python objects on the thread state may be destroyed using the wrong interpreter, leading to memory corruption. This is also important for `Py_GIL_DISABLED` builds because free lists will be associated with PyThreadStates and cleared in `PyThreadState_Clear()`. This fixes two places that called `PyThreadState_Clear()` from the wrong interpreter and adds an assertion to `PyThreadState_Clear()`.
* gh-76785: Fixes for test.support.interpreters (gh-112982)Eric Snow2023-12-121-1/+1
| | | This involves a number of changes for PEP 734.
* gh-111924: Use PyMutex for Runtime-global Locks. (gh-112207)Sam Gross2023-12-071-107/+14
| | | | | This replaces some usages of PyThread_type_lock with PyMutex, which does not require memory allocation to initialize. This simplifies some of the runtime initialization and is also one step towards avoiding changing the default raw memory allocator during initialize/finalization, which can be non-thread-safe in some circumstances.
* gh-112538: Add internal-only _PyThreadStateImpl "wrapper" for PyThreadState ↵Sam Gross2023-12-071-14/+14
| | | | | | | | (gh-112560) Every PyThreadState instance is now actually a _PyThreadStateImpl. It is safe to cast from `PyThreadState*` to `_PyThreadStateImpl*` and back. The _PyThreadStateImpl will contain fields that we do not want to expose in the public C API.
* gh-111863: Rename `Py_NOGIL` to `Py_GIL_DISABLED` (#111864)Hugo van Kemenade2023-11-201-2/+2
| | | Rename Py_NOGIL to Py_GIL_DISABLED
* gh-111956: Add thread-safe one-time initialization. (gh-111960)Sam Gross2023-11-161-2/+1
|
* gh-111569: Implement Python critical section API (gh-111571)Sam Gross2023-11-081-0/+10
| | | | | | | | Critical sections are helpers to replace the global interpreter lock with finer grained locking. They provide similar guarantees to the GIL and avoid the deadlock risk that plain locking involves. Critical sections are implicitly ended whenever the GIL would be released. They are resumed when the GIL would be acquired. Nested critical sections behave as if the sections were interleaved.
* gh-103615: Use local events for opcode tracing (GH-109472)Tian Gao2023-11-031-2/+0
| | | | | | | * Use local monitoring for opcode trace * Remove f_opcode_trace_set * Add test for setting f_trace_opcodes after settrace
* gh-76785: Crossinterp utils additions (gh-111530)Eric Snow2023-11-011-18/+1
| | | | | | | | | | | | This moves several general internal APIs out of _xxsubinterpretersmodule.c and into the new Python/crossinterp.c (and the corresponding internal headers). Specifically: * _Py_excinfo, etc.: the initial implementation for non-object exception snapshots (in pycore_pyerrors.h and Python/errors.c) * _PyXI_exception_info, etc.: helpers for passing an exception beween interpreters (wraps _Py_excinfo) * _PyXI_namespace, etc.: helpers for copying a dict of attrs between interpreters * _PyXI_Enter(), _PyXI_Exit(): functions that abstract out the transitions between one interpreter and a second that will do some work temporarily Again, these were all abstracted out of _xxsubinterpretersmodule.c as generalizations. I plan on proposing these as public API at some point.
* gh-76785: Move the Cross-Interpreter Code to Its Own File (gh-111502)Eric Snow2023-10-301-591/+4
| | | This is partly to clear this stuff out of pystate.c, but also in preparation for moving some code out of _xxsubinterpretersmodule.c. This change also moves this stuff to the internal API (new: Include/internal/pycore_crossinterp.h). @vstinner did this previously and I undid it. Now I'm re-doing it. :/
* GH-109369: Add machinery for deoptimizing tier2 executors, both individually ↵Mark Shannon2023-10-231-0/+1
| | | | and globally. (GH-110384)
* gh-76785: Clean Up the Channels Module (gh-110568)Eric Snow2023-10-171-7/+7
|
* gh-110752: Reset `ceval.eval_breaker` to 0 in `interpreter_clear` (GH-110753)Tian Gao2023-10-121-0/+4
|
* gh-76785: Add SendChannel.send_buffer() (#110246)Eric Snow2023-10-091-17/+26
| | | (This is still a test module.)
* GH-110455: Guard `assert(tstate->thread_id > 0)` with `#ifndef ↵Brett Cannon2023-10-061-2/+2
| | | | HAVE_PTHREAD_STUBS` (GH-110487)
* gh-109549: Add new states to PyThreadState to support PEP 703 (gh-109915)Sam Gross2023-10-051-46/+79
| | | This adds a new field 'state' to PyThreadState that can take on one of three values: _Py_THREAD_ATTACHED, _Py_THREAD_DETACHED, or _Py_THREAD_GC. The "attached" and "detached" states correspond closely to acquiring and releasing the GIL. The "gc" state is current unused, but will be used to implement stop-the-world GC for --disable-gil builds in the near future.