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