blob: 9a41ccffd462574a15fd4050d5120d036aa4a2fd [file] [log] [blame]
Guido van Rossuma027efa1997-05-05 20:56:21 +00001
2/* Thread and interpreter state structures and their interfaces */
3
4#include "Python.h"
5
Martin v. Löwisf0473d52001-07-18 16:17:16 +00006#ifdef HAVE_DLOPEN
7#ifdef HAVE_DLFCN_H
8#include <dlfcn.h>
9#endif
10#ifndef RTLD_LAZY
11#define RTLD_LAZY 1
12#endif
13#endif
14
15
Guido van Rossum25ce5661997-08-02 03:10:38 +000016#define ZAP(x) { \
17 PyObject *tmp = (PyObject *)(x); \
18 (x) = NULL; \
19 Py_XDECREF(tmp); \
20}
21
22
Guido van Rossum1d5ad901999-06-18 14:22:24 +000023#ifdef WITH_THREAD
24#include "pythread.h"
25static PyThread_type_lock head_mutex = NULL; /* Protects interp->tstate_head */
Moshe Zadka9fb6af92000-08-04 21:27:47 +000026#define HEAD_INIT() (void)(head_mutex || (head_mutex = PyThread_allocate_lock()))
Guido van Rossum1d5ad901999-06-18 14:22:24 +000027#define HEAD_LOCK() PyThread_acquire_lock(head_mutex, WAIT_LOCK)
28#define HEAD_UNLOCK() PyThread_release_lock(head_mutex)
29#else
30#define HEAD_INIT() /* Nothing */
31#define HEAD_LOCK() /* Nothing */
32#define HEAD_UNLOCK() /* Nothing */
33#endif
34
Guido van Rossum25ce5661997-08-02 03:10:38 +000035static PyInterpreterState *interp_head = NULL;
Guido van Rossuma027efa1997-05-05 20:56:21 +000036
Guido van Rossum18bc7c21998-12-21 18:27:28 +000037PyThreadState *_PyThreadState_Current = NULL;
Guido van Rossuma027efa1997-05-05 20:56:21 +000038
39
40PyInterpreterState *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000041PyInterpreterState_New(void)
Guido van Rossuma027efa1997-05-05 20:56:21 +000042{
43 PyInterpreterState *interp = PyMem_NEW(PyInterpreterState, 1);
Guido van Rossum25ce5661997-08-02 03:10:38 +000044
Guido van Rossuma027efa1997-05-05 20:56:21 +000045 if (interp != NULL) {
Guido van Rossum1d5ad901999-06-18 14:22:24 +000046 HEAD_INIT();
Guido van Rossum25ce5661997-08-02 03:10:38 +000047 interp->modules = NULL;
Guido van Rossuma027efa1997-05-05 20:56:21 +000048 interp->sysdict = NULL;
Guido van Rossum25ce5661997-08-02 03:10:38 +000049 interp->builtins = NULL;
50 interp->checkinterval = 10;
51 interp->tstate_head = NULL;
Martin v. Löwisf0473d52001-07-18 16:17:16 +000052#ifdef HAVE_DLOPEN
53#ifdef RTLD_NOW
54 interp->dlopenflags = RTLD_NOW;
55#else
56 interp->dlopenflags = RTLD_LAZY;
57#endif
58#endif
Guido van Rossum25ce5661997-08-02 03:10:38 +000059
Tim Peters412f2462000-09-02 09:16:15 +000060 HEAD_LOCK();
Guido van Rossum25ce5661997-08-02 03:10:38 +000061 interp->next = interp_head;
62 interp_head = interp;
Tim Peters412f2462000-09-02 09:16:15 +000063 HEAD_UNLOCK();
Guido van Rossuma027efa1997-05-05 20:56:21 +000064 }
Guido van Rossum25ce5661997-08-02 03:10:38 +000065
Guido van Rossuma027efa1997-05-05 20:56:21 +000066 return interp;
67}
68
69
70void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000071PyInterpreterState_Clear(PyInterpreterState *interp)
Guido van Rossum25ce5661997-08-02 03:10:38 +000072{
73 PyThreadState *p;
Guido van Rossum1d5ad901999-06-18 14:22:24 +000074 HEAD_LOCK();
Guido van Rossum25ce5661997-08-02 03:10:38 +000075 for (p = interp->tstate_head; p != NULL; p = p->next)
76 PyThreadState_Clear(p);
Guido van Rossum1d5ad901999-06-18 14:22:24 +000077 HEAD_UNLOCK();
Guido van Rossum25ce5661997-08-02 03:10:38 +000078 ZAP(interp->modules);
79 ZAP(interp->sysdict);
80 ZAP(interp->builtins);
81}
82
83
84static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000085zapthreads(PyInterpreterState *interp)
Guido van Rossum25ce5661997-08-02 03:10:38 +000086{
Guido van Rossum1d5ad901999-06-18 14:22:24 +000087 PyThreadState *p;
88 /* No need to lock the mutex here because this should only happen
89 when the threads are all really dead (XXX famous last words). */
90 while ((p = interp->tstate_head) != NULL) {
Guido van Rossum25ce5661997-08-02 03:10:38 +000091 PyThreadState_Delete(p);
Guido van Rossum25ce5661997-08-02 03:10:38 +000092 }
93}
94
95
96void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000097PyInterpreterState_Delete(PyInterpreterState *interp)
Guido van Rossuma027efa1997-05-05 20:56:21 +000098{
Guido van Rossum25ce5661997-08-02 03:10:38 +000099 PyInterpreterState **p;
100 zapthreads(interp);
Tim Peters412f2462000-09-02 09:16:15 +0000101 HEAD_LOCK();
Guido van Rossum25ce5661997-08-02 03:10:38 +0000102 for (p = &interp_head; ; p = &(*p)->next) {
103 if (*p == NULL)
104 Py_FatalError(
105 "PyInterpreterState_Delete: invalid interp");
106 if (*p == interp)
107 break;
108 }
109 if (interp->tstate_head != NULL)
110 Py_FatalError("PyInterpreterState_Delete: remaining threads");
111 *p = interp->next;
Tim Peters412f2462000-09-02 09:16:15 +0000112 HEAD_UNLOCK();
Guido van Rossuma027efa1997-05-05 20:56:21 +0000113 PyMem_DEL(interp);
114}
115
116
117PyThreadState *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000118PyThreadState_New(PyInterpreterState *interp)
Guido van Rossuma027efa1997-05-05 20:56:21 +0000119{
120 PyThreadState *tstate = PyMem_NEW(PyThreadState, 1);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000121
Guido van Rossuma027efa1997-05-05 20:56:21 +0000122 if (tstate != NULL) {
Guido van Rossum25ce5661997-08-02 03:10:38 +0000123 tstate->interp = interp;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000124
125 tstate->frame = NULL;
126 tstate->recursion_depth = 0;
127 tstate->ticker = 0;
128 tstate->tracing = 0;
Fred Drake9e3ad782001-07-03 23:39:52 +0000129 tstate->use_tracing = 0;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000130
Guido van Rossumede04391998-04-10 20:18:25 +0000131 tstate->dict = NULL;
132
Guido van Rossuma027efa1997-05-05 20:56:21 +0000133 tstate->curexc_type = NULL;
134 tstate->curexc_value = NULL;
135 tstate->curexc_traceback = NULL;
136
137 tstate->exc_type = NULL;
138 tstate->exc_value = NULL;
139 tstate->exc_traceback = NULL;
140
Fred Drake5755ce62001-06-27 19:19:46 +0000141 tstate->c_profilefunc = NULL;
142 tstate->c_tracefunc = NULL;
143 tstate->c_profileobj = NULL;
144 tstate->c_traceobj = NULL;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000145
Guido van Rossum1d5ad901999-06-18 14:22:24 +0000146 HEAD_LOCK();
Guido van Rossum25ce5661997-08-02 03:10:38 +0000147 tstate->next = interp->tstate_head;
148 interp->tstate_head = tstate;
Guido van Rossum1d5ad901999-06-18 14:22:24 +0000149 HEAD_UNLOCK();
Guido van Rossuma027efa1997-05-05 20:56:21 +0000150 }
Guido van Rossum25ce5661997-08-02 03:10:38 +0000151
Guido van Rossuma027efa1997-05-05 20:56:21 +0000152 return tstate;
153}
154
155
156void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000157PyThreadState_Clear(PyThreadState *tstate)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000158{
Guido van Rossum22348dc1997-11-03 22:08:36 +0000159 if (Py_VerboseFlag && tstate->frame != NULL)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000160 fprintf(stderr,
Guido van Rossum5f896a41997-08-21 02:28:19 +0000161 "PyThreadState_Clear: warning: thread still has a frame\n");
Guido van Rossum25ce5661997-08-02 03:10:38 +0000162
163 ZAP(tstate->frame);
164
Guido van Rossumede04391998-04-10 20:18:25 +0000165 ZAP(tstate->dict);
166
Guido van Rossum25ce5661997-08-02 03:10:38 +0000167 ZAP(tstate->curexc_type);
168 ZAP(tstate->curexc_value);
169 ZAP(tstate->curexc_traceback);
170
171 ZAP(tstate->exc_type);
172 ZAP(tstate->exc_value);
173 ZAP(tstate->exc_traceback);
174
Fred Drake5755ce62001-06-27 19:19:46 +0000175 tstate->c_profilefunc = NULL;
176 tstate->c_tracefunc = NULL;
177 ZAP(tstate->c_profileobj);
178 ZAP(tstate->c_traceobj);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000179}
180
181
Guido van Rossum29757862001-01-23 01:46:06 +0000182/* Common code for PyThreadState_Delete() and PyThreadState_DeleteCurrent() */
183static void
184tstate_delete_common(PyThreadState *tstate)
Guido van Rossuma027efa1997-05-05 20:56:21 +0000185{
Guido van Rossum25ce5661997-08-02 03:10:38 +0000186 PyInterpreterState *interp;
187 PyThreadState **p;
188 if (tstate == NULL)
189 Py_FatalError("PyThreadState_Delete: NULL tstate");
Guido van Rossum25ce5661997-08-02 03:10:38 +0000190 interp = tstate->interp;
191 if (interp == NULL)
192 Py_FatalError("PyThreadState_Delete: NULL interp");
Guido van Rossum1d5ad901999-06-18 14:22:24 +0000193 HEAD_LOCK();
Guido van Rossum25ce5661997-08-02 03:10:38 +0000194 for (p = &interp->tstate_head; ; p = &(*p)->next) {
195 if (*p == NULL)
196 Py_FatalError(
197 "PyThreadState_Delete: invalid tstate");
198 if (*p == tstate)
199 break;
200 }
201 *p = tstate->next;
Guido van Rossum1d5ad901999-06-18 14:22:24 +0000202 HEAD_UNLOCK();
Guido van Rossuma027efa1997-05-05 20:56:21 +0000203 PyMem_DEL(tstate);
204}
205
206
Guido van Rossum29757862001-01-23 01:46:06 +0000207void
208PyThreadState_Delete(PyThreadState *tstate)
209{
210 if (tstate == _PyThreadState_Current)
211 Py_FatalError("PyThreadState_Delete: tstate is still current");
212 tstate_delete_common(tstate);
213}
214
215
216#ifdef WITH_THREAD
217void
218PyThreadState_DeleteCurrent()
219{
220 PyThreadState *tstate = _PyThreadState_Current;
221 if (tstate == NULL)
222 Py_FatalError(
223 "PyThreadState_DeleteCurrent: no current tstate");
224 _PyThreadState_Current = NULL;
225 tstate_delete_common(tstate);
226 PyEval_ReleaseLock();
227}
228#endif /* WITH_THREAD */
229
230
Guido van Rossuma027efa1997-05-05 20:56:21 +0000231PyThreadState *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000232PyThreadState_Get(void)
Guido van Rossuma027efa1997-05-05 20:56:21 +0000233{
Guido van Rossum18bc7c21998-12-21 18:27:28 +0000234 if (_PyThreadState_Current == NULL)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000235 Py_FatalError("PyThreadState_Get: no current thread");
236
Guido van Rossum18bc7c21998-12-21 18:27:28 +0000237 return _PyThreadState_Current;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000238}
239
240
241PyThreadState *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000242PyThreadState_Swap(PyThreadState *new)
Guido van Rossuma027efa1997-05-05 20:56:21 +0000243{
Guido van Rossum18bc7c21998-12-21 18:27:28 +0000244 PyThreadState *old = _PyThreadState_Current;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000245
Guido van Rossum18bc7c21998-12-21 18:27:28 +0000246 _PyThreadState_Current = new;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000247
Guido van Rossuma027efa1997-05-05 20:56:21 +0000248 return old;
249}
Guido van Rossumede04391998-04-10 20:18:25 +0000250
251/* An extension mechanism to store arbitrary additional per-thread state.
252 PyThreadState_GetDict() returns a dictionary that can be used to hold such
253 state; the caller should pick a unique key and store its state there. If
254 PyThreadState_GetDict() returns NULL, an exception has been raised (most
255 likely MemoryError) and the caller should pass on the exception. */
256
257PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000258PyThreadState_GetDict(void)
Guido van Rossumede04391998-04-10 20:18:25 +0000259{
Guido van Rossum18bc7c21998-12-21 18:27:28 +0000260 if (_PyThreadState_Current == NULL)
Guido van Rossumede04391998-04-10 20:18:25 +0000261 Py_FatalError("PyThreadState_GetDict: no current thread");
262
Guido van Rossum18bc7c21998-12-21 18:27:28 +0000263 if (_PyThreadState_Current->dict == NULL)
264 _PyThreadState_Current->dict = PyDict_New();
265 return _PyThreadState_Current->dict;
Guido van Rossumede04391998-04-10 20:18:25 +0000266}
Guido van Rossumf5df46d2001-07-19 12:19:27 +0000267
268
269/* Routines for advanced debuggers, requested by David Beazley.
270 Don't use unless you know what you are doing! */
271
272PyInterpreterState *
273PyInterpreterState_Head(void)
274{
275 return interp_head;
276}
277
278PyInterpreterState *
279PyInterpreterState_Next(PyInterpreterState *interp) {
280 return interp->next;
281}
282
283PyThreadState *
284PyInterpreterState_ThreadHead(PyInterpreterState *interp) {
285 return interp->tstate_head;
286}
287
288PyThreadState *
289PyThreadState_Next(PyThreadState *tstate) {
290 return tstate->next;
291}