Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 1 | #ifndef Py_PYSTATE_H |
| 2 | #define Py_PYSTATE_H |
| 3 | #ifdef __cplusplus |
| 4 | extern "C" { |
| 5 | #endif |
| 6 | |
| 7 | /*********************************************************** |
| 8 | Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam, |
| 9 | The Netherlands. |
| 10 | |
| 11 | All Rights Reserved |
| 12 | |
| 13 | Permission to use, copy, modify, and distribute this software and its |
| 14 | documentation for any purpose and without fee is hereby granted, |
| 15 | provided that the above copyright notice appear in all copies and that |
| 16 | both that copyright notice and this permission notice appear in |
| 17 | supporting documentation, and that the names of Stichting Mathematisch |
| 18 | Centrum or CWI or Corporation for National Research Initiatives or |
| 19 | CNRI not be used in advertising or publicity pertaining to |
| 20 | distribution of the software without specific, written prior |
| 21 | permission. |
| 22 | |
| 23 | While CWI is the initial source for this software, a modified version |
| 24 | is made available by the Corporation for National Research Initiatives |
| 25 | (CNRI) at the Internet address ftp://ftp.python.org. |
| 26 | |
| 27 | STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH |
| 28 | REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF |
| 29 | MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH |
| 30 | CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL |
| 31 | DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR |
| 32 | PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER |
| 33 | TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR |
| 34 | PERFORMANCE OF THIS SOFTWARE. |
| 35 | |
| 36 | ******************************************************************/ |
| 37 | |
| 38 | /* Thread and interpreter state structures and their interfaces */ |
| 39 | |
| 40 | |
| 41 | /* State shared between threads */ |
| 42 | |
| 43 | #define NEXITFUNCS 32 |
| 44 | |
| 45 | typedef struct _is { |
| 46 | |
| 47 | PyObject *import_modules; |
| 48 | PyObject *sysdict; |
| 49 | |
| 50 | int nthreads; |
| 51 | |
| 52 | void (*exitfuncs[NEXITFUNCS])(); |
| 53 | int nexitfuncs; |
| 54 | |
| 55 | } PyInterpreterState; |
| 56 | |
| 57 | |
| 58 | /* State unique per thread */ |
| 59 | |
| 60 | struct _frame; /* Avoid including frameobject.h */ |
| 61 | |
| 62 | typedef struct _ts { |
| 63 | |
| 64 | PyInterpreterState *interpreter_state; |
| 65 | |
| 66 | struct _frame *frame; |
| 67 | int recursion_depth; |
| 68 | int ticker; |
| 69 | int tracing; |
| 70 | |
| 71 | PyObject *sys_profilefunc; |
| 72 | PyObject *sys_tracefunc; |
| 73 | int sys_checkinterval; |
| 74 | |
| 75 | PyObject *curexc_type; |
| 76 | PyObject *curexc_value; |
| 77 | PyObject *curexc_traceback; |
| 78 | |
| 79 | PyObject *exc_type; |
| 80 | PyObject *exc_value; |
| 81 | PyObject *exc_traceback; |
| 82 | |
| 83 | /* XXX Other state that should be here: |
| 84 | - signal handlers |
| 85 | - low-level "pending calls" |
| 86 | Problem with both is that they may be referenced from |
| 87 | interrupt handlers where there is no clear concept of a |
| 88 | "current thread"??? |
| 89 | */ |
| 90 | |
| 91 | } PyThreadState; |
| 92 | |
| 93 | |
Guido van Rossum | 59943ba | 1997-05-20 22:07:46 +0000 | [diff] [blame] | 94 | PyInterpreterState *PyInterpreterState_New Py_PROTO((void)); |
| 95 | void PyInterpreterState_Delete Py_PROTO((PyInterpreterState *)); |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 96 | |
Guido van Rossum | 59943ba | 1997-05-20 22:07:46 +0000 | [diff] [blame] | 97 | PyThreadState *PyThreadState_New Py_PROTO((PyInterpreterState *)); |
| 98 | void PyThreadState_Delete Py_PROTO((PyThreadState *)); |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 99 | |
Guido van Rossum | 59943ba | 1997-05-20 22:07:46 +0000 | [diff] [blame] | 100 | PyThreadState *PyThreadState_Get Py_PROTO((void)); |
| 101 | PyThreadState *PyThreadState_Swap Py_PROTO((PyThreadState *)); |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 102 | |
| 103 | /* Some background. |
| 104 | |
| 105 | There are lots of issues here. |
| 106 | |
| 107 | First, we can build Python without threads, with threads, or (when |
| 108 | Greg Stein's mods are out of beta, on some platforms) with free |
| 109 | threading. |
| 110 | |
| 111 | Next, assuming some form of threading is used, there can be several |
| 112 | kinds of threads. Python code can create threads with the thread |
| 113 | module. C code can create threads with the interface defined in |
| 114 | python's "thread.h". Or C code can create threads directly with |
| 115 | the OS threads interface (e.g. Solaris threads, SGI threads or |
| 116 | pthreads, whatever is being used, as long as it's the same that |
| 117 | Python is configured for). |
| 118 | |
| 119 | Next, let's discuss sharing of interpreter state between threads. |
| 120 | The exception state (sys.exc_* currently) should never be shared |
| 121 | between threads, because it is stack frame specific. The contents |
| 122 | of the sys module, in particular sys.modules and sys.path, are |
| 123 | generally shared between threads. But occasionally it is useful to |
| 124 | have separate module collections, e.g. when threads originate in C |
| 125 | code and are used to execute unrelated Python scripts. |
| 126 | (Traditionally, one would use separate processes for this, but |
| 127 | there are lots of reasons why threads are attractive.) |
| 128 | |
| 129 | */ |
| 130 | |
| 131 | #ifdef __cplusplus |
| 132 | } |
| 133 | #endif |
| 134 | #endif /* !Py_PYSTATE_H */ |