17 #define CROSS_MODULE(Function) \
18 auto cm = py::module_::import("cross_module_gil_utils"); \
19 auto target = reinterpret_cast<void (*)()>(PyLong_AsVoidPtr(cm.attr(Function).ptr()));
38 m.attr(
"defined_THREAD_SANITIZER") =
39 #if defined(THREAD_SANITIZER)
45 m.def(
"intentional_deadlock",
46 []() { std::thread([]() { py::gil_scoped_acquire gil_acquired; }).join(); });
48 py::class_<VirtClass, PyVirtClass>(
m,
"VirtClass")
53 m.def(
"test_callback_py_obj", [](py::object &
func) {
func(); });
54 m.def(
"test_callback_std_func", [](
const std::function<
void()> &
func) {
func(); });
55 m.def(
"test_callback_virtual_func", [](VirtClass &virt) { virt.virtual_func(); });
56 m.def(
"test_callback_pure_virtual_func", [](VirtClass &virt) { virt.pure_virtual_func(); });
57 m.def(
"test_cross_module_gil_released", []() {
59 py::gil_scoped_release gil_release;
62 m.def(
"test_cross_module_gil_acquired", []() {
64 py::gil_scoped_acquire gil_acquire;
67 m.def(
"test_cross_module_gil_inner_custom_released", []() {
69 py::gil_scoped_release gil_release;
72 m.def(
"test_cross_module_gil_inner_custom_acquired", []() {
74 py::gil_scoped_acquire gil_acquire;
77 m.def(
"test_cross_module_gil_inner_pybind11_released", []() {
79 py::gil_scoped_release gil_release;
82 m.def(
"test_cross_module_gil_inner_pybind11_acquired", []() {
84 py::gil_scoped_acquire gil_acquire;
87 m.def(
"test_cross_module_gil_nested_custom_released", []() {
89 py::gil_scoped_release gil_release;
92 m.def(
"test_cross_module_gil_nested_custom_acquired", []() {
94 py::gil_scoped_acquire gil_acquire;
97 m.def(
"test_cross_module_gil_nested_pybind11_released", []() {
99 py::gil_scoped_release gil_release;
102 m.def(
"test_cross_module_gil_nested_pybind11_acquired", []() {
104 py::gil_scoped_acquire gil_acquire;
107 m.def(
"test_release_acquire", [](
const py::object &obj) {
108 py::gil_scoped_release gil_released;
109 py::gil_scoped_acquire gil_acquired;
112 m.def(
"test_nested_acquire", [](
const py::object &obj) {
113 py::gil_scoped_release gil_released;
114 py::gil_scoped_acquire gil_acquired_outer;
115 py::gil_scoped_acquire gil_acquired_inner;
118 m.def(
"test_multi_acquire_release_cross_module", [](
unsigned bits) {
122 py::gil_scoped_release gil_released;
123 auto thread_f = [
bits, &internals_ids]() {
124 py::gil_scoped_acquire gil_acquired;
125 auto cm = py::module_::import(
"cross_module_gil_utils");
126 auto target =
reinterpret_cast<std::string (*)(
unsigned)
>(
127 PyLong_AsVoidPtr(
cm.attr(
"gil_multi_acquire_release_funcaddr").ptr()));
128 std::string cm_internals_id = target(
bits >> 3);
129 internals_ids.add(cm_internals_id);
131 if ((
bits & 0x1u) != 0u) {
134 if ((
bits & 0x2u) != 0u) {
135 std::thread non_python_thread(thread_f);
136 non_python_thread.join();
138 if ((
bits & 0x4u) != 0u) {
142 return internals_ids;