1997-05-05 17:56:21 -03:00
|
|
|
|
|
|
|
/* Thread and interpreter state structures and their interfaces */
|
|
|
|
|
|
|
|
#include "Python.h"
|
|
|
|
|
2004-10-09 23:47:33 -03:00
|
|
|
/* --------------------------------------------------------------------------
|
|
|
|
CAUTION
|
|
|
|
|
|
|
|
Always use malloc() and free() directly in this file. A number of these
|
|
|
|
functions are advertised as safe to call when the GIL isn't held, and in
|
|
|
|
a debug build Python redirects (e.g.) PyMem_NEW (etc) to Python's debugging
|
|
|
|
obmalloc functions. Those aren't thread-safe (they rely on the GIL to avoid
|
|
|
|
the expense of doing their own locking).
|
|
|
|
-------------------------------------------------------------------------- */
|
|
|
|
|
2001-07-18 13:17:16 -03:00
|
|
|
#ifdef HAVE_DLOPEN
|
|
|
|
#ifdef HAVE_DLFCN_H
|
|
|
|
#include <dlfcn.h>
|
|
|
|
#endif
|
|
|
|
#ifndef RTLD_LAZY
|
|
|
|
#define RTLD_LAZY 1
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
1999-06-18 11:22:24 -03:00
|
|
|
#ifdef WITH_THREAD
|
|
|
|
#include "pythread.h"
|
|
|
|
static PyThread_type_lock head_mutex = NULL; /* Protects interp->tstate_head */
|
2000-08-04 18:27:47 -03:00
|
|
|
#define HEAD_INIT() (void)(head_mutex || (head_mutex = PyThread_allocate_lock()))
|
1999-06-18 11:22:24 -03:00
|
|
|
#define HEAD_LOCK() PyThread_acquire_lock(head_mutex, WAIT_LOCK)
|
|
|
|
#define HEAD_UNLOCK() PyThread_release_lock(head_mutex)
|
2005-06-20 13:52:57 -03:00
|
|
|
|
2006-04-12 23:06:09 -03:00
|
|
|
#ifdef __cplusplus
|
|
|
|
extern "C" {
|
|
|
|
#endif
|
|
|
|
|
2005-06-20 13:52:57 -03:00
|
|
|
/* The single PyInterpreterState used by this process'
|
|
|
|
GILState implementation
|
|
|
|
*/
|
|
|
|
static PyInterpreterState *autoInterpreterState = NULL;
|
|
|
|
static int autoTLSkey = 0;
|
1999-06-18 11:22:24 -03:00
|
|
|
#else
|
|
|
|
#define HEAD_INIT() /* Nothing */
|
|
|
|
#define HEAD_LOCK() /* Nothing */
|
|
|
|
#define HEAD_UNLOCK() /* Nothing */
|
|
|
|
#endif
|
|
|
|
|
1997-08-02 00:10:38 -03:00
|
|
|
static PyInterpreterState *interp_head = NULL;
|
1997-05-05 17:56:21 -03:00
|
|
|
|
1998-12-21 14:27:28 -04:00
|
|
|
PyThreadState *_PyThreadState_Current = NULL;
|
2003-02-19 11:53:17 -04:00
|
|
|
PyThreadFrameGetter _PyThreadState_GetFrame = NULL;
|
1997-05-05 17:56:21 -03:00
|
|
|
|
2005-09-30 05:20:24 -03:00
|
|
|
#ifdef WITH_THREAD
|
2005-06-20 13:52:57 -03:00
|
|
|
static void _PyGILState_NoteThreadState(PyThreadState* tstate);
|
2005-09-30 05:20:24 -03:00
|
|
|
#endif
|
2005-06-20 13:52:57 -03:00
|
|
|
|
1997-05-05 17:56:21 -03:00
|
|
|
|
|
|
|
PyInterpreterState *
|
2000-07-22 15:47:25 -03:00
|
|
|
PyInterpreterState_New(void)
|
1997-05-05 17:56:21 -03:00
|
|
|
{
|
2010-05-09 11:46:46 -03:00
|
|
|
PyInterpreterState *interp = (PyInterpreterState *)
|
|
|
|
malloc(sizeof(PyInterpreterState));
|
1997-08-02 00:10:38 -03:00
|
|
|
|
2010-05-09 11:46:46 -03:00
|
|
|
if (interp != NULL) {
|
|
|
|
HEAD_INIT();
|
2006-07-21 02:32:28 -03:00
|
|
|
#ifdef WITH_THREAD
|
2010-05-09 11:46:46 -03:00
|
|
|
if (head_mutex == NULL)
|
|
|
|
Py_FatalError("Can't initialize threads for interpreter");
|
2006-07-21 02:32:28 -03:00
|
|
|
#endif
|
2010-05-09 11:46:46 -03:00
|
|
|
interp->modules = NULL;
|
|
|
|
interp->modules_reloading = 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;
|
2001-07-18 13:17:16 -03:00
|
|
|
#ifdef HAVE_DLOPEN
|
|
|
|
#ifdef RTLD_NOW
|
2010-05-09 11:46:46 -03:00
|
|
|
interp->dlopenflags = RTLD_NOW;
|
2001-07-18 13:17:16 -03:00
|
|
|
#else
|
2010-05-09 11:46:46 -03:00
|
|
|
interp->dlopenflags = RTLD_LAZY;
|
2001-07-18 13:17:16 -03:00
|
|
|
#endif
|
2004-06-08 05:17:44 -03:00
|
|
|
#endif
|
|
|
|
#ifdef WITH_TSC
|
2010-05-09 11:46:46 -03:00
|
|
|
interp->tscdump = 0;
|
2001-07-18 13:17:16 -03:00
|
|
|
#endif
|
1997-08-02 00:10:38 -03:00
|
|
|
|
2010-05-09 11:46:46 -03:00
|
|
|
HEAD_LOCK();
|
|
|
|
interp->next = interp_head;
|
|
|
|
interp_head = interp;
|
|
|
|
HEAD_UNLOCK();
|
|
|
|
}
|
1997-08-02 00:10:38 -03:00
|
|
|
|
2010-05-09 11:46:46 -03:00
|
|
|
return interp;
|
1997-05-05 17:56:21 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
2000-07-22 15:47:25 -03:00
|
|
|
PyInterpreterState_Clear(PyInterpreterState *interp)
|
1997-08-02 00:10:38 -03:00
|
|
|
{
|
2010-05-09 11:46:46 -03:00
|
|
|
PyThreadState *p;
|
|
|
|
HEAD_LOCK();
|
|
|
|
for (p = interp->tstate_head; p != NULL; p = p->next)
|
|
|
|
PyThreadState_Clear(p);
|
|
|
|
HEAD_UNLOCK();
|
|
|
|
Py_CLEAR(interp->codec_search_path);
|
|
|
|
Py_CLEAR(interp->codec_search_cache);
|
|
|
|
Py_CLEAR(interp->codec_error_registry);
|
|
|
|
Py_CLEAR(interp->modules);
|
|
|
|
Py_CLEAR(interp->modules_reloading);
|
|
|
|
Py_CLEAR(interp->sysdict);
|
|
|
|
Py_CLEAR(interp->builtins);
|
1997-08-02 00:10:38 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
2000-07-22 15:47:25 -03:00
|
|
|
zapthreads(PyInterpreterState *interp)
|
1997-05-05 17:56:21 -03:00
|
|
|
{
|
2010-05-09 11:46:46 -03:00
|
|
|
PyThreadState *p;
|
|
|
|
/* No need to lock the mutex here because this should only happen
|
|
|
|
when the threads are all really dead (XXX famous last words). */
|
|
|
|
while ((p = interp->tstate_head) != NULL) {
|
|
|
|
PyThreadState_Delete(p);
|
|
|
|
}
|
1997-08-02 00:10:38 -03:00
|
|
|
}
|
1997-05-05 17:56:21 -03:00
|
|
|
|
1997-08-02 00:10:38 -03:00
|
|
|
|
|
|
|
void
|
2000-07-22 15:47:25 -03:00
|
|
|
PyInterpreterState_Delete(PyInterpreterState *interp)
|
1997-08-02 00:10:38 -03:00
|
|
|
{
|
2010-05-09 11:46:46 -03:00
|
|
|
PyInterpreterState **p;
|
|
|
|
zapthreads(interp);
|
|
|
|
HEAD_LOCK();
|
|
|
|
for (p = &interp_head; ; p = &(*p)->next) {
|
|
|
|
if (*p == NULL)
|
|
|
|
Py_FatalError(
|
|
|
|
"PyInterpreterState_Delete: invalid interp");
|
|
|
|
if (*p == interp)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (interp->tstate_head != NULL)
|
|
|
|
Py_FatalError("PyInterpreterState_Delete: remaining threads");
|
|
|
|
*p = interp->next;
|
|
|
|
HEAD_UNLOCK();
|
|
|
|
free(interp);
|
1997-05-05 17:56:21 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-11-08 08:53:11 -04:00
|
|
|
/* Default implementation for _PyThreadState_GetFrame */
|
|
|
|
static struct _frame *
|
|
|
|
threadstate_getframe(PyThreadState *self)
|
|
|
|
{
|
2010-05-09 11:46:46 -03:00
|
|
|
return self->frame;
|
2002-11-08 08:53:11 -04:00
|
|
|
}
|
|
|
|
|
2010-03-03 19:20:25 -04:00
|
|
|
static PyThreadState *
|
|
|
|
new_threadstate(PyInterpreterState *interp, int init)
|
1997-05-05 17:56:21 -03:00
|
|
|
{
|
2010-05-09 11:46:46 -03:00
|
|
|
PyThreadState *tstate = (PyThreadState *)malloc(sizeof(PyThreadState));
|
2004-10-09 23:47:33 -03:00
|
|
|
|
2010-05-09 11:46:46 -03:00
|
|
|
if (_PyThreadState_GetFrame == NULL)
|
|
|
|
_PyThreadState_GetFrame = threadstate_getframe;
|
1997-08-02 00:10:38 -03:00
|
|
|
|
2010-05-09 11:46:46 -03:00
|
|
|
if (tstate != NULL) {
|
|
|
|
tstate->interp = interp;
|
1997-05-05 17:56:21 -03:00
|
|
|
|
2010-05-09 11:46:46 -03:00
|
|
|
tstate->frame = NULL;
|
|
|
|
tstate->recursion_depth = 0;
|
|
|
|
tstate->tracing = 0;
|
|
|
|
tstate->use_tracing = 0;
|
|
|
|
tstate->tick_counter = 0;
|
|
|
|
tstate->gilstate_counter = 0;
|
|
|
|
tstate->async_exc = NULL;
|
2003-07-13 07:41:53 -03:00
|
|
|
#ifdef WITH_THREAD
|
2010-05-09 11:46:46 -03:00
|
|
|
tstate->thread_id = PyThread_get_thread_ident();
|
2003-07-13 07:41:53 -03:00
|
|
|
#else
|
2010-05-09 11:46:46 -03:00
|
|
|
tstate->thread_id = 0;
|
2003-07-13 07:41:53 -03:00
|
|
|
#endif
|
1997-05-05 17:56:21 -03:00
|
|
|
|
2010-05-09 11:46:46 -03:00
|
|
|
tstate->dict = NULL;
|
1998-04-10 17:18:25 -03:00
|
|
|
|
2010-05-09 11:46:46 -03:00
|
|
|
tstate->curexc_type = NULL;
|
|
|
|
tstate->curexc_value = NULL;
|
|
|
|
tstate->curexc_traceback = NULL;
|
1997-05-05 17:56:21 -03:00
|
|
|
|
2010-05-09 11:46:46 -03:00
|
|
|
tstate->exc_type = NULL;
|
|
|
|
tstate->exc_value = NULL;
|
|
|
|
tstate->exc_traceback = NULL;
|
1997-05-05 17:56:21 -03:00
|
|
|
|
2010-05-09 11:46:46 -03:00
|
|
|
tstate->c_profilefunc = NULL;
|
|
|
|
tstate->c_tracefunc = NULL;
|
|
|
|
tstate->c_profileobj = NULL;
|
|
|
|
tstate->c_traceobj = NULL;
|
1997-05-05 17:56:21 -03:00
|
|
|
|
2010-05-09 11:46:46 -03:00
|
|
|
if (init)
|
|
|
|
_PyThreadState_Init(tstate);
|
2005-06-20 13:52:57 -03:00
|
|
|
|
2010-05-09 11:46:46 -03:00
|
|
|
HEAD_LOCK();
|
|
|
|
tstate->next = interp->tstate_head;
|
|
|
|
interp->tstate_head = tstate;
|
|
|
|
HEAD_UNLOCK();
|
|
|
|
}
|
1997-08-02 00:10:38 -03:00
|
|
|
|
2010-05-09 11:46:46 -03:00
|
|
|
return tstate;
|
1997-05-05 17:56:21 -03:00
|
|
|
}
|
|
|
|
|
2010-03-03 19:20:25 -04:00
|
|
|
PyThreadState *
|
|
|
|
PyThreadState_New(PyInterpreterState *interp)
|
|
|
|
{
|
2010-05-09 11:46:46 -03:00
|
|
|
return new_threadstate(interp, 1);
|
2010-03-03 19:20:25 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
PyThreadState *
|
|
|
|
_PyThreadState_Prealloc(PyInterpreterState *interp)
|
|
|
|
{
|
2010-05-09 11:46:46 -03:00
|
|
|
return new_threadstate(interp, 0);
|
2010-03-03 19:20:25 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
_PyThreadState_Init(PyThreadState *tstate)
|
|
|
|
{
|
|
|
|
#ifdef WITH_THREAD
|
2010-05-09 11:46:46 -03:00
|
|
|
_PyGILState_NoteThreadState(tstate);
|
2010-03-03 19:20:25 -04:00
|
|
|
#endif
|
|
|
|
}
|
1997-05-05 17:56:21 -03:00
|
|
|
|
|
|
|
void
|
2000-07-22 15:47:25 -03:00
|
|
|
PyThreadState_Clear(PyThreadState *tstate)
|
1997-05-05 17:56:21 -03:00
|
|
|
{
|
2010-05-09 11:46:46 -03:00
|
|
|
if (Py_VerboseFlag && tstate->frame != NULL)
|
|
|
|
fprintf(stderr,
|
|
|
|
"PyThreadState_Clear: warning: thread still has a frame\n");
|
1997-08-02 00:10:38 -03:00
|
|
|
|
2010-05-09 11:46:46 -03:00
|
|
|
Py_CLEAR(tstate->frame);
|
1997-05-05 17:56:21 -03:00
|
|
|
|
2010-05-09 11:46:46 -03:00
|
|
|
Py_CLEAR(tstate->dict);
|
|
|
|
Py_CLEAR(tstate->async_exc);
|
1998-04-10 17:18:25 -03:00
|
|
|
|
2010-05-09 11:46:46 -03:00
|
|
|
Py_CLEAR(tstate->curexc_type);
|
|
|
|
Py_CLEAR(tstate->curexc_value);
|
|
|
|
Py_CLEAR(tstate->curexc_traceback);
|
1997-05-05 17:56:21 -03:00
|
|
|
|
2010-05-09 11:46:46 -03:00
|
|
|
Py_CLEAR(tstate->exc_type);
|
|
|
|
Py_CLEAR(tstate->exc_value);
|
|
|
|
Py_CLEAR(tstate->exc_traceback);
|
1997-05-05 17:56:21 -03:00
|
|
|
|
2010-05-09 11:46:46 -03:00
|
|
|
tstate->c_profilefunc = NULL;
|
|
|
|
tstate->c_tracefunc = NULL;
|
|
|
|
Py_CLEAR(tstate->c_profileobj);
|
|
|
|
Py_CLEAR(tstate->c_traceobj);
|
1997-08-02 00:10:38 -03:00
|
|
|
}
|
1997-05-05 17:56:21 -03:00
|
|
|
|
|
|
|
|
2001-01-22 21:46:06 -04:00
|
|
|
/* Common code for PyThreadState_Delete() and PyThreadState_DeleteCurrent() */
|
|
|
|
static void
|
|
|
|
tstate_delete_common(PyThreadState *tstate)
|
1997-08-02 00:10:38 -03:00
|
|
|
{
|
2010-05-09 11:46:46 -03:00
|
|
|
PyInterpreterState *interp;
|
|
|
|
PyThreadState **p;
|
|
|
|
PyThreadState *prev_p = NULL;
|
|
|
|
if (tstate == NULL)
|
|
|
|
Py_FatalError("PyThreadState_Delete: NULL tstate");
|
|
|
|
interp = tstate->interp;
|
|
|
|
if (interp == NULL)
|
|
|
|
Py_FatalError("PyThreadState_Delete: NULL interp");
|
|
|
|
HEAD_LOCK();
|
|
|
|
for (p = &interp->tstate_head; ; p = &(*p)->next) {
|
|
|
|
if (*p == NULL)
|
|
|
|
Py_FatalError(
|
|
|
|
"PyThreadState_Delete: invalid tstate");
|
|
|
|
if (*p == tstate)
|
|
|
|
break;
|
|
|
|
/* Sanity check. These states should never happen but if
|
|
|
|
* they do we must abort. Otherwise we'll end up spinning in
|
|
|
|
* in a tight loop with the lock held. A similar check is done
|
|
|
|
* in thread.c find_key(). */
|
|
|
|
if (*p == prev_p)
|
|
|
|
Py_FatalError(
|
|
|
|
"PyThreadState_Delete: small circular list(!)"
|
|
|
|
" and tstate not found.");
|
|
|
|
prev_p = *p;
|
|
|
|
if ((*p)->next == interp->tstate_head)
|
|
|
|
Py_FatalError(
|
|
|
|
"PyThreadState_Delete: circular list(!) and"
|
|
|
|
" tstate not found.");
|
|
|
|
}
|
|
|
|
*p = tstate->next;
|
|
|
|
HEAD_UNLOCK();
|
|
|
|
free(tstate);
|
1997-05-05 17:56:21 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-01-22 21:46:06 -04:00
|
|
|
void
|
|
|
|
PyThreadState_Delete(PyThreadState *tstate)
|
|
|
|
{
|
2010-05-09 11:46:46 -03:00
|
|
|
if (tstate == _PyThreadState_Current)
|
|
|
|
Py_FatalError("PyThreadState_Delete: tstate is still current");
|
|
|
|
tstate_delete_common(tstate);
|
2006-02-27 13:15:31 -04:00
|
|
|
#ifdef WITH_THREAD
|
2010-09-08 09:40:49 -03:00
|
|
|
if (autoInterpreterState && PyThread_get_key_value(autoTLSkey) == tstate)
|
2010-05-09 11:46:46 -03:00
|
|
|
PyThread_delete_key_value(autoTLSkey);
|
2006-02-27 13:15:31 -04:00
|
|
|
#endif /* WITH_THREAD */
|
2001-01-22 21:46:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#ifdef WITH_THREAD
|
|
|
|
void
|
|
|
|
PyThreadState_DeleteCurrent()
|
|
|
|
{
|
2010-05-09 11:46:46 -03:00
|
|
|
PyThreadState *tstate = _PyThreadState_Current;
|
|
|
|
if (tstate == NULL)
|
|
|
|
Py_FatalError(
|
|
|
|
"PyThreadState_DeleteCurrent: no current tstate");
|
|
|
|
_PyThreadState_Current = NULL;
|
|
|
|
tstate_delete_common(tstate);
|
2010-09-08 09:40:49 -03:00
|
|
|
if (autoInterpreterState && PyThread_get_key_value(autoTLSkey) == tstate)
|
2010-05-09 11:46:46 -03:00
|
|
|
PyThread_delete_key_value(autoTLSkey);
|
|
|
|
PyEval_ReleaseLock();
|
2001-01-22 21:46:06 -04:00
|
|
|
}
|
|
|
|
#endif /* WITH_THREAD */
|
|
|
|
|
|
|
|
|
1997-05-05 17:56:21 -03:00
|
|
|
PyThreadState *
|
2000-07-22 15:47:25 -03:00
|
|
|
PyThreadState_Get(void)
|
1997-05-05 17:56:21 -03:00
|
|
|
{
|
2010-05-09 11:46:46 -03:00
|
|
|
if (_PyThreadState_Current == NULL)
|
|
|
|
Py_FatalError("PyThreadState_Get: no current thread");
|
1997-08-02 00:10:38 -03:00
|
|
|
|
2010-05-09 11:46:46 -03:00
|
|
|
return _PyThreadState_Current;
|
1997-05-05 17:56:21 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
PyThreadState *
|
2006-04-11 09:01:56 -03:00
|
|
|
PyThreadState_Swap(PyThreadState *newts)
|
1997-05-05 17:56:21 -03:00
|
|
|
{
|
2010-05-09 11:46:46 -03:00
|
|
|
PyThreadState *oldts = _PyThreadState_Current;
|
1997-08-02 00:10:38 -03:00
|
|
|
|
2010-05-09 11:46:46 -03:00
|
|
|
_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.
|
|
|
|
*/
|
2003-05-01 02:25:29 -03:00
|
|
|
#if defined(Py_DEBUG) && defined(WITH_THREAD)
|
2010-05-09 11:46:46 -03:00
|
|
|
if (newts) {
|
|
|
|
/* This can be called from PyEval_RestoreThread(). Similar
|
|
|
|
to it, we need to ensure errno doesn't change.
|
|
|
|
*/
|
|
|
|
int err = errno;
|
|
|
|
PyThreadState *check = PyGILState_GetThisThreadState();
|
|
|
|
if (check && check->interp == newts->interp && check != newts)
|
|
|
|
Py_FatalError("Invalid thread state for this thread");
|
|
|
|
errno = err;
|
|
|
|
}
|
2003-04-19 12:41:53 -03:00
|
|
|
#endif
|
2010-05-09 11:46:46 -03:00
|
|
|
return oldts;
|
1997-05-05 17:56:21 -03:00
|
|
|
}
|
1998-04-10 17:18:25 -03:00
|
|
|
|
|
|
|
/* An extension mechanism to store arbitrary additional per-thread state.
|
|
|
|
PyThreadState_GetDict() returns a dictionary that can be used to hold such
|
|
|
|
state; the caller should pick a unique key and store its state there. If
|
2003-04-15 12:12:39 -03:00
|
|
|
PyThreadState_GetDict() returns NULL, an exception has *not* been raised
|
|
|
|
and the caller should assume no per-thread state is available. */
|
1998-04-10 17:18:25 -03:00
|
|
|
|
|
|
|
PyObject *
|
2000-07-22 15:47:25 -03:00
|
|
|
PyThreadState_GetDict(void)
|
1998-04-10 17:18:25 -03:00
|
|
|
{
|
2010-05-09 11:46:46 -03:00
|
|
|
if (_PyThreadState_Current == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
if (_PyThreadState_Current->dict == NULL) {
|
|
|
|
PyObject *d;
|
|
|
|
_PyThreadState_Current->dict = d = PyDict_New();
|
|
|
|
if (d == NULL)
|
|
|
|
PyErr_Clear();
|
|
|
|
}
|
|
|
|
return _PyThreadState_Current->dict;
|
1998-04-10 17:18:25 -03:00
|
|
|
}
|
2001-07-19 09:19:27 -03:00
|
|
|
|
|
|
|
|
2003-06-28 18:53:52 -03:00
|
|
|
/* Asynchronously raise an exception in a thread.
|
|
|
|
Requested by Just van Rossum and Alex Martelli.
|
2005-02-07 22:07:57 -04:00
|
|
|
To prevent naive misuse, you must write your own extension
|
2006-08-10 19:45:34 -03:00
|
|
|
to call this, or use ctypes. Must be called with the GIL held.
|
|
|
|
Returns the number of tstates modified (normally 1, but 0 if `id` didn't
|
|
|
|
match any known thread id). Can be called with exc=NULL to clear an
|
|
|
|
existing async exception. This raises no exceptions. */
|
2003-06-28 18:53:52 -03:00
|
|
|
|
|
|
|
int
|
|
|
|
PyThreadState_SetAsyncExc(long id, PyObject *exc) {
|
2010-05-09 11:46:46 -03:00
|
|
|
PyThreadState *tstate = PyThreadState_GET();
|
|
|
|
PyInterpreterState *interp = tstate->interp;
|
|
|
|
PyThreadState *p;
|
|
|
|
|
|
|
|
/* Although the GIL is held, a few C API functions can be called
|
|
|
|
* without the GIL held, and in particular some that create and
|
|
|
|
* destroy thread and interpreter states. Those can mutate the
|
|
|
|
* list of thread states we're traversing, so to prevent that we lock
|
|
|
|
* head_mutex for the duration.
|
|
|
|
*/
|
|
|
|
HEAD_LOCK();
|
|
|
|
for (p = interp->tstate_head; p != NULL; p = p->next) {
|
|
|
|
if (p->thread_id == id) {
|
|
|
|
/* Tricky: we need to decref the current value
|
|
|
|
* (if any) in p->async_exc, but that can in turn
|
|
|
|
* allow arbitrary Python code to run, including
|
|
|
|
* perhaps calls to this function. To prevent
|
|
|
|
* deadlock, we need to release head_mutex before
|
|
|
|
* the decref.
|
|
|
|
*/
|
|
|
|
PyObject *old_exc = p->async_exc;
|
|
|
|
Py_XINCREF(exc);
|
|
|
|
p->async_exc = exc;
|
|
|
|
HEAD_UNLOCK();
|
|
|
|
Py_XDECREF(old_exc);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
HEAD_UNLOCK();
|
|
|
|
return 0;
|
2003-06-28 18:53:52 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-07-19 09:19:27 -03:00
|
|
|
/* Routines for advanced debuggers, requested by David Beazley.
|
|
|
|
Don't use unless you know what you are doing! */
|
|
|
|
|
|
|
|
PyInterpreterState *
|
|
|
|
PyInterpreterState_Head(void)
|
|
|
|
{
|
2010-05-09 11:46:46 -03:00
|
|
|
return interp_head;
|
2001-07-19 09:19:27 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
PyInterpreterState *
|
|
|
|
PyInterpreterState_Next(PyInterpreterState *interp) {
|
2010-05-09 11:46:46 -03:00
|
|
|
return interp->next;
|
2001-07-19 09:19:27 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
PyThreadState *
|
|
|
|
PyInterpreterState_ThreadHead(PyInterpreterState *interp) {
|
2010-05-09 11:46:46 -03:00
|
|
|
return interp->tstate_head;
|
2001-07-19 09:19:27 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
PyThreadState *
|
|
|
|
PyThreadState_Next(PyThreadState *tstate) {
|
2010-05-09 11:46:46 -03:00
|
|
|
return tstate->next;
|
2001-07-19 09:19:27 -03:00
|
|
|
}
|
2003-04-19 12:41:53 -03:00
|
|
|
|
2006-07-18 21:03:19 -03:00
|
|
|
/* The implementation of sys._current_frames(). This is intended to be
|
|
|
|
called with the GIL held, as it will be when called via
|
|
|
|
sys._current_frames(). It's possible it would work fine even without
|
|
|
|
the GIL held, but haven't thought enough about that.
|
|
|
|
*/
|
|
|
|
PyObject *
|
|
|
|
_PyThread_CurrentFrames(void)
|
|
|
|
{
|
2010-05-09 11:46:46 -03:00
|
|
|
PyObject *result;
|
|
|
|
PyInterpreterState *i;
|
|
|
|
|
|
|
|
result = PyDict_New();
|
|
|
|
if (result == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
/* for i in all interpreters:
|
|
|
|
* for t in all of i's thread states:
|
|
|
|
* if t's frame isn't NULL, map t's id to its frame
|
2011-03-16 07:34:31 -03:00
|
|
|
* Because these lists can mutate even when the GIL is held, we
|
2010-05-09 11:46:46 -03:00
|
|
|
* need to grab head_mutex for the duration.
|
|
|
|
*/
|
|
|
|
HEAD_LOCK();
|
|
|
|
for (i = interp_head; i != NULL; i = i->next) {
|
|
|
|
PyThreadState *t;
|
|
|
|
for (t = i->tstate_head; t != NULL; t = t->next) {
|
|
|
|
PyObject *id;
|
|
|
|
int stat;
|
|
|
|
struct _frame *frame = t->frame;
|
|
|
|
if (frame == NULL)
|
|
|
|
continue;
|
|
|
|
id = PyInt_FromLong(t->thread_id);
|
|
|
|
if (id == NULL)
|
|
|
|
goto Fail;
|
|
|
|
stat = PyDict_SetItem(result, id, (PyObject *)frame);
|
|
|
|
Py_DECREF(id);
|
|
|
|
if (stat < 0)
|
|
|
|
goto Fail;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
HEAD_UNLOCK();
|
|
|
|
return result;
|
2006-07-18 21:03:19 -03:00
|
|
|
|
|
|
|
Fail:
|
2010-05-09 11:46:46 -03:00
|
|
|
HEAD_UNLOCK();
|
|
|
|
Py_DECREF(result);
|
|
|
|
return NULL;
|
2006-07-18 21:03:19 -03:00
|
|
|
}
|
2003-06-28 18:53:52 -03:00
|
|
|
|
2003-04-19 12:41:53 -03:00
|
|
|
/* Python "auto thread state" API. */
|
|
|
|
#ifdef WITH_THREAD
|
|
|
|
|
|
|
|
/* Keep this as a static, as it is not reliable! It can only
|
|
|
|
ever be compared to the state for the *current* thread.
|
|
|
|
* If not equal, then it doesn't matter that the actual
|
|
|
|
value may change immediately after comparison, as it can't
|
|
|
|
possibly change to the current thread's state.
|
|
|
|
* If equal, then the current thread holds the lock, so the value can't
|
|
|
|
change until we yield the lock.
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
PyThreadState_IsCurrent(PyThreadState *tstate)
|
|
|
|
{
|
2010-05-09 11:46:46 -03:00
|
|
|
/* Must be the tstate for this thread */
|
|
|
|
assert(PyGILState_GetThisThreadState()==tstate);
|
|
|
|
/* On Windows at least, simple reads and writes to 32 bit values
|
|
|
|
are atomic.
|
|
|
|
*/
|
|
|
|
return tstate == _PyThreadState_Current;
|
2003-04-19 12:41:53 -03:00
|
|
|
}
|
|
|
|
|
2004-10-09 14:25:05 -03:00
|
|
|
/* Internal initialization/finalization functions called by
|
|
|
|
Py_Initialize/Py_Finalize
|
2003-04-19 12:41:53 -03:00
|
|
|
*/
|
2004-10-09 14:38:29 -03:00
|
|
|
void
|
|
|
|
_PyGILState_Init(PyInterpreterState *i, PyThreadState *t)
|
2003-04-19 12:41:53 -03:00
|
|
|
{
|
2010-05-09 11:46:46 -03:00
|
|
|
assert(i && t); /* must init with valid states */
|
|
|
|
autoTLSkey = PyThread_create_key();
|
|
|
|
autoInterpreterState = i;
|
|
|
|
assert(PyThread_get_key_value(autoTLSkey) == NULL);
|
|
|
|
assert(t->gilstate_counter == 0);
|
2005-06-20 13:52:57 -03:00
|
|
|
|
2010-05-09 11:46:46 -03:00
|
|
|
_PyGILState_NoteThreadState(t);
|
2003-04-19 12:41:53 -03:00
|
|
|
}
|
|
|
|
|
2004-10-09 14:38:29 -03:00
|
|
|
void
|
|
|
|
_PyGILState_Fini(void)
|
2003-04-19 12:41:53 -03:00
|
|
|
{
|
2010-05-09 11:46:46 -03:00
|
|
|
PyThread_delete_key(autoTLSkey);
|
|
|
|
autoInterpreterState = NULL;
|
2003-04-19 12:41:53 -03:00
|
|
|
}
|
|
|
|
|
2011-04-27 14:20:48 -03:00
|
|
|
/* Reset the TLS key - called by PyOS_AfterFork.
|
|
|
|
* This should not be necessary, but some - buggy - pthread implementations
|
|
|
|
* don't flush TLS on fork, see issue #10517.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
_PyGILState_Reinit(void)
|
|
|
|
{
|
|
|
|
PyThreadState *tstate = PyGILState_GetThisThreadState();
|
|
|
|
PyThread_delete_key(autoTLSkey);
|
|
|
|
if ((autoTLSkey = PyThread_create_key()) == -1)
|
|
|
|
Py_FatalError("Could not allocate TLS entry");
|
|
|
|
|
|
|
|
/* re-associate the current thread state with the new key */
|
|
|
|
if (PyThread_set_key_value(autoTLSkey, (void *)tstate) < 0)
|
|
|
|
Py_FatalError("Couldn't create autoTLSkey mapping");
|
|
|
|
}
|
|
|
|
|
2005-06-20 13:52:57 -03:00
|
|
|
/* When a thread state is created for a thread by some mechanism other than
|
|
|
|
PyGILState_Ensure, it's important that the GILState machinery knows about
|
|
|
|
it so it doesn't try to create another thread state for the thread (this is
|
|
|
|
a better fix for SF bug #1010677 than the first one attempted).
|
|
|
|
*/
|
2006-09-11 06:38:35 -03:00
|
|
|
static void
|
2005-06-20 13:52:57 -03:00
|
|
|
_PyGILState_NoteThreadState(PyThreadState* tstate)
|
|
|
|
{
|
2010-09-08 09:40:49 -03:00
|
|
|
/* If autoTLSkey isn't initialized, this must be the very first
|
|
|
|
threadstate created in Py_Initialize(). Don't do anything for now
|
|
|
|
(we'll be back here when _PyGILState_Init is called). */
|
|
|
|
if (!autoInterpreterState)
|
2010-05-09 11:46:46 -03:00
|
|
|
return;
|
|
|
|
|
|
|
|
/* Stick the thread state for this thread in thread local storage.
|
|
|
|
|
|
|
|
The only situation where you can legitimately have more than one
|
|
|
|
thread state for an OS level thread is when there are multiple
|
|
|
|
interpreters, when:
|
|
|
|
|
|
|
|
a) You shouldn't really be using the PyGILState_ APIs anyway,
|
|
|
|
and:
|
|
|
|
|
|
|
|
b) The slightly odd way PyThread_set_key_value works (see
|
|
|
|
comments by its implementation) means that the first thread
|
|
|
|
state created for that given OS level thread will "win",
|
|
|
|
which seems reasonable behaviour.
|
|
|
|
*/
|
|
|
|
if (PyThread_set_key_value(autoTLSkey, (void *)tstate) < 0)
|
|
|
|
Py_FatalError("Couldn't create autoTLSkey mapping");
|
|
|
|
|
|
|
|
/* PyGILState_Release must not try to delete this thread state. */
|
|
|
|
tstate->gilstate_counter = 1;
|
2005-06-20 13:52:57 -03:00
|
|
|
}
|
|
|
|
|
2003-04-19 12:41:53 -03:00
|
|
|
/* The public functions */
|
2004-10-09 14:38:29 -03:00
|
|
|
PyThreadState *
|
|
|
|
PyGILState_GetThisThreadState(void)
|
2003-04-19 12:41:53 -03:00
|
|
|
{
|
2010-09-08 09:40:49 -03:00
|
|
|
if (autoInterpreterState == NULL)
|
2010-05-09 11:46:46 -03:00
|
|
|
return NULL;
|
|
|
|
return (PyThreadState *)PyThread_get_key_value(autoTLSkey);
|
2003-04-19 12:41:53 -03:00
|
|
|
}
|
|
|
|
|
2004-10-09 14:38:29 -03:00
|
|
|
PyGILState_STATE
|
|
|
|
PyGILState_Ensure(void)
|
2003-04-19 12:41:53 -03:00
|
|
|
{
|
2010-05-09 11:46:46 -03:00
|
|
|
int current;
|
|
|
|
PyThreadState *tcur;
|
|
|
|
/* Note that we do not auto-init Python here - apart from
|
|
|
|
potential races with 2 threads auto-initializing, pep-311
|
|
|
|
spells out other issues. Embedders are expected to have
|
|
|
|
called Py_Initialize() and usually PyEval_InitThreads().
|
|
|
|
*/
|
|
|
|
assert(autoInterpreterState); /* Py_Initialize() hasn't been called! */
|
|
|
|
tcur = (PyThreadState *)PyThread_get_key_value(autoTLSkey);
|
|
|
|
if (tcur == NULL) {
|
|
|
|
/* Create a new thread state for this thread */
|
|
|
|
tcur = PyThreadState_New(autoInterpreterState);
|
|
|
|
if (tcur == NULL)
|
|
|
|
Py_FatalError("Couldn't create thread-state for new thread");
|
|
|
|
/* This is our thread state! We'll need to delete it in the
|
|
|
|
matching call to PyGILState_Release(). */
|
|
|
|
tcur->gilstate_counter = 0;
|
|
|
|
current = 0; /* new thread state is never current */
|
|
|
|
}
|
|
|
|
else
|
|
|
|
current = PyThreadState_IsCurrent(tcur);
|
|
|
|
if (current == 0)
|
|
|
|
PyEval_RestoreThread(tcur);
|
|
|
|
/* Update our counter in the thread-state - no need for locks:
|
|
|
|
- tcur will remain valid as we hold the GIL.
|
|
|
|
- the counter is safe as we are the only thread "allowed"
|
|
|
|
to modify this value
|
|
|
|
*/
|
|
|
|
++tcur->gilstate_counter;
|
|
|
|
return current ? PyGILState_LOCKED : PyGILState_UNLOCKED;
|
2003-04-19 12:41:53 -03:00
|
|
|
}
|
|
|
|
|
2004-10-09 14:38:29 -03:00
|
|
|
void
|
|
|
|
PyGILState_Release(PyGILState_STATE oldstate)
|
2003-04-19 12:41:53 -03:00
|
|
|
{
|
2010-05-09 11:46:46 -03:00
|
|
|
PyThreadState *tcur = (PyThreadState *)PyThread_get_key_value(
|
|
|
|
autoTLSkey);
|
|
|
|
if (tcur == NULL)
|
|
|
|
Py_FatalError("auto-releasing thread-state, "
|
|
|
|
"but no thread-state for this thread");
|
|
|
|
/* We must hold the GIL and have our thread state current */
|
|
|
|
/* XXX - remove the check - the assert should be fine,
|
|
|
|
but while this is very new (April 2003), the extra check
|
|
|
|
by release-only users can't hurt.
|
|
|
|
*/
|
|
|
|
if (! PyThreadState_IsCurrent(tcur))
|
|
|
|
Py_FatalError("This thread state must be current when releasing");
|
|
|
|
assert(PyThreadState_IsCurrent(tcur));
|
|
|
|
--tcur->gilstate_counter;
|
|
|
|
assert(tcur->gilstate_counter >= 0); /* illegal counter value */
|
|
|
|
|
|
|
|
/* If we're going to destroy this thread-state, we must
|
|
|
|
* clear it while the GIL is held, as destructors may run.
|
|
|
|
*/
|
|
|
|
if (tcur->gilstate_counter == 0) {
|
|
|
|
/* can't have been locked when we created it */
|
|
|
|
assert(oldstate == PyGILState_UNLOCKED);
|
|
|
|
PyThreadState_Clear(tcur);
|
|
|
|
/* Delete the thread-state. Note this releases the GIL too!
|
|
|
|
* It's vital that the GIL be held here, to avoid shutdown
|
|
|
|
* races; see bugs 225673 and 1061968 (that nasty bug has a
|
|
|
|
* habit of coming back).
|
|
|
|
*/
|
|
|
|
PyThreadState_DeleteCurrent();
|
|
|
|
}
|
|
|
|
/* Release the lock if necessary */
|
|
|
|
else if (oldstate == PyGILState_UNLOCKED)
|
|
|
|
PyEval_SaveThread();
|
2003-04-19 12:41:53 -03:00
|
|
|
}
|
2006-04-12 23:06:09 -03:00
|
|
|
|
|
|
|
#ifdef __cplusplus
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2003-04-19 12:41:53 -03:00
|
|
|
#endif /* WITH_THREAD */
|
2006-04-12 23:06:09 -03:00
|
|
|
|
|
|
|
|