comparison Python/pystate.c @ 33713:517e4ec0df89 legacy-trunk

PyInterpreterState_New(), PyThreadState_New(): use malloc/free directly. This appears to finish repairs for SF bug 1041645. This is a critical bugfix.
author Tim Peters <tim.peters@gmail.com>
date Sun, 10 Oct 2004 02:47:33 +0000
parents 664b18aa6fc1
children 635dd8a9ebd9
comparison
equal deleted inserted replaced
33712:baa2653f7a76 33713:517e4ec0df89
1 1
2 /* Thread and interpreter state structures and their interfaces */ 2 /* Thread and interpreter state structures and their interfaces */
3 3
4 #include "Python.h" 4 #include "Python.h"
5
6 /* --------------------------------------------------------------------------
7 CAUTION
8
9 Always use malloc() and free() directly in this file. A number of these
10 functions are advertised as safe to call when the GIL isn't held, and in
11 a debug build Python redirects (e.g.) PyMem_NEW (etc) to Python's debugging
12 obmalloc functions. Those aren't thread-safe (they rely on the GIL to avoid
13 the expense of doing their own locking).
14 -------------------------------------------------------------------------- */
5 15
6 #ifdef HAVE_DLOPEN 16 #ifdef HAVE_DLOPEN
7 #ifdef HAVE_DLFCN_H 17 #ifdef HAVE_DLFCN_H
8 #include <dlfcn.h> 18 #include <dlfcn.h>
9 #endif 19 #endif
39 49
40 50
41 PyInterpreterState * 51 PyInterpreterState *
42 PyInterpreterState_New(void) 52 PyInterpreterState_New(void)
43 { 53 {
44 PyInterpreterState *interp = PyMem_NEW(PyInterpreterState, 1); 54 PyInterpreterState *interp = (PyInterpreterState *)
55 malloc(sizeof(PyInterpreterState));
45 56
46 if (interp != NULL) { 57 if (interp != NULL) {
47 HEAD_INIT(); 58 HEAD_INIT();
48 interp->modules = NULL; 59 interp->modules = NULL;
49 interp->sysdict = NULL; 60 interp->sysdict = NULL;
117 } 128 }
118 if (interp->tstate_head != NULL) 129 if (interp->tstate_head != NULL)
119 Py_FatalError("PyInterpreterState_Delete: remaining threads"); 130 Py_FatalError("PyInterpreterState_Delete: remaining threads");
120 *p = interp->next; 131 *p = interp->next;
121 HEAD_UNLOCK(); 132 HEAD_UNLOCK();
122 PyMem_DEL(interp); 133 free(interp);
123 } 134 }
124 135
125 136
126 /* Default implementation for _PyThreadState_GetFrame */ 137 /* Default implementation for _PyThreadState_GetFrame */
127 static struct _frame * 138 static struct _frame *
131 } 142 }
132 143
133 PyThreadState * 144 PyThreadState *
134 PyThreadState_New(PyInterpreterState *interp) 145 PyThreadState_New(PyInterpreterState *interp)
135 { 146 {
136 PyThreadState *tstate = PyMem_NEW(PyThreadState, 1); 147 PyThreadState *tstate = (PyThreadState *)malloc(sizeof(PyThreadState));
148
137 if (_PyThreadState_GetFrame == NULL) 149 if (_PyThreadState_GetFrame == NULL)
138 _PyThreadState_GetFrame = threadstate_getframe; 150 _PyThreadState_GetFrame = threadstate_getframe;
139 151
140 if (tstate != NULL) { 152 if (tstate != NULL) {
141 tstate->interp = interp; 153 tstate->interp = interp;
224 if (*p == tstate) 236 if (*p == tstate)
225 break; 237 break;
226 } 238 }
227 *p = tstate->next; 239 *p = tstate->next;
228 HEAD_UNLOCK(); 240 HEAD_UNLOCK();
229 PyMem_DEL(tstate); 241 free(tstate);
230 } 242 }
231 243
232 244
233 void 245 void
234 PyThreadState_Delete(PyThreadState *tstate) 246 PyThreadState_Delete(PyThreadState *tstate)