LCOV - code coverage report
Current view: top level - Python - pystate.c (source / functions) Hit Total Coverage
Test: CPython lcov report Lines: 123 180 68.3 %
Date: 2017-04-19 Functions: 14 21 66.7 %

          Line data    Source code
       1             : 
       2             : /* Thread and interpreter state structures and their interfaces */
       3             : 
       4             : #include "Python.h"
       5             : 
       6             : /* --------------------------------------------------------------------------
       7             : CAUTION
       8             : 
       9             : Always use malloc() and free() directly in this file.  A number of these
      10             : functions are advertised as safe to call when the GIL isn't held, and in
      11             : a debug build Python redirects (e.g.) PyMem_NEW (etc) to Python's debugging
      12             : obmalloc functions.  Those aren't thread-safe (they rely on the GIL to avoid
      13             : the expense of doing their own locking).
      14             : -------------------------------------------------------------------------- */
      15             : 
      16             : #ifdef HAVE_DLOPEN
      17             : #ifdef HAVE_DLFCN_H
      18             : #include <dlfcn.h>
      19             : #endif
      20             : #ifndef RTLD_LAZY
      21             : #define RTLD_LAZY 1
      22             : #endif
      23             : #endif
      24             : 
      25             : #ifdef __cplusplus
      26             : extern "C" {
      27             : #endif
      28             : 
      29             : #ifdef WITH_THREAD
      30             : #include "pythread.h"
      31             : static PyThread_type_lock head_mutex = NULL; /* Protects interp->tstate_head */
      32             : #define HEAD_INIT() (void)(head_mutex || (head_mutex = PyThread_allocate_lock()))
      33             : #define HEAD_LOCK() PyThread_acquire_lock(head_mutex, WAIT_LOCK)
      34             : #define HEAD_UNLOCK() PyThread_release_lock(head_mutex)
      35             : 
      36             : /* The single PyInterpreterState used by this process'
      37             :    GILState implementation
      38             : */
      39             : static PyInterpreterState *autoInterpreterState = NULL;
      40             : static int autoTLSkey = 0;
      41             : #else
      42             : #define HEAD_INIT() /* Nothing */
      43             : #define HEAD_LOCK() /* Nothing */
      44             : #define HEAD_UNLOCK() /* Nothing */
      45             : #endif
      46             : 
      47             : static PyInterpreterState *interp_head = NULL;
      48             : 
      49             : PyThreadState *_PyThreadState_Current = NULL;
      50             : PyThreadFrameGetter _PyThreadState_GetFrame = NULL;
      51             : 
      52             : #ifdef WITH_THREAD
      53             : static void _PyGILState_NoteThreadState(PyThreadState* tstate);
      54             : #endif
      55             : 
      56             : 
      57             : PyInterpreterState *
      58           3 : PyInterpreterState_New(void)
      59             : {
      60           3 :     PyInterpreterState *interp = (PyInterpreterState *)
      61             :                                  malloc(sizeof(PyInterpreterState));
      62             : 
      63           3 :     if (interp != NULL) {
      64             :         HEAD_INIT();
      65             : #ifdef WITH_THREAD
      66             :         if (head_mutex == NULL)
      67             :             Py_FatalError("Can't initialize threads for interpreter");
      68             : #endif
      69           3 :         interp->modules = NULL;
      70           3 :         interp->modules_reloading = NULL;
      71           3 :         interp->sysdict = NULL;
      72           3 :         interp->builtins = NULL;
      73           3 :         interp->tstate_head = NULL;
      74           3 :         interp->codec_search_path = NULL;
      75           3 :         interp->codec_search_cache = NULL;
      76           3 :         interp->codec_error_registry = NULL;
      77             : #ifdef HAVE_DLOPEN
      78             : #ifdef RTLD_NOW
      79           3 :         interp->dlopenflags = RTLD_NOW;
      80             : #else
      81             :         interp->dlopenflags = RTLD_LAZY;
      82             : #endif
      83             : #endif
      84             : #ifdef WITH_TSC
      85             :         interp->tscdump = 0;
      86             : #endif
      87             : 
      88             :         HEAD_LOCK();
      89           3 :         interp->next = interp_head;
      90           3 :         interp_head = interp;
      91             :         HEAD_UNLOCK();
      92             :     }
      93             : 
      94           3 :     return interp;
      95             : }
      96             : 
      97             : 
      98             : void
      99           3 : PyInterpreterState_Clear(PyInterpreterState *interp)
     100             : {
     101             :     PyThreadState *p;
     102             :     HEAD_LOCK();
     103           6 :     for (p = interp->tstate_head; p != NULL; p = p->next)
     104           3 :         PyThreadState_Clear(p);
     105             :     HEAD_UNLOCK();
     106           3 :     Py_CLEAR(interp->codec_search_path);
     107           3 :     Py_CLEAR(interp->codec_search_cache);
     108           3 :     Py_CLEAR(interp->codec_error_registry);
     109           3 :     Py_CLEAR(interp->modules);
     110           3 :     Py_CLEAR(interp->modules_reloading);
     111           3 :     Py_CLEAR(interp->sysdict);
     112           3 :     Py_CLEAR(interp->builtins);
     113           3 : }
     114             : 
     115             : 
     116             : static void
     117           3 : zapthreads(PyInterpreterState *interp)
     118             : {
     119             :     PyThreadState *p;
     120             :     /* No need to lock the mutex here because this should only happen
     121             :        when the threads are all really dead (XXX famous last words). */
     122           9 :     while ((p = interp->tstate_head) != NULL) {
     123           3 :         PyThreadState_Delete(p);
     124             :     }
     125           3 : }
     126             : 
     127             : 
     128             : void
     129           3 : PyInterpreterState_Delete(PyInterpreterState *interp)
     130             : {
     131             :     PyInterpreterState **p;
     132           3 :     zapthreads(interp);
     133             :     HEAD_LOCK();
     134           3 :     for (p = &interp_head; ; p = &(*p)->next) {
     135           3 :         if (*p == NULL)
     136           0 :             Py_FatalError(
     137             :                 "PyInterpreterState_Delete: invalid interp");
     138           3 :         if (*p == interp)
     139           3 :             break;
     140           0 :     }
     141           3 :     if (interp->tstate_head != NULL)
     142           0 :         Py_FatalError("PyInterpreterState_Delete: remaining threads");
     143           3 :     *p = interp->next;
     144             :     HEAD_UNLOCK();
     145           3 :     free(interp);
     146           3 : }
     147             : 
     148             : 
     149             : /* Default implementation for _PyThreadState_GetFrame */
     150             : static struct _frame *
     151        6880 : threadstate_getframe(PyThreadState *self)
     152             : {
     153        6880 :     return self->frame;
     154             : }
     155             : 
     156             : static PyThreadState *
     157           3 : new_threadstate(PyInterpreterState *interp, int init)
     158             : {
     159           3 :     PyThreadState *tstate = (PyThreadState *)malloc(sizeof(PyThreadState));
     160             : 
     161           3 :     if (_PyThreadState_GetFrame == NULL)
     162           3 :         _PyThreadState_GetFrame = threadstate_getframe;
     163             : 
     164           3 :     if (tstate != NULL) {
     165           3 :         tstate->interp = interp;
     166             : 
     167           3 :         tstate->frame = NULL;
     168           3 :         tstate->recursion_depth = 0;
     169           3 :         tstate->tracing = 0;
     170           3 :         tstate->use_tracing = 0;
     171           3 :         tstate->tick_counter = 0;
     172           3 :         tstate->gilstate_counter = 0;
     173           3 :         tstate->async_exc = NULL;
     174             : #ifdef WITH_THREAD
     175             :         tstate->thread_id = PyThread_get_thread_ident();
     176             : #else
     177           3 :         tstate->thread_id = 0;
     178             : #endif
     179             : 
     180           3 :         tstate->dict = NULL;
     181             : 
     182           3 :         tstate->curexc_type = NULL;
     183           3 :         tstate->curexc_value = NULL;
     184           3 :         tstate->curexc_traceback = NULL;
     185             : 
     186           3 :         tstate->exc_type = NULL;
     187           3 :         tstate->exc_value = NULL;
     188           3 :         tstate->exc_traceback = NULL;
     189             : 
     190           3 :         tstate->c_profilefunc = NULL;
     191           3 :         tstate->c_tracefunc = NULL;
     192           3 :         tstate->c_profileobj = NULL;
     193           3 :         tstate->c_traceobj = NULL;
     194             : 
     195           3 :         tstate->trash_delete_nesting = 0;
     196           3 :         tstate->trash_delete_later = NULL;
     197             : 
     198           3 :         if (init)
     199           3 :             _PyThreadState_Init(tstate);
     200             : 
     201             :         HEAD_LOCK();
     202           3 :         tstate->next = interp->tstate_head;
     203           3 :         interp->tstate_head = tstate;
     204             :         HEAD_UNLOCK();
     205             :     }
     206             : 
     207           3 :     return tstate;
     208             : }
     209             : 
     210             : PyThreadState *
     211           3 : PyThreadState_New(PyInterpreterState *interp)
     212             : {
     213           3 :     return new_threadstate(interp, 1);
     214             : }
     215             : 
     216             : PyThreadState *
     217           0 : _PyThreadState_Prealloc(PyInterpreterState *interp)
     218             : {
     219           0 :     return new_threadstate(interp, 0);
     220             : }
     221             : 
     222             : void
     223           3 : _PyThreadState_Init(PyThreadState *tstate)
     224             : {
     225             : #ifdef WITH_THREAD
     226             :     _PyGILState_NoteThreadState(tstate);
     227             : #endif
     228           3 : }
     229             : 
     230             : void
     231           3 : PyThreadState_Clear(PyThreadState *tstate)
     232             : {
     233           3 :     if (Py_VerboseFlag && tstate->frame != NULL)
     234           0 :         fprintf(stderr,
     235             :           "PyThreadState_Clear: warning: thread still has a frame\n");
     236             : 
     237           3 :     Py_CLEAR(tstate->frame);
     238             : 
     239           3 :     Py_CLEAR(tstate->dict);
     240           3 :     Py_CLEAR(tstate->async_exc);
     241             : 
     242           3 :     Py_CLEAR(tstate->curexc_type);
     243           3 :     Py_CLEAR(tstate->curexc_value);
     244           3 :     Py_CLEAR(tstate->curexc_traceback);
     245             : 
     246           3 :     Py_CLEAR(tstate->exc_type);
     247           3 :     Py_CLEAR(tstate->exc_value);
     248           3 :     Py_CLEAR(tstate->exc_traceback);
     249             : 
     250           3 :     tstate->c_profilefunc = NULL;
     251           3 :     tstate->c_tracefunc = NULL;
     252           3 :     Py_CLEAR(tstate->c_profileobj);
     253           3 :     Py_CLEAR(tstate->c_traceobj);
     254           3 : }
     255             : 
     256             : 
     257             : /* Common code for PyThreadState_Delete() and PyThreadState_DeleteCurrent() */
     258             : static void
     259           3 : tstate_delete_common(PyThreadState *tstate)
     260             : {
     261             :     PyInterpreterState *interp;
     262             :     PyThreadState **p;
     263           3 :     PyThreadState *prev_p = NULL;
     264           3 :     if (tstate == NULL)
     265           0 :         Py_FatalError("PyThreadState_Delete: NULL tstate");
     266           3 :     interp = tstate->interp;
     267           3 :     if (interp == NULL)
     268           0 :         Py_FatalError("PyThreadState_Delete: NULL interp");
     269             :     HEAD_LOCK();
     270           3 :     for (p = &interp->tstate_head; ; p = &(*p)->next) {
     271           3 :         if (*p == NULL)
     272           0 :             Py_FatalError(
     273             :                 "PyThreadState_Delete: invalid tstate");
     274           3 :         if (*p == tstate)
     275           3 :             break;
     276             :         /* Sanity check.  These states should never happen but if
     277             :          * they do we must abort.  Otherwise we'll end up spinning in
     278             :          * in a tight loop with the lock held.  A similar check is done
     279             :          * in thread.c find_key().  */
     280           0 :         if (*p == prev_p)
     281           0 :             Py_FatalError(
     282             :                 "PyThreadState_Delete: small circular list(!)"
     283             :                 " and tstate not found.");
     284           0 :         prev_p = *p;
     285           0 :         if ((*p)->next == interp->tstate_head)
     286           0 :             Py_FatalError(
     287             :                 "PyThreadState_Delete: circular list(!) and"
     288             :                 " tstate not found.");
     289           0 :     }
     290           3 :     *p = tstate->next;
     291             :     HEAD_UNLOCK();
     292           3 :     free(tstate);
     293           3 : }
     294             : 
     295             : 
     296             : void
     297           3 : PyThreadState_Delete(PyThreadState *tstate)
     298             : {
     299           3 :     if (tstate == _PyThreadState_Current)
     300           0 :         Py_FatalError("PyThreadState_Delete: tstate is still current");
     301           3 :     tstate_delete_common(tstate);
     302             : #ifdef WITH_THREAD
     303             :     if (autoInterpreterState && PyThread_get_key_value(autoTLSkey) == tstate)
     304             :         PyThread_delete_key_value(autoTLSkey);
     305             : #endif /* WITH_THREAD */
     306           3 : }
     307             : 
     308             : 
     309             : #ifdef WITH_THREAD
     310             : void
     311             : PyThreadState_DeleteCurrent()
     312             : {
     313             :     PyThreadState *tstate = _PyThreadState_Current;
     314             :     if (tstate == NULL)
     315             :         Py_FatalError(
     316             :             "PyThreadState_DeleteCurrent: no current tstate");
     317             :     _PyThreadState_Current = NULL;
     318             :     if (autoInterpreterState && PyThread_get_key_value(autoTLSkey) == tstate)
     319             :         PyThread_delete_key_value(autoTLSkey);
     320             :     tstate_delete_common(tstate);
     321             :     PyEval_ReleaseLock();
     322             : }
     323             : #endif /* WITH_THREAD */
     324             : 
     325             : 
     326             : PyThreadState *
     327          96 : PyThreadState_Get(void)
     328             : {
     329          96 :     if (_PyThreadState_Current == NULL)
     330           0 :         Py_FatalError("PyThreadState_Get: no current thread");
     331             : 
     332          96 :     return _PyThreadState_Current;
     333             : }
     334             : 
     335             : 
     336             : PyThreadState *
     337           6 : PyThreadState_Swap(PyThreadState *newts)
     338             : {
     339           6 :     PyThreadState *oldts = _PyThreadState_Current;
     340             : 
     341           6 :     _PyThreadState_Current = newts;
     342             :     /* It should not be possible for more than one thread state
     343             :        to be used for a thread.  Check this the best we can in debug
     344             :        builds.
     345             :     */
     346             : #if defined(Py_DEBUG) && defined(WITH_THREAD)
     347             :     if (newts) {
     348             :         /* This can be called from PyEval_RestoreThread(). Similar
     349             :            to it, we need to ensure errno doesn't change.
     350             :         */
     351             :         int err = errno;
     352             :         PyThreadState *check = PyGILState_GetThisThreadState();
     353             :         if (check && check->interp == newts->interp && check != newts)
     354             :             Py_FatalError("Invalid thread state for this thread");
     355             :         errno = err;
     356             :     }
     357             : #endif
     358           6 :     return oldts;
     359             : }
     360             : 
     361             : /* An extension mechanism to store arbitrary additional per-thread state.
     362             :    PyThreadState_GetDict() returns a dictionary that can be used to hold such
     363             :    state; the caller should pick a unique key and store its state there.  If
     364             :    PyThreadState_GetDict() returns NULL, an exception has *not* been raised
     365             :    and the caller should assume no per-thread state is available. */
     366             : 
     367             : PyObject *
     368         162 : PyThreadState_GetDict(void)
     369             : {
     370         162 :     if (_PyThreadState_Current == NULL)
     371           0 :         return NULL;
     372             : 
     373         162 :     if (_PyThreadState_Current->dict == NULL) {
     374             :         PyObject *d;
     375           3 :         _PyThreadState_Current->dict = d = PyDict_New();
     376           3 :         if (d == NULL)
     377           0 :             PyErr_Clear();
     378             :     }
     379         162 :     return _PyThreadState_Current->dict;
     380             : }
     381             : 
     382             : 
     383             : /* Asynchronously raise an exception in a thread.
     384             :    Requested by Just van Rossum and Alex Martelli.
     385             :    To prevent naive misuse, you must write your own extension
     386             :    to call this, or use ctypes.  Must be called with the GIL held.
     387             :    Returns the number of tstates modified (normally 1, but 0 if `id` didn't
     388             :    match any known thread id).  Can be called with exc=NULL to clear an
     389             :    existing async exception.  This raises no exceptions. */
     390             : 
     391             : int
     392           0 : PyThreadState_SetAsyncExc(long id, PyObject *exc) {
     393           0 :     PyThreadState *tstate = PyThreadState_GET();
     394           0 :     PyInterpreterState *interp = tstate->interp;
     395             :     PyThreadState *p;
     396             : 
     397             :     /* Although the GIL is held, a few C API functions can be called
     398             :      * without the GIL held, and in particular some that create and
     399             :      * destroy thread and interpreter states.  Those can mutate the
     400             :      * list of thread states we're traversing, so to prevent that we lock
     401             :      * head_mutex for the duration.
     402             :      */
     403             :     HEAD_LOCK();
     404           0 :     for (p = interp->tstate_head; p != NULL; p = p->next) {
     405           0 :         if (p->thread_id == id) {
     406             :             /* Tricky:  we need to decref the current value
     407             :              * (if any) in p->async_exc, but that can in turn
     408             :              * allow arbitrary Python code to run, including
     409             :              * perhaps calls to this function.  To prevent
     410             :              * deadlock, we need to release head_mutex before
     411             :              * the decref.
     412             :              */
     413           0 :             PyObject *old_exc = p->async_exc;
     414           0 :             Py_XINCREF(exc);
     415           0 :             p->async_exc = exc;
     416             :             HEAD_UNLOCK();
     417           0 :             Py_XDECREF(old_exc);
     418           0 :             return 1;
     419             :         }
     420             :     }
     421             :     HEAD_UNLOCK();
     422           0 :     return 0;
     423             : }
     424             : 
     425             : 
     426             : /* Routines for advanced debuggers, requested by David Beazley.
     427             :    Don't use unless you know what you are doing! */
     428             : 
     429             : PyInterpreterState *
     430           0 : PyInterpreterState_Head(void)
     431             : {
     432           0 :     return interp_head;
     433             : }
     434             : 
     435             : PyInterpreterState *
     436           0 : PyInterpreterState_Next(PyInterpreterState *interp) {
     437           0 :     return interp->next;
     438             : }
     439             : 
     440             : PyThreadState *
     441           0 : PyInterpreterState_ThreadHead(PyInterpreterState *interp) {
     442           0 :     return interp->tstate_head;
     443             : }
     444             : 
     445             : PyThreadState *
     446           0 : PyThreadState_Next(PyThreadState *tstate) {
     447           0 :     return tstate->next;
     448             : }
     449             : 
     450             : /* The implementation of sys._current_frames().  This is intended to be
     451             :    called with the GIL held, as it will be when called via
     452             :    sys._current_frames().  It's possible it would work fine even without
     453             :    the GIL held, but haven't thought enough about that.
     454             : */
     455             : PyObject *
     456           0 : _PyThread_CurrentFrames(void)
     457             : {
     458             :     PyObject *result;
     459             :     PyInterpreterState *i;
     460             : 
     461           0 :     result = PyDict_New();
     462           0 :     if (result == NULL)
     463           0 :         return NULL;
     464             : 
     465             :     /* for i in all interpreters:
     466             :      *     for t in all of i's thread states:
     467             :      *          if t's frame isn't NULL, map t's id to its frame
     468             :      * Because these lists can mutate even when the GIL is held, we
     469             :      * need to grab head_mutex for the duration.
     470             :      */
     471             :     HEAD_LOCK();
     472           0 :     for (i = interp_head; i != NULL; i = i->next) {
     473             :         PyThreadState *t;
     474           0 :         for (t = i->tstate_head; t != NULL; t = t->next) {
     475             :             PyObject *id;
     476             :             int stat;
     477           0 :             struct _frame *frame = t->frame;
     478           0 :             if (frame == NULL)
     479           0 :                 continue;
     480           0 :             id = PyInt_FromLong(t->thread_id);
     481           0 :             if (id == NULL)
     482           0 :                 goto Fail;
     483           0 :             stat = PyDict_SetItem(result, id, (PyObject *)frame);
     484           0 :             Py_DECREF(id);
     485           0 :             if (stat < 0)
     486           0 :                 goto Fail;
     487             :         }
     488             :     }
     489             :     HEAD_UNLOCK();
     490           0 :     return result;
     491             : 
     492             :  Fail:
     493             :     HEAD_UNLOCK();
     494           0 :     Py_DECREF(result);
     495           0 :     return NULL;
     496             : }
     497             : 
     498             : /* Python "auto thread state" API. */
     499             : #ifdef WITH_THREAD
     500             : 
     501             : /* Keep this as a static, as it is not reliable!  It can only
     502             :    ever be compared to the state for the *current* thread.
     503             :    * If not equal, then it doesn't matter that the actual
     504             :      value may change immediately after comparison, as it can't
     505             :      possibly change to the current thread's state.
     506             :    * If equal, then the current thread holds the lock, so the value can't
     507             :      change until we yield the lock.
     508             : */
     509             : static int
     510             : PyThreadState_IsCurrent(PyThreadState *tstate)
     511             : {
     512             :     /* Must be the tstate for this thread */
     513             :     assert(PyGILState_GetThisThreadState()==tstate);
     514             :     /* On Windows at least, simple reads and writes to 32 bit values
     515             :        are atomic.
     516             :     */
     517             :     return tstate == _PyThreadState_Current;
     518             : }
     519             : 
     520             : /* Internal initialization/finalization functions called by
     521             :    Py_Initialize/Py_Finalize
     522             : */
     523             : void
     524             : _PyGILState_Init(PyInterpreterState *i, PyThreadState *t)
     525             : {
     526             :     assert(i && t); /* must init with valid states */
     527             :     autoTLSkey = PyThread_create_key();
     528             :     autoInterpreterState = i;
     529             :     assert(PyThread_get_key_value(autoTLSkey) == NULL);
     530             :     assert(t->gilstate_counter == 0);
     531             : 
     532             :     _PyGILState_NoteThreadState(t);
     533             : }
     534             : 
     535             : void
     536             : _PyGILState_Fini(void)
     537             : {
     538             :     PyThread_delete_key(autoTLSkey);
     539             :     autoInterpreterState = NULL;
     540             : }
     541             : 
     542             : /* When a thread state is created for a thread by some mechanism other than
     543             :    PyGILState_Ensure, it's important that the GILState machinery knows about
     544             :    it so it doesn't try to create another thread state for the thread (this is
     545             :    a better fix for SF bug #1010677 than the first one attempted).
     546             : */
     547             : static void
     548             : _PyGILState_NoteThreadState(PyThreadState* tstate)
     549             : {
     550             :     /* If autoTLSkey isn't initialized, this must be the very first
     551             :        threadstate created in Py_Initialize().  Don't do anything for now
     552             :        (we'll be back here when _PyGILState_Init is called). */
     553             :     if (!autoInterpreterState)
     554             :         return;
     555             : 
     556             :     /* Stick the thread state for this thread in thread local storage.
     557             : 
     558             :        The only situation where you can legitimately have more than one
     559             :        thread state for an OS level thread is when there are multiple
     560             :        interpreters, when:
     561             : 
     562             :            a) You shouldn't really be using the PyGILState_ APIs anyway,
     563             :           and:
     564             : 
     565             :            b) The slightly odd way PyThread_set_key_value works (see
     566             :           comments by its implementation) means that the first thread
     567             :           state created for that given OS level thread will "win",
     568             :           which seems reasonable behaviour.
     569             :     */
     570             :     if (PyThread_set_key_value(autoTLSkey, (void *)tstate) < 0)
     571             :         Py_FatalError("Couldn't create autoTLSkey mapping");
     572             : 
     573             :     /* PyGILState_Release must not try to delete this thread state. */
     574             :     tstate->gilstate_counter = 1;
     575             : }
     576             : 
     577             : /* The public functions */
     578             : PyThreadState *
     579             : PyGILState_GetThisThreadState(void)
     580             : {
     581             :     if (autoInterpreterState == NULL)
     582             :         return NULL;
     583             :     return (PyThreadState *)PyThread_get_key_value(autoTLSkey);
     584             : }
     585             : 
     586             : PyGILState_STATE
     587             : PyGILState_Ensure(void)
     588             : {
     589             :     int current;
     590             :     PyThreadState *tcur;
     591             :     /* Note that we do not auto-init Python here - apart from
     592             :        potential races with 2 threads auto-initializing, pep-311
     593             :        spells out other issues.  Embedders are expected to have
     594             :        called Py_Initialize() and usually PyEval_InitThreads().
     595             :     */
     596             :     assert(autoInterpreterState); /* Py_Initialize() hasn't been called! */
     597             :     tcur = (PyThreadState *)PyThread_get_key_value(autoTLSkey);
     598             :     if (tcur == NULL) {
     599             :         /* Create a new thread state for this thread */
     600             :         tcur = PyThreadState_New(autoInterpreterState);
     601             :         if (tcur == NULL)
     602             :             Py_FatalError("Couldn't create thread-state for new thread");
     603             :         /* This is our thread state!  We'll need to delete it in the
     604             :            matching call to PyGILState_Release(). */
     605             :         tcur->gilstate_counter = 0;
     606             :         current = 0; /* new thread state is never current */
     607             :     }
     608             :     else
     609             :         current = PyThreadState_IsCurrent(tcur);
     610             :     if (current == 0)
     611             :         PyEval_RestoreThread(tcur);
     612             :     /* Update our counter in the thread-state - no need for locks:
     613             :        - tcur will remain valid as we hold the GIL.
     614             :        - the counter is safe as we are the only thread "allowed"
     615             :          to modify this value
     616             :     */
     617             :     ++tcur->gilstate_counter;
     618             :     return current ? PyGILState_LOCKED : PyGILState_UNLOCKED;
     619             : }
     620             : 
     621             : void
     622             : PyGILState_Release(PyGILState_STATE oldstate)
     623             : {
     624             :     PyThreadState *tcur = (PyThreadState *)PyThread_get_key_value(
     625             :                                                             autoTLSkey);
     626             :     if (tcur == NULL)
     627             :         Py_FatalError("auto-releasing thread-state, "
     628             :                       "but no thread-state for this thread");
     629             :     /* We must hold the GIL and have our thread state current */
     630             :     /* XXX - remove the check - the assert should be fine,
     631             :        but while this is very new (April 2003), the extra check
     632             :        by release-only users can't hurt.
     633             :     */
     634             :     if (! PyThreadState_IsCurrent(tcur))
     635             :         Py_FatalError("This thread state must be current when releasing");
     636             :     assert(PyThreadState_IsCurrent(tcur));
     637             :     --tcur->gilstate_counter;
     638             :     assert(tcur->gilstate_counter >= 0); /* illegal counter value */
     639             : 
     640             :     /* If we're going to destroy this thread-state, we must
     641             :      * clear it while the GIL is held, as destructors may run.
     642             :      */
     643             :     if (tcur->gilstate_counter == 0) {
     644             :         /* can't have been locked when we created it */
     645             :         assert(oldstate == PyGILState_UNLOCKED);
     646             :         PyThreadState_Clear(tcur);
     647             :         /* Delete the thread-state.  Note this releases the GIL too!
     648             :          * It's vital that the GIL be held here, to avoid shutdown
     649             :          * races; see bugs 225673 and 1061968 (that nasty bug has a
     650             :          * habit of coming back).
     651             :          */
     652             :         PyThreadState_DeleteCurrent();
     653             :     }
     654             :     /* Release the lock if necessary */
     655             :     else if (oldstate == PyGILState_UNLOCKED)
     656             :         PyEval_SaveThread();
     657             : }
     658             : 
     659             : #endif /* WITH_THREAD */
     660             : 
     661             : #ifdef __cplusplus
     662             : }
     663             : #endif
     664             : 
     665             : 

Generated by: LCOV version 1.10