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); \
45 static PyThread_type_lock head_mutex
= NULL
; /* Protects interp->tstate_head */
46 #define HEAD_INIT() (head_mutex || (head_mutex = PyThread_allocate_lock()))
47 #define HEAD_LOCK() PyThread_acquire_lock(head_mutex, WAIT_LOCK)
48 #define HEAD_UNLOCK() PyThread_release_lock(head_mutex)
50 #define HEAD_INIT() /* Nothing */
51 #define HEAD_LOCK() /* Nothing */
52 #define HEAD_UNLOCK() /* Nothing */
55 static PyInterpreterState
*interp_head
= NULL
;
57 PyThreadState
*_PyThreadState_Current
= NULL
;
61 PyInterpreterState_New()
63 PyInterpreterState
*interp
= PyMem_NEW(PyInterpreterState
, 1);
67 interp
->modules
= NULL
;
68 interp
->sysdict
= NULL
;
69 interp
->builtins
= NULL
;
70 interp
->checkinterval
= 10;
71 interp
->tstate_head
= NULL
;
73 interp
->next
= interp_head
;
82 PyInterpreterState_Clear(interp
)
83 PyInterpreterState
*interp
;
87 for (p
= interp
->tstate_head
; p
!= NULL
; p
= p
->next
)
88 PyThreadState_Clear(p
);
92 ZAP(interp
->builtins
);
98 PyInterpreterState
*interp
;
101 /* No need to lock the mutex here because this should only happen
102 when the threads are all really dead (XXX famous last words). */
103 while ((p
= interp
->tstate_head
) != NULL
) {
104 PyThreadState_Delete(p
);
110 PyInterpreterState_Delete(interp
)
111 PyInterpreterState
*interp
;
113 PyInterpreterState
**p
;
115 for (p
= &interp_head
; ; p
= &(*p
)->next
) {
118 "PyInterpreterState_Delete: invalid interp");
122 if (interp
->tstate_head
!= NULL
)
123 Py_FatalError("PyInterpreterState_Delete: remaining threads");
130 PyThreadState_New(interp
)
131 PyInterpreterState
*interp
;
133 PyThreadState
*tstate
= PyMem_NEW(PyThreadState
, 1);
135 if (tstate
!= NULL
) {
136 tstate
->interp
= interp
;
138 tstate
->frame
= NULL
;
139 tstate
->recursion_depth
= 0;
145 tstate
->curexc_type
= NULL
;
146 tstate
->curexc_value
= NULL
;
147 tstate
->curexc_traceback
= NULL
;
149 tstate
->exc_type
= NULL
;
150 tstate
->exc_value
= NULL
;
151 tstate
->exc_traceback
= NULL
;
153 tstate
->sys_profilefunc
= NULL
;
154 tstate
->sys_tracefunc
= NULL
;
157 tstate
->next
= interp
->tstate_head
;
158 interp
->tstate_head
= tstate
;
167 PyThreadState_Clear(tstate
)
168 PyThreadState
*tstate
;
170 if (Py_VerboseFlag
&& tstate
->frame
!= NULL
)
172 "PyThreadState_Clear: warning: thread still has a frame\n");
178 ZAP(tstate
->curexc_type
);
179 ZAP(tstate
->curexc_value
);
180 ZAP(tstate
->curexc_traceback
);
182 ZAP(tstate
->exc_type
);
183 ZAP(tstate
->exc_value
);
184 ZAP(tstate
->exc_traceback
);
186 ZAP(tstate
->sys_profilefunc
);
187 ZAP(tstate
->sys_tracefunc
);
192 PyThreadState_Delete(tstate
)
193 PyThreadState
*tstate
;
195 PyInterpreterState
*interp
;
198 Py_FatalError("PyThreadState_Delete: NULL tstate");
199 if (tstate
== _PyThreadState_Current
)
200 Py_FatalError("PyThreadState_Delete: tstate is still current");
201 interp
= tstate
->interp
;
203 Py_FatalError("PyThreadState_Delete: NULL interp");
205 for (p
= &interp
->tstate_head
; ; p
= &(*p
)->next
) {
208 "PyThreadState_Delete: invalid tstate");
221 if (_PyThreadState_Current
== NULL
)
222 Py_FatalError("PyThreadState_Get: no current thread");
224 return _PyThreadState_Current
;
229 PyThreadState_Swap(new)
232 PyThreadState
*old
= _PyThreadState_Current
;
234 _PyThreadState_Current
= new;
239 /* An extension mechanism to store arbitrary additional per-thread state.
240 PyThreadState_GetDict() returns a dictionary that can be used to hold such
241 state; the caller should pick a unique key and store its state there. If
242 PyThreadState_GetDict() returns NULL, an exception has been raised (most
243 likely MemoryError) and the caller should pass on the exception. */
246 PyThreadState_GetDict()
248 if (_PyThreadState_Current
== NULL
)
249 Py_FatalError("PyThreadState_GetDict: no current thread");
251 if (_PyThreadState_Current
->dict
== NULL
)
252 _PyThreadState_Current
->dict
= PyDict_New();
253 return _PyThreadState_Current
->dict
;