◐ Shell
clean mode source ↗

gh-116322: Add Py_mod_gil module slot by swtaarrs · Pull Request #116882 · python/cpython

@swtaarrs

@swtaarrs

@swtaarrs swtaarrs marked this pull request as ready for review

March 22, 2024 21:54

ericsnowcurrently

swtaarrs

More details:
- Fix a race while enabling the GIL by checking if the GIL was enabled between
  a no-op call to `_PyEval_AcquireLock()` and the thread attaching, and trying
  again if it was.
- Enable the GIL before running a module init function, since we can't know if
  it's a single-phase init module that doesn't support free-threading. Look at
  the state of the module after initialization to determine if it's safe to
  disable the GIL again.
- Add `PyModule_SetGIL()`, which can be used by single-phase init modules to
  declare that they support running without the GIL.
- Change `gil->enabled` from a simple on/off switch to a count of active
  requests to enable the GIL. This allows us to support multiple interleaved
  imports that each independently track whether the GIL should remain
  enabled. See the big comment in `pycore_ceval.h` for more details.

colesbury

@swtaarrs

Yhg1s

SonicField pushed a commit to SonicField/cpython that referenced this pull request

May 8, 2024
This PR adds the ability to enable the GIL if it was disabled at
interpreter startup, and modifies the multi-phase module initialization
path to enable the GIL when loading a module, unless that module's spec
includes a slot indicating it can run safely without the GIL.

PEP 703 called the constant for the slot `Py_mod_gil_not_used`; I went
with `Py_MOD_GIL_NOT_USED` for consistency with pythongh-104148.

A warning will be issued up to once per interpreter for the first
GIL-using module that is loaded. If `-v` is given, a shorter message
will be printed to stderr every time a GIL-using module is loaded
(including the first one that issues a warning).

serhiy-storchaka added a commit to serhiy-storchaka/cpython that referenced this pull request

Jul 25, 2024

serhiy-storchaka

colesbury added a commit to colesbury/cpython that referenced this pull request

Jul 25, 2024

stefanholek added a commit to stefanholek/rl that referenced this pull request

Oct 14, 2024