2 /* Thread and interpreter state structures and their interfaces */
17 PyObject *tmp = (PyObject *)(x); \
25 static PyThread_type_lock head_mutex
= NULL
; /* Protects interp->tstate_head */
26 #define HEAD_INIT() (void)(head_mutex || (head_mutex = PyThread_allocate_lock()))
27 #define HEAD_LOCK() PyThread_acquire_lock(head_mutex, WAIT_LOCK)
28 #define HEAD_UNLOCK() PyThread_release_lock(head_mutex)
30 #define HEAD_INIT() /* Nothing */
31 #define HEAD_LOCK() /* Nothing */
32 #define HEAD_UNLOCK() /* Nothing */
35 static PyInterpreterState
*interp_head
= NULL
;
37 PyThreadState
*_PyThreadState_Current
= NULL
;
38 unaryfunc _PyThreadState_GetFrame
= NULL
;
42 PyInterpreterState_New(void)
44 PyInterpreterState
*interp
= PyMem_NEW(PyInterpreterState
, 1);
48 interp
->modules
= NULL
;
49 interp
->sysdict
= NULL
;
50 interp
->builtins
= NULL
;
51 interp
->checkinterval
= 10;
52 interp
->tstate_head
= NULL
;
53 interp
->codec_search_path
= NULL
;
54 interp
->codec_search_cache
= NULL
;
57 interp
->dlopenflags
= RTLD_NOW
;
59 interp
->dlopenflags
= RTLD_LAZY
;
64 interp
->next
= interp_head
;
74 PyInterpreterState_Clear(PyInterpreterState
*interp
)
78 for (p
= interp
->tstate_head
; p
!= NULL
; p
= p
->next
)
79 PyThreadState_Clear(p
);
81 ZAP(interp
->codec_search_path
);
82 ZAP(interp
->codec_search_cache
);
85 ZAP(interp
->builtins
);
90 zapthreads(PyInterpreterState
*interp
)
93 /* No need to lock the mutex here because this should only happen
94 when the threads are all really dead (XXX famous last words). */
95 while ((p
= interp
->tstate_head
) != NULL
) {
96 PyThreadState_Delete(p
);
102 PyInterpreterState_Delete(PyInterpreterState
*interp
)
104 PyInterpreterState
**p
;
107 for (p
= &interp_head
; ; p
= &(*p
)->next
) {
110 "PyInterpreterState_Delete: invalid interp");
114 if (interp
->tstate_head
!= NULL
)
115 Py_FatalError("PyInterpreterState_Delete: remaining threads");
122 /* Default implementation for _PyThreadState_GetFrame */
123 static struct _frame
*
124 threadstate_getframe(PyThreadState
*self
)
130 PyThreadState_New(PyInterpreterState
*interp
)
132 PyThreadState
*tstate
= PyMem_NEW(PyThreadState
, 1);
133 if (_PyThreadState_GetFrame
== NULL
)
134 _PyThreadState_GetFrame
= (unaryfunc
)threadstate_getframe
;
136 if (tstate
!= NULL
) {
137 tstate
->interp
= interp
;
139 tstate
->frame
= NULL
;
140 tstate
->recursion_depth
= 0;
143 tstate
->use_tracing
= 0;
144 tstate
->tick_counter
= 0;
148 tstate
->curexc_type
= NULL
;
149 tstate
->curexc_value
= NULL
;
150 tstate
->curexc_traceback
= NULL
;
152 tstate
->exc_type
= NULL
;
153 tstate
->exc_value
= NULL
;
154 tstate
->exc_traceback
= NULL
;
156 tstate
->c_profilefunc
= NULL
;
157 tstate
->c_tracefunc
= NULL
;
158 tstate
->c_profileobj
= NULL
;
159 tstate
->c_traceobj
= NULL
;
162 tstate
->next
= interp
->tstate_head
;
163 interp
->tstate_head
= tstate
;
172 PyThreadState_Clear(PyThreadState
*tstate
)
174 if (Py_VerboseFlag
&& tstate
->frame
!= NULL
)
176 "PyThreadState_Clear: warning: thread still has a frame\n");
182 ZAP(tstate
->curexc_type
);
183 ZAP(tstate
->curexc_value
);
184 ZAP(tstate
->curexc_traceback
);
186 ZAP(tstate
->exc_type
);
187 ZAP(tstate
->exc_value
);
188 ZAP(tstate
->exc_traceback
);
190 tstate
->c_profilefunc
= NULL
;
191 tstate
->c_tracefunc
= NULL
;
192 ZAP(tstate
->c_profileobj
);
193 ZAP(tstate
->c_traceobj
);
197 /* Common code for PyThreadState_Delete() and PyThreadState_DeleteCurrent() */
199 tstate_delete_common(PyThreadState
*tstate
)
201 PyInterpreterState
*interp
;
204 Py_FatalError("PyThreadState_Delete: NULL tstate");
205 interp
= tstate
->interp
;
207 Py_FatalError("PyThreadState_Delete: NULL interp");
209 for (p
= &interp
->tstate_head
; ; p
= &(*p
)->next
) {
212 "PyThreadState_Delete: invalid tstate");
223 PyThreadState_Delete(PyThreadState
*tstate
)
225 if (tstate
== _PyThreadState_Current
)
226 Py_FatalError("PyThreadState_Delete: tstate is still current");
227 tstate_delete_common(tstate
);
233 PyThreadState_DeleteCurrent()
235 PyThreadState
*tstate
= _PyThreadState_Current
;
238 "PyThreadState_DeleteCurrent: no current tstate");
239 _PyThreadState_Current
= NULL
;
240 tstate_delete_common(tstate
);
241 PyEval_ReleaseLock();
243 #endif /* WITH_THREAD */
247 PyThreadState_Get(void)
249 if (_PyThreadState_Current
== NULL
)
250 Py_FatalError("PyThreadState_Get: no current thread");
252 return _PyThreadState_Current
;
257 PyThreadState_Swap(PyThreadState
*new)
259 PyThreadState
*old
= _PyThreadState_Current
;
261 _PyThreadState_Current
= new;
266 /* An extension mechanism to store arbitrary additional per-thread state.
267 PyThreadState_GetDict() returns a dictionary that can be used to hold such
268 state; the caller should pick a unique key and store its state there. If
269 PyThreadState_GetDict() returns NULL, an exception has been raised (most
270 likely MemoryError) and the caller should pass on the exception. */
273 PyThreadState_GetDict(void)
275 if (_PyThreadState_Current
== NULL
)
276 Py_FatalError("PyThreadState_GetDict: no current thread");
278 if (_PyThreadState_Current
->dict
== NULL
)
279 _PyThreadState_Current
->dict
= PyDict_New();
280 return _PyThreadState_Current
->dict
;
284 /* Routines for advanced debuggers, requested by David Beazley.
285 Don't use unless you know what you are doing! */
288 PyInterpreterState_Head(void)
294 PyInterpreterState_Next(PyInterpreterState
*interp
) {
299 PyInterpreterState_ThreadHead(PyInterpreterState
*interp
) {
300 return interp
->tstate_head
;
304 PyThreadState_Next(PyThreadState
*tstate
) {