您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 
 

403 行
15 KiB

  1. #ifndef GREENLET_PYTHON_STATE_CPP
  2. #define GREENLET_PYTHON_STATE_CPP
  3. #include <Python.h>
  4. #include "TGreenlet.hpp"
  5. namespace greenlet {
  6. PythonState::PythonState()
  7. : _top_frame()
  8. #if GREENLET_USE_CFRAME
  9. ,cframe(nullptr)
  10. ,use_tracing(0)
  11. #endif
  12. #if GREENLET_PY314
  13. ,py_recursion_depth(0)
  14. #elif GREENLET_PY312
  15. ,py_recursion_depth(0)
  16. ,c_recursion_depth(0)
  17. #else
  18. ,recursion_depth(0)
  19. #endif
  20. #if GREENLET_PY313
  21. ,delete_later(nullptr)
  22. #else
  23. ,trash_delete_nesting(0)
  24. #endif
  25. #if GREENLET_PY311
  26. ,current_frame(nullptr)
  27. ,datastack_chunk(nullptr)
  28. ,datastack_top(nullptr)
  29. ,datastack_limit(nullptr)
  30. #endif
  31. {
  32. #if GREENLET_USE_CFRAME
  33. /*
  34. The PyThreadState->cframe pointer usually points to memory on
  35. the stack, alloceted in a call into PyEval_EvalFrameDefault.
  36. Initially, before any evaluation begins, it points to the
  37. initial PyThreadState object's ``root_cframe`` object, which is
  38. statically allocated for the lifetime of the thread.
  39. A greenlet can last for longer than a call to
  40. PyEval_EvalFrameDefault, so we can't set its ``cframe`` pointer
  41. to be the current ``PyThreadState->cframe``; nor could we use
  42. one from the greenlet parent for the same reason. Yet a further
  43. no: we can't allocate one scoped to the greenlet and then
  44. destroy it when the greenlet is deallocated, because inside the
  45. interpreter the _PyCFrame objects form a linked list, and that too
  46. can result in accessing memory beyond its dynamic lifetime (if
  47. the greenlet doesn't actually finish before it dies, its entry
  48. could still be in the list).
  49. Using the ``root_cframe`` is problematic, though, because its
  50. members are never modified by the interpreter and are set to 0,
  51. meaning that its ``use_tracing`` flag is never updated. We don't
  52. want to modify that value in the ``root_cframe`` ourself: it
  53. *shouldn't* matter much because we should probably never get
  54. back to the point where that's the only cframe on the stack;
  55. even if it did matter, the major consequence of an incorrect
  56. value for ``use_tracing`` is that if its true the interpreter
  57. does some extra work --- however, it's just good code hygiene.
  58. Our solution: before a greenlet runs, after its initial
  59. creation, it uses the ``root_cframe`` just to have something to
  60. put there. However, once the greenlet is actually switched to
  61. for the first time, ``g_initialstub`` (which doesn't actually
  62. "return" while the greenlet is running) stores a new _PyCFrame on
  63. its local stack, and copies the appropriate values from the
  64. currently running _PyCFrame; this is then made the _PyCFrame for the
  65. newly-minted greenlet. ``g_initialstub`` then proceeds to call
  66. ``glet.run()``, which results in ``PyEval_...`` adding the
  67. _PyCFrame to the list. Switches continue as normal. Finally, when
  68. the greenlet finishes, the call to ``glet.run()`` returns and
  69. the _PyCFrame is taken out of the linked list and the stack value
  70. is now unused and free to expire.
  71. XXX: I think we can do better. If we're deallocing in the same
  72. thread, can't we traverse the list and unlink our frame?
  73. Can we just keep a reference to the thread state in case we
  74. dealloc in another thread? (Is that even possible if we're still
  75. running and haven't returned from g_initialstub?)
  76. */
  77. this->cframe = &PyThreadState_GET()->root_cframe;
  78. #endif
  79. }
  80. inline void PythonState::may_switch_away() noexcept
  81. {
  82. #if GREENLET_PY311
  83. // PyThreadState_GetFrame is probably going to have to allocate a
  84. // new frame object. That may trigger garbage collection. Because
  85. // we call this during the early phases of a switch (it doesn't
  86. // matter to which greenlet, as this has a global effect), if a GC
  87. // triggers a switch away, two things can happen, both bad:
  88. // - We might not get switched back to, halting forward progress.
  89. // this is pathological, but possible.
  90. // - We might get switched back to with a different set of
  91. // arguments or a throw instead of a switch. That would corrupt
  92. // our state (specifically, PyErr_Occurred() and this->args()
  93. // would no longer agree).
  94. //
  95. // Thus, when we call this API, we need to have GC disabled.
  96. // This method serves as a bottleneck we call when maybe beginning
  97. // a switch. In this way, it is always safe -- no risk of GC -- to
  98. // use ``_GetFrame()`` whenever we need to, just as it was in
  99. // <=3.10 (because subsequent calls will be cached and not
  100. // allocate memory).
  101. GCDisabledGuard no_gc;
  102. Py_XDECREF(PyThreadState_GetFrame(PyThreadState_GET()));
  103. #endif
  104. }
  105. void PythonState::operator<<(const PyThreadState *const tstate) noexcept
  106. {
  107. this->_context.steal(tstate->context);
  108. #if GREENLET_USE_CFRAME
  109. /*
  110. IMPORTANT: ``cframe`` is a pointer into the STACK. Thus, because
  111. the call to ``slp_switch()`` changes the contents of the stack,
  112. you cannot read from ``ts_current->cframe`` after that call and
  113. necessarily get the same values you get from reading it here.
  114. Anything you need to restore from now to then must be saved in a
  115. global/threadlocal variable (because we can't use stack
  116. variables here either). For things that need to persist across
  117. the switch, use `will_switch_from`.
  118. */
  119. this->cframe = tstate->cframe;
  120. #if !GREENLET_PY312
  121. this->use_tracing = tstate->cframe->use_tracing;
  122. #endif
  123. #endif // GREENLET_USE_CFRAME
  124. #if GREENLET_PY311
  125. #if GREENLET_PY314
  126. this->py_recursion_depth = tstate->py_recursion_limit - tstate->py_recursion_remaining;
  127. #elif GREENLET_PY312
  128. this->py_recursion_depth = tstate->py_recursion_limit - tstate->py_recursion_remaining;
  129. this->c_recursion_depth = Py_C_RECURSION_LIMIT - tstate->c_recursion_remaining;
  130. #else // not 312
  131. this->recursion_depth = tstate->recursion_limit - tstate->recursion_remaining;
  132. #endif // GREENLET_PY312
  133. #if GREENLET_PY313
  134. this->current_frame = tstate->current_frame;
  135. #elif GREENLET_USE_CFRAME
  136. this->current_frame = tstate->cframe->current_frame;
  137. #endif
  138. this->datastack_chunk = tstate->datastack_chunk;
  139. this->datastack_top = tstate->datastack_top;
  140. this->datastack_limit = tstate->datastack_limit;
  141. PyFrameObject *frame = PyThreadState_GetFrame((PyThreadState *)tstate);
  142. Py_XDECREF(frame); // PyThreadState_GetFrame gives us a new
  143. // reference.
  144. this->_top_frame.steal(frame);
  145. #if GREENLET_PY313
  146. this->delete_later = Py_XNewRef(tstate->delete_later);
  147. #elif GREENLET_PY312
  148. this->trash_delete_nesting = tstate->trash.delete_nesting;
  149. #else // not 312
  150. this->trash_delete_nesting = tstate->trash_delete_nesting;
  151. #endif // GREENLET_PY312
  152. #else // Not 311
  153. this->recursion_depth = tstate->recursion_depth;
  154. this->_top_frame.steal(tstate->frame);
  155. this->trash_delete_nesting = tstate->trash_delete_nesting;
  156. #endif // GREENLET_PY311
  157. }
  158. #if GREENLET_PY312
  159. void GREENLET_NOINLINE(PythonState::unexpose_frames)()
  160. {
  161. if (!this->top_frame()) {
  162. return;
  163. }
  164. // See GreenletState::expose_frames() and the comment on frames_were_exposed
  165. // for more information about this logic.
  166. _PyInterpreterFrame *iframe = this->_top_frame->f_frame;
  167. while (iframe != nullptr) {
  168. _PyInterpreterFrame *prev_exposed = iframe->previous;
  169. assert(iframe->frame_obj);
  170. memcpy(&iframe->previous, &iframe->frame_obj->_f_frame_data[0],
  171. sizeof(void *));
  172. iframe = prev_exposed;
  173. }
  174. }
  175. #else
  176. void PythonState::unexpose_frames()
  177. {}
  178. #endif
  179. void PythonState::operator>>(PyThreadState *const tstate) noexcept
  180. {
  181. tstate->context = this->_context.relinquish_ownership();
  182. /* Incrementing this value invalidates the contextvars cache,
  183. which would otherwise remain valid across switches */
  184. tstate->context_ver++;
  185. #if GREENLET_USE_CFRAME
  186. tstate->cframe = this->cframe;
  187. /*
  188. If we were tracing, we need to keep tracing.
  189. There should never be the possibility of hitting the
  190. root_cframe here. See note above about why we can't
  191. just copy this from ``origin->cframe->use_tracing``.
  192. */
  193. #if !GREENLET_PY312
  194. tstate->cframe->use_tracing = this->use_tracing;
  195. #endif
  196. #endif // GREENLET_USE_CFRAME
  197. #if GREENLET_PY311
  198. #if GREENLET_PY314
  199. tstate->py_recursion_remaining = tstate->py_recursion_limit - this->py_recursion_depth;
  200. this->unexpose_frames();
  201. #elif GREENLET_PY312
  202. tstate->py_recursion_remaining = tstate->py_recursion_limit - this->py_recursion_depth;
  203. tstate->c_recursion_remaining = Py_C_RECURSION_LIMIT - this->c_recursion_depth;
  204. this->unexpose_frames();
  205. #else // \/ 3.11
  206. tstate->recursion_remaining = tstate->recursion_limit - this->recursion_depth;
  207. #endif // GREENLET_PY312
  208. #if GREENLET_PY313
  209. tstate->current_frame = this->current_frame;
  210. #elif GREENLET_USE_CFRAME
  211. tstate->cframe->current_frame = this->current_frame;
  212. #endif
  213. tstate->datastack_chunk = this->datastack_chunk;
  214. tstate->datastack_top = this->datastack_top;
  215. tstate->datastack_limit = this->datastack_limit;
  216. this->_top_frame.relinquish_ownership();
  217. #if GREENLET_PY313
  218. Py_XDECREF(tstate->delete_later);
  219. tstate->delete_later = this->delete_later;
  220. Py_CLEAR(this->delete_later);
  221. #elif GREENLET_PY312
  222. tstate->trash.delete_nesting = this->trash_delete_nesting;
  223. #else // not 3.12
  224. tstate->trash_delete_nesting = this->trash_delete_nesting;
  225. #endif // GREENLET_PY312
  226. #else // not 3.11
  227. tstate->frame = this->_top_frame.relinquish_ownership();
  228. tstate->recursion_depth = this->recursion_depth;
  229. tstate->trash_delete_nesting = this->trash_delete_nesting;
  230. #endif // GREENLET_PY311
  231. }
  232. inline void PythonState::will_switch_from(PyThreadState *const origin_tstate) noexcept
  233. {
  234. #if GREENLET_USE_CFRAME && !GREENLET_PY312
  235. // The weird thing is, we don't actually save this for an
  236. // effect on the current greenlet, it's saved for an
  237. // effect on the target greenlet. That is, we want
  238. // continuity of this setting across the greenlet switch.
  239. this->use_tracing = origin_tstate->cframe->use_tracing;
  240. #endif
  241. }
  242. void PythonState::set_initial_state(const PyThreadState* const tstate) noexcept
  243. {
  244. this->_top_frame = nullptr;
  245. #if GREENLET_PY314
  246. this->py_recursion_depth = tstate->py_recursion_limit - tstate->py_recursion_remaining;
  247. #elif GREENLET_PY312
  248. this->py_recursion_depth = tstate->py_recursion_limit - tstate->py_recursion_remaining;
  249. // XXX: TODO: Comment from a reviewer:
  250. // Should this be ``Py_C_RECURSION_LIMIT - tstate->c_recursion_remaining``?
  251. // But to me it looks more like that might not be the right
  252. // initialization either?
  253. this->c_recursion_depth = tstate->py_recursion_limit - tstate->py_recursion_remaining;
  254. #elif GREENLET_PY311
  255. this->recursion_depth = tstate->recursion_limit - tstate->recursion_remaining;
  256. #else
  257. this->recursion_depth = tstate->recursion_depth;
  258. #endif
  259. }
  260. // TODO: Better state management about when we own the top frame.
  261. int PythonState::tp_traverse(visitproc visit, void* arg, bool own_top_frame) noexcept
  262. {
  263. Py_VISIT(this->_context.borrow());
  264. if (own_top_frame) {
  265. Py_VISIT(this->_top_frame.borrow());
  266. }
  267. return 0;
  268. }
  269. void PythonState::tp_clear(bool own_top_frame) noexcept
  270. {
  271. PythonStateContext::tp_clear();
  272. // If we get here owning a frame,
  273. // we got dealloc'd without being finished. We may or may not be
  274. // in the same thread.
  275. if (own_top_frame) {
  276. this->_top_frame.CLEAR();
  277. }
  278. }
  279. #if GREENLET_USE_CFRAME
  280. void PythonState::set_new_cframe(_PyCFrame& frame) noexcept
  281. {
  282. frame = *PyThreadState_GET()->cframe;
  283. /* Make the target greenlet refer to the stack value. */
  284. this->cframe = &frame;
  285. /*
  286. And restore the link to the previous frame so this one gets
  287. unliked appropriately.
  288. */
  289. this->cframe->previous = &PyThreadState_GET()->root_cframe;
  290. }
  291. #endif
  292. const PythonState::OwnedFrame& PythonState::top_frame() const noexcept
  293. {
  294. return this->_top_frame;
  295. }
  296. void PythonState::did_finish(PyThreadState* tstate) noexcept
  297. {
  298. #if GREENLET_PY311
  299. // See https://github.com/gevent/gevent/issues/1924 and
  300. // https://github.com/python-greenlet/greenlet/issues/328. In
  301. // short, Python 3.11 allocates memory for frames as a sort of
  302. // linked list that's kept as part of PyThreadState in the
  303. // ``datastack_chunk`` member and friends. These are saved and
  304. // restored as part of switching greenlets.
  305. //
  306. // When we initially switch to a greenlet, we set those to NULL.
  307. // That causes the frame management code to treat this like a
  308. // brand new thread and start a fresh list of chunks, beginning
  309. // with a new "root" chunk. As we make calls in this greenlet,
  310. // those chunks get added, and as calls return, they get popped.
  311. // But the frame code (pystate.c) is careful to make sure that the
  312. // root chunk never gets popped.
  313. //
  314. // Thus, when a greenlet exits for the last time, there will be at
  315. // least a single root chunk that we must be responsible for
  316. // deallocating.
  317. //
  318. // The complex part is that these chunks are allocated and freed
  319. // using ``_PyObject_VirtualAlloc``/``Free``. Those aren't public
  320. // functions, and they aren't exported for linking. It so happens
  321. // that we know they are just thin wrappers around the Arena
  322. // allocator, so we can use that directly to deallocate in a
  323. // compatible way.
  324. //
  325. // CAUTION: Check this implementation detail on every major version.
  326. //
  327. // It might be nice to be able to do this in our destructor, but
  328. // can we be sure that no one else is using that memory? Plus, as
  329. // described below, our pointers may not even be valid anymore. As
  330. // a special case, there is one time that we know we can do this,
  331. // and that's from the destructor of the associated UserGreenlet
  332. // (NOT main greenlet)
  333. PyObjectArenaAllocator alloc;
  334. _PyStackChunk* chunk = nullptr;
  335. if (tstate) {
  336. // We really did finish, we can never be switched to again.
  337. chunk = tstate->datastack_chunk;
  338. // Unfortunately, we can't do much sanity checking. Our
  339. // this->datastack_chunk pointer is out of date (evaluation may
  340. // have popped down through it already) so we can't verify that
  341. // we deallocate it. I don't think we can even check datastack_top
  342. // for the same reason.
  343. PyObject_GetArenaAllocator(&alloc);
  344. tstate->datastack_chunk = nullptr;
  345. tstate->datastack_limit = nullptr;
  346. tstate->datastack_top = nullptr;
  347. }
  348. else if (this->datastack_chunk) {
  349. // The UserGreenlet (NOT the main greenlet!) is being deallocated. If we're
  350. // still holding a stack chunk, it's garbage because we know
  351. // we can never switch back to let cPython clean it up.
  352. // Because the last time we got switched away from, and we
  353. // haven't run since then, we know our chain is valid and can
  354. // be dealloced.
  355. chunk = this->datastack_chunk;
  356. PyObject_GetArenaAllocator(&alloc);
  357. }
  358. if (alloc.free && chunk) {
  359. // In case the arena mechanism has been torn down already.
  360. while (chunk) {
  361. _PyStackChunk *prev = chunk->previous;
  362. chunk->previous = nullptr;
  363. alloc.free(alloc.ctx, chunk, chunk->size);
  364. chunk = prev;
  365. }
  366. }
  367. this->datastack_chunk = nullptr;
  368. this->datastack_limit = nullptr;
  369. this->datastack_top = nullptr;
  370. #endif
  371. }
  372. }; // namespace greenlet
  373. #endif // GREENLET_PYTHON_STATE_CPP