}
+#ifdef WITH_THREAD
+#include "pythread.h"
+static PyThread_type_lock head_mutex = NULL; /* Protects interp->tstate_head */
+#define HEAD_INIT() (head_mutex || (head_mutex = PyThread_allocate_lock()))
+#define HEAD_LOCK() PyThread_acquire_lock(head_mutex, WAIT_LOCK)
+#define HEAD_UNLOCK() PyThread_release_lock(head_mutex)
+#else
+#define HEAD_INIT() /* Nothing */
+#define HEAD_LOCK() /* Nothing */
+#define HEAD_UNLOCK() /* Nothing */
+#endif
+
static PyInterpreterState *interp_head = NULL;
PyThreadState *_PyThreadState_Current = NULL;
PyInterpreterState *interp = PyMem_NEW(PyInterpreterState, 1);
if (interp != NULL) {
+ HEAD_INIT();
interp->modules = NULL;
interp->sysdict = NULL;
interp->builtins = NULL;
PyInterpreterState *interp;
{
PyThreadState *p;
+ HEAD_LOCK();
for (p = interp->tstate_head; p != NULL; p = p->next)
PyThreadState_Clear(p);
+ HEAD_UNLOCK();
ZAP(interp->modules);
ZAP(interp->sysdict);
ZAP(interp->builtins);
zapthreads(interp)
PyInterpreterState *interp;
{
- PyThreadState *p, *q;
- p = interp->tstate_head;
- while (p != NULL) {
- q = p->next;
+ 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);
- p = q;
}
}
tstate->sys_profilefunc = NULL;
tstate->sys_tracefunc = NULL;
+ HEAD_LOCK();
tstate->next = interp->tstate_head;
interp->tstate_head = tstate;
+ HEAD_UNLOCK();
}
return 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(
break;
}
*p = tstate->next;
+ HEAD_UNLOCK();
PyMem_DEL(tstate);
}