summaryrefslogtreecommitdiffstats
path: root/Python
diff options
context:
space:
mode:
authorVictor Stinner <victor.stinner@haypocalc.com>2010-03-03 23:20:25 (GMT)
committerVictor Stinner <victor.stinner@haypocalc.com>2010-03-03 23:20:25 (GMT)
commit71fb87e64c51627564262fd64299f7ac79625404 (patch)
treedf72f580ff07a3bd5419e3a3509a8c5806a51836 /Python
parent2379bb664ac33b77d076a9a8db59f73af9eb3c8f (diff)
downloadcpython-71fb87e64c51627564262fd64299f7ac79625404.zip
cpython-71fb87e64c51627564262fd64299f7ac79625404.tar.gz
cpython-71fb87e64c51627564262fd64299f7ac79625404.tar.bz2
Issue #7544: Preallocate thread memory before creating the thread to avoid a
fatal error in low memory condition.
Diffstat (limited to 'Python')
-rw-r--r--Python/pystate.c28
1 files changed, 23 insertions, 5 deletions
diff --git a/Python/pystate.c b/Python/pystate.c
index da417c1..343a97b 100644
--- a/Python/pystate.c
+++ b/Python/pystate.c
@@ -154,8 +154,8 @@ threadstate_getframe(PyThreadState *self)
return self->frame;
}
-PyThreadState *
-PyThreadState_New(PyInterpreterState *interp)
+static PyThreadState *
+new_threadstate(PyInterpreterState *interp, int init)
{
PyThreadState *tstate = (PyThreadState *)malloc(sizeof(PyThreadState));
@@ -193,9 +193,8 @@ PyThreadState_New(PyInterpreterState *interp)
tstate->c_profileobj = NULL;
tstate->c_traceobj = NULL;
-#ifdef WITH_THREAD
- _PyGILState_NoteThreadState(tstate);
-#endif
+ if (init)
+ _PyThreadState_Init(tstate);
HEAD_LOCK();
tstate->next = interp->tstate_head;
@@ -206,6 +205,25 @@ PyThreadState_New(PyInterpreterState *interp)
return tstate;
}
+PyThreadState *
+PyThreadState_New(PyInterpreterState *interp)
+{
+ return new_threadstate(interp, 1);
+}
+
+PyThreadState *
+_PyThreadState_Prealloc(PyInterpreterState *interp)
+{
+ return new_threadstate(interp, 0);
+}
+
+void
+_PyThreadState_Init(PyThreadState *tstate)
+{
+#ifdef WITH_THREAD
+ _PyGILState_NoteThreadState(tstate);
+#endif
+}
void
PyThreadState_Clear(PyThreadState *tstate)