gh-108724: Add PyMutex and _PyParkingLot APIs#109344
Merged
Merged
Conversation
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
PyMutexis a one byte lock with fast, inlineable lock and unlock functions for the common uncontended case. The design is based on WebKit'sWTF::Lock.PyMutex is built using the
_PyParkingLotAPIs, which provides a cross-platform futex-like API (based on WebKit'sWTF::ParkingLot). This internal API will be used for building other synchronization primitives used to implement PEP 703, such as one-time initialization and events.This also includes tests and a mini benchmark in
Tools/lockbench/lockbench.pyto compare with the existingPyThread_type_lock.Uncontended acquisition + release:
Linux (x86-64): PyMutex: 11 ns, PyThread_type_lock: 44 ns
macOS (arm64): PyMutex: 13 ns, PyThread_type_lock: 18 ns
Windows (x86-64): PyMutex: 13 ns, PyThread_type_lock: 38 ns
PR Overview
The primary purpose of this PR is to implement
PyMutex, but there are a number of support pieces (described below).PyMutex: A 1-byte lock that doesn't require memory allocation to initialize and is generally faster than the existingPyThread_type_lock. The API is internal only for now._PyParking_Lot: A futex-like API based on the API of the same name in WebKit. Used to implementPyMutex._PyRawMutex: A word sized lock used to implement_PyParking_LotPyEvent: a one time event. This was used a bunch in the "nogil" fork and is useful for testing thePyMuteximplementation, so I've included it as part of the PR.pycore_llist.h: Defines common operations on doubly-linked list. Not strictly necessary (could do the list operations manually), but they come up frequently in the "nogil" fork. (Similar to https://man.freebsd.org/cgi/man.cgi?queue)Linked issue