2021-03-08 14:54:22 +00:00
|
|
|
/*
|
|
|
|
tests/cross_module_gil_utils.cpp -- tools for acquiring GIL from a different module
|
|
|
|
|
|
|
|
Copyright (c) 2019 Google LLC
|
|
|
|
|
|
|
|
All rights reserved. Use of this source code is governed by a
|
|
|
|
BSD-style license that can be found in the LICENSE file.
|
|
|
|
*/
|
2023-04-07 00:39:04 +00:00
|
|
|
#if defined(PYBIND11_INTERNALS_VERSION)
|
|
|
|
# undef PYBIND11_INTERNALS_VERSION
|
|
|
|
#endif
|
|
|
|
#define PYBIND11_INTERNALS_VERSION 21814642 // Ensure this module has its own `internals` instance.
|
2021-03-08 14:54:22 +00:00
|
|
|
#include <pybind11/pybind11.h>
|
2022-06-20 22:59:56 +00:00
|
|
|
|
2021-03-08 14:54:22 +00:00
|
|
|
#include <cstdint>
|
2023-04-07 00:39:04 +00:00
|
|
|
#include <string>
|
|
|
|
#include <thread>
|
2021-03-08 14:54:22 +00:00
|
|
|
|
|
|
|
// This file mimics a DSO that makes pybind11 calls but does not define a
|
|
|
|
// PYBIND11_MODULE. The purpose is to test that such a DSO can create a
|
|
|
|
// py::gil_scoped_acquire when the running thread is in a GIL-released state.
|
|
|
|
//
|
|
|
|
// Note that we define a Python module here for convenience, but in general
|
|
|
|
// this need not be the case. The typical scenario would be a DSO that implements
|
|
|
|
// shared logic used internally by multiple pybind11 modules.
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
namespace py = pybind11;
|
2023-04-07 00:39:04 +00:00
|
|
|
|
2021-03-08 14:54:22 +00:00
|
|
|
void gil_acquire() { py::gil_scoped_acquire gil; }
|
|
|
|
|
2023-04-07 00:39:04 +00:00
|
|
|
std::string gil_multi_acquire_release(unsigned bits) {
|
|
|
|
if ((bits & 0x1u) != 0u) {
|
|
|
|
py::gil_scoped_acquire gil;
|
|
|
|
}
|
|
|
|
if ((bits & 0x2u) != 0u) {
|
|
|
|
py::gil_scoped_release gil;
|
|
|
|
}
|
|
|
|
if ((bits & 0x4u) != 0u) {
|
|
|
|
py::gil_scoped_acquire gil;
|
|
|
|
}
|
|
|
|
if ((bits & 0x8u) != 0u) {
|
|
|
|
py::gil_scoped_release gil;
|
|
|
|
}
|
|
|
|
return PYBIND11_INTERNALS_ID;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct CustomAutoGIL {
|
|
|
|
CustomAutoGIL() : gstate(PyGILState_Ensure()) {}
|
|
|
|
~CustomAutoGIL() { PyGILState_Release(gstate); }
|
|
|
|
|
|
|
|
PyGILState_STATE gstate;
|
|
|
|
};
|
|
|
|
struct CustomAutoNoGIL {
|
|
|
|
CustomAutoNoGIL() : save(PyEval_SaveThread()) {}
|
|
|
|
~CustomAutoNoGIL() { PyEval_RestoreThread(save); }
|
|
|
|
|
|
|
|
PyThreadState *save;
|
|
|
|
};
|
|
|
|
|
|
|
|
template <typename Acquire, typename Release>
|
|
|
|
void gil_acquire_inner() {
|
|
|
|
Acquire acquire_outer;
|
|
|
|
Acquire acquire_inner;
|
|
|
|
Release release;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename Acquire, typename Release>
|
|
|
|
void gil_acquire_nested() {
|
|
|
|
Acquire acquire_outer;
|
|
|
|
Acquire acquire_inner;
|
|
|
|
Release release;
|
|
|
|
auto thread = std::thread(&gil_acquire_inner<Acquire, Release>);
|
|
|
|
thread.join();
|
|
|
|
}
|
|
|
|
|
2021-03-08 14:54:22 +00:00
|
|
|
constexpr char kModuleName[] = "cross_module_gil_utils";
|
|
|
|
|
2023-04-07 00:39:04 +00:00
|
|
|
struct PyModuleDef moduledef = {
|
|
|
|
PyModuleDef_HEAD_INIT, kModuleName, nullptr, 0, nullptr, nullptr, nullptr, nullptr, nullptr};
|
2021-03-08 14:54:22 +00:00
|
|
|
|
2022-06-20 22:59:56 +00:00
|
|
|
} // namespace
|
2021-03-08 14:54:22 +00:00
|
|
|
|
2023-04-07 00:39:04 +00:00
|
|
|
#define ADD_FUNCTION(Name, ...) \
|
|
|
|
PyModule_AddObject(m, Name, PyLong_FromVoidPtr(reinterpret_cast<void *>(&__VA_ARGS__)));
|
2021-03-08 14:54:22 +00:00
|
|
|
|
2023-04-07 00:39:04 +00:00
|
|
|
extern "C" PYBIND11_EXPORT PyObject *PyInit_cross_module_gil_utils() {
|
2021-03-08 14:54:22 +00:00
|
|
|
|
2023-04-07 00:39:04 +00:00
|
|
|
PyObject *m = PyModule_Create(&moduledef);
|
|
|
|
|
|
|
|
if (m != nullptr) {
|
2022-06-20 22:59:56 +00:00
|
|
|
static_assert(sizeof(&gil_acquire) == sizeof(void *),
|
|
|
|
"Function pointer must have the same size as void*");
|
2023-04-07 00:39:04 +00:00
|
|
|
ADD_FUNCTION("gil_acquire_funcaddr", gil_acquire)
|
|
|
|
ADD_FUNCTION("gil_multi_acquire_release_funcaddr", gil_multi_acquire_release)
|
|
|
|
ADD_FUNCTION("gil_acquire_inner_custom_funcaddr",
|
|
|
|
gil_acquire_inner<CustomAutoGIL, CustomAutoNoGIL>)
|
|
|
|
ADD_FUNCTION("gil_acquire_nested_custom_funcaddr",
|
|
|
|
gil_acquire_nested<CustomAutoGIL, CustomAutoNoGIL>)
|
|
|
|
ADD_FUNCTION("gil_acquire_inner_pybind11_funcaddr",
|
|
|
|
gil_acquire_inner<py::gil_scoped_acquire, py::gil_scoped_release>)
|
|
|
|
ADD_FUNCTION("gil_acquire_nested_pybind11_funcaddr",
|
|
|
|
gil_acquire_nested<py::gil_scoped_acquire, py::gil_scoped_release>)
|
2021-03-08 14:54:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return m;
|
|
|
|
}
|