Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit ef32bc8

Browse files
author
Victor Stinner
committed
Merged revisions 78639 via svnmerge from
svn+ssh://[email protected]/python/branches/py3k ................ r78639 | victor.stinner | 2010-03-04 00:28:07 +0100 (jeu., 04 mars 2010) | 10 lines Merged revisions 78638 via svnmerge from svn+ssh://[email protected]/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 925ca76 commit ef32bc8

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
@@ -113,6 +113,8 @@ PyAPI_FUNC(int) _PyState_AddModule(PyObject*, struct PyModuleDef*);
113113
PyAPI_FUNC(PyObject*) PyState_FindModule(struct PyModuleDef*);
114114

115115
PyAPI_FUNC(PyThreadState *) PyThreadState_New(PyInterpreterState *);
116+
PyAPI_FUNC(PyThreadState *) _PyThreadState_Prealloc(PyInterpreterState *);
117+
PyAPI_FUNC(void) _PyThreadState_Init(PyThreadState *);
116118
PyAPI_FUNC(void) PyThreadState_Clear(PyThreadState *);
117119
PyAPI_FUNC(void) PyThreadState_Delete(PyThreadState *);
118120
#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.1.2?
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
@@ -423,6 +423,7 @@ struct bootstate {
423423
PyObject *func;
424424
PyObject *args;
425425
PyObject *keyw;
426+
PyThreadState *tstate;
426427
};
427428

428429
static void
@@ -432,8 +433,9 @@ t_bootstrap(void *boot_raw)
432433
PyThreadState *tstate;
433434
PyObject *res;
434435

435-
tstate = PyThreadState_New(boot->interp);
436-
436+
tstate = boot->tstate;
437+
tstate->thread_id = PyThread_get_thread_ident();
438+
_PyThreadState_Init(tstate);
437439
PyEval_AcquireThread(tstate);
438440
res = PyEval_CallObjectWithKeywords(
439441
boot->func, boot->args, boot->keyw);
@@ -496,6 +498,11 @@ thread_PyThread_start_new_thread(PyObject *self, PyObject *fargs)
496498
boot->func = func;
497499
boot->args = args;
498500
boot->keyw = keyw;
501+
boot->tstate = _PyThreadState_Prealloc(boot->interp);
502+
if (boot->tstate == NULL) {
503+
PyMem_DEL(boot);
504+
return PyErr_NoMemory();
505+
}
499506
Py_INCREF(func);
500507
Py_INCREF(args);
501508
Py_XINCREF(keyw);
@@ -506,6 +513,7 @@ thread_PyThread_start_new_thread(PyObject *self, PyObject *fargs)
506513
Py_DECREF(func);
507514
Py_DECREF(args);
508515
Py_XDECREF(keyw);
516+
PyThreadState_Clear(boot->tstate);
509517
PyMem_DEL(boot);
510518
return NULL;
511519
}

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)