gil.h 8.3 KB

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