Skip to content

Commit 45b9be5

Browse files
author
Victor Stinner
committed
Merged revisions 78638 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk ........ r78638 | victor.stinner | 2010-03-04 00:20:25 +0100 (jeu., 04 mars 2010) | 3 lines Issue #7544: Preallocate thread memory before creating the thread to avoid a fatal error in low memory condition. ........
1 parent a761227 commit 45b9be5

4 files changed

Lines changed: 39 additions & 7 deletions

File tree

‎Include/pystate.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,8 @@ PyAPI_FUNC(int) _PyState_AddModule(PyObject*, struct PyModuleDef*);
115115
PyAPI_FUNC(PyObject*) PyState_FindModule(struct PyModuleDef*);
116116

117117
PyAPI_FUNC(PyThreadState *) PyThreadState_New(PyInterpreterState *);
118+
PyAPI_FUNC(PyThreadState *) _PyThreadState_Prealloc(PyInterpreterState *);
119+
PyAPI_FUNC(void) _PyThreadState_Init(PyThreadState *);
118120
PyAPI_FUNC(void) PyThreadState_Clear(PyThreadState *);
119121
PyAPI_FUNC(void) PyThreadState_Delete(PyThreadState *);
120122
#ifdef WITH_THREAD

‎Misc/NEWS‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ What's New in Python 3.2 Alpha 1?
1212
Core and Builtins
1313
-----------------
1414

15+
- Issue #7544: Preallocate thread memory before creating the thread to avoid
16+
a fatal error in low memory condition.
17+
1518
- Issue #7820: The parser tokenizer restores all bytes in the right if
1619
the BOM check fails.
1720

‎Modules/_threadmodule.c‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -695,6 +695,7 @@ struct bootstate {
695695
PyObject *func;
696696
PyObject *args;
697697
PyObject *keyw;
698+
PyThreadState *tstate;
698699
};
699700

700701
static void
@@ -704,8 +705,9 @@ t_bootstrap(void *boot_raw)
704705
PyThreadState *tstate;
705706
PyObject *res;
706707

707-
tstate = PyThreadState_New(boot->interp);
708-
708+
tstate = boot->tstate;
709+
tstate->thread_id = PyThread_get_thread_ident();
710+
_PyThreadState_Init(tstate);
709711
PyEval_AcquireThread(tstate);
710712
nb_threads++;
711713
res = PyEval_CallObjectWithKeywords(
@@ -770,6 +772,11 @@ thread_PyThread_start_new_thread(PyObject *self, PyObject *fargs)
770772
boot->func = func;
771773
boot->args = args;
772774
boot->keyw = keyw;
775+
boot->tstate = _PyThreadState_Prealloc(boot->interp);
776+
if (boot->tstate == NULL) {
777+
PyMem_DEL(boot);
778+
return PyErr_NoMemory();
779+
}
773780
Py_INCREF(func);
774781
Py_INCREF(args);
775782
Py_XINCREF(keyw);
@@ -780,6 +787,7 @@ thread_PyThread_start_new_thread(PyObject *self, PyObject *fargs)
780787
Py_DECREF(func);
781788
Py_DECREF(args);
782789
Py_XDECREF(keyw);
790+
PyThreadState_Clear(boot->tstate);
783791
PyMem_DEL(boot);
784792
return NULL;
785793
}

‎Python/pystate.c‎

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -157,8 +157,8 @@ threadstate_getframe(PyThreadState *self)
157157
return self->frame;
158158
}
159159

160-
PyThreadState *
161-
PyThreadState_New(PyInterpreterState *interp)
160+
static PyThreadState *
161+
new_threadstate(PyInterpreterState *interp, int init)
162162
{
163163
PyThreadState *tstate = (PyThreadState *)malloc(sizeof(PyThreadState));
164164

@@ -198,9 +198,8 @@ PyThreadState_New(PyInterpreterState *interp)
198198
tstate->c_profileobj = NULL;
199199
tstate->c_traceobj = NULL;
200200

201-
#ifdef WITH_THREAD
202-
_PyGILState_NoteThreadState(tstate);
203-
#endif
201+
if (init)
202+
_PyThreadState_Init(tstate);
204203

205204
HEAD_LOCK();
206205
tstate->next = interp->tstate_head;
@@ -211,6 +210,26 @@ PyThreadState_New(PyInterpreterState *interp)
211210
return tstate;
212211
}
213212

213+
PyThreadState *
214+
PyThreadState_New(PyInterpreterState *interp)
215+
{
216+
return new_threadstate(interp, 1);
217+
}
218+
219+
PyThreadState *
220+
_PyThreadState_Prealloc(PyInterpreterState *interp)
221+
{
222+
return new_threadstate(interp, 0);
223+
}
224+
225+
void
226+
_PyThreadState_Init(PyThreadState *tstate)
227+
{
228+
#ifdef WITH_THREAD
229+
_PyGILState_NoteThreadState(tstate);
230+
#endif
231+
}
232+
214233
PyObject*
215234
PyState_FindModule(struct PyModuleDef* m)
216235
{

0 commit comments

Comments
 (0)