gil.h 8.1 KB

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