Back to index

python3.2  3.2.2
Defines | Functions | Variables
pystate.c File Reference
#include "Python.h"

Go to the source code of this file.

Defines

#define HEAD_INIT()   /* Nothing */
#define HEAD_LOCK()   /* Nothing */
#define HEAD_UNLOCK()   /* Nothing */

Functions

PyInterpreterStatePyInterpreterState_New (void)
void PyInterpreterState_Clear (PyInterpreterState *interp)
static void zapthreads (PyInterpreterState *interp)
void PyInterpreterState_Delete (PyInterpreterState *interp)
static struct _framethreadstate_getframe (PyThreadState *self)
static PyThreadStatenew_threadstate (PyInterpreterState *interp, int init)
PyThreadStatePyThreadState_New (PyInterpreterState *interp)
PyThreadState_PyThreadState_Prealloc (PyInterpreterState *interp)
void _PyThreadState_Init (PyThreadState *tstate)
PyObjectPyState_FindModule (struct PyModuleDef *m)
int _PyState_AddModule (PyObject *module, struct PyModuleDef *def)
void PyThreadState_Clear (PyThreadState *tstate)
static void tstate_delete_common (PyThreadState *tstate)
void PyThreadState_Delete (PyThreadState *tstate)
PyThreadStatePyThreadState_Get (void)
PyThreadStatePyThreadState_Swap (PyThreadState *newts)
PyObjectPyThreadState_GetDict (void)
int PyThreadState_SetAsyncExc (long id, PyObject *exc)
PyInterpreterStatePyInterpreterState_Head (void)
PyInterpreterStatePyInterpreterState_Next (PyInterpreterState *interp)
PyThreadStatePyInterpreterState_ThreadHead (PyInterpreterState *interp)
PyThreadStatePyThreadState_Next (PyThreadState *tstate)
PyObject_PyThread_CurrentFrames (void)

Variables

static PyInterpreterStateinterp_head = NULL
_Py_atomic_address _PyThreadState_Current = {NULL}
PyThreadFrameGetter _PyThreadState_GetFrame = NULL

Define Documentation

#define HEAD_INIT ( )    /* Nothing */

Definition at line 43 of file pystate.c.

#define HEAD_LOCK ( )    /* Nothing */

Definition at line 44 of file pystate.c.

#define HEAD_UNLOCK ( )    /* Nothing */

Definition at line 45 of file pystate.c.


Function Documentation

int _PyState_AddModule ( PyObject module,
struct PyModuleDef def 
)

Definition at line 253 of file pystate.c.

{
    PyInterpreterState *state = PyThreadState_GET()->interp;
    if (!def)
        return -1;
    if (!state->modules_by_index) {
        state->modules_by_index = PyList_New(0);
        if (!state->modules_by_index)
            return -1;
    }
    while(PyList_GET_SIZE(state->modules_by_index) <= def->m_base.m_index)
        if (PyList_Append(state->modules_by_index, Py_None) < 0)
            return -1;
    Py_INCREF(module);
    return PyList_SetItem(state->modules_by_index,
                          def->m_base.m_index, module);
}

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 504 of file pystate.c.

{
    PyObject *result;
    PyInterpreterState *i;

    result = PyDict_New();
    if (result == NULL)
        return NULL;

    /* for i in all interpreters:
     *     for t in all of i's thread states:
     *          if t's frame isn't NULL, map t's id to its frame
     * Because these lists can mutate even when the GIL is held, we
     * need to grab head_mutex for the duration.
     */
    HEAD_LOCK();
    for (i = interp_head; i != NULL; i = i->next) {
        PyThreadState *t;
        for (t = i->tstate_head; t != NULL; t = t->next) {
            PyObject *id;
            int stat;
            struct _frame *frame = t->frame;
            if (frame == NULL)
                continue;
            id = PyLong_FromLong(t->thread_id);
            if (id == NULL)
                goto Fail;
            stat = PyDict_SetItem(result, id, (PyObject *)frame);
            Py_DECREF(id);
            if (stat < 0)
                goto Fail;
        }
    }
    HEAD_UNLOCK();
    return result;

 Fail:
    HEAD_UNLOCK();
    Py_DECREF(result);
    return NULL;
}

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 229 of file pystate.c.

{
#ifdef WITH_THREAD
    _PyGILState_NoteThreadState(tstate);
#endif
}

Here is the caller graph for this function:

Definition at line 223 of file pystate.c.

{
    return new_threadstate(interp, 0);
}

Here is the call graph for this function:

Here is the caller graph for this function:

static PyThreadState* new_threadstate ( PyInterpreterState interp,
int  init 
) [static]

Definition at line 164 of file pystate.c.

