]> git.ipfire.org Git - thirdparty/Python/cpython.git/commit
gh-108724: Add PyMutex and _PyParkingLot APIs (gh-109344)
authorSam Gross <colesbury@gmail.com>
Tue, 19 Sep 2023 15:54:29 +0000 (11:54 -0400)
committerGitHub <noreply@github.com>
Tue, 19 Sep 2023 15:54:29 +0000 (09:54 -0600)
commit0c89056fe59ac42f09978582479d40e58a236856
tree06cd5a790da2a6dd3862567419c25572f96ae373
parent0a31ff0050eec5079fd4c9cafd33b4e3e9afd9ab
gh-108724: Add PyMutex and _PyParkingLot APIs (gh-109344)

PyMutex is a one byte lock with fast, inlineable lock and unlock functions for the common uncontended case.  The design is based on WebKit's WTF::Lock.

PyMutex is built using the _PyParkingLot APIs, which provides a cross-platform futex-like API (based on WebKit's WTF::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.py to compare with the existing PyThread_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 existing PyThread_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 implement PyMutex.
* _PyRawMutex:  A word sized lock used to implement _PyParking_Lot.
* PyEvent:  A one time event.  This was used a bunch in the "nogil" fork and is useful for testing the PyMutex implementation, 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)

---------

Co-authored-by: Eric Snow <ericsnowcurrently@gmail.com>
29 files changed:
Include/Python.h
Include/cpython/pyatomic.h
Include/cpython/pyatomic_msc.h
Include/internal/pycore_llist.h [new file with mode: 0644]
Include/internal/pycore_lock.h [new file with mode: 0644]
Include/internal/pycore_parking_lot.h [new file with mode: 0644]
Include/internal/pycore_semaphore.h [new file with mode: 0644]
Include/pyatomic.h [new file with mode: 0644]
Lib/test/test_capi/test_misc.py
Makefile.pre.in
Misc/NEWS.d/next/C API/2023-09-12-13-09-36.gh-issue-108724.-yMsC8.rst [new file with mode: 0644]
Modules/Setup.stdlib.in
Modules/_testcapi/pyatomic.c
Modules/_testinternalcapi.c
Modules/_testinternalcapi/clinic/test_lock.c.h [new file with mode: 0644]
Modules/_testinternalcapi/parts.h
Modules/_testinternalcapi/test_lock.c [new file with mode: 0644]
PCbuild/_testinternalcapi.vcxproj
PCbuild/_testinternalcapi.vcxproj.filters
PCbuild/pythoncore.vcxproj
PCbuild/pythoncore.vcxproj.filters
Python/lock.c [new file with mode: 0644]
Python/parking_lot.c [new file with mode: 0644]
Python/pystate.c
Tools/c-analyzer/cpython/_parser.py
Tools/c-analyzer/cpython/ignored.tsv
Tools/lockbench/lockbench.py [new file with mode: 0644]
configure
configure.ac