| 1 |
#ifndef Py_INTERNAL_CEVAL_H |
| 2 |
#define Py_INTERNAL_CEVAL_H |
| 3 |
#ifdef __cplusplus |
| 4 |
extern "C" { |
| 5 |
#endif |
| 6 |
|
| 7 |
#ifndef Py_BUILD_CORE |
| 8 |
# error "this header requires Py_BUILD_CORE define" |
| 9 |
#endif |
| 10 |
|
| 11 |
#include "dynamic_annotations.h" // _Py_ANNOTATE_RWLOCK_CREATE |
| 12 |
|
| 13 |
#include "pycore_interp.h" // PyInterpreterState.eval_frame |
| 14 |
#include "pycore_pystate.h" // _PyThreadState_GET() |
| 15 |
|
| 16 |
/* Forward declarations */ |
| 17 |
struct pyruntimestate; |
| 18 |
struct _ceval_runtime_state; |
| 19 |
|
| 20 |
// Export for '_lsprof' shared extension |
| 21 |
PyAPI_FUNC(int) _PyEval_SetProfile(PyThreadState *tstate, Py_tracefunc func, PyObject *arg); |
| 22 |
extern int _PyEval_SetProfileAllThreads(PyInterpreterState *interp, Py_tracefunc func, PyObject *arg); |
| 23 |
|
| 24 |
extern int _PyEval_SetTrace(PyThreadState *tstate, Py_tracefunc func, PyObject *arg); |
| 25 |
extern int _PyEval_SetTraceAllThreads(PyInterpreterState *interp, Py_tracefunc func, PyObject *arg); |
| 26 |
|
| 27 |
extern int _PyEval_SetOpcodeTrace(PyFrameObject *f, bool enable); |
| 28 |
|
| 29 |
// Helper to look up a builtin object |
| 30 |
// Export for 'array' shared extension |
| 31 |
PyAPI_FUNC(PyObject*) _PyEval_GetBuiltin(PyObject *); |
| 32 |
|
| 33 |
extern PyObject* _PyEval_GetBuiltinId(_Py_Identifier *); |
| 34 |
|
| 35 |
extern void _PyEval_SetSwitchInterval(unsigned long microseconds); |
| 36 |
extern unsigned long _PyEval_GetSwitchInterval(void); |
| 37 |
|
| 38 |
// Export for '_queue' shared extension |
| 39 |
PyAPI_FUNC(int) _PyEval_MakePendingCalls(PyThreadState *); |
| 40 |
|
| 41 |
#ifndef Py_DEFAULT_RECURSION_LIMIT |
| 42 |
# define Py_DEFAULT_RECURSION_LIMIT 1000 |
| 43 |
#endif |
| 44 |
|
| 45 |
extern void _Py_FinishPendingCalls(PyThreadState *tstate); |
| 46 |
extern void _PyEval_InitState(PyInterpreterState *); |
| 47 |
extern void _PyEval_SignalReceived(void); |
| 48 |
|
| 49 |
// bitwise flags: |
| 50 |
#define _Py_PENDING_MAINTHREADONLY 1 |
| 51 |
#define _Py_PENDING_RAWFREE 2 |
| 52 |
|
| 53 |
typedef int _Py_add_pending_call_result; |
| 54 |
#define _Py_ADD_PENDING_SUCCESS 0 |
| 55 |
#define _Py_ADD_PENDING_FULL -1 |
| 56 |
|
| 57 |
// Export for '_testinternalcapi' shared extension |
| 58 |
PyAPI_FUNC(_Py_add_pending_call_result) _PyEval_AddPendingCall( |
| 59 |
PyInterpreterState *interp, |
| 60 |
_Py_pending_call_func func, |
| 61 |
void *arg, |
| 62 |
int flags); |
| 63 |
|
| 64 |
#ifdef HAVE_FORK |
| 65 |
extern PyStatus _PyEval_ReInitThreads(PyThreadState *tstate); |
| 66 |
#endif |
| 67 |
|
| 68 |
// Used by sys.call_tracing() |
| 69 |
extern PyObject* _PyEval_CallTracing(PyObject *func, PyObject *args); |
| 70 |
|
| 71 |
// Used by sys.get_asyncgen_hooks() |
| 72 |
extern PyObject* _PyEval_GetAsyncGenFirstiter(void); |
| 73 |
extern PyObject* _PyEval_GetAsyncGenFinalizer(void); |
| 74 |
|
| 75 |
// Used by sys.set_asyncgen_hooks() |
| 76 |
extern int _PyEval_SetAsyncGenFirstiter(PyObject *); |
| 77 |
extern int _PyEval_SetAsyncGenFinalizer(PyObject *); |
| 78 |
|
| 79 |
// Used by sys.get_coroutine_origin_tracking_depth() |
| 80 |
// and sys.set_coroutine_origin_tracking_depth() |
| 81 |
extern int _PyEval_GetCoroutineOriginTrackingDepth(void); |
| 82 |
extern int _PyEval_SetCoroutineOriginTrackingDepth(int depth); |
| 83 |
|
| 84 |
extern void _PyEval_Fini(void); |
| 85 |
|
| 86 |
|
| 87 |
extern PyObject* _PyEval_GetBuiltins(PyThreadState *tstate); |
| 88 |
extern PyObject* _PyEval_BuiltinsFromGlobals( |
| 89 |
PyThreadState *tstate, |
| 90 |
PyObject *globals); |
| 91 |
|
| 92 |
// Trampoline API |
| 93 |
|
| 94 |
typedef struct { |
| 95 |
// Callback to initialize the trampoline state |
| 96 |
void* (*init_state)(void); |
| 97 |
// Callback to register every trampoline being created |
| 98 |
void (*write_state)(void* state, const void *code_addr, |
| 99 |
unsigned int code_size, PyCodeObject* code); |
| 100 |
// Callback to free the trampoline state |
| 101 |
int (*free_state)(void* state); |
| 102 |
} _PyPerf_Callbacks; |
| 103 |
|
| 104 |
extern int _PyPerfTrampoline_SetCallbacks(_PyPerf_Callbacks *); |
| 105 |
extern void _PyPerfTrampoline_GetCallbacks(_PyPerf_Callbacks *); |
| 106 |
extern int _PyPerfTrampoline_Init(int activate); |
| 107 |
extern int _PyPerfTrampoline_Fini(void); |
| 108 |
extern int _PyIsPerfTrampolineActive(void); |
| 109 |
extern PyStatus _PyPerfTrampoline_AfterFork_Child(void); |
| 110 |
#ifdef PY_HAVE_PERF_TRAMPOLINE |
| 111 |
extern _PyPerf_Callbacks _Py_perfmap_callbacks; |
| 112 |
extern _PyPerf_Callbacks _Py_perfmap_jit_callbacks; |
| 113 |
#endif |
| 114 |
|
| 115 |
static inline PyObject* |
| 116 |
_PyEval_EvalFrame(PyThreadState *tstate, struct _PyInterpreterFrame *frame, int throwflag) |
| 117 |
{ |
| 118 |
EVAL_CALL_STAT_INC(EVAL_CALL_TOTAL); |
| 119 |
if (tstate->interp->eval_frame == NULL) { |
| 120 |
return _PyEval_EvalFrameDefault(tstate, frame, throwflag); |
| 121 |
} |
| 122 |
return tstate->interp->eval_frame(tstate, frame, throwflag); |
| 123 |
} |
| 124 |
|
| 125 |
extern PyObject* |
| 126 |
_PyEval_Vector(PyThreadState *tstate, |
| 127 |
PyFunctionObject *func, PyObject *locals, |
| 128 |
PyObject* const* args, size_t argcount, |
| 129 |
PyObject *kwnames); |
| 130 |
|
| 131 |
extern int _PyEval_ThreadsInitialized(void); |
| 132 |
extern void _PyEval_InitGIL(PyThreadState *tstate, int own_gil); |
| 133 |
extern void _PyEval_FiniGIL(PyInterpreterState *interp); |
| 134 |
|
| 135 |
extern void _PyEval_AcquireLock(PyThreadState *tstate); |
| 136 |
|
| 137 |
extern void _PyEval_ReleaseLock(PyInterpreterState *, PyThreadState *, |
| 138 |
int final_release); |
| 139 |
|
| 140 |
#ifdef Py_GIL_DISABLED |
| 141 |
// Returns 0 or 1 if the GIL for the given thread's interpreter is disabled or |
| 142 |
// enabled, respectively. |
| 143 |
// |
| 144 |
// The enabled state of the GIL will not change while one or more threads are |
| 145 |
// attached. |
| 146 |
static inline int |
| 147 |
_PyEval_IsGILEnabled(PyThreadState *tstate) |
| 148 |
{ |
| 149 |
struct _gil_runtime_state *gil = tstate->interp->ceval.gil; |
| 150 |
return _Py_atomic_load_int_relaxed(&gil->enabled) != 0; |
| 151 |
} |
| 152 |
|
| 153 |
// Enable or disable the GIL used by the interpreter that owns tstate, which |
| 154 |
// must be the current thread. This may affect other interpreters, if the GIL |
| 155 |
// is shared. All three functions will be no-ops (and return 0) if the |
| 156 |
// interpreter's `enable_gil' config is not _PyConfig_GIL_DEFAULT. |
| 157 |
// |
| 158 |
// Every call to _PyEval_EnableGILTransient() must be paired with exactly one |
| 159 |
// call to either _PyEval_EnableGILPermanent() or |
| 160 |
// _PyEval_DisableGIL(). _PyEval_EnableGILPermanent() and _PyEval_DisableGIL() |
| 161 |
// must only be called while the GIL is enabled from a call to |
| 162 |
// _PyEval_EnableGILTransient(). |
| 163 |
// |
| 164 |
// _PyEval_EnableGILTransient() returns 1 if it enabled the GIL, or 0 if the |
| 165 |
// GIL was already enabled, whether transiently or permanently. The caller will |
| 166 |
// hold the GIL upon return. |
| 167 |
// |
| 168 |
// _PyEval_EnableGILPermanent() returns 1 if it permanently enabled the GIL |
| 169 |
// (which must already be enabled), or 0 if it was already permanently |
| 170 |
// enabled. Once _PyEval_EnableGILPermanent() has been called once, all |
| 171 |
// subsequent calls to any of the three functions will be no-ops. |
| 172 |
// |
| 173 |
// _PyEval_DisableGIL() returns 1 if it disabled the GIL, or 0 if the GIL was |
| 174 |
// kept enabled because of another request, whether transient or permanent. |
| 175 |
// |
| 176 |
// All three functions must be called by an attached thread (this implies that |
| 177 |
// if the GIL is enabled, the current thread must hold it). |
| 178 |
extern int _PyEval_EnableGILTransient(PyThreadState *tstate); |
| 179 |
extern int _PyEval_EnableGILPermanent(PyThreadState *tstate); |
| 180 |
extern int _PyEval_DisableGIL(PyThreadState *state); |
| 181 |
#endif |
| 182 |
|
| 183 |
extern void _PyEval_DeactivateOpCache(void); |
| 184 |
|
| 185 |
|
| 186 |
/* --- _Py_EnterRecursiveCall() ----------------------------------------- */ |
| 187 |
|
| 188 |
#ifdef USE_STACKCHECK |
| 189 |
/* With USE_STACKCHECK macro defined, trigger stack checks in |
| 190 |
_Py_CheckRecursiveCall() on every 64th call to _Py_EnterRecursiveCall. */ |
| 191 |
static inline int _Py_MakeRecCheck(PyThreadState *tstate) { |
| 192 |
return (tstate->c_recursion_remaining-- < 0 |
| 193 |
|| (tstate->c_recursion_remaining & 63) == 0); |
| 194 |
} |
| 195 |
#else |
| 196 |
static inline int _Py_MakeRecCheck(PyThreadState *tstate) { |
| 197 |
return tstate->c_recursion_remaining-- < 0; |
| 198 |
} |
| 199 |
#endif |
| 200 |
|
| 201 |
// Export for '_json' shared extension, used via _Py_EnterRecursiveCall() |
| 202 |
// static inline function. |
| 203 |
PyAPI_FUNC(int) _Py_CheckRecursiveCall( |
| 204 |
PyThreadState *tstate, |
| 205 |
const char *where); |
| 206 |
|
| 207 |
int _Py_CheckRecursiveCallPy( |
| 208 |
PyThreadState *tstate); |
| 209 |
|
| 210 |
static inline int _Py_EnterRecursiveCallTstate(PyThreadState *tstate, |
| 211 |
const char *where) { |
| 212 |
return (_Py_MakeRecCheck(tstate) && _Py_CheckRecursiveCall(tstate, where)); |
| 213 |
} |
| 214 |
|
| 215 |
static inline void _Py_EnterRecursiveCallTstateUnchecked(PyThreadState *tstate) { |
| 216 |
assert(tstate->c_recursion_remaining > 0); |
| 217 |
tstate->c_recursion_remaining--; |
| 218 |
} |
| 219 |
|
| 220 |
static inline int _Py_EnterRecursiveCall(const char *where) { |
| 221 |
PyThreadState *tstate = _PyThreadState_GET(); |
| 222 |
return _Py_EnterRecursiveCallTstate(tstate, where); |
| 223 |
} |
| 224 |
|
| 225 |
static inline void _Py_LeaveRecursiveCallTstate(PyThreadState *tstate) { |
| 226 |
tstate->c_recursion_remaining++; |
| 227 |
} |
| 228 |
|
| 229 |
static inline void _Py_LeaveRecursiveCall(void) { |
| 230 |
PyThreadState *tstate = _PyThreadState_GET(); |
| 231 |
_Py_LeaveRecursiveCallTstate(tstate); |
| 232 |
} |
| 233 |
|
| 234 |
extern struct _PyInterpreterFrame* _PyEval_GetFrame(void); |
| 235 |
|
| 236 |
PyAPI_FUNC(PyObject *)_Py_MakeCoro(PyFunctionObject *func); |
| 237 |
|
| 238 |
/* Handle signals, pending calls, GIL drop request |
| 239 |
and asynchronous exception */ |
| 240 |
PyAPI_FUNC(int) _Py_HandlePending(PyThreadState *tstate); |
| 241 |
|
| 242 |
extern PyObject * _PyEval_GetFrameLocals(void); |
| 243 |
|
| 244 |
typedef PyObject *(*conversion_func)(PyObject *); |
| 245 |
|
| 246 |
PyAPI_DATA(const binaryfunc) _PyEval_BinaryOps[]; |
| 247 |
PyAPI_DATA(const conversion_func) _PyEval_ConversionFuncs[]; |
| 248 |
|
| 249 |
PyAPI_FUNC(int) _PyEval_CheckExceptStarTypeValid(PyThreadState *tstate, PyObject* right); |
| 250 |
PyAPI_FUNC(int) _PyEval_CheckExceptTypeValid(PyThreadState *tstate, PyObject* right); |
| 251 |
PyAPI_FUNC(int) _PyEval_ExceptionGroupMatch(PyObject* exc_value, PyObject *match_type, PyObject **match, PyObject **rest); |
| 252 |
PyAPI_FUNC(void) _PyEval_FormatAwaitableError(PyThreadState *tstate, PyTypeObject *type, int oparg); |
| 253 |
PyAPI_FUNC(void) _PyEval_FormatExcCheckArg(PyThreadState *tstate, PyObject *exc, const char *format_str, PyObject *obj); |
| 254 |
PyAPI_FUNC(void) _PyEval_FormatExcUnbound(PyThreadState *tstate, PyCodeObject *co, int oparg); |
| 255 |
PyAPI_FUNC(void) _PyEval_FormatKwargsError(PyThreadState *tstate, PyObject *func, PyObject *kwargs); |
| 256 |
PyAPI_FUNC(PyObject *)_PyEval_MatchClass(PyThreadState *tstate, PyObject *subject, PyObject *type, Py_ssize_t nargs, PyObject *kwargs); |
| 257 |
PyAPI_FUNC(PyObject *)_PyEval_MatchKeys(PyThreadState *tstate, PyObject *map, PyObject *keys); |
| 258 |
PyAPI_FUNC(int) _PyEval_UnpackIterable(PyThreadState *tstate, PyObject *v, int argcnt, int argcntafter, PyObject **sp); |
| 259 |
PyAPI_FUNC(void) _PyEval_MonitorRaise(PyThreadState *tstate, _PyInterpreterFrame *frame, _Py_CODEUNIT *instr); |
| 260 |
PyAPI_FUNC(bool) _PyEval_NoToolsForUnwind(PyThreadState *tstate); |
| 261 |
PyAPI_FUNC(void) _PyEval_FrameClearAndPop(PyThreadState *tstate, _PyInterpreterFrame *frame); |
| 262 |
|
| 263 |
|
| 264 |
/* Bits that can be set in PyThreadState.eval_breaker */ |
| 265 |
#define _PY_GIL_DROP_REQUEST_BIT (1U << 0) |
| 266 |
#define _PY_SIGNALS_PENDING_BIT (1U << 1) |
| 267 |
#define _PY_CALLS_TO_DO_BIT (1U << 2) |
| 268 |
#define _PY_ASYNC_EXCEPTION_BIT (1U << 3) |
| 269 |
#define _PY_GC_SCHEDULED_BIT (1U << 4) |
| 270 |
#define _PY_EVAL_PLEASE_STOP_BIT (1U << 5) |
| 271 |
#define _PY_EVAL_EXPLICIT_MERGE_BIT (1U << 6) |
| 272 |
|
| 273 |
/* Reserve a few bits for future use */ |
| 274 |
#define _PY_EVAL_EVENTS_BITS 8 |
| 275 |
#define _PY_EVAL_EVENTS_MASK ((1 << _PY_EVAL_EVENTS_BITS)-1) |
| 276 |
|
| 277 |
static inline void |
| 278 |
_Py_set_eval_breaker_bit(PyThreadState *tstate, uintptr_t bit) |
| 279 |
{ |
| 280 |
_Py_atomic_or_uintptr(&tstate->eval_breaker, bit); |
| 281 |
} |
| 282 |
|
| 283 |
static inline void |
| 284 |
_Py_unset_eval_breaker_bit(PyThreadState *tstate, uintptr_t bit) |
| 285 |
{ |
| 286 |
_Py_atomic_and_uintptr(&tstate->eval_breaker, ~bit); |
| 287 |
} |
| 288 |
|
| 289 |
static inline int |
| 290 |
_Py_eval_breaker_bit_is_set(PyThreadState *tstate, uintptr_t bit) |
| 291 |
{ |
| 292 |
uintptr_t b = _Py_atomic_load_uintptr_relaxed(&tstate->eval_breaker); |
| 293 |
return (b & bit) != 0; |
| 294 |
} |
| 295 |
|
| 296 |
// Free-threaded builds use these functions to set or unset a bit on all |
| 297 |
// threads in the given interpreter. |
| 298 |
void _Py_set_eval_breaker_bit_all(PyInterpreterState *interp, uintptr_t bit); |
| 299 |
void _Py_unset_eval_breaker_bit_all(PyInterpreterState *interp, uintptr_t bit); |
| 300 |
|
| 301 |
|
| 302 |
#ifdef __cplusplus |
| 303 |
} |
| 304 |
#endif |
| 305 |
#endif /* !Py_INTERNAL_CEVAL_H */ |