pycore_ceval.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. #ifndef Py_INTERNAL_CEVAL_H
  2. #define Py_INTERNAL_CEVAL_H
  3. #ifdef __cplusplus
  4. extern "C" {
  5. #endif
  6. #ifndef Py_BUILD_CORE
  7. # error "this header requires Py_BUILD_CORE define"
  8. #endif
  9. /* Forward declarations */
  10. struct pyruntimestate;
  11. struct _ceval_runtime_state;
  12. #ifndef Py_DEFAULT_RECURSION_LIMIT
  13. # define Py_DEFAULT_RECURSION_LIMIT 1000
  14. #endif
  15. #include "pycore_interp.h" // PyInterpreterState.eval_frame
  16. #include "pycore_pystate.h" // _PyThreadState_GET()
  17. extern void _Py_FinishPendingCalls(PyThreadState *tstate);
  18. extern void _PyEval_InitState(PyInterpreterState *, PyThread_type_lock);
  19. extern void _PyEval_FiniState(struct _ceval_state *ceval);
  20. PyAPI_FUNC(void) _PyEval_SignalReceived(PyInterpreterState *interp);
  21. PyAPI_FUNC(int) _PyEval_AddPendingCall(
  22. PyInterpreterState *interp,
  23. int (*func)(void *),
  24. void *arg,
  25. int mainthreadonly);
  26. PyAPI_FUNC(void) _PyEval_SignalAsyncExc(PyInterpreterState *interp);
  27. #ifdef HAVE_FORK
  28. extern PyStatus _PyEval_ReInitThreads(PyThreadState *tstate);
  29. #endif
  30. // Used by sys.call_tracing()
  31. extern PyObject* _PyEval_CallTracing(PyObject *func, PyObject *args);
  32. // Used by sys.get_asyncgen_hooks()
  33. extern PyObject* _PyEval_GetAsyncGenFirstiter(void);
  34. extern PyObject* _PyEval_GetAsyncGenFinalizer(void);
  35. // Used by sys.set_asyncgen_hooks()
  36. extern int _PyEval_SetAsyncGenFirstiter(PyObject *);
  37. extern int _PyEval_SetAsyncGenFinalizer(PyObject *);
  38. // Used by sys.get_coroutine_origin_tracking_depth()
  39. // and sys.set_coroutine_origin_tracking_depth()
  40. extern int _PyEval_GetCoroutineOriginTrackingDepth(void);
  41. extern int _PyEval_SetCoroutineOriginTrackingDepth(int depth);
  42. extern void _PyEval_Fini(void);
  43. extern PyObject* _PyEval_GetBuiltins(PyThreadState *tstate);
  44. extern PyObject* _PyEval_BuiltinsFromGlobals(
  45. PyThreadState *tstate,
  46. PyObject *globals);
  47. // Trampoline API
  48. typedef struct {
  49. // Callback to initialize the trampoline state
  50. void* (*init_state)(void);
  51. // Callback to register every trampoline being created
  52. void (*write_state)(void* state, const void *code_addr,
  53. unsigned int code_size, PyCodeObject* code);
  54. // Callback to free the trampoline state
  55. int (*free_state)(void* state);
  56. } _PyPerf_Callbacks;
  57. extern int _PyPerfTrampoline_SetCallbacks(_PyPerf_Callbacks *);
  58. extern void _PyPerfTrampoline_GetCallbacks(_PyPerf_Callbacks *);
  59. extern int _PyPerfTrampoline_Init(int activate);
  60. extern int _PyPerfTrampoline_Fini(void);
  61. extern void _PyPerfTrampoline_FreeArenas(void);
  62. extern int _PyIsPerfTrampolineActive(void);
  63. extern PyStatus _PyPerfTrampoline_AfterFork_Child(void);
  64. #ifdef PY_HAVE_PERF_TRAMPOLINE
  65. extern _PyPerf_Callbacks _Py_perfmap_callbacks;
  66. #endif
  67. static inline PyObject*
  68. _PyEval_EvalFrame(PyThreadState *tstate, struct _PyInterpreterFrame *frame, int throwflag)
  69. {
  70. EVAL_CALL_STAT_INC(EVAL_CALL_TOTAL);
  71. if (tstate->interp->eval_frame == NULL) {
  72. return _PyEval_EvalFrameDefault(tstate, frame, throwflag);
  73. }
  74. return tstate->interp->eval_frame(tstate, frame, throwflag);
  75. }
  76. extern PyObject*
  77. _PyEval_Vector(PyThreadState *tstate,
  78. PyFunctionObject *func, PyObject *locals,
  79. PyObject* const* args, size_t argcount,
  80. PyObject *kwnames);
  81. extern int _PyEval_ThreadsInitialized(void);
  82. extern PyStatus _PyEval_InitGIL(PyThreadState *tstate, int own_gil);
  83. extern void _PyEval_FiniGIL(PyInterpreterState *interp);
  84. extern void _PyEval_AcquireLock(PyThreadState *tstate);
  85. extern void _PyEval_ReleaseLock(PyInterpreterState *, PyThreadState *);
  86. extern PyThreadState * _PyThreadState_SwapNoGIL(PyThreadState *);
  87. extern void _PyEval_DeactivateOpCache(void);
  88. /* --- _Py_EnterRecursiveCall() ----------------------------------------- */
  89. #ifdef USE_STACKCHECK
  90. /* With USE_STACKCHECK macro defined, trigger stack checks in
  91. _Py_CheckRecursiveCall() on every 64th call to _Py_EnterRecursiveCall. */
  92. static inline int _Py_MakeRecCheck(PyThreadState *tstate) {
  93. return (tstate->c_recursion_remaining-- <= 0
  94. || (tstate->c_recursion_remaining & 63) == 0);
  95. }
  96. #else
  97. static inline int _Py_MakeRecCheck(PyThreadState *tstate) {
  98. return tstate->c_recursion_remaining-- <= 0;
  99. }
  100. #endif
  101. PyAPI_FUNC(int) _Py_CheckRecursiveCall(
  102. PyThreadState *tstate,
  103. const char *where);
  104. int _Py_CheckRecursiveCallPy(
  105. PyThreadState *tstate);
  106. static inline int _Py_EnterRecursiveCallTstate(PyThreadState *tstate,
  107. const char *where) {
  108. return (_Py_MakeRecCheck(tstate) && _Py_CheckRecursiveCall(tstate, where));
  109. }
  110. static inline int _Py_EnterRecursiveCall(const char *where) {
  111. PyThreadState *tstate = _PyThreadState_GET();
  112. return _Py_EnterRecursiveCallTstate(tstate, where);
  113. }
  114. static inline void _Py_LeaveRecursiveCallTstate(PyThreadState *tstate) {
  115. tstate->c_recursion_remaining++;
  116. }
  117. static inline void _Py_LeaveRecursiveCall(void) {
  118. PyThreadState *tstate = _PyThreadState_GET();
  119. _Py_LeaveRecursiveCallTstate(tstate);
  120. }
  121. extern struct _PyInterpreterFrame* _PyEval_GetFrame(void);
  122. extern PyObject* _Py_MakeCoro(PyFunctionObject *func);
  123. extern int _Py_HandlePending(PyThreadState *tstate);
  124. extern PyObject * _PyEval_GetFrameLocals(void);
  125. #ifdef __cplusplus
  126. }
  127. #endif
  128. #endif /* !Py_INTERNAL_CEVAL_H */