|
| 1 | +// Copyright (c) 2022 The pybind Community. |
| 2 | +// All rights reserved. Use of this source code is governed by a |
| 3 | +// BSD-style license that can be found in the LICENSE file. |
| 4 | + |
| 5 | +#pragma once |
| 6 | + |
| 7 | +#include "common.h" |
| 8 | + |
| 9 | +#if defined(WITH_THREAD) && defined(PYBIND11_SIMPLE_GIL_MANAGEMENT) |
| 10 | +# include "../gil.h" |
| 11 | +#endif |
| 12 | + |
| 13 | +#include "../pytypes.h" |
| 14 | +#include "abi_platform_id.h" |
| 15 | + |
| 16 | +#include <string> |
| 17 | + |
| 18 | +PYBIND11_NAMESPACE_BEGIN(PYBIND11_NAMESPACE) |
| 19 | +PYBIND11_NAMESPACE_BEGIN(detail) |
| 20 | + |
| 21 | +inline object get_python_state_dict() { |
| 22 | + object state_dict; |
| 23 | +#if (PYBIND11_INTERNALS_VERSION <= 4 && PY_VERSION_HEX < 0x030C0000) \ |
| 24 | + || PY_VERSION_HEX < 0x03080000 || defined(PYPY_VERSION) |
| 25 | + state_dict = reinterpret_borrow<object>(PyEval_GetBuiltins()); |
| 26 | +#else |
| 27 | +# if PY_VERSION_HEX < 0x03090000 |
| 28 | + PyInterpreterState *istate = _PyInterpreterState_Get(); |
| 29 | +# else |
| 30 | + PyInterpreterState *istate = PyInterpreterState_Get(); |
| 31 | +# endif |
| 32 | + if (istate) { |
| 33 | + state_dict = reinterpret_borrow<object>(PyInterpreterState_GetDict(istate)); |
| 34 | + } |
| 35 | +#endif |
| 36 | + if (!state_dict) { |
| 37 | + raise_from(PyExc_SystemError, "pybind11::detail::get_python_state_dict() FAILED"); |
| 38 | + } |
| 39 | + return state_dict; |
| 40 | +} |
| 41 | + |
| 42 | +#if defined(WITH_THREAD) |
| 43 | +# if defined(PYBIND11_SIMPLE_GIL_MANAGEMENT) |
| 44 | +using gil_scoped_acquire_simple = gil_scoped_acquire; |
| 45 | +# else |
| 46 | +// Cannot use py::gil_scoped_acquire here since that constructor calls get_internals. |
| 47 | +struct gil_scoped_acquire_simple { |
| 48 | + gil_scoped_acquire_simple() : state(PyGILState_Ensure()) {} |
| 49 | + gil_scoped_acquire_simple(const gil_scoped_acquire_simple &) = delete; |
| 50 | + gil_scoped_acquire_simple &operator=(const gil_scoped_acquire_simple &) = delete; |
| 51 | + ~gil_scoped_acquire_simple() { PyGILState_Release(state); } |
| 52 | + const PyGILState_STATE state; |
| 53 | +}; |
| 54 | +# endif |
| 55 | +#endif |
| 56 | + |
| 57 | +/* NOTE: struct cross_extension_shared_state is in |
| 58 | + namespace pybind11::detail |
| 59 | + but all types using this struct are meant to live in |
| 60 | + namespace pybind11::cross_extension_shared_states |
| 61 | + to make them easy to discover and reason about. |
| 62 | + */ |
| 63 | +template <typename AdapterType> |
| 64 | +struct cross_extension_shared_state { |
| 65 | + static constexpr const char *abi_id() { return AdapterType::abi_id(); } |
| 66 | + |
| 67 | + using payload_type = typename AdapterType::payload_type; |
| 68 | + |
| 69 | + static payload_type **&payload_pp() { |
| 70 | + // The reason for the double-indirection is documented here: |
| 71 | + // https://github.com/pybind/pybind11/pull/1092 |
| 72 | + static payload_type **pp; |
| 73 | + return pp; |
| 74 | + } |
| 75 | + |
| 76 | + static payload_type *get_existing() { |
| 77 | + if (payload_pp() && *payload_pp()) { |
| 78 | + return *payload_pp(); |
| 79 | + } |
| 80 | + |
| 81 | + gil_scoped_acquire_simple gil; |
| 82 | + error_scope err_scope; |
| 83 | + |
| 84 | + str abi_id_str(AdapterType::abi_id()); |
| 85 | + dict state_dict = get_python_state_dict(); |
| 86 | + if (!state_dict.contains(abi_id_str)) { |
| 87 | + return nullptr; |
| 88 | + } |
| 89 | + |
| 90 | + void *raw_ptr = PyCapsule_GetPointer(state_dict[abi_id_str].ptr(), AdapterType::abi_id()); |
| 91 | + if (raw_ptr == nullptr) { |
| 92 | + raise_from(PyExc_SystemError, |
| 93 | + ("pybind11::detail::cross_extension_shared_state::get_existing():" |
| 94 | + " Retrieve payload_type** from capsule FAILED for ABI ID \"" |
| 95 | + + std::string(AdapterType::abi_id()) + "\"") |
| 96 | + .c_str()); |
| 97 | + } |
| 98 | + payload_pp() = static_cast<payload_type **>(raw_ptr); |
| 99 | + return *payload_pp(); |
| 100 | + } |
| 101 | + |
| 102 | + static payload_type &get() { |
| 103 | + payload_type *existing = get_existing(); |
| 104 | + if (existing != nullptr) { |
| 105 | + return *existing; |
| 106 | + } |
| 107 | + if (payload_pp() == nullptr) { |
| 108 | + payload_pp() = new payload_type *(); |
| 109 | + } |
| 110 | + *payload_pp() = new payload_type(); |
| 111 | + get_python_state_dict()[AdapterType::abi_id()] |
| 112 | + = capsule(payload_pp(), AdapterType::abi_id()); |
| 113 | + return **payload_pp(); |
| 114 | + } |
| 115 | + |
| 116 | + struct scoped_clear { |
| 117 | + // To be called BEFORE Py_Finalize(). |
| 118 | + scoped_clear() { |
| 119 | + payload_type *existing = get_existing(); |
| 120 | + if (existing != nullptr) { |
| 121 | + AdapterType::payload_clear(*existing); |
| 122 | + arm_dtor = true; |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + // To be called AFTER Py_Finalize(). |
| 127 | + ~scoped_clear() { |
| 128 | + if (arm_dtor) { |
| 129 | + delete *payload_pp(); |
| 130 | + *payload_pp() = nullptr; |
| 131 | + } |
| 132 | + } |
| 133 | + |
| 134 | + scoped_clear(const scoped_clear &) = delete; |
| 135 | + scoped_clear &operator=(const scoped_clear &) = delete; |
| 136 | + |
| 137 | + bool arm_dtor = false; |
| 138 | + }; |
| 139 | +}; |
| 140 | + |
| 141 | +PYBIND11_NAMESPACE_END(detail) |
| 142 | +PYBIND11_NAMESPACE_END(PYBIND11_NAMESPACE) |
0 commit comments