Ignore:
Timestamp:
Mar 19, 2014, 11:11:30 AM (11 years ago)
Author:
dmik
Message:

python: Update vendor to 2.7.6.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • python/vendor/current/Python/pystate.c

    r2 r388  
    2323#endif
    2424
     25#ifdef __cplusplus
     26extern "C" {
     27#endif
    2528
    2629#ifdef WITH_THREAD
     
    3033#define HEAD_LOCK() PyThread_acquire_lock(head_mutex, WAIT_LOCK)
    3134#define HEAD_UNLOCK() PyThread_release_lock(head_mutex)
    32 
    33 #ifdef __cplusplus
    34 extern "C" {
    35 #endif
    3635
    3736/* The single PyInterpreterState used by this process'
     
    5958PyInterpreterState_New(void)
    6059{
    61         PyInterpreterState *interp = (PyInterpreterState *)
    62                                      malloc(sizeof(PyInterpreterState));
    63 
    64         if (interp != NULL) {
    65                 HEAD_INIT();
    66 #ifdef WITH_THREAD
    67                 if (head_mutex == NULL)
    68                         Py_FatalError("Can't initialize threads for interpreter");
    69 #endif
    70                 interp->modules = NULL;
    71                 interp->modules_reloading = NULL;
    72                 interp->sysdict = NULL;
    73                 interp->builtins = NULL;
    74                 interp->tstate_head = NULL;
    75                 interp->codec_search_path = NULL;
    76                 interp->codec_search_cache = NULL;
    77                 interp->codec_error_registry = NULL;
     60    PyInterpreterState *interp = (PyInterpreterState *)
     61                                 malloc(sizeof(PyInterpreterState));
     62
     63    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        interp->modules = NULL;
     70        interp->modules_reloading = NULL;
     71        interp->sysdict = NULL;
     72        interp->builtins = NULL;
     73        interp->tstate_head = NULL;
     74        interp->codec_search_path = NULL;
     75        interp->codec_search_cache = NULL;
     76        interp->codec_error_registry = NULL;
    7877#ifdef HAVE_DLOPEN
    7978#ifdef RTLD_NOW
    80                 interp->dlopenflags = RTLD_NOW;
     79        interp->dlopenflags = RTLD_NOW;
    8180#else
    82                 interp->dlopenflags = RTLD_LAZY;
     81        interp->dlopenflags = RTLD_LAZY;
    8382#endif
    8483#endif
    8584#ifdef WITH_TSC
    86                 interp->tscdump = 0;
    87 #endif
    88 
    89                 HEAD_LOCK();
    90                 interp->next = interp_head;
    91                 interp_head = interp;
    92                 HEAD_UNLOCK();
    93         }
    94 
    95         return interp;
     85        interp->tscdump = 0;
     86#endif
     87
     88        HEAD_LOCK();
     89        interp->next = interp_head;
     90        interp_head = interp;
     91        HEAD_UNLOCK();
     92    }
     93
     94    return interp;
    9695}
    9796
     
    10099PyInterpreterState_Clear(PyInterpreterState *interp)
    101100{
    102         PyThreadState *p;
    103         HEAD_LOCK();
    104         for (p = interp->tstate_head; p != NULL; p = p->next)
    105                 PyThreadState_Clear(p);
    106         HEAD_UNLOCK();
    107         Py_CLEAR(interp->codec_search_path);
    108         Py_CLEAR(interp->codec_search_cache);
    109         Py_CLEAR(interp->codec_error_registry);
    110         Py_CLEAR(interp->modules);
    111         Py_CLEAR(interp->modules_reloading);
    112         Py_CLEAR(interp->sysdict);
    113         Py_CLEAR(interp->builtins);
     101    PyThreadState *p;
     102    HEAD_LOCK();
     103    for (p = interp->tstate_head; p != NULL; p = p->next)
     104        PyThreadState_Clear(p);
     105    HEAD_UNLOCK();
     106    Py_CLEAR(interp->codec_search_path);
     107    Py_CLEAR(interp->codec_search_cache);
     108    Py_CLEAR(interp->codec_error_registry);
     109    Py_CLEAR(interp->modules);
     110    Py_CLEAR(interp->modules_reloading);
     111    Py_CLEAR(interp->sysdict);
     112    Py_CLEAR(interp->builtins);
    114113}
    115114
     
    118117zapthreads(PyInterpreterState *interp)
    119118{
    120         PyThreadState *p;
    121         /* No need to lock the mutex here because this should only happen
    122            when the threads are all really dead (XXX famous last words). */
    123         while ((p = interp->tstate_head) != NULL) {
    124                 PyThreadState_Delete(p);
    125         }
     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    while ((p = interp->tstate_head) != NULL) {
     123        PyThreadState_Delete(p);
     124    }
    126125}
    127126
     
    130129PyInterpreterState_Delete(PyInterpreterState *interp)
    131130{
    132         PyInterpreterState **p;
    133         zapthreads(interp);
    134         HEAD_LOCK();
    135         for (p = &interp_head; ; p = &(*p)->next) {
    136                 if (*p == NULL)
    137                         Py_FatalError(
    138                                 "PyInterpreterState_Delete: invalid interp");
    139                 if (*p == interp)
    140                         break;
    141         }
    142         if (interp->tstate_head != NULL)
    143                 Py_FatalError("PyInterpreterState_Delete: remaining threads");
    144         *p = interp->next;
    145         HEAD_UNLOCK();
    146         free(interp);
     131    PyInterpreterState **p;
     132    zapthreads(interp);
     133    HEAD_LOCK();
     134    for (p = &interp_head; ; p = &(*p)->next) {
     135        if (*p == NULL)
     136            Py_FatalError(
     137                "PyInterpreterState_Delete: invalid interp");
     138        if (*p == interp)
     139            break;
     140    }
     141    if (interp->tstate_head != NULL)
     142        Py_FatalError("PyInterpreterState_Delete: remaining threads");
     143    *p = interp->next;
     144    HEAD_UNLOCK();
     145    free(interp);
    147146}
    148147
     
    152151threadstate_getframe(PyThreadState *self)
    153152{
    154         return self->frame;
     153    return self->frame;
     154}
     155
     156static PyThreadState *
     157new_threadstate(PyInterpreterState *interp, int init)
     158{
     159    PyThreadState *tstate = (PyThreadState *)malloc(sizeof(PyThreadState));
     160
     161    if (_PyThreadState_GetFrame == NULL)
     162        _PyThreadState_GetFrame = threadstate_getframe;
     163
     164    if (tstate != NULL) {
     165        tstate->interp = interp;
     166
     167        tstate->frame = NULL;
     168        tstate->recursion_depth = 0;
     169        tstate->tracing = 0;
     170        tstate->use_tracing = 0;
     171        tstate->tick_counter = 0;
     172        tstate->gilstate_counter = 0;
     173        tstate->async_exc = NULL;
     174#ifdef WITH_THREAD
     175        tstate->thread_id = PyThread_get_thread_ident();
     176#else
     177        tstate->thread_id = 0;
     178#endif
     179
     180        tstate->dict = NULL;
     181
     182        tstate->curexc_type = NULL;
     183        tstate->curexc_value = NULL;
     184        tstate->curexc_traceback = NULL;
     185
     186        tstate->exc_type = NULL;
     187        tstate->exc_value = NULL;
     188        tstate->exc_traceback = NULL;
     189
     190        tstate->c_profilefunc = NULL;
     191        tstate->c_tracefunc = NULL;
     192        tstate->c_profileobj = NULL;
     193        tstate->c_traceobj = NULL;
     194
     195        tstate->trash_delete_nesting = 0;
     196        tstate->trash_delete_later = NULL;
     197
     198        if (init)
     199            _PyThreadState_Init(tstate);
     200
     201        HEAD_LOCK();
     202        tstate->next = interp->tstate_head;
     203        interp->tstate_head = tstate;
     204        HEAD_UNLOCK();
     205    }
     206
     207    return tstate;
    155208}
    156209
     
    158211PyThreadState_New(PyInterpreterState *interp)
    159212{
    160         PyThreadState *tstate = (PyThreadState *)malloc(sizeof(PyThreadState));
    161 
    162         if (_PyThreadState_GetFrame == NULL)
    163                 _PyThreadState_GetFrame = threadstate_getframe;
    164 
    165         if (tstate != NULL) {
    166                 tstate->interp = interp;
    167 
    168                 tstate->frame = NULL;
    169                 tstate->recursion_depth = 0;
    170                 tstate->tracing = 0;
    171                 tstate->use_tracing = 0;
    172                 tstate->tick_counter = 0;
    173                 tstate->gilstate_counter = 0;
    174                 tstate->async_exc = NULL;
    175 #ifdef WITH_THREAD
    176                 tstate->thread_id = PyThread_get_thread_ident();
    177 #else
    178                 tstate->thread_id = 0;
    179 #endif
    180 
    181                 tstate->dict = NULL;
    182 
    183                 tstate->curexc_type = NULL;
    184                 tstate->curexc_value = NULL;
    185                 tstate->curexc_traceback = NULL;
    186 
    187                 tstate->exc_type = NULL;
    188                 tstate->exc_value = NULL;
    189                 tstate->exc_traceback = NULL;
    190 
    191                 tstate->c_profilefunc = NULL;
    192                 tstate->c_tracefunc = NULL;
    193                 tstate->c_profileobj = NULL;
    194                 tstate->c_traceobj = NULL;
    195 
    196 #ifdef WITH_THREAD
    197                 _PyGILState_NoteThreadState(tstate);
    198 #endif
    199 
    200                 HEAD_LOCK();
    201                 tstate->next = interp->tstate_head;
    202                 interp->tstate_head = tstate;
    203                 HEAD_UNLOCK();
    204         }
    205 
    206         return tstate;
    207 }
    208 
     213    return new_threadstate(interp, 1);
     214}
     215
     216PyThreadState *
     217_PyThreadState_Prealloc(PyInterpreterState *interp)
     218{
     219    return new_threadstate(interp, 0);
     220}
     221
     222void
     223_PyThreadState_Init(PyThreadState *tstate)
     224{
     225#ifdef WITH_THREAD
     226    _PyGILState_NoteThreadState(tstate);
     227#endif
     228}
    209229
    210230void
    211231PyThreadState_Clear(PyThreadState *tstate)
    212232{
    213         if (Py_VerboseFlag && tstate->frame != NULL)
    214                 fprintf(stderr,
    215                   "PyThreadState_Clear: warning: thread still has a frame\n");
    216 
    217         Py_CLEAR(tstate->frame);
    218 
    219         Py_CLEAR(tstate->dict);
    220         Py_CLEAR(tstate->async_exc);
    221 
    222         Py_CLEAR(tstate->curexc_type);
    223         Py_CLEAR(tstate->curexc_value);
    224         Py_CLEAR(tstate->curexc_traceback);
    225 
    226         Py_CLEAR(tstate->exc_type);
    227         Py_CLEAR(tstate->exc_value);
    228         Py_CLEAR(tstate->exc_traceback);
    229 
    230         tstate->c_profilefunc = NULL;
    231         tstate->c_tracefunc = NULL;
    232         Py_CLEAR(tstate->c_profileobj);
    233         Py_CLEAR(tstate->c_traceobj);
     233    if (Py_VerboseFlag && tstate->frame != NULL)
     234        fprintf(stderr,
     235          "PyThreadState_Clear: warning: thread still has a frame\n");
     236
     237    Py_CLEAR(tstate->frame);
     238
     239    Py_CLEAR(tstate->dict);
     240    Py_CLEAR(tstate->async_exc);
     241
     242    Py_CLEAR(tstate->curexc_type);
     243    Py_CLEAR(tstate->curexc_value);
     244    Py_CLEAR(tstate->curexc_traceback);
     245
     246    Py_CLEAR(tstate->exc_type);
     247    Py_CLEAR(tstate->exc_value);
     248    Py_CLEAR(tstate->exc_traceback);
     249
     250    tstate->c_profilefunc = NULL;
     251    tstate->c_tracefunc = NULL;
     252    Py_CLEAR(tstate->c_profileobj);
     253    Py_CLEAR(tstate->c_traceobj);
    234254}
    235255
     
    239259tstate_delete_common(PyThreadState *tstate)
    240260{
    241         PyInterpreterState *interp;
    242         PyThreadState **p;
    243         PyThreadState *prev_p = NULL;
    244         if (tstate == NULL)
    245                 Py_FatalError("PyThreadState_Delete: NULL tstate");
    246         interp = tstate->interp;
    247         if (interp == NULL)
    248                 Py_FatalError("PyThreadState_Delete: NULL interp");
    249         HEAD_LOCK();
    250         for (p = &interp->tstate_head; ; p = &(*p)->next) {
    251                 if (*p == NULL)
    252                         Py_FatalError(
    253                                 "PyThreadState_Delete: invalid tstate");
    254                 if (*p == tstate)
    255                         break;
    256                 /* Sanity check.  These states should never happen but if
    257                 * they do we must abort.  Otherwise we'll end up spinning in
    258                 * in a tight loop with the lock held.  A similar check is done
    259                 * in thread.c find_key().  */
    260                 if (*p == prev_p)
    261                         Py_FatalError(
    262                                 "PyThreadState_Delete: small circular list(!)"
    263                                 " and tstate not found.");
    264                 prev_p = *p;
    265                 if ((*p)->next == interp->tstate_head)
    266                         Py_FatalError(
    267                                 "PyThreadState_Delete: circular list(!) and"
    268                                 " tstate not found.");
    269         }
    270         *p = tstate->next;
    271         HEAD_UNLOCK();
    272         free(tstate);
     261    PyInterpreterState *interp;
     262    PyThreadState **p;
     263    PyThreadState *prev_p = NULL;
     264    if (tstate == NULL)
     265        Py_FatalError("PyThreadState_Delete: NULL tstate");
     266    interp = tstate->interp;
     267    if (interp == NULL)
     268        Py_FatalError("PyThreadState_Delete: NULL interp");
     269    HEAD_LOCK();
     270    for (p = &interp->tstate_head; ; p = &(*p)->next) {
     271        if (*p == NULL)
     272            Py_FatalError(
     273                "PyThreadState_Delete: invalid tstate");
     274        if (*p == tstate)
     275            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        if (*p == prev_p)
     281            Py_FatalError(
     282                "PyThreadState_Delete: small circular list(!)"
     283                " and tstate not found.");
     284        prev_p = *p;
     285        if ((*p)->next == interp->tstate_head)
     286            Py_FatalError(
     287                "PyThreadState_Delete: circular list(!) and"
     288                " tstate not found.");
     289    }
     290    *p = tstate->next;
     291    HEAD_UNLOCK();
     292    free(tstate);
    273293}
    274294
     
    277297PyThreadState_Delete(PyThreadState *tstate)
    278298{
    279         if (tstate == _PyThreadState_Current)
    280                 Py_FatalError("PyThreadState_Delete: tstate is still current");
    281         tstate_delete_common(tstate);
    282 #ifdef WITH_THREAD
    283         if (autoTLSkey && PyThread_get_key_value(autoTLSkey) == tstate)
    284                 PyThread_delete_key_value(autoTLSkey);
     299    if (tstate == _PyThreadState_Current)
     300        Py_FatalError("PyThreadState_Delete: tstate is still current");
     301    tstate_delete_common(tstate);
     302#ifdef WITH_THREAD
     303    if (autoInterpreterState && PyThread_get_key_value(autoTLSkey) == tstate)
     304        PyThread_delete_key_value(autoTLSkey);
    285305#endif /* WITH_THREAD */
    286306}
     
    291311PyThreadState_DeleteCurrent()
    292312{
    293         PyThreadState *tstate = _PyThreadState_Current;
    294         if (tstate == NULL)
    295                 Py_FatalError(
    296                         "PyThreadState_DeleteCurrent: no current tstate");
    297         _PyThreadState_Current = NULL;
    298         tstate_delete_common(tstate);
    299         if (autoTLSkey && PyThread_get_key_value(autoTLSkey) == tstate)
    300                 PyThread_delete_key_value(autoTLSkey);
    301         PyEval_ReleaseLock();
     313    PyThreadState *tstate = _PyThreadState_Current;
     314    if (tstate == NULL)
     315        Py_FatalError(
     316            "PyThreadState_DeleteCurrent: no current tstate");
     317    _PyThreadState_Current = NULL;
     318    tstate_delete_common(tstate);
     319    if (autoInterpreterState && PyThread_get_key_value(autoTLSkey) == tstate)
     320        PyThread_delete_key_value(autoTLSkey);
     321    PyEval_ReleaseLock();
    302322}
    303323#endif /* WITH_THREAD */
     
    307327PyThreadState_Get(void)
    308328{
    309         if (_PyThreadState_Current == NULL)
    310                 Py_FatalError("PyThreadState_Get: no current thread");
    311 
    312         return _PyThreadState_Current;
     329    if (_PyThreadState_Current == NULL)
     330        Py_FatalError("PyThreadState_Get: no current thread");
     331
     332    return _PyThreadState_Current;
    313333}
    314334
     
    317337PyThreadState_Swap(PyThreadState *newts)
    318338{
    319         PyThreadState *oldts = _PyThreadState_Current;
    320 
    321         _PyThreadState_Current = newts;
    322         /* It should not be possible for more than one thread state
    323            to be used for a thread.  Check this the best we can in debug
    324            builds.
    325         */
     339    PyThreadState *oldts = _PyThreadState_Current;
     340
     341    _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    */
    326346#if defined(Py_DEBUG) && defined(WITH_THREAD)
    327         if (newts) {
    328                 /* This can be called from PyEval_RestoreThread(). Similar
    329                    to it, we need to ensure errno doesn't change.
    330                 */
    331                 int err = errno;
    332                 PyThreadState *check = PyGILState_GetThisThreadState();
    333                 if (check && check->interp == newts->interp && check != newts)
    334                         Py_FatalError("Invalid thread state for this thread");
    335                 errno = err;
    336         }
    337 #endif
    338         return oldts;
     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    return oldts;
    339359}
    340360
     
    348368PyThreadState_GetDict(void)
    349369{
    350         if (_PyThreadState_Current == NULL)
    351                 return NULL;
    352 
    353         if (_PyThreadState_Current->dict == NULL) {
    354                 PyObject *d;
    355                 _PyThreadState_Current->dict = d = PyDict_New();
    356                 if (d == NULL)
    357                         PyErr_Clear();
    358         }
    359         return _PyThreadState_Current->dict;
     370    if (_PyThreadState_Current == NULL)
     371        return NULL;
     372
     373    if (_PyThreadState_Current->dict == NULL) {
     374        PyObject *d;
     375        _PyThreadState_Current->dict = d = PyDict_New();
     376        if (d == NULL)
     377            PyErr_Clear();
     378    }
     379    return _PyThreadState_Current->dict;
    360380}
    361381
     
    371391int
    372392PyThreadState_SetAsyncExc(long id, PyObject *exc) {
    373         PyThreadState *tstate = PyThreadState_GET();
    374         PyInterpreterState *interp = tstate->interp;
    375         PyThreadState *p;
    376 
    377         /* Although the GIL is held, a few C API functions can be called
    378         * without the GIL held, and in particular some that create and
    379         * destroy thread and interpreter states.  Those can mutate the
    380         * list of thread states we're traversing, so to prevent that we lock
    381         * head_mutex for the duration.
    382         */
    383         HEAD_LOCK();
    384         for (p = interp->tstate_head; p != NULL; p = p->next) {
    385                 if (p->thread_id == id) {
    386                         /* Tricky:  we need to decref the current value
    387                         * (if any) in p->async_exc, but that can in turn
    388                         * allow arbitrary Python code to run, including
    389                         * perhaps calls to this function.  To prevent
    390                         * deadlock, we need to release head_mutex before
    391                         * the decref.
    392                         */
    393                         PyObject *old_exc = p->async_exc;
    394                         Py_XINCREF(exc);
    395                         p->async_exc = exc;
    396                         HEAD_UNLOCK();
    397                         Py_XDECREF(old_exc);
    398                         return 1;
    399                 }
    400         }
    401         HEAD_UNLOCK();
    402         return 0;
     393    PyThreadState *tstate = PyThreadState_GET();
     394    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    for (p = interp->tstate_head; p != NULL; p = p->next) {
     405        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            PyObject *old_exc = p->async_exc;
     414            Py_XINCREF(exc);
     415            p->async_exc = exc;
     416            HEAD_UNLOCK();
     417            Py_XDECREF(old_exc);
     418            return 1;
     419        }
     420    }
     421    HEAD_UNLOCK();
     422    return 0;
    403423}
    404424
     
    410430PyInterpreterState_Head(void)
    411431{
    412         return interp_head;
     432    return interp_head;
    413433}
    414434
    415435PyInterpreterState *
    416436PyInterpreterState_Next(PyInterpreterState *interp) {
    417         return interp->next;
     437    return interp->next;
    418438}
    419439
    420440PyThreadState *
    421441PyInterpreterState_ThreadHead(PyInterpreterState *interp) {
    422         return interp->tstate_head;
     442    return interp->tstate_head;
    423443}
    424444
    425445PyThreadState *
    426446PyThreadState_Next(PyThreadState *tstate) {
    427         return tstate->next;
     447    return tstate->next;
    428448}
    429449
     
    436456_PyThread_CurrentFrames(void)
    437457{
    438         PyObject *result;
    439         PyInterpreterState *i;
    440 
    441         result = PyDict_New();
    442         if (result == NULL)
    443                 return NULL;
    444 
    445         /* for i in all interpreters:
    446         *     for t in all of i's thread states:
    447         *          if t's frame isn't NULL, map t's id to its frame
    448          * Because these lists can mutute even when the GIL is held, we
    449         * need to grab head_mutex for the duration.
    450         */
    451         HEAD_LOCK();
    452         for (i = interp_head; i != NULL; i = i->next) {
    453                 PyThreadState *t;
    454                 for (t = i->tstate_head; t != NULL; t = t->next) {
    455                         PyObject *id;
    456                         int stat;
    457                         struct _frame *frame = t->frame;
    458                         if (frame == NULL)
    459                                 continue;
    460                         id = PyInt_FromLong(t->thread_id);
    461                         if (id == NULL)
    462                                 goto Fail;
    463                         stat = PyDict_SetItem(result, id, (PyObject *)frame);
    464                         Py_DECREF(id);
    465                         if (stat < 0)
    466                                 goto Fail;
    467                 }
    468         }
    469         HEAD_UNLOCK();
    470         return result;
     458    PyObject *result;
     459    PyInterpreterState *i;
     460
     461    result = PyDict_New();
     462    if (result == NULL)
     463        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    for (i = interp_head; i != NULL; i = i->next) {
     473        PyThreadState *t;
     474        for (t = i->tstate_head; t != NULL; t = t->next) {
     475            PyObject *id;
     476            int stat;
     477            struct _frame *frame = t->frame;
     478            if (frame == NULL)
     479                continue;
     480            id = PyInt_FromLong(t->thread_id);
     481            if (id == NULL)
     482                goto Fail;
     483            stat = PyDict_SetItem(result, id, (PyObject *)frame);
     484            Py_DECREF(id);
     485            if (stat < 0)
     486                goto Fail;
     487        }
     488    }
     489    HEAD_UNLOCK();
     490    return result;
    471491
    472492 Fail:
    473         HEAD_UNLOCK();
    474         Py_DECREF(result);
    475         return NULL;
     493    HEAD_UNLOCK();
     494    Py_DECREF(result);
     495    return NULL;
    476496}
    477497
     
    490510PyThreadState_IsCurrent(PyThreadState *tstate)
    491511{
    492         /* Must be the tstate for this thread */
    493         assert(PyGILState_GetThisThreadState()==tstate);
    494         /* On Windows at least, simple reads and writes to 32 bit values
    495            are atomic.
    496         */
    497         return tstate == _PyThreadState_Current;
     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;
    498518}
    499519
     
    504524_PyGILState_Init(PyInterpreterState *i, PyThreadState *t)
    505525{
    506         assert(i && t); /* must init with valid states */
    507         autoTLSkey = PyThread_create_key();
    508         autoInterpreterState = i;
    509         assert(PyThread_get_key_value(autoTLSkey) == NULL);
    510         assert(t->gilstate_counter == 0);
    511 
    512         _PyGILState_NoteThreadState(t);
     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);
    513533}
    514534
     
    516536_PyGILState_Fini(void)
    517537{
    518         PyThread_delete_key(autoTLSkey);
    519         autoTLSkey = 0;
    520         autoInterpreterState = NULL;
     538    PyThread_delete_key(autoTLSkey);
     539    autoInterpreterState = NULL;
    521540}
    522541
     
    529548_PyGILState_NoteThreadState(PyThreadState* tstate)
    530549{
    531         /* If autoTLSkey is 0, this must be the very first threadstate created
    532            in Py_Initialize().  Don't do anything for now (we'll be back here
    533           when _PyGILState_Init is called). */
    534         if (!autoTLSkey)
    535                 return;
    536 
    537         /* Stick the thread state for this thread in thread local storage.
    538 
    539            The only situation where you can legitimately have more than one
    540            thread state for an OS level thread is when there are multiple
    541            interpreters, when:
    542 
    543                a) You shouldn't really be using the PyGILState_ APIs anyway,
    544                   and:
    545 
    546                b) The slightly odd way PyThread_set_key_value works (see
    547                   comments by its implementation) means that the first thread
    548                   state created for that given OS level thread will "win",
    549                   which seems reasonable behaviour.
    550         */
    551         if (PyThread_set_key_value(autoTLSkey, (void *)tstate) < 0)
    552                 Py_FatalError("Couldn't create autoTLSkey mapping");
    553 
    554         /* PyGILState_Release must not try to delete this thread state. */
    555         tstate->gilstate_counter = 1;
     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;
    556575}
    557576
     
    560579PyGILState_GetThisThreadState(void)
    561580{
    562         if (autoInterpreterState == NULL || autoTLSkey == 0)
    563                 return NULL;
    564         return (PyThreadState *)PyThread_get_key_value(autoTLSkey);
     581    if (autoInterpreterState == NULL)
     582        return NULL;
     583    return (PyThreadState *)PyThread_get_key_value(autoTLSkey);
    565584}
    566585
     
    568587PyGILState_Ensure(void)
    569588{
    570         int current;
    571         PyThreadState *tcur;
    572         /* Note that we do not auto-init Python here - apart from
    573            potential races with 2 threads auto-initializing, pep-311
    574            spells out other issues.  Embedders are expected to have
    575            called Py_Initialize() and usually PyEval_InitThreads().
    576         */
    577         assert(autoInterpreterState); /* Py_Initialize() hasn't been called! */
    578         tcur = (PyThreadState *)PyThread_get_key_value(autoTLSkey);
    579         if (tcur == NULL) {
    580                 /* Create a new thread state for this thread */
    581                 tcur = PyThreadState_New(autoInterpreterState);
    582                 if (tcur == NULL)
    583                         Py_FatalError("Couldn't create thread-state for new thread");
    584                 /* This is our thread state!  We'll need to delete it in the
    585                    matching call to PyGILState_Release(). */
    586                 tcur->gilstate_counter = 0;
    587                 current = 0; /* new thread state is never current */
    588         }
    589         else
    590                 current = PyThreadState_IsCurrent(tcur);
    591         if (current == 0)
    592                 PyEval_RestoreThread(tcur);
    593         /* Update our counter in the thread-state - no need for locks:
    594            - tcur will remain valid as we hold the GIL.
    595            - the counter is safe as we are the only thread "allowed"
    596              to modify this value
    597         */
    598         ++tcur->gilstate_counter;
    599         return current ? PyGILState_LOCKED : PyGILState_UNLOCKED;
     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;
    600619}
    601620
     
    603622PyGILState_Release(PyGILState_STATE oldstate)
    604623{
    605         PyThreadState *tcur = (PyThreadState *)PyThread_get_key_value(
    606                                                                 autoTLSkey);
    607         if (tcur == NULL)
    608                 Py_FatalError("auto-releasing thread-state, "
    609                               "but no thread-state for this thread");
    610         /* We must hold the GIL and have our thread state current */
    611         /* XXX - remove the check - the assert should be fine,
    612            but while this is very new (April 2003), the extra check
    613            by release-only users can't hurt.
    614         */
    615         if (! PyThreadState_IsCurrent(tcur))
    616                 Py_FatalError("This thread state must be current when releasing");
    617         assert(PyThreadState_IsCurrent(tcur));
    618         --tcur->gilstate_counter;
    619         assert(tcur->gilstate_counter >= 0); /* illegal counter value */
    620 
    621         /* If we're going to destroy this thread-state, we must
    622          * clear it while the GIL is held, as destructors may run.
    623          */
    624         if (tcur->gilstate_counter == 0) {
    625                 /* can't have been locked when we created it */
    626                 assert(oldstate == PyGILState_UNLOCKED);
    627                 PyThreadState_Clear(tcur);
    628                 /* Delete the thread-state.  Note this releases the GIL too!
    629                  * It's vital that the GIL be held here, to avoid shutdown
    630                  * races; see bugs 225673 and 1061968 (that nasty bug has a
    631                  * habit of coming back).
    632                  */
    633                 PyThreadState_DeleteCurrent();
    634         }
    635         /* Release the lock if necessary */
    636         else if (oldstate == PyGILState_UNLOCKED)
    637                 PyEval_SaveThread();
    638 }
     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 */
    639660
    640661#ifdef __cplusplus
     
    642663#endif
    643664
    644 #endif /* WITH_THREAD */
    645 
    646 
     665
Note: See TracChangeset for help on using the changeset viewer.