Factor out pybind11/gil_simple.h (#5614)

* Factor out pybind11/gil_simple.h

* Remove disarm() member functions in pybind11/gil_simple.h
This commit is contained in:
Ralf W. Grosse-Kunstleve
2025-04-13 19:48:32 -07:00
committed by GitHub
parent ee04df0d02
commit cbcc23855e
5 changed files with 60 additions and 55 deletions

View File

@@ -158,6 +158,7 @@ set(PYBIND11_HEADERS
include/pybind11/eval.h
include/pybind11/gil.h
include/pybind11/gil_safe_call_once.h
include/pybind11/gil_simple.h
include/pybind11/iostream.h
include/pybind11/functional.h
include/pybind11/native_enum.h

View File

@@ -9,15 +9,12 @@
#pragma once
#include "common.h"
#if defined(PYBIND11_SIMPLE_GIL_MANAGEMENT)
# include <pybind11/gil.h>
#endif
#include <pybind11/conduit/pybind11_platform_abi_id.h>
#include <pybind11/gil_simple.h>
#include <pybind11/pytypes.h>
#include "common.h"
#include <exception>
#include <mutex>
#include <thread>
@@ -425,19 +422,7 @@ PYBIND11_NOINLINE internals &get_internals() {
return **internals_pp;
}
#if defined(PYBIND11_SIMPLE_GIL_MANAGEMENT)
gil_scoped_acquire gil;
#else
// Ensure that the GIL is held since we will need to make Python calls.
// 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;
#endif
gil_scoped_acquire_simple gil;
error_scope err_scope;
dict state_dict = get_python_state_dict();

View File

@@ -9,13 +9,24 @@
#pragma once
#include "detail/common.h"
#if defined(PYBIND11_SIMPLE_GIL_MANAGEMENT)
#include <cassert>
# include "detail/common.h"
# include "gil_simple.h"
#if !defined(PYBIND11_SIMPLE_GIL_MANAGEMENT)
PYBIND11_NAMESPACE_BEGIN(PYBIND11_NAMESPACE)
using gil_scoped_acquire = gil_scoped_acquire_simple;
using gil_scoped_release = gil_scoped_release_simple;
PYBIND11_NAMESPACE_END(PYBIND11_NAMESPACE)
#else
# include "detail/common.h"
# include "detail/internals.h"
#endif
# include <cassert>
PYBIND11_NAMESPACE_BEGIN(PYBIND11_NAMESPACE)
@@ -26,8 +37,6 @@ PyThreadState *get_thread_state_unchecked();
PYBIND11_NAMESPACE_END(detail)
#if !defined(PYBIND11_SIMPLE_GIL_MANAGEMENT)
/* The functions below essentially reproduce the PyGILState_* API using a RAII
* pattern, but there are a few important differences:
*
@@ -182,34 +191,6 @@ private:
bool active = true;
};
#else // PYBIND11_SIMPLE_GIL_MANAGEMENT
class gil_scoped_acquire {
PyGILState_STATE state;
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() {}
};
class gil_scoped_release {
PyThreadState *state;
public:
// PRECONDITION: The GIL must be held when this constructor is called.
gil_scoped_release() {
assert(PyGILState_Check());
state = PyEval_SaveThread();
}
gil_scoped_release(const gil_scoped_release &) = delete;
gil_scoped_release &operator=(const gil_scoped_release &) = delete;
~gil_scoped_release() { PyEval_RestoreThread(state); }
void disarm() {}
};
#endif // PYBIND11_SIMPLE_GIL_MANAGEMENT
PYBIND11_NAMESPACE_END(PYBIND11_NAMESPACE)
#endif // !PYBIND11_SIMPLE_GIL_MANAGEMENT

View File

@@ -0,0 +1,37 @@
// Copyright (c) 2016-2025 The Pybind Development Team.
// All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
#pragma once
#include "detail/common.h"
#include <cassert>
PYBIND11_NAMESPACE_BEGIN(PYBIND11_NAMESPACE)
class gil_scoped_acquire_simple {
PyGILState_STATE state;
public:
gil_scoped_acquire_simple() : state{PyGILState_Ensure()} {}
gil_scoped_acquire_simple(const gil_scoped_acquire_simple &) = delete;
gil_scoped_acquire_simple &operator=(const gil_scoped_acquire_simple &) = delete;
~gil_scoped_acquire_simple() { PyGILState_Release(state); }
};
class gil_scoped_release_simple {
PyThreadState *state;
public:
// PRECONDITION: The GIL must be held when this constructor is called.
gil_scoped_release_simple() {
assert(PyGILState_Check());
state = PyEval_SaveThread();
}
gil_scoped_release_simple(const gil_scoped_release_simple &) = delete;
gil_scoped_release_simple &operator=(const gil_scoped_release_simple &) = delete;
~gil_scoped_release_simple() { PyEval_RestoreThread(state); }
};
PYBIND11_NAMESPACE_END(PYBIND11_NAMESPACE)

View File

@@ -37,6 +37,7 @@ main_headers = {
"include/pybind11/functional.h",
"include/pybind11/gil.h",
"include/pybind11/gil_safe_call_once.h",
"include/pybind11/gil_simple.h",
"include/pybind11/iostream.h",
"include/pybind11/native_enum.h",
"include/pybind11/numpy.h",