fix: support free-threaded CPython with GIL disabled (#5148)

* Support free-threaded CPython (PEP 703)

Some additional locking is added in the free-threaded build when
`Py_GIL_DISABLED` is defined:

- Most accesses to internals are protected by a single mutex
- The registered_instances uses a striped lock to improve concurrency

Pybind11 modules can indicate support for running with the GIL disabled
by calling `set_gil_not_used()`.

* refactor: use PYBIND11_MODULE (#11)

Signed-off-by: Henry Schreiner <henryschreineriii@gmail.com>

* Address code review

* Suppress MSVC warning

* Changes from review

* style: pre-commit fixes

* `py::mod_gil_not_used()` suggestion.

* Update include/pybind11/pybind11.h

---------

Signed-off-by: Henry Schreiner <henryschreineriii@gmail.com>
Co-authored-by: Henry Schreiner <HenrySchreinerIII@gmail.com>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Ralf W. Grosse-Kunstleve <rwgk@google.com>
This commit is contained in:
Sam Gross
2024-06-18 13:54:38 -07:00
committed by GitHub
co-authored by Henry Schreiner pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Ralf W. Grosse-Kunstleve
parent 1961b96a8c
commit baa540ec34
15 changed files with 352 additions and 137 deletions
+1 -1
View File
@@ -6,7 +6,7 @@ namespace py = pybind11;
* modules aren't preserved over a finalize/initialize.
*/
PYBIND11_MODULE(external_module, m) {
PYBIND11_MODULE(external_module, m, py::mod_gil_not_used()) {
class A {
public:
explicit A(int value) : v{value} {};