cross_module_gil_utils.cpp
Go to the documentation of this file.
1 /*
2  tests/cross_module_gil_utils.cpp -- tools for acquiring GIL from a different module
3 
4  Copyright (c) 2019 Google LLC
5 
6  All rights reserved. Use of this source code is governed by a
7  BSD-style license that can be found in the LICENSE file.
8 */
9 #include <pybind11/pybind11.h>
10 
11 #include <cstdint>
12 
13 // This file mimics a DSO that makes pybind11 calls but does not define a
14 // PYBIND11_MODULE. The purpose is to test that such a DSO can create a
15 // py::gil_scoped_acquire when the running thread is in a GIL-released state.
16 //
17 // Note that we define a Python module here for convenience, but in general
18 // this need not be the case. The typical scenario would be a DSO that implements
19 // shared logic used internally by multiple pybind11 modules.
20 
21 namespace {
22 
23 namespace py = pybind11;
24 void gil_acquire() { py::gil_scoped_acquire gil; }
25 
26 constexpr char kModuleName[] = "cross_module_gil_utils";
27 
28 struct PyModuleDef moduledef = {
29  PyModuleDef_HEAD_INIT, kModuleName, nullptr, 0, nullptr, nullptr, nullptr, nullptr, nullptr};
30 
31 } // namespace
32 
34 
35  PyObject *m = PyModule_Create(&moduledef);
36 
37  if (m != nullptr) {
38  static_assert(sizeof(&gil_acquire) == sizeof(void *),
39  "Function pointer must have the same size as void*");
40  PyModule_AddObject(
41  m, "gil_acquire_funcaddr", PyLong_FromVoidPtr(reinterpret_cast<void *>(&gil_acquire)));
42  }
43 
44  return m;
45 }
Matrix3f m
PYBIND11_EXPORT PyObject * PyInit_cross_module_gil_utils()


gtsam
Author(s):
autogenerated on Tue Jul 4 2023 02:34:06