gil.h 6.4 KB

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