diff options
Diffstat (limited to 'Python/pystate.c')
| -rw-r--r-- | Python/pystate.c | 95 |
1 files changed, 23 insertions, 72 deletions
diff --git a/Python/pystate.c b/Python/pystate.c index d5d98b0..d7d127b 100644 --- a/Python/pystate.c +++ b/Python/pystate.c @@ -22,6 +22,9 @@ the expense of doing their own locking). #endif #endif +#ifdef __cplusplus +extern "C" { +#endif #ifdef WITH_THREAD #include "pythread.h" @@ -30,10 +33,6 @@ static PyThread_type_lock head_mutex = NULL; /* Protects interp->tstate_head */ #define HEAD_LOCK() PyThread_acquire_lock(head_mutex, WAIT_LOCK) #define HEAD_UNLOCK() PyThread_release_lock(head_mutex) -#ifdef __cplusplus -extern "C" { -#endif - /* The single PyInterpreterState used by this process' GILState implementation */ @@ -47,9 +46,7 @@ static int autoTLSkey = 0; static PyInterpreterState *interp_head = NULL; -/* Assuming the current thread holds the GIL, this is the - PyThreadState for the current thread. */ -_Py_atomic_address _PyThreadState_Current = {NULL}; +PyThreadState *_PyThreadState_Current = NULL; PyThreadFrameGetter _PyThreadState_GetFrame = NULL; #ifdef WITH_THREAD @@ -71,14 +68,12 @@ PyInterpreterState_New(void) #endif interp->modules = NULL; interp->modules_reloading = NULL; - interp->modules_by_index = NULL; interp->sysdict = NULL; interp->builtins = NULL; interp->tstate_head = NULL; interp->codec_search_path = NULL; interp->codec_search_cache = NULL; interp->codec_error_registry = NULL; - interp->codecs_initialized = 0; #ifdef HAVE_DLOPEN #ifdef RTLD_NOW interp->dlopenflags = RTLD_NOW; @@ -112,7 +107,6 @@ PyInterpreterState_Clear(PyInterpreterState *interp) Py_CLEAR(interp->codec_search_cache); Py_CLEAR(interp->codec_error_registry); Py_CLEAR(interp->modules); - Py_CLEAR(interp->modules_by_index); Py_CLEAR(interp->modules_reloading); Py_CLEAR(interp->sysdict); Py_CLEAR(interp->builtins); @@ -172,8 +166,6 @@ new_threadstate(PyInterpreterState *interp, int init) tstate->frame = NULL; tstate->recursion_depth = 0; - tstate->overflowed = 0; - tstate->recursion_critical = 0; tstate->tracing = 0; tstate->use_tracing = 0; tstate->tick_counter = 0; @@ -232,41 +224,6 @@ _PyThreadState_Init(PyThreadState *tstate) #endif } -PyObject* -PyState_FindModule(struct PyModuleDef* m) -{ - Py_ssize_t index = m->m_base.m_index; - PyInterpreterState *state = PyThreadState_GET()->interp; - PyObject *res; - if (index == 0) - return NULL; - if (state->modules_by_index == NULL) - return NULL; - if (index > PyList_GET_SIZE(state->modules_by_index)) - return NULL; - res = PyList_GET_ITEM(state->modules_by_index, index); - return res==Py_None ? NULL : res; -} - -int -_PyState_AddModule(PyObject* module, struct PyModuleDef* def) -{ - PyInterpreterState *state = PyThreadState_GET()->interp; - if (!def) - return -1; - if (!state->modules_by_index) { - state->modules_by_index = PyList_New(0); - if (!state->modules_by_index) - return -1; - } - while(PyList_GET_SIZE(state->modules_by_index) <= def->m_base.m_index) - if (PyList_Append(state->modules_by_index, Py_None) < 0) - return -1; - Py_INCREF(module); - return PyList_SetItem(state->modules_by_index, - def->m_base.m_index, module); -} - void PyThreadState_Clear(PyThreadState *tstate) { @@ -336,7 +293,7 @@ tstate_delete_common(PyThreadState *tstate) void PyThreadState_Delete(PyThreadState *tstate) { - if (tstate == _Py_atomic_load_relaxed(&_PyThreadState_Current)) + if (tstate == _PyThreadState_Current) Py_FatalError("PyThreadState_Delete: tstate is still current"); tstate_delete_common(tstate); #ifdef WITH_THREAD @@ -350,12 +307,11 @@ PyThreadState_Delete(PyThreadState *tstate) void PyThreadState_DeleteCurrent() { - PyThreadState *tstate = (PyThreadState*)_Py_atomic_load_relaxed( - &_PyThreadState_Current); + PyThreadState *tstate = _PyThreadState_Current; if (tstate == NULL) Py_FatalError( "PyThreadState_DeleteCurrent: no current tstate"); - _Py_atomic_store_relaxed(&_PyThreadState_Current, NULL); + _PyThreadState_Current = NULL; tstate_delete_common(tstate); if (autoInterpreterState && PyThread_get_key_value(autoTLSkey) == tstate) PyThread_delete_key_value(autoTLSkey); @@ -367,22 +323,19 @@ PyThreadState_DeleteCurrent() PyThreadState * PyThreadState_Get(void) { - PyThreadState *tstate = (PyThreadState*)_Py_atomic_load_relaxed( - &_PyThreadState_Current); - if (tstate == NULL) + if (_PyThreadState_Current == NULL) Py_FatalError("PyThreadState_Get: no current thread"); - return tstate; + return _PyThreadState_Current; } PyThreadState * PyThreadState_Swap(PyThreadState *newts) { - PyThreadState *oldts = (PyThreadState*)_Py_atomic_load_relaxed( - &_PyThreadState_Current); + PyThreadState *oldts = _PyThreadState_Current; - _Py_atomic_store_relaxed(&_PyThreadState_Current, newts); + _PyThreadState_Current = newts; /* It should not be possible for more than one thread state to be used for a thread. Check this the best we can in debug builds. @@ -411,18 +364,16 @@ PyThreadState_Swap(PyThreadState *newts) PyObject * PyThreadState_GetDict(void) { - PyThreadState *tstate = (PyThreadState*)_Py_atomic_load_relaxed( - &_PyThreadState_Current); - if (tstate == NULL) + if (_PyThreadState_Current == NULL) return NULL; - if (tstate->dict == NULL) { + if (_PyThreadState_Current->dict == NULL) { PyObject *d; - tstate->dict = d = PyDict_New(); + _PyThreadState_Current->dict = d = PyDict_New(); if (d == NULL) PyErr_Clear(); } - return tstate->dict; + return _PyThreadState_Current->dict; } @@ -461,7 +412,6 @@ PyThreadState_SetAsyncExc(long id, PyObject *exc) { p->async_exc = exc; HEAD_UNLOCK(); Py_XDECREF(old_exc); - _PyEval_SignalAsyncExc(); return 1; } } @@ -512,7 +462,7 @@ _PyThread_CurrentFrames(void) /* for i in all interpreters: * for t in all of i's thread states: * if t's frame isn't NULL, map t's id to its frame - * Because these lists can mutute even when the GIL is held, we + * Because these lists can mutate even when the GIL is held, we * need to grab head_mutex for the duration. */ HEAD_LOCK(); @@ -524,7 +474,7 @@ _PyThread_CurrentFrames(void) struct _frame *frame = t->frame; if (frame == NULL) continue; - id = PyLong_FromLong(t->thread_id); + id = PyInt_FromLong(t->thread_id); if (id == NULL) goto Fail; stat = PyDict_SetItem(result, id, (PyObject *)frame); @@ -558,7 +508,10 @@ PyThreadState_IsCurrent(PyThreadState *tstate) { /* Must be the tstate for this thread */ assert(PyGILState_GetThisThreadState()==tstate); - return tstate == _Py_atomic_load_relaxed(&_PyThreadState_Current); + /* On Windows at least, simple reads and writes to 32 bit values + are atomic. + */ + return tstate == _PyThreadState_Current; } /* Internal initialization/finalization functions called by @@ -569,8 +522,6 @@ _PyGILState_Init(PyInterpreterState *i, PyThreadState *t) { assert(i && t); /* must init with valid states */ autoTLSkey = PyThread_create_key(); - if (autoTLSkey == -1) - Py_FatalError("Could not allocate TLS entry"); autoInterpreterState = i; assert(PyThread_get_key_value(autoTLSkey) == NULL); assert(t->gilstate_counter == 0); @@ -702,10 +653,10 @@ PyGILState_Release(PyGILState_STATE oldstate) PyEval_SaveThread(); } +#endif /* WITH_THREAD */ + #ifdef __cplusplus } #endif -#endif /* WITH_THREAD */ - |
