blob: 857931ea5c0220411d6f9d9b47339e30e2bbc90a [file] [log] [blame]
Guido van Rossuma027efa1997-05-05 20:56:21 +00001/***********************************************************
2Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
3The Netherlands.
4
5 All Rights Reserved
6
7Permission to use, copy, modify, and distribute this software and its
8documentation for any purpose and without fee is hereby granted,
9provided that the above copyright notice appear in all copies and that
10both that copyright notice and this permission notice appear in
11supporting documentation, and that the names of Stichting Mathematisch
12Centrum or CWI or Corporation for National Research Initiatives or
13CNRI not be used in advertising or publicity pertaining to
14distribution of the software without specific, written prior
15permission.
16
17While CWI is the initial source for this software, a modified version
18is made available by the Corporation for National Research Initiatives
19(CNRI) at the Internet address ftp://ftp.python.org.
20
21STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH
22REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF
23MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH
24CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
25DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
26PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
27TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
28PERFORMANCE OF THIS SOFTWARE.
29
30******************************************************************/
31
32/* Thread and interpreter state structures and their interfaces */
33
34#include "Python.h"
35
36
37static PyThreadState *current_tstate = NULL;
38
39
40PyInterpreterState *
41PyInterpreterState_New()
42{
43 PyInterpreterState *interp = PyMem_NEW(PyInterpreterState, 1);
44 if (interp != NULL) {
45 interp->import_modules = NULL;
46 interp->sysdict = NULL;
47 interp->nthreads = 0;
48 interp->nexitfuncs = 0;
49 }
50 return interp;
51}
52
53
54void
Guido van Rossumf9cba091997-05-20 22:23:34 +000055PyInterpreterState_Delete(interp)
56 PyInterpreterState *interp;
Guido van Rossuma027efa1997-05-05 20:56:21 +000057{
58 Py_XDECREF(interp->import_modules);
59 Py_XDECREF(interp->sysdict);
60
61 PyMem_DEL(interp);
62}
63
64
65PyThreadState *
Guido van Rossumf9cba091997-05-20 22:23:34 +000066PyThreadState_New(interp)
67 PyInterpreterState *interp;
Guido van Rossuma027efa1997-05-05 20:56:21 +000068{
69 PyThreadState *tstate = PyMem_NEW(PyThreadState, 1);
70 /* fprintf(stderr, "new tstate -> %p\n", tstate); */
71 if (tstate != NULL) {
72 tstate->interpreter_state = interp;
73
74 tstate->frame = NULL;
75 tstate->recursion_depth = 0;
76 tstate->ticker = 0;
77 tstate->tracing = 0;
78
79 tstate->curexc_type = NULL;
80 tstate->curexc_value = NULL;
81 tstate->curexc_traceback = NULL;
82
83 tstate->exc_type = NULL;
84 tstate->exc_value = NULL;
85 tstate->exc_traceback = NULL;
86
87 tstate->sys_profilefunc = NULL;
88 tstate->sys_tracefunc = NULL;
89 tstate->sys_checkinterval = 0;
90
91 interp->nthreads++;
92 }
93 return tstate;
94}
95
96
97void
Guido van Rossumf9cba091997-05-20 22:23:34 +000098PyThreadState_Delete(tstate)
99 PyThreadState *tstate;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000100{
101 /* fprintf(stderr, "delete tstate %p\n", tstate); */
102 if (tstate == current_tstate)
103 current_tstate = NULL;
104 tstate->interpreter_state->nthreads--;
105
106 Py_XDECREF((PyObject *) (tstate->frame)); /* XXX really? */
107
108 Py_XDECREF(tstate->curexc_type);
109 Py_XDECREF(tstate->curexc_value);
110 Py_XDECREF(tstate->curexc_traceback);
111
112 Py_XDECREF(tstate->exc_type);
113 Py_XDECREF(tstate->exc_value);
114 Py_XDECREF(tstate->exc_traceback);
115
116 Py_XDECREF(tstate->sys_profilefunc);
117 Py_XDECREF(tstate->sys_tracefunc);
118
119 PyMem_DEL(tstate);
120}
121
122
123PyThreadState *
124PyThreadState_Get()
125{
126 /* fprintf(stderr, "get tstate -> %p\n", current_tstate); */
127 return current_tstate;
128}
129
130
131PyThreadState *
Guido van Rossumf9cba091997-05-20 22:23:34 +0000132PyThreadState_Swap(new)
133 PyThreadState *new;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000134{
135 PyThreadState *old = current_tstate;
136 /* fprintf(stderr, "swap tstate new=%p <--> old=%p\n", new, old); */
137 current_tstate = new;
138 return old;
139}
140
141
142/* How should one use this code?
143
144 1. Standard Python interpreter, assuming no other interpreters or threads:
145
146 PyInterpreterState *interp;
147 PyThreadState *tstate;
148 interp = PyInterpreterState_New();
149 if (interp == NULL)
150 Py_FatalError("...");
151 tstate = PyThreadState_New(interp);
152 if (tstate == NULL)
153 Py_FatalError("...");
154 (void) PyThreadState_Swap(tstate);
155 PyInitialize();
156 .
157 . (use the interpreter here)
158 .
159 Py_Cleanup();
160 PyThreadState_Delete(tstate);
161 PyInterpreterState_Delete(interp);
162
163 2. Totally indepent interpreter invocation in a separate C thread:
164
165 XXX Need to interact with the thread lock nevertheless!!!
166
167*/