|
| 1 | +#ifndef Py_INTERNAL_CONTEXTCHAIN_H |
| 2 | +#define Py_INTERNAL_CONTEXTCHAIN_H |
| 3 | + |
| 4 | +#ifndef Py_BUILD_CORE |
| 5 | +# error "this header requires Py_BUILD_CORE define" |
| 6 | +#endif |
| 7 | + |
| 8 | +#include "pytypedefs.h" // PyObject |
| 9 | + |
| 10 | + |
| 11 | +// Circularly linked chain of multiple independent context stacks, used to give |
| 12 | +// coroutines (including generators) their own (optional) independent context |
| 13 | +// stacks. |
| 14 | +// |
| 15 | +// Detailed notes on how this chain is used: |
| 16 | +// * The chain is circular simply to save a pointer's worth of memory in |
| 17 | +// _PyThreadStateImpl. It is actually used as an ordinary linear linked |
| 18 | +// list. It is called "chain" instead of "stack" or "list" to evoke "call |
| 19 | +// chain", which it is related to, and to avoid confusion with "context |
| 20 | +// stack". |
| 21 | +// * There is one chain per thread, and _PyThreadStateImpl::_ctx_chain::prev |
| 22 | +// points to the head of the thread's chain. |
| 23 | +// * A thread's chain is never empty. |
| 24 | +// * _PyThreadStateImpl::_ctx_chain is always the tail entry of the thread's |
| 25 | +// chain. |
| 26 | +// * _PyThreadStateImpl::_ctx_chain is usually the only link in the thread's |
| 27 | +// chain, so _PyThreadStateImpl::_ctx_chain::prev usually points to the |
| 28 | +// _PyThreadStateImpl::_ctx_chain itself. |
| 29 | +// * The "active context stack" is always at the head link in a thread's |
| 30 | +// context chain. Contexts are entered by pushing onto the active context |
| 31 | +// stack and exited by popping off of the active context stack. |
| 32 | +// * The "current context" is the top context in the active context stack. |
| 33 | +// Context variable accesses (reads/writes) use the current context. |
| 34 | +// * A *dependent* coroutine or generator is a coroutine or generator that |
| 35 | +// does not have its own independent context stack. When a dependent |
| 36 | +// coroutine starts or resumes execution, the current context -- as |
| 37 | +// observed by the coroutine -- is the same context that was current just |
| 38 | +// before the coroutine's `send` method was called. This means that the |
| 39 | +// current context as observed by a dependent coroutine can change |
| 40 | +// arbitrarily during a yield/await. Dependent coroutines are so-named |
| 41 | +// because they depend on their senders to enter the appropriate context |
| 42 | +// before each send. Coroutines and generators are dependent by default |
| 43 | +// for backwards compatibility. |
| 44 | +// * The purpose of the context chain is to enable *independent* coroutines |
| 45 | +// and generators, which have their own context stacks. Whenever an |
| 46 | +// independent coroutine starts or resumes execution, the current context |
| 47 | +// automatically switches to the context associated with the coroutine. |
| 48 | +// This is accomplished by linking the coroutine's chain link (at |
| 49 | +// PyGenObject::_ctx_chain) to the head of the thread's chain. Independent |
| 50 | +// coroutines are so-named because they do not depend on their senders to |
| 51 | +// enter the appropriate context before each send. |
| 52 | +// * The head link is unlinked from the thread's chain when its associated |
| 53 | +// independent coroutine or generator stops executing (yields, awaits, |
| 54 | +// returns, or throws). |
| 55 | +// * A running dependent coroutine's chain link is linked into the thread's |
| 56 | +// chain if the coroutine is upgraded from dependent to independent by |
| 57 | +// assigning a context to the coroutine's `_context` property. The chain |
| 58 | +// link is inserted at the position corresponding to the coroutine's |
| 59 | +// position in the call chain relative to any other currently running |
| 60 | +// independent coroutines. For example, if dependent coroutine `coro_a` |
| 61 | +// calls function `func_b` which resumes independent coroutine `coro_c` |
| 62 | +// which assigns a context to `coro_a._context`, then `coro_a` becomes an |
| 63 | +// independent coroutine with its chain link inserted after `coro_c`'s |
| 64 | +// chain link (which remains the head link). |
| 65 | +// * A running independent coroutine's chain link is unlinked from the |
| 66 | +// thread's chain if the coroutine is downgraded from independent to |
| 67 | +// dependent by assigning `None` to its `_context` property. |
| 68 | +// * The references to the object at the `prev` link in the chain are |
| 69 | +// implicit (borrowed). |
| 70 | +typedef struct _PyContextChain { |
| 71 | + // NULL for dependent coroutines/generators, non-NULL for independent |
| 72 | + // coroutines/generators. |
| 73 | + PyObject *ctx; |
| 74 | + // NULL if unlinked from the thread's context chain, non-NULL otherwise. |
| 75 | + struct _PyContextChain *prev; |
| 76 | +} _PyContextChain; |
| 77 | + |
| 78 | + |
| 79 | +#endif /* !Py_INTERNAL_CONTEXTCHAIN_H */ |
0 commit comments