chore: Delete copy ctor/assign for GIL RAIIs (#4183)

* chore: Delete copy ctor/assign for GIL RAIIs

* Fix typo

* Delete copy ops for local gil scoped acquire
This commit is contained in:
Aaron Gokaslan 2022-09-19 12:56:31 -04:00 committed by GitHub
parent 1874f8fa87
commit 9c04c7b0f1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 12 additions and 0 deletions

View File

@ -412,6 +412,8 @@ PYBIND11_NOINLINE internals &get_internals() {
// Cannot use py::gil_scoped_acquire here since that constructor calls get_internals. // Cannot use py::gil_scoped_acquire here since that constructor calls get_internals.
struct gil_scoped_acquire_local { struct gil_scoped_acquire_local {
gil_scoped_acquire_local() : state(PyGILState_Ensure()) {} 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); } ~gil_scoped_acquire_local() { PyGILState_Release(state); }
const PyGILState_STATE state; const PyGILState_STATE state;
} gil; } gil;

View File

@ -80,6 +80,9 @@ public:
inc_ref(); 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; } void inc_ref() { ++tstate->gilstate_counter; }
PYBIND11_NOINLINE void dec_ref() { PYBIND11_NOINLINE void dec_ref() {
@ -144,6 +147,9 @@ public:
} }
} }
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 /// This method will disable the PyThreadState_DeleteCurrent call and the
/// GIL won't be acquired. This method should be used if the interpreter /// 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 /// could be shutting down when this is called, as thread deletion is not
@ -178,6 +184,8 @@ class gil_scoped_acquire {
public: public:
gil_scoped_acquire() { state = PyGILState_Ensure(); } 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); } ~gil_scoped_acquire() { PyGILState_Release(state); }
void disarm() {} void disarm() {}
}; };
@ -187,6 +195,8 @@ class gil_scoped_release {
public: public:
gil_scoped_release() { state = PyEval_SaveThread(); } 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); } ~gil_scoped_release() { PyEval_RestoreThread(state); }
void disarm() {} void disarm() {}
}; };