2 /* Thread and interpreter state structures and their interfaces */
7 PyObject *tmp = (PyObject *)(x); \
15 static PyThread_type_lock head_mutex
= NULL
; /* Protects interp->tstate_head */
16 #define HEAD_INIT() (void)(head_mutex || (head_mutex = PyThread_allocate_lock()))
17 #define HEAD_LOCK() PyThread_acquire_lock(head_mutex, WAIT_LOCK)
18 #define HEAD_UNLOCK() PyThread_release_lock(head_mutex)
20 #define HEAD_INIT() /* Nothing */
21 #define HEAD_LOCK() /* Nothing */
22 #define HEAD_UNLOCK() /* Nothing */
25 static PyInterpreterState
*interp_head
= NULL
;
27 PyThreadState
*_PyThreadState_Current
= NULL
;
31 PyInterpreterState_New(void)
33 PyInterpreterState
*interp
= PyMem_NEW(PyInterpreterState
, 1);
37 interp
->modules
= NULL
;
38 interp
->sysdict
= NULL
;
39 interp
->builtins
= NULL
;
40 interp
->checkinterval
= 10;
41 interp
->tstate_head
= NULL
;
44 interp
->next
= interp_head
;
54 PyInterpreterState_Clear(PyInterpreterState
*interp
)
58 for (p
= interp
->tstate_head
; p
!= NULL
; p
= p
->next
)
59 PyThreadState_Clear(p
);
63 ZAP(interp
->builtins
);
68 zapthreads(PyInterpreterState
*interp
)
71 /* No need to lock the mutex here because this should only happen
72 when the threads are all really dead (XXX famous last words). */
73 while ((p
= interp
->tstate_head
) != NULL
) {
74 PyThreadState_Delete(p
);
80 PyInterpreterState_Delete(PyInterpreterState
*interp
)
82 PyInterpreterState
**p
;
85 for (p
= &interp_head
; ; p
= &(*p
)->next
) {
88 "PyInterpreterState_Delete: invalid interp");
92 if (interp
->tstate_head
!= NULL
)
93 Py_FatalError("PyInterpreterState_Delete: remaining threads");
101 PyThreadState_New(PyInterpreterState
*interp
)
103 PyThreadState
*tstate
= PyMem_NEW(PyThreadState
, 1);
105 if (tstate
!= NULL
) {
106 tstate
->interp
= interp
;
108 tstate
->frame
= NULL
;
109 tstate
->recursion_depth
= 0;
115 tstate
->curexc_type
= NULL
;
116 tstate
->curexc_value
= NULL
;
117 tstate
->curexc_traceback
= NULL
;
119 tstate
->exc_type
= NULL
;
120 tstate
->exc_value
= NULL
;
121 tstate
->exc_traceback
= NULL
;
123 tstate
->sys_profilefunc
= NULL
;
124 tstate
->sys_tracefunc
= NULL
;
127 tstate
->next
= interp
->tstate_head
;
128 interp
->tstate_head
= tstate
;
137 PyThreadState_Clear(PyThreadState
*tstate
)
139 if (Py_VerboseFlag
&& tstate
->frame
!= NULL
)
141 "PyThreadState_Clear: warning: thread still has a frame\n");
147 ZAP(tstate
->curexc_type
);
148 ZAP(tstate
->curexc_value
);
149 ZAP(tstate
->curexc_traceback
);
151 ZAP(tstate
->exc_type
);
152 ZAP(tstate
->exc_value
);
153 ZAP(tstate
->exc_traceback
);
155 ZAP(tstate
->sys_profilefunc
);
156 ZAP(tstate
->sys_tracefunc
);
160 /* Common code for PyThreadState_Delete() and PyThreadState_DeleteCurrent() */
162 tstate_delete_common(PyThreadState
*tstate
)
164 PyInterpreterState
*interp
;
167 Py_FatalError("PyThreadState_Delete: NULL tstate");
168 interp
= tstate
->interp
;
170 Py_FatalError("PyThreadState_Delete: NULL interp");
172 for (p
= &interp
->tstate_head
; ; p
= &(*p
)->next
) {
175 "PyThreadState_Delete: invalid tstate");
186 PyThreadState_Delete(PyThreadState
*tstate
)
188 if (tstate
== _PyThreadState_Current
)
189 Py_FatalError("PyThreadState_Delete: tstate is still current");
190 tstate_delete_common(tstate
);
196 PyThreadState_DeleteCurrent()
198 PyThreadState
*tstate
= _PyThreadState_Current
;
201 "PyThreadState_DeleteCurrent: no current tstate");
202 _PyThreadState_Current
= NULL
;
203 tstate_delete_common(tstate
);
204 PyEval_ReleaseLock();
206 #endif /* WITH_THREAD */
210 PyThreadState_Get(void)
212 if (_PyThreadState_Current
== NULL
)
213 Py_FatalError("PyThreadState_Get: no current thread");
215 return _PyThreadState_Current
;
220 PyThreadState_Swap(PyThreadState
*new)
222 PyThreadState
*old
= _PyThreadState_Current
;
224 _PyThreadState_Current
= new;
229 /* An extension mechanism to store arbitrary additional per-thread state.
230 PyThreadState_GetDict() returns a dictionary that can be used to hold such
231 state; the caller should pick a unique key and store its state there. If
232 PyThreadState_GetDict() returns NULL, an exception has been raised (most
233 likely MemoryError) and the caller should pass on the exception. */
236 PyThreadState_GetDict(void)
238 if (_PyThreadState_Current
== NULL
)
239 Py_FatalError("PyThreadState_GetDict: no current thread");
241 if (_PyThreadState_Current
->dict
== NULL
)
242 _PyThreadState_Current
->dict
= PyDict_New();
243 return _PyThreadState_Current
->dict
;