summaryrefslogtreecommitdiffstats
path: root/Python/qsbr.c
Commit message (Collapse)AuthorAgeFilesLines
* fix typos in crossinterp.c and qsbr.c (#142612)wangjingcun2025-12-121-1/+1
|
* gh-131253: free-threaded build support for pystats (gh-137189)Neil Schemenauer2025-11-031-1/+2
| | | | | | | | Allow the --enable-pystats build option to be used with free-threading. The stats are now stored on a per-interpreter basis, rather than process global. For free-threaded builds, the stats structure is allocated per-thread and then periodically merged into the per-interpreter stats structure (on thread exit or when the reporting function is called). Most of the pystats related code has be moved into the file Python/pystats.c.
* GH-133136: Revise QSBR to reduce excess memory held (gh-135473)Neil Schemenauer2025-06-251-10/+2
| | | | | | | | | | | | | | | | | The free threading build uses QSBR to delay the freeing of dictionary keys and list arrays when the objects are accessed by multiple threads in order to allow concurrent reads to proceed with holding the object lock. The requests are processed in batches to reduce execution overhead, but for large memory blocks this can lead to excess memory usage. Take into account the size of the memory block when deciding when to process QSBR requests. Also track the amount of memory being held by QSBR for mimalloc pages. Advance the write sequence if this memory exceeds a limit. Advancing the sequence will allow it to be freed more quickly. Process the held QSBR items from the "eval breaker", rather than from `_PyMem_FreeDelayed()`. This gives a higher chance that the global read sequence has advanced enough so that items can be freed. Co-authored-by: Sam Gross <colesbury@gmail.com>
* gh-119786: Add InternalDocs/qsbr.md. (gh-135411)Neil Schemenauer2025-06-231-1/+1
| | | Add internal doc for the Quiescent-State Based Reclamation (QSBR) implementation.
* gh-131238: Add explicit includes to pycore headers (#131257)Victor Stinner2025-03-171-3/+3
|
* gh-124878: Fix race conditions during interpreter finalization (#130649)Sam Gross2025-03-061-1/+1
| | | | | | | | | | | | | | | | | | | | The PyThreadState field gains a reference count field to avoid issues with PyThreadState being a dangling pointer to freed memory. The refcount starts with a value of two: one reference is owned by the interpreter's linked list of thread states and one reference is owned by the OS thread. The reference count is decremented when the thread state is removed from the interpreter's linked list and before the OS thread calls `PyThread_hang_thread()`. The thread that decrements it to zero frees the `PyThreadState` memory. The `holds_gil` field is moved out of the `_status` bit field, to avoid a data race where on thread calls `PyThreadState_Clear()`, modifying the `_status` bit field while the OS thread reads `holds_gil` when attempting to acquire the GIL. The `PyThreadState.state` field now has `_Py_THREAD_SHUTTING_DOWN` as a possible value. This corresponds to the `_PyThreadState_MustExit()` check. This avoids race conditions in the free threading build when checking `_PyThreadState_MustExit()`.
* gh-130794: Process interpreter QSBR queue in _PyMem_AbandonDelayed. (gh-130808)Sam Gross2025-03-041-0/+1
| | | | | This avoids a case where the interpreter's queue of memory to be freed could grow rapidly if there are many short lived threads.
* gh-129732: Fix race on `shared->array` in qsbr code under free-threading ↵Peter Hawkins2025-02-061-6/+6
| | | | | | (gh-129738) The read of `shared->array` should happen under the lock to avoid a race.
* Fix typos in comments (#120481)Xie Yanbo2024-06-201-2/+2
|
* gh-117657: Fix TSAN race in QSBR assertion (#119887)Sam Gross2024-06-011-1/+2
| | | | Due to a limitation in TSAN, all reads from `PyThreadState.state` must be atomic to avoid reported races.
* gh-119369: Fix deadlock during thread exit in free-threaded build (#119528)Sam Gross2024-05-311-0/+5
| | | | | | | Release the GIL before calling `_Py_qsbr_unregister`. The deadlock could occur when the GIL was enabled at runtime. The `_Py_qsbr_unregister` call might block while holding the GIL because the thread state was not active, but the GIL was still held.
* gh-117657: Fix QSBR race condition (#118843)Alex Turner2024-05-101-5/+6
| | | | | | `_Py_qsbr_unregister` is called when the PyThreadState is already detached, so the access to `tstate->qsbr` isn't safe without locking the shared mutex. Grab the `struct _qsbr_shared` from the interpreter instead.
* gh-115103: Fix unregistering of QSBR state (#116480)Sam Gross2024-03-081-3/+7
| | | | | If a thread blocks while waiting on the `shared->mutex` lock, the array of QSBR states may be reallocated. The `tstate->qsbr` values before the lock is acquired may not be the same as the value after the lock is acquired.
* gh-115103: Delay reuse of mimalloc pages that store PyObjects (#115435)Sam Gross2024-03-061-10/+2
| | | | | | | | | | | | | | | | | | This implements the delayed reuse of mimalloc pages that contain Python objects in the free-threaded build. Allocations of the same size class are grouped in data structures called pages. These are different from operating system pages. For thread-safety, we want to ensure that memory used to store PyObjects remains valid as long as there may be concurrent lock-free readers; we want to delay using it for other size classes, in other heaps, or returning it to the operating system. When a mimalloc page becomes empty, instead of immediately freeing it, we tag it with a QSBR goal and insert it into a per-thread state linked list of pages to be freed. When mimalloc needs a fresh page, we process the queue and free any still empty pages that are now deemed safe to be freed. Pages waiting to be freed are still available for allocations of the same size class and allocating from a page prevent it from being freed. There is additional logic to handle abandoned pages when threads exit.
* gh-115103: Implement delayed memory reclamation (QSBR) (#115180)Sam Gross2024-02-161-0/+286
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.