gil.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. /*
  2. pybind11/gil.h: RAII helpers for managing the GIL
  3. Copyright (c) 2016 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. #if defined(PYBIND11_SIMPLE_GIL_MANAGEMENT)
  9. # include "detail/common.h"
  10. # include "gil_simple.h"
  11. PYBIND11_NAMESPACE_BEGIN(PYBIND11_NAMESPACE)
  12. using gil_scoped_acquire = gil_scoped_acquire_simple;
  13. using gil_scoped_release = gil_scoped_release_simple;
  14. PYBIND11_NAMESPACE_END(PYBIND11_NAMESPACE)
  15. #else
  16. # include "detail/common.h"
  17. # include "detail/internals.h"
  18. # include <cassert>
  19. PYBIND11_NAMESPACE_BEGIN(PYBIND11_NAMESPACE)
  20. PYBIND11_NAMESPACE_BEGIN(detail)
  21. PYBIND11_WARNING_PUSH
  22. PYBIND11_WARNING_DISABLE_GCC("-Wredundant-decls")
  23. // forward declarations
  24. PyThreadState *get_thread_state_unchecked();
  25. PYBIND11_WARNING_POP
  26. PYBIND11_NAMESPACE_END(detail)
  27. /* The functions below essentially reproduce the PyGILState_* API using a RAII
  28. * pattern, but there are a few important differences:
  29. *
  30. * 1. When acquiring the GIL from an non-main thread during the finalization
  31. * phase, the GILState API blindly terminates the calling thread, which
  32. * is often not what is wanted. This API does not do this.
  33. *
  34. * 2. The gil_scoped_release function can optionally cut the relationship
  35. * of a PyThreadState and its associated thread, which allows moving it to
  36. * another thread (this is a fairly rare/advanced use case).
  37. *
  38. * 3. The reference count of an acquired thread state can be controlled. This
  39. * can be handy to prevent cases where callbacks issued from an external
  40. * thread would otherwise constantly construct and destroy thread state data
  41. * structures.
  42. *
  43. * See the Python bindings of NanoGUI (http://github.com/wjakob/nanogui) for an
  44. * example which uses features 2 and 3 to migrate the Python thread of
  45. * execution to another thread (to run the event loop on the original thread,
  46. * in this case).
  47. */
  48. class gil_scoped_acquire {
  49. public:
  50. PYBIND11_NOINLINE gil_scoped_acquire() {
  51. auto &internals = detail::get_internals();
  52. tstate = internals.tstate.get();
  53. if (!tstate) {
  54. /* Check if the GIL was acquired using the PyGILState_* API instead (e.g. if
  55. calling from a Python thread). Since we use a different key, this ensures
  56. we don't create a new thread state and deadlock in PyEval_AcquireThread
  57. below. Note we don't save this state with internals.tstate, since we don't
  58. create it we would fail to clear it (its reference count should be > 0). */
  59. tstate = PyGILState_GetThisThreadState();
  60. }
  61. if (!tstate) {
  62. tstate = PyThreadState_New(internals.istate);
  63. # if defined(PYBIND11_DETAILED_ERROR_MESSAGES)
  64. if (!tstate) {
  65. pybind11_fail("scoped_acquire: could not create thread state!");
  66. }
  67. # endif
  68. tstate->gilstate_counter = 0;
  69. internals.tstate = tstate;
  70. } else {
  71. release = detail::get_thread_state_unchecked() != tstate;
  72. }
  73. if (release) {
  74. PyEval_AcquireThread(tstate);
  75. }
  76. inc_ref();
  77. }
  78. gil_scoped_acquire(const gil_scoped_acquire &) = delete;
  79. gil_scoped_acquire &operator=(const gil_scoped_acquire &) = delete;
  80. void inc_ref() { ++tstate->gilstate_counter; }
  81. PYBIND11_NOINLINE void dec_ref() {
  82. --tstate->gilstate_counter;
  83. # if defined(PYBIND11_DETAILED_ERROR_MESSAGES)
  84. if (detail::get_thread_state_unchecked() != tstate) {
  85. pybind11_fail("scoped_acquire::dec_ref(): thread state must be current!");
  86. }
  87. if (tstate->gilstate_counter < 0) {
  88. pybind11_fail("scoped_acquire::dec_ref(): reference count underflow!");
  89. }
  90. # endif
  91. if (tstate->gilstate_counter == 0) {
  92. # if defined(PYBIND11_DETAILED_ERROR_MESSAGES)
  93. if (!release) {
  94. pybind11_fail("scoped_acquire::dec_ref(): internal error!");
  95. }
  96. # endif
  97. PyThreadState_Clear(tstate);
  98. if (active) {
  99. PyThreadState_DeleteCurrent();
  100. }
  101. detail::get_internals().tstate.reset();
  102. release = false;
  103. }
  104. }
  105. /// This method will disable the PyThreadState_DeleteCurrent call and the
  106. /// GIL won't be released. This method should be used if the interpreter
  107. /// could be shutting down when this is called, as thread deletion is not
  108. /// allowed during shutdown. Check _Py_IsFinalizing() on Python 3.7+, and
  109. /// protect subsequent code.
  110. PYBIND11_NOINLINE void disarm() { active = false; }
  111. PYBIND11_NOINLINE ~gil_scoped_acquire() {
  112. dec_ref();
  113. if (release) {
  114. PyEval_SaveThread();
  115. }
  116. }
  117. private:
  118. PyThreadState *tstate = nullptr;
  119. bool release = true;
  120. bool active = true;
  121. };
  122. class gil_scoped_release {
  123. public:
  124. // PRECONDITION: The GIL must be held when this constructor is called.
  125. explicit gil_scoped_release(bool disassoc = false) : disassoc(disassoc) {
  126. assert(PyGILState_Check());
  127. // `get_internals()` must be called here unconditionally in order to initialize
  128. // `internals.tstate` for subsequent `gil_scoped_acquire` calls. Otherwise, an
  129. // initialization race could occur as multiple threads try `gil_scoped_acquire`.
  130. auto &internals = detail::get_internals();
  131. // NOLINTNEXTLINE(cppcoreguidelines-prefer-member-initializer)
  132. tstate = PyEval_SaveThread();
  133. if (disassoc) {
  134. internals.tstate.reset();
  135. }
  136. }
  137. gil_scoped_release(const gil_scoped_release &) = delete;
  138. gil_scoped_release &operator=(const gil_scoped_release &) = delete;
  139. /// This method will disable the PyThreadState_DeleteCurrent call and the
  140. /// GIL won't be acquired. This method should be used if the interpreter
  141. /// could be shutting down when this is called, as thread deletion is not
  142. /// allowed during shutdown. Check _Py_IsFinalizing() on Python 3.7+, and
  143. /// protect subsequent code.
  144. PYBIND11_NOINLINE void disarm() { active = false; }
  145. ~gil_scoped_release() {
  146. if (!tstate) {
  147. return;
  148. }
  149. // `PyEval_RestoreThread()` should not be called if runtime is finalizing
  150. if (active) {
  151. PyEval_RestoreThread(tstate);
  152. }
  153. if (disassoc) {
  154. detail::get_internals().tstate = tstate;
  155. }
  156. }
  157. private:
  158. PyThreadState *tstate;
  159. bool disassoc;
  160. bool active = true;
  161. };
  162. PYBIND11_NAMESPACE_END(PYBIND11_NAMESPACE)
  163. #endif // !PYBIND11_SIMPLE_GIL_MANAGEMENT