1 /***********************************************************
2 Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
7 Permission to use, copy, modify, and distribute this software and its
8 documentation for any purpose and without fee is hereby granted,
9 provided that the above copyright notice appear in all copies and that
10 both that copyright notice and this permission notice appear in
11 supporting documentation, and that the names of Stichting Mathematisch
12 Centrum or CWI or Corporation for National Research Initiatives or
13 CNRI not be used in advertising or publicity pertaining to
14 distribution of the software without specific, written prior
17 While CWI is the initial source for this software, a modified version
18 is made available by the Corporation for National Research Initiatives
19 (CNRI) at the Internet address ftp://ftp.python.org.
21 STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH
22 REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF
23 MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH
24 CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
25 DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
26 PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
27 TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
28 PERFORMANCE OF THIS SOFTWARE.
30 ******************************************************************/
32 /* Thread and interpreter state structures and their interfaces */
37 PyObject *tmp = (PyObject *)(x); \
43 static PyInterpreterState
*interp_head
= NULL
;
45 PyThreadState
*_PyThreadState_Current
= NULL
;
49 PyInterpreterState_New()
51 PyInterpreterState
*interp
= PyMem_NEW(PyInterpreterState
, 1);
54 interp
->modules
= NULL
;
55 interp
->sysdict
= NULL
;
56 interp
->builtins
= NULL
;
57 interp
->checkinterval
= 10;
58 interp
->tstate_head
= NULL
;
60 interp
->next
= interp_head
;
69 PyInterpreterState_Clear(interp
)
70 PyInterpreterState
*interp
;
73 for (p
= interp
->tstate_head
; p
!= NULL
; p
= p
->next
)
74 PyThreadState_Clear(p
);
77 ZAP(interp
->builtins
);
83 PyInterpreterState
*interp
;
86 p
= interp
->tstate_head
;
89 PyThreadState_Delete(p
);
96 PyInterpreterState_Delete(interp
)
97 PyInterpreterState
*interp
;
99 PyInterpreterState
**p
;
101 for (p
= &interp_head
; ; p
= &(*p
)->next
) {
104 "PyInterpreterState_Delete: invalid interp");
108 if (interp
->tstate_head
!= NULL
)
109 Py_FatalError("PyInterpreterState_Delete: remaining threads");
116 PyThreadState_New(interp
)
117 PyInterpreterState
*interp
;
119 PyThreadState
*tstate
= PyMem_NEW(PyThreadState
, 1);
121 if (tstate
!= NULL
) {
122 tstate
->interp
= interp
;
124 tstate
->frame
= NULL
;
125 tstate
->recursion_depth
= 0;
131 tstate
->curexc_type
= NULL
;
132 tstate
->curexc_value
= NULL
;
133 tstate
->curexc_traceback
= NULL
;
135 tstate
->exc_type
= NULL
;
136 tstate
->exc_value
= NULL
;
137 tstate
->exc_traceback
= NULL
;
139 tstate
->sys_profilefunc
= NULL
;
140 tstate
->sys_tracefunc
= NULL
;
142 tstate
->next
= interp
->tstate_head
;
143 interp
->tstate_head
= tstate
;
151 PyThreadState_Clear(tstate
)
152 PyThreadState
*tstate
;
154 if (Py_VerboseFlag
&& tstate
->frame
!= NULL
)
156 "PyThreadState_Clear: warning: thread still has a frame\n");
162 ZAP(tstate
->curexc_type
);
163 ZAP(tstate
->curexc_value
);
164 ZAP(tstate
->curexc_traceback
);
166 ZAP(tstate
->exc_type
);
167 ZAP(tstate
->exc_value
);
168 ZAP(tstate
->exc_traceback
);
170 ZAP(tstate
->sys_profilefunc
);
171 ZAP(tstate
->sys_tracefunc
);
176 PyThreadState_Delete(tstate
)
177 PyThreadState
*tstate
;
179 PyInterpreterState
*interp
;
182 Py_FatalError("PyThreadState_Delete: NULL tstate");
183 if (tstate
== _PyThreadState_Current
)
184 Py_FatalError("PyThreadState_Delete: tstate is still current");
185 interp
= tstate
->interp
;
187 Py_FatalError("PyThreadState_Delete: NULL interp");
188 for (p
= &interp
->tstate_head
; ; p
= &(*p
)->next
) {
191 "PyThreadState_Delete: invalid tstate");
203 if (_PyThreadState_Current
== NULL
)
204 Py_FatalError("PyThreadState_Get: no current thread");
206 return _PyThreadState_Current
;
211 PyThreadState_Swap(new)
214 PyThreadState
*old
= _PyThreadState_Current
;
216 _PyThreadState_Current
= new;
221 /* An extension mechanism to store arbitrary additional per-thread state.
222 PyThreadState_GetDict() returns a dictionary that can be used to hold such
223 state; the caller should pick a unique key and store its state there. If
224 PyThreadState_GetDict() returns NULL, an exception has been raised (most
225 likely MemoryError) and the caller should pass on the exception. */
228 PyThreadState_GetDict()
230 if (_PyThreadState_Current
== NULL
)
231 Py_FatalError("PyThreadState_GetDict: no current thread");
233 if (_PyThreadState_Current
->dict
== NULL
)
234 _PyThreadState_Current
->dict
= PyDict_New();
235 return _PyThreadState_Current
->dict
;