1 /***********************************************************
2 Copyright (c) 2000, BeOpen.com.
3 Copyright (c) 1995-2000, Corporation for National Research Initiatives.
4 Copyright (c) 1990-1995, Stichting Mathematisch Centrum.
7 See the file "Misc/COPYRIGHT" for information on usage and
8 redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES.
9 ******************************************************************/
11 /* Thread and interpreter state structures and their interfaces */
16 PyObject *tmp = (PyObject *)(x); \
24 static PyThread_type_lock head_mutex
= NULL
; /* Protects interp->tstate_head */
25 #define HEAD_INIT() (void)(head_mutex || (head_mutex = PyThread_allocate_lock()))
26 #define HEAD_LOCK() PyThread_acquire_lock(head_mutex, WAIT_LOCK)
27 #define HEAD_UNLOCK() PyThread_release_lock(head_mutex)
29 #define HEAD_INIT() /* Nothing */
30 #define HEAD_LOCK() /* Nothing */
31 #define HEAD_UNLOCK() /* Nothing */
34 static PyInterpreterState
*interp_head
= NULL
;
36 PyThreadState
*_PyThreadState_Current
= NULL
;
40 PyInterpreterState_New(void)
42 PyInterpreterState
*interp
= PyMem_NEW(PyInterpreterState
, 1);
46 interp
->modules
= NULL
;
47 interp
->sysdict
= NULL
;
48 interp
->builtins
= NULL
;
49 interp
->checkinterval
= 10;
50 interp
->tstate_head
= NULL
;
52 interp
->next
= interp_head
;
61 PyInterpreterState_Clear(PyInterpreterState
*interp
)
65 for (p
= interp
->tstate_head
; p
!= NULL
; p
= p
->next
)
66 PyThreadState_Clear(p
);
70 ZAP(interp
->builtins
);
75 zapthreads(PyInterpreterState
*interp
)
78 /* No need to lock the mutex here because this should only happen
79 when the threads are all really dead (XXX famous last words). */
80 while ((p
= interp
->tstate_head
) != NULL
) {
81 PyThreadState_Delete(p
);
87 PyInterpreterState_Delete(PyInterpreterState
*interp
)
89 PyInterpreterState
**p
;
91 for (p
= &interp_head
; ; p
= &(*p
)->next
) {
94 "PyInterpreterState_Delete: invalid interp");
98 if (interp
->tstate_head
!= NULL
)
99 Py_FatalError("PyInterpreterState_Delete: remaining threads");
106 PyThreadState_New(PyInterpreterState
*interp
)
108 PyThreadState
*tstate
= PyMem_NEW(PyThreadState
, 1);
110 if (tstate
!= NULL
) {
111 tstate
->interp
= interp
;
113 tstate
->frame
= NULL
;
114 tstate
->recursion_depth
= 0;
120 tstate
->curexc_type
= NULL
;
121 tstate
->curexc_value
= NULL
;
122 tstate
->curexc_traceback
= NULL
;
124 tstate
->exc_type
= NULL
;
125 tstate
->exc_value
= NULL
;
126 tstate
->exc_traceback
= NULL
;
128 tstate
->sys_profilefunc
= NULL
;
129 tstate
->sys_tracefunc
= NULL
;
132 tstate
->next
= interp
->tstate_head
;
133 interp
->tstate_head
= tstate
;
142 PyThreadState_Clear(PyThreadState
*tstate
)
144 if (Py_VerboseFlag
&& tstate
->frame
!= NULL
)
146 "PyThreadState_Clear: warning: thread still has a frame\n");
152 ZAP(tstate
->curexc_type
);
153 ZAP(tstate
->curexc_value
);
154 ZAP(tstate
->curexc_traceback
);
156 ZAP(tstate
->exc_type
);
157 ZAP(tstate
->exc_value
);
158 ZAP(tstate
->exc_traceback
);
160 ZAP(tstate
->sys_profilefunc
);
161 ZAP(tstate
->sys_tracefunc
);
166 PyThreadState_Delete(PyThreadState
*tstate
)
168 PyInterpreterState
*interp
;
171 Py_FatalError("PyThreadState_Delete: NULL tstate");
172 if (tstate
== _PyThreadState_Current
)
173 Py_FatalError("PyThreadState_Delete: tstate is still current");
174 interp
= tstate
->interp
;
176 Py_FatalError("PyThreadState_Delete: NULL interp");
178 for (p
= &interp
->tstate_head
; ; p
= &(*p
)->next
) {
181 "PyThreadState_Delete: invalid tstate");
192 PyThreadState_Get(void)
194 if (_PyThreadState_Current
== NULL
)
195 Py_FatalError("PyThreadState_Get: no current thread");
197 return _PyThreadState_Current
;
202 PyThreadState_Swap(PyThreadState
*new)
204 PyThreadState
*old
= _PyThreadState_Current
;
206 _PyThreadState_Current
= new;
211 /* An extension mechanism to store arbitrary additional per-thread state.
212 PyThreadState_GetDict() returns a dictionary that can be used to hold such
213 state; the caller should pick a unique key and store its state there. If
214 PyThreadState_GetDict() returns NULL, an exception has been raised (most
215 likely MemoryError) and the caller should pass on the exception. */
218 PyThreadState_GetDict(void)
220 if (_PyThreadState_Current
== NULL
)
221 Py_FatalError("PyThreadState_GetDict: no current thread");
223 if (_PyThreadState_Current
->dict
== NULL
)
224 _PyThreadState_Current
->dict
= PyDict_New();
225 return _PyThreadState_Current
->dict
;