Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 1 | /*********************************************************** |
| 2 | Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam, |
| 3 | The Netherlands. |
| 4 | |
| 5 | All Rights Reserved |
| 6 | |
| 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 |
| 15 | permission. |
| 16 | |
| 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. |
| 20 | |
| 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. |
| 29 | |
| 30 | ******************************************************************/ |
| 31 | |
| 32 | /* Thread and interpreter state structures and their interfaces */ |
| 33 | |
| 34 | #include "Python.h" |
| 35 | |
| 36 | |
| 37 | static PyThreadState *current_tstate = NULL; |
| 38 | |
| 39 | |
| 40 | PyInterpreterState * |
| 41 | PyInterpreterState_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 | |
| 54 | void |
Guido van Rossum | f9cba09 | 1997-05-20 22:23:34 +0000 | [diff] [blame] | 55 | PyInterpreterState_Delete(interp) |
| 56 | PyInterpreterState *interp; |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 57 | { |
| 58 | Py_XDECREF(interp->import_modules); |
| 59 | Py_XDECREF(interp->sysdict); |
| 60 | |
| 61 | PyMem_DEL(interp); |
| 62 | } |
| 63 | |
| 64 | |
| 65 | PyThreadState * |
Guido van Rossum | f9cba09 | 1997-05-20 22:23:34 +0000 | [diff] [blame] | 66 | PyThreadState_New(interp) |
| 67 | PyInterpreterState *interp; |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 68 | { |
| 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 | |
| 97 | void |
Guido van Rossum | f9cba09 | 1997-05-20 22:23:34 +0000 | [diff] [blame] | 98 | PyThreadState_Delete(tstate) |
| 99 | PyThreadState *tstate; |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 100 | { |
| 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 | |
| 123 | PyThreadState * |
| 124 | PyThreadState_Get() |
| 125 | { |
| 126 | /* fprintf(stderr, "get tstate -> %p\n", current_tstate); */ |
| 127 | return current_tstate; |
| 128 | } |
| 129 | |
| 130 | |
| 131 | PyThreadState * |
Guido van Rossum | f9cba09 | 1997-05-20 22:23:34 +0000 | [diff] [blame] | 132 | PyThreadState_Swap(new) |
| 133 | PyThreadState *new; |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 134 | { |
| 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 | */ |