cross_module_gil_utils.cpp revision 14299:2fbea9df56d2
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#include <cstdint> 11 12// This file mimics a DSO that makes pybind11 calls but does not define a 13// PYBIND11_MODULE. The purpose is to test that such a DSO can create a 14// py::gil_scoped_acquire when the running thread is in a GIL-released state. 15// 16// Note that we define a Python module here for convenience, but in general 17// this need not be the case. The typical scenario would be a DSO that implements 18// shared logic used internally by multiple pybind11 modules. 19 20namespace { 21 22namespace py = pybind11; 23void gil_acquire() { py::gil_scoped_acquire gil; } 24 25constexpr char kModuleName[] = "cross_module_gil_utils"; 26 27#if PY_MAJOR_VERSION >= 3 28struct PyModuleDef moduledef = { 29 PyModuleDef_HEAD_INIT, 30 kModuleName, 31 NULL, 32 0, 33 NULL, 34 NULL, 35 NULL, 36 NULL, 37 NULL 38}; 39#else 40PyMethodDef module_methods[] = { 41 {NULL, NULL, 0, NULL} 42}; 43#endif 44 45} // namespace 46 47extern "C" PYBIND11_EXPORT 48#if PY_MAJOR_VERSION >= 3 49PyObject* PyInit_cross_module_gil_utils() 50#else 51void initcross_module_gil_utils() 52#endif 53{ 54 55 PyObject* m = 56#if PY_MAJOR_VERSION >= 3 57 PyModule_Create(&moduledef); 58#else 59 Py_InitModule(kModuleName, module_methods); 60#endif 61 62 if (m != NULL) { 63 static_assert( 64 sizeof(&gil_acquire) == sizeof(void*), 65 "Function pointer must have the same size as void*"); 66 PyModule_AddObject(m, "gil_acquire_funcaddr", 67 PyLong_FromVoidPtr(reinterpret_cast<void*>(&gil_acquire))); 68 } 69 70#if PY_MAJOR_VERSION >= 3 71 return m; 72#endif 73} 74