{
    PyThreadState *tstate = (PyThreadState *)malloc(sizeof(PyThreadState));

    if (_PyThreadState_GetFrame == NULL)
        _PyThreadState_GetFrame = threadstate_getframe;

    if (tstate != NULL) {
        tstate->interp = interp;

        tstate->frame = NULL;
        tstate->recursion_depth = 0;
        tstate->overflowed = 0;
        tstate->recursion_critical = 0;
        tstate->tracing = 0;
        tstate->use_tracing = 0;
        tstate->tick_counter = 0;
        tstate->gilstate_counter = 0;
        tstate->async_exc = NULL;
#ifdef WITH_THREAD
        tstate->thread_id = PyThread_get_thread_ident();
#else
        tstate->thread_id = 0;
#endif

        tstate->dict = NULL;

        tstate->curexc_type = NULL;
        tstate->curexc_value = NULL;
        tstate->curexc_traceback = NULL;

        tstate->exc_type = NULL;
        tstate->exc_value = NULL;
        tstate->exc_traceback = NULL;

        tstate->c_profilefunc = NULL;
        tstate->c_tracefunc = NULL;
        tstate->c_profileobj = NULL;
        tstate->c_traceobj = NULL;

        if (init)
            _PyThreadState_Init(tstate);

        HEAD_LOCK();
        tstate->next = interp->tstate_head;
        interp->tstate_head = tstate;
        HEAD_UNLOCK();
    }

    return tstate;
}

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 105 of file pystate.c.

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 136 of file pystate.c.

{
    PyInterpreterState **p;
    zapthreads(interp);
    HEAD_LOCK();
    for (p = &interp_head; ; p = &(*p)->next) {
        if (*p == NULL)
            Py_FatalError(
                "PyInterpreterState_Delete: invalid interp");
        if (*p == interp)
            break;
    }
    if (interp->tstate_head != NULL)
        Py_FatalError("PyInterpreterState_Delete: remaining threads");
    *p = interp->next;
    HEAD_UNLOCK();
    free(interp);
}

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 478 of file pystate.c.

{
    return interp_head;
}

Definition at line 61 of file pystate.c.

{
    PyInterpreterState *interp = (PyInterpreterState *)
                                 malloc(sizeof(PyInterpreterState));

    if (interp != NULL) {
        HEAD_INIT();
#ifdef WITH_THREAD
        if (head_mutex == NULL)
            Py_FatalError("Can't initialize threads for interpreter");
#endif
        interp->modules = NULL;
        interp->modules_reloading = NULL;
        interp->modules_by_index = NULL;
        interp->sysdict = NULL;
        interp->builtins = NULL;
        interp->tstate_head = NULL;
        interp->codec_search_path = NULL;
        interp->codec_search_cache = NULL;
        interp->codec_error_registry = NULL;
        interp->codecs_initialized = 0;
        interp->fscodec_initialized = 0;
#ifdef HAVE_DLOPEN
#ifdef RTLD_NOW
        interp->dlopenflags = RTLD_NOW;
#else
        interp->dlopenflags = RTLD_LAZY;
#endif
#endif
#ifdef WITH_TSC
        interp->tscdump = 0;
#endif

        HEAD_LOCK();
        interp->next = interp_head;
        interp_head = interp;
        HEAD_UNLOCK();
    }

    return interp;
}

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 484 of file pystate.c.

                                                    {
    return interp->next;
}

Definition at line 489 of file pystate.c.

                                                          {
    return interp->tstate_head;
}

Here is the caller graph for this function:

Definition at line 237 of file pystate.c.

{
    Py_ssize_t index = m->m_base.m_index;
    PyInterpreterState *state = PyThreadState_GET()->interp;
    PyObject *res;
    if (index == 0)
        return NULL;
    if (state->modules_by_index == NULL)
        return NULL;
    if (index > PyList_GET_SIZE(state->modules_by_index))
        return NULL;
    res = PyList_GET_ITEM(state->modules_by_index, index);
    return res==Py_None ? NULL : res;
}

Here is the caller graph for this function:

Definition at line 272 of file pystate.c.

{
    if (Py_VerboseFlag && tstate->frame != NULL)
        fprintf(stderr,
          "PyThreadState_Clear: warning: thread still has a frame\n");

    Py_CLEAR(tstate->frame);

    Py_CLEAR(tstate->dict);
    Py_CLEAR(tstate->async_exc);

    Py_CLEAR(tstate->curexc_type);
    Py_CLEAR(tstate->curexc_value);
    Py_CLEAR(tstate->curexc_traceback);

    Py_CLEAR(tstate->exc_type);
    Py_CLEAR(tstate->exc_value);
    Py_CLEAR(tstate->exc_traceback);

    tstate->c_profilefunc = NULL;
    tstate->c_tracefunc = NULL;
    Py_CLEAR(tstate->c_profileobj);
    Py_CLEAR(tstate->c_traceobj);
}

Here is the caller graph for this function:

Definition at line 338 of file pystate.c.

{
    if (tstate == _Py_atomic_load_relaxed(&_PyThreadState_Current))
        Py_FatalError("PyThreadState_Delete: tstate is still current");
    tstate_delete_common(tstate);
#ifdef WITH_THREAD
    if (autoInterpreterState && PyThread_get_key_value(autoTLSkey) == tstate)
        PyThread_delete_key_value(autoTLSkey);
#endif /* WITH_THREAD */
}

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 369 of file pystate.c.

