Home | History | Annotate | Download | only in Python
      1 
      2 /* Thread and interpreter state structures and their interfaces */
      3 
      4 #include "Python.h"
      5 
      6 #define GET_TSTATE() \
      7     ((PyThreadState*)_Py_atomic_load_relaxed(&_PyThreadState_Current))
      8 #define SET_TSTATE(value) \
      9     _Py_atomic_store_relaxed(&_PyThreadState_Current, (uintptr_t)(value))
     10 #define GET_INTERP_STATE() \
     11     (GET_TSTATE()->interp)
     12 
     13 
     14 /* --------------------------------------------------------------------------
     15 CAUTION
     16 
     17 Always use PyMem_RawMalloc() and PyMem_RawFree() directly in this file.  A
     18 number of these functions are advertised as safe to call when the GIL isn't
     19 held, and in a debug build Python redirects (e.g.) PyMem_NEW (etc) to Python's
     20 debugging obmalloc functions.  Those aren't thread-safe (they rely on the GIL
     21 to avoid the expense of doing their own locking).
     22 -------------------------------------------------------------------------- */
     23 
     24 #ifdef HAVE_DLOPEN
     25 #ifdef HAVE_DLFCN_H
     26 #include <dlfcn.h>
     27 #endif
     28 #if !HAVE_DECL_RTLD_LAZY
     29 #define RTLD_LAZY 1
     30 #endif
     31 #endif
     32 
     33 #ifdef __cplusplus
     34 extern "C" {
     35 #endif
     36 
     37 int _PyGILState_check_enabled = 1;
     38 
     39 #ifdef WITH_THREAD
     40 #include "pythread.h"
     41 static PyThread_type_lock head_mutex = NULL; /* Protects interp->tstate_head */
     42 #define HEAD_INIT() (void)(head_mutex || (head_mutex = PyThread_allocate_lock()))
     43 #define HEAD_LOCK() PyThread_acquire_lock(head_mutex, WAIT_LOCK)
     44 #define HEAD_UNLOCK() PyThread_release_lock(head_mutex)
     45 
     46 /* The single PyInterpreterState used by this process'
     47    GILState implementation
     48 */
     49 static PyInterpreterState *autoInterpreterState = NULL;
     50 static int autoTLSkey = -1;
     51 #else
     52 #define HEAD_INIT() /* Nothing */
     53 #define HEAD_LOCK() /* Nothing */
     54 #define HEAD_UNLOCK() /* Nothing */
     55 #endif
     56 
     57 static PyInterpreterState *interp_head = NULL;
     58 
     59 /* Assuming the current thread holds the GIL, this is the
     60    PyThreadState for the current thread. */
     61 _Py_atomic_address _PyThreadState_Current = {0};
     62 PyThreadFrameGetter _PyThreadState_GetFrame = NULL;
     63 
     64 #ifdef WITH_THREAD
     65 static void _PyGILState_NoteThreadState(PyThreadState* tstate);
     66 #endif
     67 
     68 
     69 PyInterpreterState *
     70 PyInterpreterState_New(void)
     71 {
     72     PyInterpreterState *interp = (PyInterpreterState *)
     73                                  PyMem_RawMalloc(sizeof(PyInterpreterState));
     74 
     75     if (interp != NULL) {
     76         HEAD_INIT();
     77 #ifdef WITH_THREAD
     78         if (head_mutex == NULL)
     79             Py_FatalError("Can't initialize threads for interpreter");
     80 #endif
     81         interp->modules = NULL;
     82         interp->modules_by_index = NULL;
     83         interp->sysdict = NULL;
     84         interp->builtins = NULL;
     85         interp->builtins_copy = NULL;
     86         interp->tstate_head = NULL;
     87         interp->codec_search_path = NULL;
     88         interp->codec_search_cache = NULL;
     89         interp->codec_error_registry = NULL;
     90         interp->codecs_initialized = 0;
     91         interp->fscodec_initialized = 0;
     92         interp->importlib = NULL;
     93         interp->import_func = NULL;
     94         interp->eval_frame = _PyEval_EvalFrameDefault;
     95 #ifdef HAVE_DLOPEN
     96 #if HAVE_DECL_RTLD_NOW
     97         interp->dlopenflags = RTLD_NOW;
     98 #else
     99         interp->dlopenflags = RTLD_LAZY;
    100 #endif
    101 #endif
    102 
    103         HEAD_LOCK();
    104         interp->next = interp_head;
    105         interp_head = interp;
    106         HEAD_UNLOCK();
    107     }
    108 
    109     return interp;
    110 }
    111 
    112 
    113 void
    114 PyInterpreterState_Clear(PyInterpreterState *interp)
    115 {
    116     PyThreadState *p;
    117     HEAD_LOCK();
    118     for (p = interp->tstate_head; p != NULL; p = p->next)
    119         PyThreadState_Clear(p);
    120     HEAD_UNLOCK();
    121     Py_CLEAR(interp->codec_search_path);
    122     Py_CLEAR(interp->codec_search_cache);
    123     Py_CLEAR(interp->codec_error_registry);
    124     Py_CLEAR(interp->modules);
    125     Py_CLEAR(interp->modules_by_index);
    126     Py_CLEAR(interp->sysdict);
    127     Py_CLEAR(interp->builtins);
    128     Py_CLEAR(interp->builtins_copy);
    129     Py_CLEAR(interp->importlib);
    130     Py_CLEAR(interp->import_func);
    131 }
    132 
    133 
    134 static void
    135 zapthreads(PyInterpreterState *interp)
    136 {
    137     PyThreadState *p;
    138     /* No need to lock the mutex here because this should only happen
    139        when the threads are all really dead (XXX famous last words). */
    140     while ((p = interp->tstate_head) != NULL) {
    141         PyThreadState_Delete(p);
    142     }
    143 }
    144 
    145 
    146 void
    147 PyInterpreterState_Delete(PyInterpreterState *interp)
    148 {
    149     PyInterpreterState **p;
    150     zapthreads(interp);
    151     HEAD_LOCK();
    152     for (p = &interp_head; ; p = &(*p)->next) {
    153         if (*p == NULL)
    154             Py_FatalError(
    155                 "PyInterpreterState_Delete: invalid interp");
    156         if (*p == interp)
    157             break;
    158     }
    159     if (interp->tstate_head != NULL)
    160         Py_FatalError("PyInterpreterState_Delete: remaining threads");
    161     *p = interp->next;
    162     HEAD_UNLOCK();
    163     PyMem_RawFree(interp);
    164 #ifdef WITH_THREAD
    165     if (interp_head == NULL && head_mutex != NULL) {
    166         PyThread_free_lock(head_mutex);
    167         head_mutex = NULL;
    168     }
    169 #endif
    170 }
    171 
    172 
    173 /* Default implementation for _PyThreadState_GetFrame */
    174 static struct _frame *
    175 threadstate_getframe(PyThreadState *self)
    176 {
    177     return self->frame;
    178 }
    179 
    180 static PyThreadState *
    181 new_threadstate(PyInterpreterState *interp, int init)
    182 {
    183     PyThreadState *tstate = (PyThreadState *)PyMem_RawMalloc(sizeof(PyThreadState));
    184 
    185     if (_PyThreadState_GetFrame == NULL)
    186         _PyThreadState_GetFrame = threadstate_getframe;
    187 
    188     if (tstate != NULL) {
    189         tstate->interp = interp;
    190 
    191         tstate->frame = NULL;
    192         tstate->recursion_depth = 0;
    193         tstate->overflowed = 0;
    194         tstate->recursion_critical = 0;
    195         tstate->tracing = 0;
    196         tstate->use_tracing = 0;
    197         tstate->gilstate_counter = 0;
    198         tstate->async_exc = NULL;
    199 #ifdef WITH_THREAD
    200         tstate->thread_id = PyThread_get_thread_ident();
    201 #else
    202         tstate->thread_id = 0;
    203 #endif
    204 
    205         tstate->dict = NULL;
    206 
    207         tstate->curexc_type = NULL;
    208         tstate->curexc_value = NULL;
    209         tstate->curexc_traceback = NULL;
    210 
    211         tstate->exc_type = NULL;
    212         tstate->exc_value = NULL;
    213         tstate->exc_traceback = NULL;
    214 
    215         tstate->c_profilefunc = NULL;
    216         tstate->c_tracefunc = NULL;
    217         tstate->c_profileobj = NULL;
    218         tstate->c_traceobj = NULL;
    219 
    220         tstate->trash_delete_nesting = 0;
    221         tstate->trash_delete_later = NULL;
    222         tstate->on_delete = NULL;
    223         tstate->on_delete_data = NULL;
    224 
    225         tstate->coroutine_wrapper = NULL;
    226         tstate->in_coroutine_wrapper = 0;
    227         tstate->co_extra_user_count = 0;
    228 
    229         tstate->async_gen_firstiter = NULL;
    230         tstate->async_gen_finalizer = NULL;
    231 
    232         if (init)
    233             _PyThreadState_Init(tstate);
    234 
    235         HEAD_LOCK();
    236         tstate->prev = NULL;
    237         tstate->next = interp->tstate_head;
    238         if (tstate->next)
    239             tstate->next->prev = tstate;
    240         interp->tstate_head = tstate;
    241         HEAD_UNLOCK();
    242     }
    243 
    244     return tstate;
    245 }
    246 
    247 PyThreadState *
    248 PyThreadState_New(PyInterpreterState *interp)
    249 {
    250     return new_threadstate(interp, 1);
    251 }
    252 
    253 PyThreadState *
    254 _PyThreadState_Prealloc(PyInterpreterState *interp)
    255 {
    256     return new_threadstate(interp, 0);
    257 }
    258 
    259 void
    260 _PyThreadState_Init(PyThreadState *tstate)
    261 {
    262 #ifdef WITH_THREAD
    263     _PyGILState_NoteThreadState(tstate);
    264 #endif
    265 }
    266 
    267 PyObject*
    268 PyState_FindModule(struct PyModuleDef* module)
    269 {
    270     Py_ssize_t index = module->m_base.m_index;
    271     PyInterpreterState *state = GET_INTERP_STATE();
    272     PyObject *res;
    273     if (module->m_slots) {
    274         return NULL;
    275     }
    276     if (index == 0)
    277         return NULL;
    278     if (state->modules_by_index == NULL)
    279         return NULL;
    280     if (index >= PyList_GET_SIZE(state->modules_by_index))
    281         return NULL;
    282     res = PyList_GET_ITEM(state->modules_by_index, index);
    283     return res==Py_None ? NULL : res;
    284 }
    285 
    286 int
    287 _PyState_AddModule(PyObject* module, struct PyModuleDef* def)
    288 {
    289     PyInterpreterState *state;
    290     if (!def) {
    291         assert(PyErr_Occurred());
    292         return -1;
    293     }
    294     if (def->m_slots) {
    295         PyErr_SetString(PyExc_SystemError,
    296                         "PyState_AddModule called on module with slots");
    297         return -1;
    298     }
    299     state = GET_INTERP_STATE();
    300     if (!state->modules_by_index) {
    301         state->modules_by_index = PyList_New(0);
    302         if (!state->modules_by_index)
    303             return -1;
    304     }
    305     while(PyList_GET_SIZE(state->modules_by_index) <= def->m_base.m_index)
    306         if (PyList_Append(state->modules_by_index, Py_None) < 0)
    307             return -1;
    308     Py_INCREF(module);
    309     return PyList_SetItem(state->modules_by_index,
    310                           def->m_base.m_index, module);
    311 }
    312 
    313 int
    314 PyState_AddModule(PyObject* module, struct PyModuleDef* def)
    315 {
    316     Py_ssize_t index;
    317     PyInterpreterState *state = GET_INTERP_STATE();
    318     if (!def) {
    319         Py_FatalError("PyState_AddModule: Module Definition is NULL");
    320         return -1;
    321     }
    322     index = def->m_base.m_index;
    323     if (state->modules_by_index) {
    324         if(PyList_GET_SIZE(state->modules_by_index) >= index) {
    325             if(module == PyList_GET_ITEM(state->modules_by_index, index)) {
    326                 Py_FatalError("PyState_AddModule: Module already added!");
    327                 return -1;
    328             }
    329         }
    330     }
    331     return _PyState_AddModule(module, def);
    332 }
    333 
    334 int
    335 PyState_RemoveModule(struct PyModuleDef* def)
    336 {
    337     PyInterpreterState *state;
    338     Py_ssize_t index = def->m_base.m_index;
    339     if (def->m_slots) {
    340         PyErr_SetString(PyExc_SystemError,
    341                         "PyState_RemoveModule called on module with slots");
    342         return -1;
    343     }
    344     state = GET_INTERP_STATE();
    345     if (index == 0) {
    346         Py_FatalError("PyState_RemoveModule: Module index invalid.");
    347         return -1;
    348     }
    349     if (state->modules_by_index == NULL) {
    350         Py_FatalError("PyState_RemoveModule: Interpreters module-list not acessible.");
    351         return -1;
    352     }
    353     if (index > PyList_GET_SIZE(state->modules_by_index)) {
    354         Py_FatalError("PyState_RemoveModule: Module index out of bounds.");
    355         return -1;
    356     }
    357     return PyList_SetItem(state->modules_by_index, index, Py_None);
    358 }
    359 
    360 /* used by import.c:PyImport_Cleanup */
    361 void
    362 _PyState_ClearModules(void)
    363 {
    364     PyInterpreterState *state = GET_INTERP_STATE();
    365     if (state->modules_by_index) {
    366         Py_ssize_t i;
    367         for (i = 0; i < PyList_GET_SIZE(state->modules_by_index); i++) {
    368             PyObject *m = PyList_GET_ITEM(state->modules_by_index, i);
    369             if (PyModule_Check(m)) {
    370                 /* cleanup the saved copy of module dicts */
    371                 PyModuleDef *md = PyModule_GetDef(m);
    372                 if (md)
    373                     Py_CLEAR(md->m_base.m_copy);
    374             }
    375         }
    376         /* Setting modules_by_index to NULL could be dangerous, so we
    377            clear the list instead. */
    378         if (PyList_SetSlice(state->modules_by_index,
    379                             0, PyList_GET_SIZE(state->modules_by_index),
    380                             NULL))
    381             PyErr_WriteUnraisable(state->modules_by_index);
    382     }
    383 }
    384 
    385 void
    386 PyThreadState_Clear(PyThreadState *tstate)
    387 {
    388     if (Py_VerboseFlag && tstate->frame != NULL)
    389         fprintf(stderr,
    390           "PyThreadState_Clear: warning: thread still has a frame\n");
    391 
    392     Py_CLEAR(tstate->frame);
    393 
    394     Py_CLEAR(tstate->dict);
    395     Py_CLEAR(tstate->async_exc);
    396 
    397     Py_CLEAR(tstate->curexc_type);
    398     Py_CLEAR(tstate->curexc_value);
    399     Py_CLEAR(tstate->curexc_traceback);
    400 
    401     Py_CLEAR(tstate->exc_type);
    402     Py_CLEAR(tstate->exc_value);
    403     Py_CLEAR(tstate->exc_traceback);
    404 
    405     tstate->c_profilefunc = NULL;
    406     tstate->c_tracefunc = NULL;
    407     Py_CLEAR(tstate->c_profileobj);
    408     Py_CLEAR(tstate->c_traceobj);
    409 
    410     Py_CLEAR(tstate->coroutine_wrapper);
    411     Py_CLEAR(tstate->async_gen_firstiter);
    412     Py_CLEAR(tstate->async_gen_finalizer);
    413 }
    414 
    415 
    416 /* Common code for PyThreadState_Delete() and PyThreadState_DeleteCurrent() */
    417 static void
    418 tstate_delete_common(PyThreadState *tstate)
    419 {
    420     PyInterpreterState *interp;
    421     if (tstate == NULL)
    422         Py_FatalError("PyThreadState_Delete: NULL tstate");
    423     interp = tstate->interp;
    424     if (interp == NULL)
    425         Py_FatalError("PyThreadState_Delete: NULL interp");
    426     HEAD_LOCK();
    427     if (tstate->prev)
    428         tstate->prev->next = tstate->next;
    429     else
    430         interp->tstate_head = tstate->next;
    431     if (tstate->next)
    432         tstate->next->prev = tstate->prev;
    433     HEAD_UNLOCK();
    434     if (tstate->on_delete != NULL) {
    435         tstate->on_delete(tstate->on_delete_data);
    436     }
    437     PyMem_RawFree(tstate);
    438 }
    439 
    440 
    441 void
    442 PyThreadState_Delete(PyThreadState *tstate)
    443 {
    444     if (tstate == GET_TSTATE())
    445         Py_FatalError("PyThreadState_Delete: tstate is still current");
    446 #ifdef WITH_THREAD
    447     if (autoInterpreterState && PyThread_get_key_value(autoTLSkey) == tstate)
    448         PyThread_delete_key_value(autoTLSkey);
    449 #endif /* WITH_THREAD */
    450     tstate_delete_common(tstate);
    451 }
    452 
    453 
    454 #ifdef WITH_THREAD
    455 void
    456 PyThreadState_DeleteCurrent()
    457 {
    458     PyThreadState *tstate = GET_TSTATE();
    459     if (tstate == NULL)
    460         Py_FatalError(
    461             "PyThreadState_DeleteCurrent: no current tstate");
    462     tstate_delete_common(tstate);
    463     if (autoInterpreterState && PyThread_get_key_value(autoTLSkey) == tstate)
    464         PyThread_delete_key_value(autoTLSkey);
    465     SET_TSTATE(NULL);
    466     PyEval_ReleaseLock();
    467 }
    468 #endif /* WITH_THREAD */
    469 
    470 
    471 /*
    472  * Delete all thread states except the one passed as argument.
    473  * Note that, if there is a current thread state, it *must* be the one
    474  * passed as argument.  Also, this won't touch any other interpreters
    475  * than the current one, since we don't know which thread state should
    476  * be kept in those other interpreteres.
    477  */
    478 void
    479 _PyThreadState_DeleteExcept(PyThreadState *tstate)
    480 {
    481     PyInterpreterState *interp = tstate->interp;
    482     PyThreadState *p, *next, *garbage;
    483     HEAD_LOCK();
    484     /* Remove all thread states, except tstate, from the linked list of
    485        thread states.  This will allow calling PyThreadState_Clear()
    486        without holding the lock. */
    487     garbage = interp->tstate_head;
    488     if (garbage == tstate)
    489         garbage = tstate->next;
    490     if (tstate->prev)
    491         tstate->prev->next = tstate->next;
    492     if (tstate->next)
    493         tstate->next->prev = tstate->prev;
    494     tstate->prev = tstate->next = NULL;
    495     interp->tstate_head = tstate;
    496     HEAD_UNLOCK();
    497     /* Clear and deallocate all stale thread states.  Even if this
    498        executes Python code, we should be safe since it executes
    499        in the current thread, not one of the stale threads. */
    500     for (p = garbage; p; p = next) {
    501         next = p->next;
    502         PyThreadState_Clear(p);
    503         PyMem_RawFree(p);
    504     }
    505 }
    506 
    507 
    508 PyThreadState *
    509 _PyThreadState_UncheckedGet(void)
    510 {
    511     return GET_TSTATE();
    512 }
    513 
    514 
    515 PyThreadState *
    516 PyThreadState_Get(void)
    517 {
    518     PyThreadState *tstate = GET_TSTATE();
    519     if (tstate == NULL)
    520         Py_FatalError("PyThreadState_Get: no current thread");
    521 
    522     return tstate;
    523 }
    524 
    525 
    526 PyThreadState *
    527 PyThreadState_Swap(PyThreadState *newts)
    528 {
    529     PyThreadState *oldts = GET_TSTATE();
    530 
    531     SET_TSTATE(newts);
    532     /* It should not be possible for more than one thread state
    533        to be used for a thread.  Check this the best we can in debug
    534        builds.
    535     */
    536 #if defined(Py_DEBUG) && defined(WITH_THREAD)
    537     if (newts) {
    538         /* This can be called from PyEval_RestoreThread(). Similar
    539            to it, we need to ensure errno doesn't change.
    540         */
    541         int err = errno;
    542         PyThreadState *check = PyGILState_GetThisThreadState();
    543         if (check && check->interp == newts->interp && check != newts)
    544             Py_FatalError("Invalid thread state for this thread");
    545         errno = err;
    546     }
    547 #endif
    548     return oldts;
    549 }
    550 
    551 /* An extension mechanism to store arbitrary additional per-thread state.
    552    PyThreadState_GetDict() returns a dictionary that can be used to hold such
    553    state; the caller should pick a unique key and store its state there.  If
    554    PyThreadState_GetDict() returns NULL, an exception has *not* been raised
    555    and the caller should assume no per-thread state is available. */
    556 
    557 PyObject *
    558 PyThreadState_GetDict(void)
    559 {
    560     PyThreadState *tstate = GET_TSTATE();
    561     if (tstate == NULL)
    562         return NULL;
    563 
    564     if (tstate->dict == NULL) {
    565         PyObject *d;
    566         tstate->dict = d = PyDict_New();
    567         if (d == NULL)
    568             PyErr_Clear();
    569     }
    570     return tstate->dict;
    571 }
    572 
    573 
    574 /* Asynchronously raise an exception in a thread.
    575    Requested by Just van Rossum and Alex Martelli.
    576    To prevent naive misuse, you must write your own extension
    577    to call this, or use ctypes.  Must be called with the GIL held.
    578    Returns the number of tstates modified (normally 1, but 0 if `id` didn't
    579    match any known thread id).  Can be called with exc=NULL to clear an
    580    existing async exception.  This raises no exceptions. */
    581 
    582 int
    583 PyThreadState_SetAsyncExc(long id, PyObject *exc) {
    584     PyInterpreterState *interp = GET_INTERP_STATE();
    585     PyThreadState *p;
    586 
    587     /* Although the GIL is held, a few C API functions can be called
    588      * without the GIL held, and in particular some that create and
    589      * destroy thread and interpreter states.  Those can mutate the
    590      * list of thread states we're traversing, so to prevent that we lock
    591      * head_mutex for the duration.
    592      */
    593     HEAD_LOCK();
    594     for (p = interp->tstate_head; p != NULL; p = p->next) {
    595         if (p->thread_id == id) {
    596             /* Tricky:  we need to decref the current value
    597              * (if any) in p->async_exc, but that can in turn
    598              * allow arbitrary Python code to run, including
    599              * perhaps calls to this function.  To prevent
    600              * deadlock, we need to release head_mutex before
    601              * the decref.
    602              */
    603             PyObject *old_exc = p->async_exc;
    604             Py_XINCREF(exc);
    605             p->async_exc = exc;
    606             HEAD_UNLOCK();
    607             Py_XDECREF(old_exc);
    608             _PyEval_SignalAsyncExc();
    609             return 1;
    610         }
    611     }
    612     HEAD_UNLOCK();
    613     return 0;
    614 }
    615 
    616 
    617 /* Routines for advanced debuggers, requested by David Beazley.
    618    Don't use unless you know what you are doing! */
    619 
    620 PyInterpreterState *
    621 PyInterpreterState_Head(void)
    622 {
    623     return interp_head;
    624 }
    625 
    626 PyInterpreterState *
    627 PyInterpreterState_Next(PyInterpreterState *interp) {
    628     return interp->next;
    629 }
    630 
    631 PyThreadState *
    632 PyInterpreterState_ThreadHead(PyInterpreterState *interp) {
    633     return interp->tstate_head;
    634 }
    635 
    636 PyThreadState *
    637 PyThreadState_Next(PyThreadState *tstate) {
    638     return tstate->next;
    639 }
    640 
    641 /* The implementation of sys._current_frames().  This is intended to be
    642    called with the GIL held, as it will be when called via
    643    sys._current_frames().  It's possible it would work fine even without
    644    the GIL held, but haven't thought enough about that.
    645 */
    646 PyObject *
    647 _PyThread_CurrentFrames(void)
    648 {
    649     PyObject *result;
    650     PyInterpreterState *i;
    651 
    652     result = PyDict_New();
    653     if (result == NULL)
    654         return NULL;
    655 
    656     /* for i in all interpreters:
    657      *     for t in all of i's thread states:
    658      *          if t's frame isn't NULL, map t's id to its frame
    659      * Because these lists can mutate even when the GIL is held, we
    660      * need to grab head_mutex for the duration.
    661      */
    662     HEAD_LOCK();
    663     for (i = interp_head; i != NULL; i = i->next) {
    664         PyThreadState *t;
    665         for (t = i->tstate_head; t != NULL; t = t->next) {
    666             PyObject *id;
    667             int stat;
    668             struct _frame *frame = t->frame;
    669             if (frame == NULL)
    670                 continue;
    671             id = PyLong_FromLong(t->thread_id);
    672             if (id == NULL)
    673                 goto Fail;
    674             stat = PyDict_SetItem(result, id, (PyObject *)frame);
    675             Py_DECREF(id);
    676             if (stat < 0)
    677                 goto Fail;
    678         }
    679     }
    680     HEAD_UNLOCK();
    681     return result;
    682 
    683  Fail:
    684     HEAD_UNLOCK();
    685     Py_DECREF(result);
    686     return NULL;
    687 }
    688 
    689 /* Python "auto thread state" API. */
    690 #ifdef WITH_THREAD
    691 
    692 /* Keep this as a static, as it is not reliable!  It can only
    693    ever be compared to the state for the *current* thread.
    694    * If not equal, then it doesn't matter that the actual
    695      value may change immediately after comparison, as it can't
    696      possibly change to the current thread's state.
    697    * If equal, then the current thread holds the lock, so the value can't
    698      change until we yield the lock.
    699 */
    700 static int
    701 PyThreadState_IsCurrent(PyThreadState *tstate)
    702 {
    703     /* Must be the tstate for this thread */
    704     assert(PyGILState_GetThisThreadState()==tstate);
    705     return tstate == GET_TSTATE();
    706 }
    707 
    708 /* Internal initialization/finalization functions called by
    709    Py_Initialize/Py_FinalizeEx
    710 */
    711 void
    712 _PyGILState_Init(PyInterpreterState *i, PyThreadState *t)
    713 {
    714     assert(i && t); /* must init with valid states */
    715     autoTLSkey = PyThread_create_key();
    716     if (autoTLSkey == -1)
    717         Py_FatalError("Could not allocate TLS entry");
    718     autoInterpreterState = i;
    719     assert(PyThread_get_key_value(autoTLSkey) == NULL);
    720     assert(t->gilstate_counter == 0);
    721 
    722     _PyGILState_NoteThreadState(t);
    723 }
    724 
    725 PyInterpreterState *
    726 _PyGILState_GetInterpreterStateUnsafe(void)
    727 {
    728     return autoInterpreterState;
    729 }
    730 
    731 void
    732 _PyGILState_Fini(void)
    733 {
    734     PyThread_delete_key(autoTLSkey);
    735     autoTLSkey = -1;
    736     autoInterpreterState = NULL;
    737 }
    738 
    739 /* Reset the TLS key - called by PyOS_AfterFork().
    740  * This should not be necessary, but some - buggy - pthread implementations
    741  * don't reset TLS upon fork(), see issue #10517.
    742  */
    743 void
    744 _PyGILState_Reinit(void)
    745 {
    746     PyThreadState *tstate = PyGILState_GetThisThreadState();
    747     PyThread_delete_key(autoTLSkey);
    748     if ((autoTLSkey = PyThread_create_key()) == -1)
    749         Py_FatalError("Could not allocate TLS entry");
    750 
    751     /* If the thread had an associated auto thread state, reassociate it with
    752      * the new key. */
    753     if (tstate && PyThread_set_key_value(autoTLSkey, (void *)tstate) < 0)
    754         Py_FatalError("Couldn't create autoTLSkey mapping");
    755 }
    756 
    757 /* When a thread state is created for a thread by some mechanism other than
    758    PyGILState_Ensure, it's important that the GILState machinery knows about
    759    it so it doesn't try to create another thread state for the thread (this is
    760    a better fix for SF bug #1010677 than the first one attempted).
    761 */
    762 static void
    763 _PyGILState_NoteThreadState(PyThreadState* tstate)
    764 {
    765     /* If autoTLSkey isn't initialized, this must be the very first
    766        threadstate created in Py_Initialize().  Don't do anything for now
    767        (we'll be back here when _PyGILState_Init is called). */
    768     if (!autoInterpreterState)
    769         return;
    770 
    771     /* Stick the thread state for this thread in thread local storage.
    772 
    773        The only situation where you can legitimately have more than one
    774        thread state for an OS level thread is when there are multiple
    775        interpreters.
    776 
    777        You shouldn't really be using the PyGILState_ APIs anyway (see issues
    778        #10915 and #15751).
    779 
    780        The first thread state created for that given OS level thread will
    781        "win", which seems reasonable behaviour.
    782     */
    783     if (PyThread_get_key_value(autoTLSkey) == NULL) {
    784         if (PyThread_set_key_value(autoTLSkey, (void *)tstate) < 0)
    785             Py_FatalError("Couldn't create autoTLSkey mapping");
    786     }
    787 
    788     /* PyGILState_Release must not try to delete this thread state. */
    789     tstate->gilstate_counter = 1;
    790 }
    791 
    792 /* The public functions */
    793 PyThreadState *
    794 PyGILState_GetThisThreadState(void)
    795 {
    796     if (autoInterpreterState == NULL)
    797         return NULL;
    798     return (PyThreadState *)PyThread_get_key_value(autoTLSkey);
    799 }
    800 
    801 int
    802 PyGILState_Check(void)
    803 {
    804     PyThreadState *tstate;
    805 
    806     if (!_PyGILState_check_enabled)
    807         return 1;
    808 
    809     if (autoTLSkey == -1)
    810         return 1;
    811 
    812     tstate = GET_TSTATE();
    813     if (tstate == NULL)
    814         return 0;
    815 
    816     return (tstate == PyGILState_GetThisThreadState());
    817 }
    818 
    819 PyGILState_STATE
    820 PyGILState_Ensure(void)
    821 {
    822     int current;
    823     PyThreadState *tcur;
    824     /* Note that we do not auto-init Python here - apart from
    825        potential races with 2 threads auto-initializing, pep-311
    826        spells out other issues.  Embedders are expected to have
    827        called Py_Initialize() and usually PyEval_InitThreads().
    828     */
    829     assert(autoInterpreterState); /* Py_Initialize() hasn't been called! */
    830     tcur = (PyThreadState *)PyThread_get_key_value(autoTLSkey);
    831     if (tcur == NULL) {
    832         /* At startup, Python has no concrete GIL. If PyGILState_Ensure() is
    833            called from a new thread for the first time, we need the create the
    834            GIL. */
    835         PyEval_InitThreads();
    836 
    837         /* Create a new thread state for this thread */
    838         tcur = PyThreadState_New(autoInterpreterState);
    839         if (tcur == NULL)
    840             Py_FatalError("Couldn't create thread-state for new thread");
    841         /* This is our thread state!  We'll need to delete it in the
    842            matching call to PyGILState_Release(). */
    843         tcur->gilstate_counter = 0;
    844         current = 0; /* new thread state is never current */
    845     }
    846     else
    847         current = PyThreadState_IsCurrent(tcur);
    848     if (current == 0)
    849         PyEval_RestoreThread(tcur);
    850     /* Update our counter in the thread-state - no need for locks:
    851        - tcur will remain valid as we hold the GIL.
    852        - the counter is safe as we are the only thread "allowed"
    853          to modify this value
    854     */
    855     ++tcur->gilstate_counter;
    856     return current ? PyGILState_LOCKED : PyGILState_UNLOCKED;
    857 }
    858 
    859 void
    860 PyGILState_Release(PyGILState_STATE oldstate)
    861 {
    862     PyThreadState *tcur = (PyThreadState *)PyThread_get_key_value(
    863                                                             autoTLSkey);
    864     if (tcur == NULL)
    865         Py_FatalError("auto-releasing thread-state, "
    866                       "but no thread-state for this thread");
    867     /* We must hold the GIL and have our thread state current */
    868     /* XXX - remove the check - the assert should be fine,
    869        but while this is very new (April 2003), the extra check
    870        by release-only users can't hurt.
    871     */
    872     if (! PyThreadState_IsCurrent(tcur))
    873         Py_FatalError("This thread state must be current when releasing");
    874     assert(PyThreadState_IsCurrent(tcur));
    875     --tcur->gilstate_counter;
    876     assert(tcur->gilstate_counter >= 0); /* illegal counter value */
    877 
    878     /* If we're going to destroy this thread-state, we must
    879      * clear it while the GIL is held, as destructors may run.
    880      */
    881     if (tcur->gilstate_counter == 0) {
    882         /* can't have been locked when we created it */
    883         assert(oldstate == PyGILState_UNLOCKED);
    884         PyThreadState_Clear(tcur);
    885         /* Delete the thread-state.  Note this releases the GIL too!
    886          * It's vital that the GIL be held here, to avoid shutdown
    887          * races; see bugs 225673 and 1061968 (that nasty bug has a
    888          * habit of coming back).
    889          */
    890         PyThreadState_DeleteCurrent();
    891     }
    892     /* Release the lock if necessary */
    893     else if (oldstate == PyGILState_UNLOCKED)
    894         PyEval_SaveThread();
    895 }
    896 
    897 #endif /* WITH_THREAD */
    898 
    899 #ifdef __cplusplus
    900 }
    901 #endif
    902 
    903 
    904