blob: 7ce499a0963b0c37aba9d58f7ed97d41308df234 [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
55PyInterpreterState_Delete(PyInterpreterState *interp)
56{
57 Py_XDECREF(interp->import_modules);
58 Py_XDECREF(interp->sysdict);
59
60 PyMem_DEL(interp);
61}
62
63
64PyThreadState *
65PyThreadState_New(PyInterpreterState *interp)
66{
67 PyThreadState *tstate = PyMem_NEW(PyThreadState, 1);
68 /* fprintf(stderr, "new tstate -> %p\n", tstate); */
69 if (tstate != NULL) {
70 tstate->interpreter_state = interp;
71
72 tstate->frame = NULL;
73 tstate->recursion_depth = 0;
74 tstate->ticker = 0;
75 tstate->tracing = 0;
76
77 tstate->curexc_type = NULL;
78 tstate->curexc_value = NULL;
79 tstate->curexc_traceback = NULL;
80
81 tstate->exc_type = NULL;
82 tstate->exc_value = NULL;
83 tstate->exc_traceback = NULL;
84
85 tstate->sys_profilefunc = NULL;
86 tstate->sys_tracefunc = NULL;
87 tstate->sys_checkinterval = 0;
88
89 interp->nthreads++;
90 }
91 return tstate;
92}
93
94
95void
96PyThreadState_Delete(PyThreadState *tstate)
97{
98 /* fprintf(stderr, "delete tstate %p\n", tstate); */
99 if (tstate == current_tstate)
100 current_tstate = NULL;
101 tstate->interpreter_state->nthreads--;
102
103 Py_XDECREF((PyObject *) (tstate->frame)); /* XXX really? */
104
105 Py_XDECREF(tstate->curexc_type);
106 Py_XDECREF(tstate->curexc_value);
107 Py_XDECREF(tstate->curexc_traceback);
108
109 Py_XDECREF(tstate->exc_type);
110 Py_XDECREF(tstate->exc_value);
111 Py_XDECREF(tstate->exc_traceback);
112
113 Py_XDECREF(tstate->sys_profilefunc);
114 Py_XDECREF(tstate->sys_tracefunc);
115
116 PyMem_DEL(tstate);
117}
118
119
120PyThreadState *
121PyThreadState_Get()
122{
123 /* fprintf(stderr, "get tstate -> %p\n", current_tstate); */
124 return current_tstate;
125}
126
127
128PyThreadState *
129PyThreadState_Swap(PyThreadState *new)
130{
131 PyThreadState *old = current_tstate;
132 /* fprintf(stderr, "swap tstate new=%p <--> old=%p\n", new, old); */
133 current_tstate = new;
134 return old;
135}
136
137
138/* How should one use this code?
139
140 1. Standard Python interpreter, assuming no other interpreters or threads:
141
142 PyInterpreterState *interp;
143 PyThreadState *tstate;
144 interp = PyInterpreterState_New();
145 if (interp == NULL)
146 Py_FatalError("...");
147 tstate = PyThreadState_New(interp);
148 if (tstate == NULL)
149 Py_FatalError("...");
150 (void) PyThreadState_Swap(tstate);
151 PyInitialize();
152 .
153 . (use the interpreter here)
154 .
155 Py_Cleanup();
156 PyThreadState_Delete(tstate);
157 PyInterpreterState_Delete(interp);
158
159 2. Totally indepent interpreter invocation in a separate C thread:
160
161 XXX Need to interact with the thread lock nevertheless!!!
162
163*/