{
    PyThreadState *tstate = (PyThreadState*)_Py_atomic_load_relaxed(
        &_PyThreadState_Current);
    if (tstate == NULL)
        Py_FatalError("PyThreadState_Get: no current thread");

    return tstate;
}

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 413 of file pystate.c.

{
    PyThreadState *tstate = (PyThreadState*)_Py_atomic_load_relaxed(
        &_PyThreadState_Current);
    if (tstate == NULL)
        return NULL;

    if (tstate->dict == NULL) {
        PyObject *d;
        tstate->dict = d = PyDict_New();
        if (d == NULL)
            PyErr_Clear();
    }
    return tstate->dict;
}

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 217 of file pystate.c.

{
    return new_threadstate(interp, 1);
}

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 494 of file pystate.c.

                                          {
    return tstate->next;
}

Here is the caller graph for this function:

Definition at line 439 of file pystate.c.

                                                  {
    PyThreadState *tstate = PyThreadState_GET();
    PyInterpreterState *interp = tstate->interp;
    PyThreadState *p;

    /* Although the GIL is held, a few C API functions can be called
     * without the GIL held, and in particular some that create and
     * destroy thread and interpreter states.  Those can mutate the
     * list of thread states we're traversing, so to prevent that we lock
     * head_mutex for the duration.
     */
    HEAD_LOCK();
    for (p = interp->tstate_head; p != NULL; p = p->next) {
        if (p->thread_id == id) {
            /* Tricky:  we need to decref the current value
             * (if any) in p->async_exc, but that can in turn
             * allow arbitrary Python code to run, including
             * perhaps calls to this function.  To prevent
             * deadlock, we need to release head_mutex before
             * the decref.
             */
            PyObject *old_exc = p->async_exc;
            Py_XINCREF(exc);
            p->async_exc = exc;
            HEAD_UNLOCK();
            Py_XDECREF(old_exc);
            _PyEval_SignalAsyncExc();
            return 1;
        }
    }
    HEAD_UNLOCK();
    return 0;
}

Here is the call graph for this function:

Definition at line 381 of file pystate.c.

{
    PyThreadState *oldts = (PyThreadState*)_Py_atomic_load_relaxed(
        &_PyThreadState_Current);

    _Py_atomic_store_relaxed(&_PyThreadState_Current, newts);
    /* It should not be possible for more than one thread state
       to be used for a thread.  Check this the best we can in debug
       builds.
    */
#if defined(Py_DEBUG) && defined(WITH_THREAD)
    if (newts) {
        /* This can be called from PyEval_RestoreThread(). Similar
           to it, we need to ensure errno doesn't change.
        */
        int err = errno;
        PyThreadState *check = PyGILState_GetThisThreadState();
        if (check && check->interp == newts->interp && check != newts)
            Py_FatalError("Invalid thread state for this thread");
        errno = err;
    }
#endif
    return oldts;
}

Here is the call graph for this function:

Here is the caller graph for this function:

static struct _frame* threadstate_getframe ( PyThreadState self) [static, read]

Definition at line 158 of file pystate.c.

{
    return self->frame;
}

Here is the caller graph for this function:

static void tstate_delete_common ( PyThreadState tstate) [static]

Definition at line 300 of file pystate.c.

{
    PyInterpreterState *interp;
    PyThreadState **p;
    PyThreadState *prev_p = NULL;
    if (tstate == NULL)
        Py_FatalError("PyThreadState_Delete: NULL tstate");
    interp = tstate->interp;
    if (interp == NULL)
        Py_FatalError("PyThreadState_Delete: NULL interp");
    HEAD_LOCK();
    for (p = &interp->tstate_head; ; p = &(*p)->next) {
        if (*p == NULL)
            Py_FatalError(
                "PyThreadState_Delete: invalid tstate");
        if (*p == tstate)
            break;
        /* Sanity check.  These states should never happen but if
         * they do we must abort.  Otherwise we'll end up spinning in
         * in a tight loop with the lock held.  A similar check is done
         * in thread.c find_key().  */
        if (*p == prev_p)
            Py_FatalError(
                "PyThreadState_Delete: small circular list(!)"
                " and tstate not found.");
        prev_p = *p;
        if ((*p)->next == interp->tstate_head)
            Py_FatalError(
                "PyThreadState_Delete: circular list(!) and"
                " tstate not found.");
    }
    *p = tstate->next;
    HEAD_UNLOCK();
    free(tstate);
}

Here is the call graph for this function:

Here is the caller graph for this function:

static void zapthreads ( PyInterpreterState interp) [static]

Definition at line 124 of file pystate.c.

{
    PyThreadState *p;
    /* No need to lock the mutex here because this should only happen
       when the threads are all really dead (XXX famous last words). */
    while ((p = interp->tstate_head) != NULL) {
        PyThreadState_Delete(p);
    }
}

Here is the call graph for this function:

Here is the caller graph for this function:


Variable Documentation

Definition at line 52 of file pystate.c.

Definition at line 53 of file pystate.c.

Definition at line 48 of file pystate.c.