Skip to content

Commit

Permalink
py::mod_gil_not_used() suggestion.
Browse files Browse the repository at this point in the history
  • Loading branch information
Ralf W. Grosse-Kunstleve committed Jun 8, 2024
1 parent f53dcb5 commit a1c0f8e
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 22 deletions.
32 changes: 15 additions & 17 deletions include/pybind11/pybind11.h
Original file line number Diff line number Diff line change
Expand Up @@ -1206,7 +1206,15 @@ struct handle_type_name<cpp_function> {

PYBIND11_NAMESPACE_END(detail)

struct gil_not_used {};
// Use to activate Py_MOD_GIL_NOT_USED.
class mod_gil_not_used {
public:
mod_gil_not_used(bool flag = true) : flag_(flag) {}
bool flag() const { return flag_; }

private:
bool flag_;
};

/// Wrapper for Python extension modules
class module_ : public object {
Expand Down Expand Up @@ -1308,21 +1316,11 @@ class module_ : public object {
``def`` should point to a statically allocated module_def.
\endrst */
static module_ create_extension_module(const char *name, const char *doc, module_def *def) {
return _create_extension_module(name, doc, def, false);
}

static module_
create_extension_module(const char *name, const char *doc, module_def *def, gil_not_used) {
return _create_extension_module(name, doc, def, true);
}

private:
static module_ _create_extension_module(const char *name,
const char *doc,
module_def *def,
bool gil_disabled) {

static module_ create_extension_module(const char *name,
const char *doc,
module_def *def,
mod_gil_not_used gil_not_used
= mod_gil_not_used(false)) {
// module_def is PyModuleDef
// Placement new (not an allocation).
def = new (def)
Expand All @@ -1342,7 +1340,7 @@ class module_ : public object {
}
pybind11_fail("Internal error in module_::create_extension_module()");
}
if (gil_disabled) {
if (gil_not_used.flag()) {
#ifdef Py_GIL_DISABLED
PyUnstable_Module_SetGIL(m, Py_MOD_GIL_NOT_USED);
#endif
Expand Down
2 changes: 1 addition & 1 deletion tests/eigen_tensor_avoid_stl_array.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@

#include "test_eigen_tensor.inl"

PYBIND11_MODULE(eigen_tensor_avoid_stl_array, m, pybind11::gil_not_used()) {
PYBIND11_MODULE(eigen_tensor_avoid_stl_array, m, pybind11::mod_gil_not_used()) {
eigen_tensor_test::test_module(m);
}
2 changes: 1 addition & 1 deletion tests/pybind11_cross_module_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
#include <numeric>
#include <utility>

PYBIND11_MODULE(pybind11_cross_module_tests, m, py::gil_not_used()) {
PYBIND11_MODULE(pybind11_cross_module_tests, m, py::mod_gil_not_used()) {
m.doc() = "pybind11 cross-module test module";

// test_local_bindings.py tests:
Expand Down
2 changes: 1 addition & 1 deletion tests/pybind11_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ const char *cpp_std() {
#endif
}

PYBIND11_MODULE(pybind11_tests, m, py::gil_not_used()) {
PYBIND11_MODULE(pybind11_tests, m, py::mod_gil_not_used()) {
m.doc() = "pybind11 test module";

// Intentionally kept minimal to not create a maintenance chore
Expand Down
2 changes: 1 addition & 1 deletion tests/test_cmake_build/main.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include <pybind11/pybind11.h>
namespace py = pybind11;

PYBIND11_MODULE(test_cmake_build, m, py::gil_not_used()) {
PYBIND11_MODULE(test_cmake_build, m, py::mod_gil_not_used()) {
m.def("add", [](int i, int j) { return i + j; });
}
2 changes: 1 addition & 1 deletion tests/test_embed/external_module.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace py = pybind11;
* modules aren't preserved over a finalize/initialize.
*/

PYBIND11_MODULE(external_module, m, py::gil_not_used()) {
PYBIND11_MODULE(external_module, m, py::mod_gil_not_used()) {
class A {
public:
explicit A(int value) : v{value} {};
Expand Down

0 comments on commit a1c0f8e

Please sign in to comment.