File: /Users/paulross/dev/Python-3.6.2/Include/pystate.h

Green shading in the line number column means the source is part of the translation unit, red means it is conditionally excluded. Highlighted line numbers link to the translation unit page. Highlighted macros link to the macro page.

       1: 
       2: /* Thread and interpreter state structures and their interfaces */
       3: 
       4: 
       5: #ifndef Py_PYSTATE_H
       6: #define Py_PYSTATE_H
       7: #ifdef __cplusplus
       8: extern "C" {
       9: #endif
      10: 
      11: /* This limitation is for performance and simplicity. If needed it can be
      12: removed (with effort). */
      13: #define MAX_CO_EXTRA_USERS 255
      14: 
      15: /* State shared between threads */
      16: 
      17: struct _ts; /* Forward */
      18: struct _is; /* Forward */
      19: struct _frame; /* Forward declaration for PyFrameObject. */
      20: 
      21: #ifdef Py_LIMITED_API
      22: typedef struct _is PyInterpreterState;
      23: #else
      24: typedef PyObject* (*_PyFrameEvalFunction)(struct _frame *, int);
      25: 
      26: typedef struct _is {
      27: 
      28:     struct _is *next;
      29:     struct _ts *tstate_head;
      30: 
      31:     PyObject *modules;
      32:     PyObject *modules_by_index;
      33:     PyObject *sysdict;
      34:     PyObject *builtins;
      35:     PyObject *importlib;
      36: 
      37:     PyObject *codec_search_path;
      38:     PyObject *codec_search_cache;
      39:     PyObject *codec_error_registry;
      40:     int codecs_initialized;
      41:     int fscodec_initialized;
      42: 
      43: #ifdef HAVE_DLOPEN
      44:     int dlopenflags;
      45: #endif
      46: 
      47:     PyObject *builtins_copy;
      48:     PyObject *import_func;
      49:     /* Initialized to PyEval_EvalFrameDefault(). */
      50:     _PyFrameEvalFunction eval_frame;
      51: } PyInterpreterState;
      52: #endif
      53: 
      54: typedef struct _co_extra_state {
      55:     struct _co_extra_state *next;
      56:     PyInterpreterState* interp;
      57: 
      58:     Py_ssize_t co_extra_user_count;
      59:     freefunc co_extra_freefuncs[MAX_CO_EXTRA_USERS];
      60: } __PyCodeExtraState;
      61: 
      62: /* This is temporary for backwards compat in 3.6 and will be removed in 3.7 */
      63: __PyCodeExtraState* __PyCodeExtraState_Get(void);
      64: 
      65: /* State unique per thread */
      66: 
      67: #ifndef Py_LIMITED_API
      68: /* Py_tracefunc return -1 when raising an exception, or 0 for success. */
      69: typedef int (*Py_tracefunc)(PyObject *, struct _frame *, int, PyObject *);
      70: 
      71: /* The following values are used for 'what' for tracefunc functions: */
      72: #define PyTrace_CALL 0
      73: #define PyTrace_EXCEPTION 1
      74: #define PyTrace_LINE 2
      75: #define PyTrace_RETURN 3
      76: #define PyTrace_C_CALL 4
      77: #define PyTrace_C_EXCEPTION 5
      78: #define PyTrace_C_RETURN 6
      79: #endif
      80: 
      81: #ifdef Py_LIMITED_API
      82: typedef struct _ts PyThreadState;
      83: #else
      84: typedef struct _ts {
      85:     /* See Python/ceval.c for comments explaining most fields */
      86: 
      87:     struct _ts *prev;
      88:     struct _ts *next;
      89:     PyInterpreterState *interp;
      90: 
      91:     struct _frame *frame;
      92:     int recursion_depth;
      93:     char overflowed; /* The stack has overflowed. Allow 50 more calls
      94:                         to handle the runtime error. */
      95:     char recursion_critical; /* The current calls must not cause
      96:                                 a stack overflow. */
      97:     /* 'tracing' keeps track of the execution depth when tracing/profiling.
      98:        This is to prevent the actual trace/profile code from being recorded in
      99:        the trace/profile. */
     100:     int tracing;
     101:     int use_tracing;
     102: 
     103:     Py_tracefunc c_profilefunc;
     104:     Py_tracefunc c_tracefunc;
     105:     PyObject *c_profileobj;
     106:     PyObject *c_traceobj;
     107: 
     108:     PyObject *curexc_type;
     109:     PyObject *curexc_value;
     110:     PyObject *curexc_traceback;
     111: 
     112:     PyObject *exc_type;
     113:     PyObject *exc_value;
     114:     PyObject *exc_traceback;
     115: 
     116:     PyObject *dict;  /* Stores per-thread state */
     117: 
     118:     int gilstate_counter;
     119: 
     120:     PyObject *async_exc; /* Asynchronous exception to raise */
     121:     long thread_id; /* Thread id where this tstate was created */
     122: 
     123:     int trash_delete_nesting;
     124:     PyObject *trash_delete_later;
     125: 
     126:     /* Called when a thread state is deleted normally, but not when it
     127:      * is destroyed after fork().
     128:      * Pain:  to prevent rare but fatal shutdown errors (issue 18808),
     129:      * Thread.join() must wait for the join'ed thread's tstate to be unlinked
     130:      * from the tstate chain.  That happens at the end of a thread's life,
     131:      * in pystate.c.
     132:      * The obvious way doesn't quite work:  create a lock which the tstate
     133:      * unlinking code releases, and have Thread.join() wait to acquire that
     134:      * lock.  The problem is that we _are_ at the end of the thread's life:
     135:      * if the thread holds the last reference to the lock, decref'ing the
     136:      * lock will delete the lock, and that may trigger arbitrary Python code
     137:      * if there's a weakref, with a callback, to the lock.  But by this time
     138:      * _PyThreadState_Current is already NULL, so only the simplest of C code
     139:      * can be allowed to run (in particular it must not be possible to
     140:      * release the GIL).
     141:      * So instead of holding the lock directly, the tstate holds a weakref to
     142:      * the lock:  that's the value of on_delete_data below.  Decref'ing a
     143:      * weakref is harmless.
     144:      * on_delete points to _threadmodule.c's static release_sentinel() function.
     145:      * After the tstate is unlinked, release_sentinel is called with the
     146:      * weakref-to-lock (on_delete_data) argument, and release_sentinel releases
     147:      * the indirectly held lock.
     148:      */
     149:     void (*on_delete)(void *);
     150:     void *on_delete_data;
     151: 
     152:     PyObject *coroutine_wrapper;
     153:     int in_coroutine_wrapper;
     154: 
     155:     /* Now used from PyInterpreterState, kept here for ABI
     156:        compatibility with PyThreadState */
     157:     Py_ssize_t _preserve_36_ABI_1;
     158:     freefunc _preserve_36_ABI_2[MAX_CO_EXTRA_USERS];
     159: 
     160:     PyObject *async_gen_firstiter;
     161:     PyObject *async_gen_finalizer;
     162: 
     163:     /* XXX signal handlers should also be here */
     164: 
     165: } PyThreadState;
     166: #endif
     167: 
     168: 
     169: PyAPI_FUNC(PyInterpreterState *) PyInterpreterState_New(void);
     170: PyAPI_FUNC(void) PyInterpreterState_Clear(PyInterpreterState *);
     171: PyAPI_FUNC(void) PyInterpreterState_Delete(PyInterpreterState *);
     172: #ifndef Py_LIMITED_API
     173: PyAPI_FUNC(int) _PyState_AddModule(PyObject*, struct PyModuleDef*);
     174: #endif /* !Py_LIMITED_API */
     175: #if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x03030000
     176: /* New in 3.3 */
     177: PyAPI_FUNC(int) PyState_AddModule(PyObject*, struct PyModuleDef*);
     178: PyAPI_FUNC(int) PyState_RemoveModule(struct PyModuleDef*);
     179: #endif
     180: PyAPI_FUNC(PyObject*) PyState_FindModule(struct PyModuleDef*);
     181: #ifndef Py_LIMITED_API
     182: PyAPI_FUNC(void) _PyState_ClearModules(void);
     183: #endif
     184: 
     185: PyAPI_FUNC(PyThreadState *) PyThreadState_New(PyInterpreterState *);
     186: #ifndef Py_LIMITED_API
     187: PyAPI_FUNC(PyThreadState *) _PyThreadState_Prealloc(PyInterpreterState *);
     188: PyAPI_FUNC(void) _PyThreadState_Init(PyThreadState *);
     189: #endif /* !Py_LIMITED_API */
     190: PyAPI_FUNC(void) PyThreadState_Clear(PyThreadState *);
     191: PyAPI_FUNC(void) PyThreadState_Delete(PyThreadState *);
     192: #ifndef Py_LIMITED_API
     193: PyAPI_FUNC(void) _PyThreadState_DeleteExcept(PyThreadState *tstate);
     194: #endif /* !Py_LIMITED_API */
     195: #ifdef WITH_THREAD
     196: PyAPI_FUNC(void) PyThreadState_DeleteCurrent(void);
     197: #ifndef Py_LIMITED_API
     198: PyAPI_FUNC(void) _PyGILState_Reinit(void);
     199: #endif /* !Py_LIMITED_API */
     200: #endif
     201: 
     202: /* Return the current thread state. The global interpreter lock must be held.
     203:  * When the current thread state is NULL, this issues a fatal error (so that
     204:  * the caller needn't check for NULL). */
     205: PyAPI_FUNC(PyThreadState *) PyThreadState_Get(void);
     206: 
     207: #ifndef Py_LIMITED_API
     208: /* Similar to PyThreadState_Get(), but don't issue a fatal error
     209:  * if it is NULL. */
     210: PyAPI_FUNC(PyThreadState *) _PyThreadState_UncheckedGet(void);
     211: #endif /* !Py_LIMITED_API */
     212: 
     213: PyAPI_FUNC(PyThreadState *) PyThreadState_Swap(PyThreadState *);
     214: PyAPI_FUNC(PyObject *) PyThreadState_GetDict(void);
     215: PyAPI_FUNC(int) PyThreadState_SetAsyncExc(long, PyObject *);
     216: 
     217: 
     218: /* Variable and macro for in-line access to current thread state */
     219: 
     220: /* Assuming the current thread holds the GIL, this is the
     221:    PyThreadState for the current thread. */
     222: #ifdef Py_BUILD_CORE
     223: PyAPI_DATA(_Py_atomic_address) _PyThreadState_Current;
     224: #  define PyThreadState_GET() \
     225:              ((PyThreadState*)_Py_atomic_load_relaxed(&_PyThreadState_Current))
     226: #else
     227: #  define PyThreadState_GET() PyThreadState_Get()
     228: #endif
     229: 
     230: typedef
     231:     enum {PyGILState_LOCKED, PyGILState_UNLOCKED}
     232:         PyGILState_STATE;
     233: 
     234: #ifdef WITH_THREAD
     235: 
     236: /* Ensure that the current thread is ready to call the Python
     237:    C API, regardless of the current state of Python, or of its
     238:    thread lock.  This may be called as many times as desired
     239:    by a thread so long as each call is matched with a call to
     240:    PyGILState_Release().  In general, other thread-state APIs may
     241:    be used between _Ensure() and _Release() calls, so long as the
     242:    thread-state is restored to its previous state before the Release().
     243:    For example, normal use of the Py_BEGIN_ALLOW_THREADS/
     244:    Py_END_ALLOW_THREADS macros are acceptable.
     245: 
     246:    The return value is an opaque "handle" to the thread state when
     247:    PyGILState_Ensure() was called, and must be passed to
     248:    PyGILState_Release() to ensure Python is left in the same state. Even
     249:    though recursive calls are allowed, these handles can *not* be shared -
     250:    each unique call to PyGILState_Ensure must save the handle for its
     251:    call to PyGILState_Release.
     252: 
     253:    When the function returns, the current thread will hold the GIL.
     254: 
     255:    Failure is a fatal error.
     256: */
     257: PyAPI_FUNC(PyGILState_STATE) PyGILState_Ensure(void);
     258: 
     259: /* Release any resources previously acquired.  After this call, Python's
     260:    state will be the same as it was prior to the corresponding
     261:    PyGILState_Ensure() call (but generally this state will be unknown to
     262:    the caller, hence the use of the GILState API.)
     263: 
     264:    Every call to PyGILState_Ensure must be matched by a call to
     265:    PyGILState_Release on the same thread.
     266: */
     267: PyAPI_FUNC(void) PyGILState_Release(PyGILState_STATE);
     268: 
     269: /* Helper/diagnostic function - get the current thread state for
     270:    this thread.  May return NULL if no GILState API has been used
     271:    on the current thread.  Note that the main thread always has such a
     272:    thread-state, even if no auto-thread-state call has been made
     273:    on the main thread.
     274: */
     275: PyAPI_FUNC(PyThreadState *) PyGILState_GetThisThreadState(void);
     276: 
     277: #ifndef Py_LIMITED_API
     278: /* Issue #26558: Flag to disable PyGILState_Check().
     279:    If set to non-zero, PyGILState_Check() always return 1. */
     280: PyAPI_DATA(int) _PyGILState_check_enabled;
     281: 
     282: /* Helper/diagnostic function - return 1 if the current thread
     283:    currently holds the GIL, 0 otherwise.
     284: 
     285:    The function returns 1 if _PyGILState_check_enabled is non-zero. */
     286: PyAPI_FUNC(int) PyGILState_Check(void);
     287: 
     288: /* Unsafe function to get the single PyInterpreterState used by this process'
     289:    GILState implementation.
     290: 
     291:    Return NULL before _PyGILState_Init() is called and after _PyGILState_Fini()
     292:    is called. */
     293: PyAPI_FUNC(PyInterpreterState *) _PyGILState_GetInterpreterStateUnsafe(void);
     294: #endif
     295: 
     296: #endif   /* #ifdef WITH_THREAD */
     297: 
     298: /* The implementation of sys._current_frames()  Returns a dict mapping
     299:    thread id to that thread's current frame.
     300: */
     301: #ifndef Py_LIMITED_API
     302: PyAPI_FUNC(PyObject *) _PyThread_CurrentFrames(void);
     303: #endif
     304: 
     305: /* Routines for advanced debuggers, requested by David Beazley.
     306:    Don't use unless you know what you are doing! */
     307: #ifndef Py_LIMITED_API
     308: PyAPI_FUNC(PyInterpreterState *) PyInterpreterState_Head(void);
     309: PyAPI_FUNC(PyInterpreterState *) PyInterpreterState_Next(PyInterpreterState *);
     310: PyAPI_FUNC(PyThreadState *) PyInterpreterState_ThreadHead(PyInterpreterState *);
     311: PyAPI_FUNC(PyThreadState *) PyThreadState_Next(PyThreadState *);
     312: 
     313: typedef struct _frame *(*PyThreadFrameGetter)(PyThreadState *self_);
     314: #endif
     315: 
     316: /* hook for PyEval_GetFrame(), requested for Psyco */
     317: #ifndef Py_LIMITED_API
     318: PyAPI_DATA(PyThreadFrameGetter) _PyThreadState_GetFrame;
     319: #endif
     320: 
     321: #ifdef __cplusplus
     322: }
     323: #endif
     324: #endif /* !Py_PYSTATE_H */
     325: