summaryrefslogtreecommitdiff
path: root/Python
diff options
context:
space:
mode:
authorVictor Stinner <victor.stinner@haypocalc.com>2010-03-03 23:20:25 +0000
committerVictor Stinner <victor.stinner@haypocalc.com>2010-03-03 23:20:25 +0000
commitbc94ce6da5e1340819bed18d4320140fe38b5bd3 (patch)
treef5ed7ef8ce19539e69d16ff533778e25ae5f637a /Python
parent70a150881435be17082d1b7864199d3fe5a04477 (diff)
downloadcpython-bc94ce6da5e1340819bed18d4320140fe38b5bd3.tar.gz
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 da417c1032..343a97b00d 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)