From 249d307c0c6e281185bdcd5f6855d7db91194ce2 Mon Sep 17 00:00:00 2001 From: Aaron Gokaslan Date: Fri, 16 Sep 2022 11:45:15 -0700 Subject: [PATCH 1/3] chore: Delete copy ctor/assign for GIL RAIIs --- include/pybind11/gil.h | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/include/pybind11/gil.h b/include/pybind11/gil.h index a0b5de1514..f192294107 100644 --- a/include/pybind11/gil.h +++ b/include/pybind11/gil.h @@ -80,6 +80,9 @@ class gil_scoped_acquire { inc_ref(); } + gil_scoped_acquire(const gil_scoped_acquire &) = delete; + gil_scoped_acquire &operator=(const gil_scoped_acquire &) = delete; + void inc_ref() { ++tstate->gilstate_counter; } PYBIND11_NOINLINE void dec_ref() { @@ -144,6 +147,9 @@ class gil_scoped_release { } } + gil_scoped_release(const gil_scoped_acquire &) = delete; + gil_scoped_release &operator=(const gil_scoped_acquire &) = delete; + /// This method will disable the PyThreadState_DeleteCurrent call and the /// GIL won't be acquired. This method should be used if the interpreter /// could be shutting down when this is called, as thread deletion is not @@ -178,6 +184,8 @@ class gil_scoped_acquire { public: gil_scoped_acquire() { state = PyGILState_Ensure(); } + gil_scoped_acquire(const gil_scoped_acquire &) = delete; + gil_scoped_acquire &operator=(const gil_scoped_acquire &) = delete; ~gil_scoped_acquire() { PyGILState_Release(state); } void disarm() {} }; @@ -187,6 +195,8 @@ class gil_scoped_release { public: gil_scoped_release() { state = PyEval_SaveThread(); } + gil_scoped_release(const gil_scoped_release) = delete; + gil_scoped_release &operator=(const gil_scoped_acquire &) = delete; ~gil_scoped_release() { PyEval_RestoreThread(state); } void disarm() {} }; From c79d8aee7d7ea2c15a15529fef38edea623e2ded Mon Sep 17 00:00:00 2001 From: Aaron Gokaslan Date: Fri, 16 Sep 2022 12:50:16 -0700 Subject: [PATCH 2/3] Fix typo --- include/pybind11/gil.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/pybind11/gil.h b/include/pybind11/gil.h index f192294107..1ef5f0a8c8 100644 --- a/include/pybind11/gil.h +++ b/include/pybind11/gil.h @@ -195,7 +195,7 @@ class gil_scoped_release { public: gil_scoped_release() { state = PyEval_SaveThread(); } - gil_scoped_release(const gil_scoped_release) = delete; + gil_scoped_release(const gil_scoped_release &) = delete; gil_scoped_release &operator=(const gil_scoped_acquire &) = delete; ~gil_scoped_release() { PyEval_RestoreThread(state); } void disarm() {} From ed8d6703c97362feeaf6530d88a6f17e4348489f Mon Sep 17 00:00:00 2001 From: Aaron Gokaslan Date: Mon, 19 Sep 2022 12:06:46 -0400 Subject: [PATCH 3/3] Delete copy ops for local gil scoped acquire --- include/pybind11/detail/internals.h | 2 ++ 1 file changed, 2 insertions(+) diff --git a/include/pybind11/detail/internals.h b/include/pybind11/detail/internals.h index 6ca5e1482f..86991955b5 100644 --- a/include/pybind11/detail/internals.h +++ b/include/pybind11/detail/internals.h @@ -412,6 +412,8 @@ PYBIND11_NOINLINE internals &get_internals() { // Cannot use py::gil_scoped_acquire here since that constructor calls get_internals. struct gil_scoped_acquire_local { gil_scoped_acquire_local() : state(PyGILState_Ensure()) {} + gil_scoped_acquire_local(const gil_scoped_acquire_local &) = delete; + gil_scoped_acquire_local &operator=(const gil_scoped_acquire_local &) = delete; ~gil_scoped_acquire_local() { PyGILState_Release(state); } const PyGILState_STATE state; } gil;