internals.h 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799
  1. /*
  2. pybind11/detail/internals.h: Internal data structure and related functions
  3. Copyright (c) 2017 Wenzel Jakob <wenzel.jakob@epfl.ch>
  4. All rights reserved. Use of this source code is governed by a
  5. BSD-style license that can be found in the LICENSE file.
  6. */
  7. #pragma once
  8. #include <pybind11/conduit/pybind11_platform_abi_id.h>
  9. #include <pybind11/gil_simple.h>
  10. #include <pybind11/pytypes.h>
  11. #include <pybind11/trampoline_self_life_support.h>
  12. #include "common.h"
  13. #include "struct_smart_holder.h"
  14. #include <atomic>
  15. #include <cstdint>
  16. #include <exception>
  17. #include <limits>
  18. #include <mutex>
  19. #include <thread>
  20. /// Tracks the `internals` and `type_info` ABI version independent of the main library version.
  21. ///
  22. /// Some portions of the code use an ABI that is conditional depending on this
  23. /// version number. That allows ABI-breaking changes to be "pre-implemented".
  24. /// Once the default version number is incremented, the conditional logic that
  25. /// no longer applies can be removed. Additionally, users that need not
  26. /// maintain ABI compatibility can increase the version number in order to take
  27. /// advantage of any functionality/efficiency improvements that depend on the
  28. /// newer ABI.
  29. ///
  30. /// WARNING: If you choose to manually increase the ABI version, note that
  31. /// pybind11 may not be tested as thoroughly with a non-default ABI version, and
  32. /// further ABI-incompatible changes may be made before the ABI is officially
  33. /// changed to the new version.
  34. #ifndef PYBIND11_INTERNALS_VERSION
  35. # define PYBIND11_INTERNALS_VERSION 11
  36. #endif
  37. #if PYBIND11_INTERNALS_VERSION < 11
  38. # error "PYBIND11_INTERNALS_VERSION 11 is the minimum for all platforms for pybind11v3."
  39. #endif
  40. PYBIND11_NAMESPACE_BEGIN(PYBIND11_NAMESPACE)
  41. using ExceptionTranslator = void (*)(std::exception_ptr);
  42. // The old Python Thread Local Storage (TLS) API is deprecated in Python 3.7 in favor of the new
  43. // Thread Specific Storage (TSS) API.
  44. // Avoid unnecessary allocation of `Py_tss_t`, since we cannot use
  45. // `Py_LIMITED_API` anyway.
  46. #define PYBIND11_TLS_KEY_REF Py_tss_t &
  47. #if defined(__clang__)
  48. # define PYBIND11_TLS_KEY_INIT(var) \
  49. _Pragma("clang diagnostic push") /**/ \
  50. _Pragma("clang diagnostic ignored \"-Wmissing-field-initializers\"") /**/ \
  51. Py_tss_t var \
  52. = Py_tss_NEEDS_INIT; \
  53. _Pragma("clang diagnostic pop")
  54. #elif defined(__GNUC__) && !defined(__INTEL_COMPILER)
  55. # define PYBIND11_TLS_KEY_INIT(var) \
  56. _Pragma("GCC diagnostic push") /**/ \
  57. _Pragma("GCC diagnostic ignored \"-Wmissing-field-initializers\"") /**/ \
  58. Py_tss_t var \
  59. = Py_tss_NEEDS_INIT; \
  60. _Pragma("GCC diagnostic pop")
  61. #else
  62. # define PYBIND11_TLS_KEY_INIT(var) Py_tss_t var = Py_tss_NEEDS_INIT;
  63. #endif
  64. #define PYBIND11_TLS_KEY_CREATE(var) (PyThread_tss_create(&(var)) == 0)
  65. #define PYBIND11_TLS_GET_VALUE(key) PyThread_tss_get(&(key))
  66. #define PYBIND11_TLS_REPLACE_VALUE(key, value) PyThread_tss_set(&(key), (value))
  67. #define PYBIND11_TLS_DELETE_VALUE(key) PyThread_tss_set(&(key), nullptr)
  68. #define PYBIND11_TLS_FREE(key) PyThread_tss_delete(&(key))
  69. /// A smart-pointer-like wrapper around a thread-specific value. get/set of the pointer applies to
  70. /// the current thread only.
  71. template <typename T>
  72. class thread_specific_storage {
  73. public:
  74. thread_specific_storage() {
  75. // NOLINTNEXTLINE(bugprone-assignment-in-if-condition)
  76. if (!PYBIND11_TLS_KEY_CREATE(key_)) {
  77. pybind11_fail(
  78. "thread_specific_storage constructor: could not initialize the TSS key!");
  79. }
  80. }
  81. ~thread_specific_storage() {
  82. // This destructor is often called *after* Py_Finalize(). That *SHOULD BE* fine on most
  83. // platforms. The following details what happens when PyThread_tss_free is called in
  84. // CPython. PYBIND11_TLS_FREE is PyThread_tss_free on python 3.7+. On older python, it does
  85. // nothing. PyThread_tss_free calls PyThread_tss_delete and PyMem_RawFree.
  86. // PyThread_tss_delete just calls TlsFree (on Windows) or pthread_key_delete (on *NIX).
  87. // Neither of those have anything to do with CPython internals. PyMem_RawFree *requires*
  88. // that the `key` be allocated with the CPython allocator (as it is by
  89. // PyThread_tss_create).
  90. // However, in GraalPy (as of v24.2 or older), TSS is implemented by Java and this call
  91. // requires a living Python interpreter.
  92. #ifdef GRAALVM_PYTHON
  93. if (!Py_IsInitialized() || _Py_IsFinalizing()) {
  94. return;
  95. }
  96. #endif
  97. PYBIND11_TLS_FREE(key_);
  98. }
  99. thread_specific_storage(thread_specific_storage const &) = delete;
  100. thread_specific_storage(thread_specific_storage &&) = delete;
  101. thread_specific_storage &operator=(thread_specific_storage const &) = delete;
  102. thread_specific_storage &operator=(thread_specific_storage &&) = delete;
  103. T *get() const { return reinterpret_cast<T *>(PYBIND11_TLS_GET_VALUE(key_)); }
  104. T &operator*() const { return *get(); }
  105. explicit operator T *() const { return get(); }
  106. explicit operator bool() const { return get() != nullptr; }
  107. void set(T *val) { PYBIND11_TLS_REPLACE_VALUE(key_, reinterpret_cast<void *>(val)); }
  108. void reset(T *p = nullptr) { set(p); }
  109. thread_specific_storage &operator=(T *pval) {
  110. set(pval);
  111. return *this;
  112. }
  113. private:
  114. PYBIND11_TLS_KEY_INIT(mutable key_)
  115. };
  116. PYBIND11_NAMESPACE_BEGIN(detail)
  117. // This does NOT actually exist as a module.
  118. #define PYBIND11_DUMMY_MODULE_NAME "pybind11_builtins"
  119. // Forward declarations
  120. inline PyTypeObject *make_static_property_type();
  121. inline PyTypeObject *make_default_metaclass();
  122. inline PyObject *make_object_base_type(PyTypeObject *metaclass);
  123. inline void translate_exception(std::exception_ptr p);
  124. // Python loads modules by default with dlopen with the RTLD_LOCAL flag; under libc++ and possibly
  125. // other STLs, this means `typeid(A)` from one module won't equal `typeid(A)` from another module
  126. // even when `A` is the same, non-hidden-visibility type (e.g. from a common include). Under
  127. // libstdc++, this doesn't happen: equality and the type_index hash are based on the type name,
  128. // which works. If not under a known-good stl, provide our own name-based hash and equality
  129. // functions that use the type name.
  130. #if !defined(_LIBCPP_VERSION)
  131. inline bool same_type(const std::type_info &lhs, const std::type_info &rhs) { return lhs == rhs; }
  132. using type_hash = std::hash<std::type_index>;
  133. using type_equal_to = std::equal_to<std::type_index>;
  134. #else
  135. inline bool same_type(const std::type_info &lhs, const std::type_info &rhs) {
  136. return lhs.name() == rhs.name() || std::strcmp(lhs.name(), rhs.name()) == 0;
  137. }
  138. struct type_hash {
  139. size_t operator()(const std::type_index &t) const {
  140. size_t hash = 5381;
  141. const char *ptr = t.name();
  142. while (auto c = static_cast<unsigned char>(*ptr++)) {
  143. hash = (hash * 33) ^ c;
  144. }
  145. return hash;
  146. }
  147. };
  148. struct type_equal_to {
  149. bool operator()(const std::type_index &lhs, const std::type_index &rhs) const {
  150. return lhs.name() == rhs.name() || std::strcmp(lhs.name(), rhs.name()) == 0;
  151. }
  152. };
  153. #endif
  154. template <typename value_type>
  155. using type_map = std::unordered_map<std::type_index, value_type, type_hash, type_equal_to>;
  156. struct override_hash {
  157. inline size_t operator()(const std::pair<const PyObject *, const char *> &v) const {
  158. size_t value = std::hash<const void *>()(v.first);
  159. value ^= std::hash<const void *>()(v.second) + 0x9e3779b9 + (value << 6) + (value >> 2);
  160. return value;
  161. }
  162. };
  163. using instance_map = std::unordered_multimap<const void *, instance *>;
  164. #ifdef Py_GIL_DISABLED
  165. // Wrapper around PyMutex to provide BasicLockable semantics
  166. class pymutex {
  167. PyMutex mutex;
  168. public:
  169. pymutex() : mutex({}) {}
  170. void lock() { PyMutex_Lock(&mutex); }
  171. void unlock() { PyMutex_Unlock(&mutex); }
  172. };
  173. // Instance map shards are used to reduce mutex contention in free-threaded Python.
  174. struct instance_map_shard {
  175. instance_map registered_instances;
  176. pymutex mutex;
  177. // alignas(64) would be better, but causes compile errors in macOS before 10.14 (see #5200)
  178. char padding[64 - (sizeof(instance_map) + sizeof(pymutex)) % 64];
  179. };
  180. static_assert(sizeof(instance_map_shard) % 64 == 0,
  181. "instance_map_shard size is not a multiple of 64 bytes");
  182. inline uint64_t round_up_to_next_pow2(uint64_t x) {
  183. // Round-up to the next power of two.
  184. // See https://graphics.stanford.edu/~seander/bithacks.html#RoundUpPowerOf2
  185. x--;
  186. x |= (x >> 1);
  187. x |= (x >> 2);
  188. x |= (x >> 4);
  189. x |= (x >> 8);
  190. x |= (x >> 16);
  191. x |= (x >> 32);
  192. x++;
  193. return x;
  194. }
  195. #endif
  196. class loader_life_support;
  197. /// Internal data structure used to track registered instances and types.
  198. /// Whenever binary incompatible changes are made to this structure,
  199. /// `PYBIND11_INTERNALS_VERSION` must be incremented.
  200. struct internals {
  201. #ifdef Py_GIL_DISABLED
  202. pymutex mutex;
  203. pymutex exception_translator_mutex;
  204. #endif
  205. // std::type_index -> pybind11's type information
  206. type_map<type_info *> registered_types_cpp;
  207. // PyTypeObject* -> base type_info(s)
  208. std::unordered_map<PyTypeObject *, std::vector<type_info *>> registered_types_py;
  209. #ifdef Py_GIL_DISABLED
  210. std::unique_ptr<instance_map_shard[]> instance_shards; // void * -> instance*
  211. size_t instance_shards_mask = 0;
  212. #else
  213. instance_map registered_instances; // void * -> instance*
  214. #endif
  215. std::unordered_set<std::pair<const PyObject *, const char *>, override_hash>
  216. inactive_override_cache;
  217. type_map<std::vector<bool (*)(PyObject *, void *&)>> direct_conversions;
  218. std::unordered_map<const PyObject *, std::vector<PyObject *>> patients;
  219. std::forward_list<ExceptionTranslator> registered_exception_translators;
  220. std::unordered_map<std::string, void *> shared_data; // Custom data to be shared across
  221. // extensions
  222. std::forward_list<std::string> static_strings; // Stores the std::strings backing
  223. // detail::c_str()
  224. PyTypeObject *static_property_type = nullptr;
  225. PyTypeObject *default_metaclass = nullptr;
  226. PyObject *instance_base = nullptr;
  227. // Unused if PYBIND11_SIMPLE_GIL_MANAGEMENT is defined:
  228. thread_specific_storage<PyThreadState> tstate;
  229. thread_specific_storage<loader_life_support> loader_life_support_tls;
  230. // Unused if PYBIND11_SIMPLE_GIL_MANAGEMENT is defined:
  231. PyInterpreterState *istate = nullptr;
  232. type_map<PyObject *> native_enum_type_map;
  233. internals()
  234. : static_property_type(make_static_property_type()),
  235. default_metaclass(make_default_metaclass()) {
  236. PyThreadState *cur_tstate = PyThreadState_Get();
  237. tstate = cur_tstate;
  238. istate = cur_tstate->interp;
  239. registered_exception_translators.push_front(&translate_exception);
  240. #ifdef Py_GIL_DISABLED
  241. // Scale proportional to the number of cores. 2x is a heuristic to reduce contention.
  242. // Make sure the number isn't unreasonable by limiting it to 16 bits (65K)
  243. auto num_shards = static_cast<std::uint16_t>(
  244. std::min<std::size_t>(round_up_to_next_pow2(2 * std::thread::hardware_concurrency()),
  245. std::numeric_limits<std::uint16_t>::max()));
  246. if (num_shards == 0) {
  247. num_shards = 1;
  248. }
  249. instance_shards.reset(new instance_map_shard[num_shards]);
  250. instance_shards_mask = num_shards - 1;
  251. #endif
  252. }
  253. internals(const internals &other) = delete;
  254. internals(internals &&other) = delete;
  255. internals &operator=(const internals &other) = delete;
  256. internals &operator=(internals &&other) = delete;
  257. ~internals() = default;
  258. };
  259. // the internals struct (above) is shared between all the modules. local_internals are only
  260. // for a single module. Any changes made to internals may require an update to
  261. // PYBIND11_INTERNALS_VERSION, breaking backwards compatibility. local_internals is, by design,
  262. // restricted to a single module. Whether a module has local internals or not should not
  263. // impact any other modules, because the only things accessing the local internals is the
  264. // module that contains them.
  265. struct local_internals {
  266. type_map<type_info *> registered_types_cpp;
  267. std::forward_list<ExceptionTranslator> registered_exception_translators;
  268. PyTypeObject *function_record_py_type = nullptr;
  269. };
  270. enum class holder_enum_t : uint8_t {
  271. undefined,
  272. std_unique_ptr, // Default, lacking interop with std::shared_ptr.
  273. std_shared_ptr, // Lacking interop with std::unique_ptr.
  274. smart_holder, // Full std::unique_ptr / std::shared_ptr interop.
  275. custom_holder,
  276. };
  277. /// Additional type information which does not fit into the PyTypeObject.
  278. /// Changes to this struct also require bumping `PYBIND11_INTERNALS_VERSION`.
  279. struct type_info {
  280. PyTypeObject *type;
  281. const std::type_info *cpptype;
  282. size_t type_size, type_align, holder_size_in_ptrs;
  283. void *(*operator_new)(size_t);
  284. void (*init_instance)(instance *, const void *);
  285. void (*dealloc)(value_and_holder &v_h);
  286. // Cross-DSO-safe function pointers, to sidestep cross-DSO RTTI issues
  287. // on platforms like macOS (see PR #5728 for details):
  288. memory::get_guarded_delete_fn get_memory_guarded_delete = memory::get_guarded_delete;
  289. get_trampoline_self_life_support_fn get_trampoline_self_life_support = nullptr;
  290. std::vector<PyObject *(*) (PyObject *, PyTypeObject *)> implicit_conversions;
  291. std::vector<std::pair<const std::type_info *, void *(*) (void *)>> implicit_casts;
  292. std::vector<bool (*)(PyObject *, void *&)> *direct_conversions;
  293. buffer_info *(*get_buffer)(PyObject *, void *) = nullptr;
  294. void *get_buffer_data = nullptr;
  295. void *(*module_local_load)(PyObject *, const type_info *) = nullptr;
  296. holder_enum_t holder_enum_v = holder_enum_t::undefined;
  297. /* A simple type never occurs as a (direct or indirect) parent
  298. * of a class that makes use of multiple inheritance.
  299. * A type can be simple even if it has non-simple ancestors as long as it has no descendants.
  300. */
  301. bool simple_type : 1;
  302. /* True if there is no multiple inheritance in this type's inheritance tree */
  303. bool simple_ancestors : 1;
  304. /* true if this is a type registered with py::module_local */
  305. bool module_local : 1;
  306. };
  307. #define PYBIND11_INTERNALS_ID \
  308. "__pybind11_internals_v" PYBIND11_TOSTRING(PYBIND11_INTERNALS_VERSION) \
  309. PYBIND11_COMPILER_TYPE_LEADING_UNDERSCORE PYBIND11_PLATFORM_ABI_ID "__"
  310. #define PYBIND11_MODULE_LOCAL_ID \
  311. "__pybind11_module_local_v" PYBIND11_TOSTRING(PYBIND11_INTERNALS_VERSION) \
  312. PYBIND11_COMPILER_TYPE_LEADING_UNDERSCORE PYBIND11_PLATFORM_ABI_ID "__"
  313. inline PyThreadState *get_thread_state_unchecked() {
  314. #if defined(PYPY_VERSION) || defined(GRAALVM_PYTHON)
  315. return PyThreadState_GET();
  316. #elif PY_VERSION_HEX < 0x030D0000
  317. return _PyThreadState_UncheckedGet();
  318. #else
  319. return PyThreadState_GetUnchecked();
  320. #endif
  321. }
  322. /// We use this counter to figure out if there are or have been multiple subinterpreters active at
  323. /// any point. This must never decrease while any interpreter may be running in any thread!
  324. inline std::atomic<int> &get_num_interpreters_seen() {
  325. static std::atomic<int> counter(0);
  326. return counter;
  327. }
  328. template <class T,
  329. enable_if_t<std::is_same<std::nested_exception, remove_cvref_t<T>>::value, int> = 0>
  330. bool handle_nested_exception(const T &exc, const std::exception_ptr &p) {
  331. std::exception_ptr nested = exc.nested_ptr();
  332. if (nested != nullptr && nested != p) {
  333. translate_exception(nested);
  334. return true;
  335. }
  336. return false;
  337. }
  338. template <class T,
  339. enable_if_t<!std::is_same<std::nested_exception, remove_cvref_t<T>>::value, int> = 0>
  340. bool handle_nested_exception(const T &exc, const std::exception_ptr &p) {
  341. if (const auto *nep = dynamic_cast<const std::nested_exception *>(std::addressof(exc))) {
  342. return handle_nested_exception(*nep, p);
  343. }
  344. return false;
  345. }
  346. inline bool raise_err(PyObject *exc_type, const char *msg) {
  347. if (PyErr_Occurred()) {
  348. raise_from(exc_type, msg);
  349. return true;
  350. }
  351. set_error(exc_type, msg);
  352. return false;
  353. }
  354. inline void translate_exception(std::exception_ptr p) {
  355. if (!p) {
  356. return;
  357. }
  358. try {
  359. std::rethrow_exception(p);
  360. } catch (error_already_set &e) {
  361. handle_nested_exception(e, p);
  362. e.restore();
  363. return;
  364. } catch (const builtin_exception &e) {
  365. // Could not use template since it's an abstract class.
  366. if (const auto *nep = dynamic_cast<const std::nested_exception *>(std::addressof(e))) {
  367. handle_nested_exception(*nep, p);
  368. }
  369. e.set_error();
  370. return;
  371. } catch (const std::bad_alloc &e) {
  372. handle_nested_exception(e, p);
  373. raise_err(PyExc_MemoryError, e.what());
  374. return;
  375. } catch (const std::domain_error &e) {
  376. handle_nested_exception(e, p);
  377. raise_err(PyExc_ValueError, e.what());
  378. return;
  379. } catch (const std::invalid_argument &e) {
  380. handle_nested_exception(e, p);
  381. raise_err(PyExc_ValueError, e.what());
  382. return;
  383. } catch (const std::length_error &e) {
  384. handle_nested_exception(e, p);
  385. raise_err(PyExc_ValueError, e.what());
  386. return;
  387. } catch (const std::out_of_range &e) {
  388. handle_nested_exception(e, p);
  389. raise_err(PyExc_IndexError, e.what());
  390. return;
  391. } catch (const std::range_error &e) {
  392. handle_nested_exception(e, p);
  393. raise_err(PyExc_ValueError, e.what());
  394. return;
  395. } catch (const std::overflow_error &e) {
  396. handle_nested_exception(e, p);
  397. raise_err(PyExc_OverflowError, e.what());
  398. return;
  399. } catch (const std::exception &e) {
  400. handle_nested_exception(e, p);
  401. raise_err(PyExc_RuntimeError, e.what());
  402. return;
  403. } catch (const std::nested_exception &e) {
  404. handle_nested_exception(e, p);
  405. raise_err(PyExc_RuntimeError, "Caught an unknown nested exception!");
  406. return;
  407. } catch (...) {
  408. raise_err(PyExc_RuntimeError, "Caught an unknown exception!");
  409. return;
  410. }
  411. }
  412. #if !defined(__GLIBCXX__)
  413. inline void translate_local_exception(std::exception_ptr p) {
  414. try {
  415. if (p) {
  416. std::rethrow_exception(p);
  417. }
  418. } catch (error_already_set &e) {
  419. e.restore();
  420. return;
  421. } catch (const builtin_exception &e) {
  422. e.set_error();
  423. return;
  424. }
  425. }
  426. #endif
  427. inline object get_python_state_dict() {
  428. object state_dict;
  429. #if defined(PYPY_VERSION) || defined(GRAALVM_PYTHON)
  430. state_dict = reinterpret_borrow<object>(PyEval_GetBuiltins());
  431. #else
  432. # if PY_VERSION_HEX < 0x03090000
  433. PyInterpreterState *istate = _PyInterpreterState_Get();
  434. # else
  435. PyInterpreterState *istate = PyInterpreterState_Get();
  436. # endif
  437. if (istate) {
  438. state_dict = reinterpret_borrow<object>(PyInterpreterState_GetDict(istate));
  439. }
  440. #endif
  441. if (!state_dict) {
  442. raise_from(PyExc_SystemError, "pybind11::detail::get_python_state_dict() FAILED");
  443. throw error_already_set();
  444. }
  445. return state_dict;
  446. }
  447. template <typename InternalsType>
  448. class internals_pp_manager {
  449. public:
  450. using on_fetch_function = void(InternalsType *);
  451. internals_pp_manager(char const *id, on_fetch_function *on_fetch)
  452. : holder_id_(id), on_fetch_(on_fetch) {}
  453. /// Get the current pointer-to-pointer, allocating it if it does not already exist. May
  454. /// acquire the GIL. Will never return nullptr.
  455. std::unique_ptr<InternalsType> *get_pp() {
  456. #ifdef PYBIND11_HAS_SUBINTERPRETER_SUPPORT
  457. if (get_num_interpreters_seen() > 1) {
  458. // Whenever the interpreter changes on the current thread we need to invalidate the
  459. // internals_pp so that it can be pulled from the interpreter's state dict. That is
  460. // slow, so we use the current PyThreadState to check if it is necessary.
  461. auto *tstate = get_thread_state_unchecked();
  462. if (!tstate || tstate->interp != last_istate_.get()) {
  463. gil_scoped_acquire_simple gil;
  464. if (!tstate) {
  465. tstate = get_thread_state_unchecked();
  466. }
  467. last_istate_ = tstate->interp;
  468. internals_tls_p_ = get_or_create_pp_in_state_dict();
  469. }
  470. return internals_tls_p_.get();
  471. }
  472. #endif
  473. if (!internals_singleton_pp_) {
  474. gil_scoped_acquire_simple gil;
  475. internals_singleton_pp_ = get_or_create_pp_in_state_dict();
  476. }
  477. return internals_singleton_pp_;
  478. }
  479. /// Drop all the references we're currently holding.
  480. void unref() {
  481. #ifdef PYBIND11_HAS_SUBINTERPRETER_SUPPORT
  482. if (get_num_interpreters_seen() > 1) {
  483. last_istate_.reset();
  484. internals_tls_p_.reset();
  485. return;
  486. }
  487. #endif
  488. internals_singleton_pp_ = nullptr;
  489. }
  490. void destroy() {
  491. #ifdef PYBIND11_HAS_SUBINTERPRETER_SUPPORT
  492. if (get_num_interpreters_seen() > 1) {
  493. auto *tstate = get_thread_state_unchecked();
  494. // this could be called without an active interpreter, just use what was cached
  495. if (!tstate || tstate->interp == last_istate_.get()) {
  496. auto tpp = internals_tls_p_.get();
  497. if (tpp) {
  498. delete tpp;
  499. }
  500. }
  501. unref();
  502. return;
  503. }
  504. #endif
  505. delete internals_singleton_pp_;
  506. unref();
  507. }
  508. private:
  509. std::unique_ptr<InternalsType> *get_or_create_pp_in_state_dict() {
  510. error_scope err_scope;
  511. dict state_dict = get_python_state_dict();
  512. auto internals_obj
  513. = reinterpret_steal<object>(dict_getitemstringref(state_dict.ptr(), holder_id_));
  514. std::unique_ptr<InternalsType> *pp = nullptr;
  515. if (internals_obj) {
  516. void *raw_ptr = PyCapsule_GetPointer(internals_obj.ptr(), /*name=*/nullptr);
  517. if (!raw_ptr) {
  518. raise_from(PyExc_SystemError,
  519. "pybind11::detail::internals_pp_manager::get_pp_from_dict() FAILED");
  520. throw error_already_set();
  521. }
  522. pp = reinterpret_cast<std::unique_ptr<InternalsType> *>(raw_ptr);
  523. if (on_fetch_ && pp) {
  524. on_fetch_(pp->get());
  525. }
  526. } else {
  527. pp = new std::unique_ptr<InternalsType>;
  528. // NOLINTNEXTLINE(bugprone-casting-through-void)
  529. state_dict[holder_id_] = capsule(reinterpret_cast<void *>(pp));
  530. }
  531. return pp;
  532. }
  533. char const *holder_id_ = nullptr;
  534. on_fetch_function *on_fetch_ = nullptr;
  535. #ifdef PYBIND11_HAS_SUBINTERPRETER_SUPPORT
  536. thread_specific_storage<PyInterpreterState> last_istate_;
  537. thread_specific_storage<std::unique_ptr<InternalsType>> internals_tls_p_;
  538. #endif
  539. std::unique_ptr<InternalsType> *internals_singleton_pp_;
  540. };
  541. // If We loaded the internals through `state_dict`, our `error_already_set`
  542. // and `builtin_exception` may be different local classes than the ones set up in the
  543. // initial exception translator, below, so add another for our local exception classes.
  544. //
  545. // libstdc++ doesn't require this (types there are identified only by name)
  546. // libc++ with CPython doesn't require this (types are explicitly exported)
  547. // libc++ with PyPy still need it, awaiting further investigation
  548. #if !defined(__GLIBCXX__)
  549. inline void check_internals_local_exception_translator(internals *internals_ptr) {
  550. if (internals_ptr) {
  551. for (auto et : internals_ptr->registered_exception_translators) {
  552. if (et == &translate_local_exception) {
  553. return;
  554. }
  555. }
  556. internals_ptr->registered_exception_translators.push_front(&translate_local_exception);
  557. }
  558. }
  559. #endif
  560. inline internals_pp_manager<internals> &get_internals_pp_manager() {
  561. #if defined(__GLIBCXX__)
  562. # define ON_FETCH_FN nullptr
  563. #else
  564. # define ON_FETCH_FN &check_internals_local_exception_translator
  565. #endif
  566. static internals_pp_manager<internals> internals_pp_manager(PYBIND11_INTERNALS_ID,
  567. ON_FETCH_FN);
  568. #undef ON_FETCH_FN
  569. return internals_pp_manager;
  570. }
  571. /// Return a reference to the current `internals` data
  572. PYBIND11_NOINLINE internals &get_internals() {
  573. auto &ppmgr = get_internals_pp_manager();
  574. auto &internals_ptr = *ppmgr.get_pp();
  575. if (!internals_ptr) {
  576. // Slow path, something needs fetched from the state dict or created
  577. gil_scoped_acquire_simple gil;
  578. error_scope err_scope;
  579. internals_ptr.reset(new internals());
  580. if (!internals_ptr->instance_base) {
  581. // This calls get_internals, so cannot be called from within the internals constructor
  582. // called above because internals_ptr must be set before get_internals is called again
  583. internals_ptr->instance_base = make_object_base_type(internals_ptr->default_metaclass);
  584. }
  585. }
  586. return *internals_ptr;
  587. }
  588. inline internals_pp_manager<local_internals> &get_local_internals_pp_manager() {
  589. // Use the address of this static itself as part of the key, so that the value is uniquely tied
  590. // to where the module is loaded in memory
  591. static const std::string this_module_idstr
  592. = PYBIND11_MODULE_LOCAL_ID
  593. + std::to_string(reinterpret_cast<uintptr_t>(&this_module_idstr));
  594. static internals_pp_manager<local_internals> local_internals_pp_manager(
  595. this_module_idstr.c_str(), nullptr);
  596. return local_internals_pp_manager;
  597. }
  598. /// Works like `get_internals`, but for things which are locally registered.
  599. inline local_internals &get_local_internals() {
  600. auto &ppmgr = get_local_internals_pp_manager();
  601. auto &internals_ptr = *ppmgr.get_pp();
  602. if (!internals_ptr) {
  603. internals_ptr.reset(new local_internals());
  604. }
  605. return *internals_ptr;
  606. }
  607. #ifdef Py_GIL_DISABLED
  608. # define PYBIND11_LOCK_INTERNALS(internals) std::unique_lock<pymutex> lock((internals).mutex)
  609. #else
  610. # define PYBIND11_LOCK_INTERNALS(internals)
  611. #endif
  612. template <typename F>
  613. inline auto with_internals(const F &cb) -> decltype(cb(get_internals())) {
  614. auto &internals = get_internals();
  615. PYBIND11_LOCK_INTERNALS(internals);
  616. return cb(internals);
  617. }
  618. template <typename F>
  619. inline auto with_exception_translators(const F &cb)
  620. -> decltype(cb(get_internals().registered_exception_translators,
  621. get_local_internals().registered_exception_translators)) {
  622. auto &internals = get_internals();
  623. #ifdef Py_GIL_DISABLED
  624. std::unique_lock<pymutex> lock((internals).exception_translator_mutex);
  625. #endif
  626. auto &local_internals = get_local_internals();
  627. return cb(internals.registered_exception_translators,
  628. local_internals.registered_exception_translators);
  629. }
  630. inline std::uint64_t mix64(std::uint64_t z) {
  631. // David Stafford's variant 13 of the MurmurHash3 finalizer popularized
  632. // by the SplitMix PRNG.
  633. // https://zimbry.blogspot.com/2011/09/better-bit-mixing-improving-on.html
  634. z = (z ^ (z >> 30)) * 0xbf58476d1ce4e5b9;
  635. z = (z ^ (z >> 27)) * 0x94d049bb133111eb;
  636. return z ^ (z >> 31);
  637. }
  638. template <typename F>
  639. inline auto with_instance_map(const void *ptr, const F &cb)
  640. -> decltype(cb(std::declval<instance_map &>())) {
  641. auto &internals = get_internals();
  642. #ifdef Py_GIL_DISABLED
  643. // Hash address to compute shard, but ignore low bits. We'd like allocations
  644. // from the same thread/core to map to the same shard and allocations from
  645. // other threads/cores to map to other shards. Using the high bits is a good
  646. // heuristic because memory allocators often have a per-thread
  647. // arena/superblock/segment from which smaller allocations are served.
  648. auto addr = reinterpret_cast<std::uintptr_t>(ptr);
  649. auto hash = mix64(static_cast<std::uint64_t>(addr >> 20));
  650. auto idx = static_cast<size_t>(hash & internals.instance_shards_mask);
  651. auto &shard = internals.instance_shards[idx];
  652. std::unique_lock<pymutex> lock(shard.mutex);
  653. return cb(shard.registered_instances);
  654. #else
  655. (void) ptr;
  656. return cb(internals.registered_instances);
  657. #endif
  658. }
  659. // Returns the number of registered instances for testing purposes. The result may not be
  660. // consistent if other threads are registering or unregistering instances concurrently.
  661. inline size_t num_registered_instances() {
  662. auto &internals = get_internals();
  663. #ifdef Py_GIL_DISABLED
  664. size_t count = 0;
  665. for (size_t i = 0; i <= internals.instance_shards_mask; ++i) {
  666. auto &shard = internals.instance_shards[i];
  667. std::unique_lock<pymutex> lock(shard.mutex);
  668. count += shard.registered_instances.size();
  669. }
  670. return count;
  671. #else
  672. return internals.registered_instances.size();
  673. #endif
  674. }
  675. /// Constructs a std::string with the given arguments, stores it in `internals`, and returns its
  676. /// `c_str()`. Such strings objects have a long storage duration -- the internal strings are only
  677. /// cleared when the program exits or after interpreter shutdown (when embedding), and so are
  678. /// suitable for c-style strings needed by Python internals (such as PyTypeObject's tp_name).
  679. template <typename... Args>
  680. const char *c_str(Args &&...args) {
  681. // GCC 4.8 doesn't like parameter unpack within lambda capture, so use
  682. // PYBIND11_LOCK_INTERNALS.
  683. auto &internals = get_internals();
  684. PYBIND11_LOCK_INTERNALS(internals);
  685. auto &strings = internals.static_strings;
  686. strings.emplace_front(std::forward<Args>(args)...);
  687. return strings.front().c_str();
  688. }
  689. PYBIND11_NAMESPACE_END(detail)
  690. /// Returns a named pointer that is shared among all extension modules (using the same
  691. /// pybind11 version) running in the current interpreter. Names starting with underscores
  692. /// are reserved for internal usage. Returns `nullptr` if no matching entry was found.
  693. PYBIND11_NOINLINE void *get_shared_data(const std::string &name) {
  694. return detail::with_internals([&](detail::internals &internals) {
  695. auto it = internals.shared_data.find(name);
  696. return it != internals.shared_data.end() ? it->second : nullptr;
  697. });
  698. }
  699. /// Set the shared data that can be later recovered by `get_shared_data()`.
  700. PYBIND11_NOINLINE void *set_shared_data(const std::string &name, void *data) {
  701. return detail::with_internals([&](detail::internals &internals) {
  702. internals.shared_data[name] = data;
  703. return data;
  704. });
  705. }
  706. /// Returns a typed reference to a shared data entry (by using `get_shared_data()`) if
  707. /// such entry exists. Otherwise, a new object of default-constructible type `T` is
  708. /// added to the shared data under the given name and a reference to it is returned.
  709. template <typename T>
  710. T &get_or_create_shared_data(const std::string &name) {
  711. return *detail::with_internals([&](detail::internals &internals) {
  712. auto it = internals.shared_data.find(name);
  713. T *ptr = (T *) (it != internals.shared_data.end() ? it->second : nullptr);
  714. if (!ptr) {
  715. ptr = new T();
  716. internals.shared_data[name] = ptr;
  717. }
  718. return ptr;
  719. });
  720. }
  721. PYBIND11_NAMESPACE_END(PYBIND11_NAMESPACE)