Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 1 | |
| 2 | /* Thread and interpreter state structures and their interfaces */ |
| 3 | |
| 4 | #include "Python.h" |
| 5 | |
Tim Peters | 8470558 | 2004-10-10 02:47:33 +0000 | [diff] [blame] | 6 | /* -------------------------------------------------------------------------- |
| 7 | CAUTION |
| 8 | |
| 9 | Always use malloc() and free() directly in this file. A number of these |
| 10 | functions are advertised as safe to call when the GIL isn't held, and in |
| 11 | a debug build Python redirects (e.g.) PyMem_NEW (etc) to Python's debugging |
| 12 | obmalloc functions. Those aren't thread-safe (they rely on the GIL to avoid |
| 13 | the expense of doing their own locking). |
| 14 | -------------------------------------------------------------------------- */ |
| 15 | |
Martin v. Löwis | f0473d5 | 2001-07-18 16:17:16 +0000 | [diff] [blame] | 16 | #ifdef HAVE_DLOPEN |
| 17 | #ifdef HAVE_DLFCN_H |
| 18 | #include <dlfcn.h> |
| 19 | #endif |
| 20 | #ifndef RTLD_LAZY |
| 21 | #define RTLD_LAZY 1 |
| 22 | #endif |
| 23 | #endif |
| 24 | |
| 25 | |
Guido van Rossum | 1d5ad90 | 1999-06-18 14:22:24 +0000 | [diff] [blame] | 26 | #ifdef WITH_THREAD |
| 27 | #include "pythread.h" |
| 28 | static PyThread_type_lock head_mutex = NULL; /* Protects interp->tstate_head */ |
Moshe Zadka | 9fb6af9 | 2000-08-04 21:27:47 +0000 | [diff] [blame] | 29 | #define HEAD_INIT() (void)(head_mutex || (head_mutex = PyThread_allocate_lock())) |
Guido van Rossum | 1d5ad90 | 1999-06-18 14:22:24 +0000 | [diff] [blame] | 30 | #define HEAD_LOCK() PyThread_acquire_lock(head_mutex, WAIT_LOCK) |
| 31 | #define HEAD_UNLOCK() PyThread_release_lock(head_mutex) |
Michael W. Hudson | 188d436 | 2005-06-20 16:52:57 +0000 | [diff] [blame] | 32 | |
Anthony Baxter | ac6bd46 | 2006-04-13 02:06:09 +0000 | [diff] [blame] | 33 | #ifdef __cplusplus |
| 34 | extern "C" { |
| 35 | #endif |
| 36 | |
Michael W. Hudson | 188d436 | 2005-06-20 16:52:57 +0000 | [diff] [blame] | 37 | /* The single PyInterpreterState used by this process' |
| 38 | GILState implementation |
| 39 | */ |
| 40 | static PyInterpreterState *autoInterpreterState = NULL; |
| 41 | static int autoTLSkey = 0; |
Guido van Rossum | 1d5ad90 | 1999-06-18 14:22:24 +0000 | [diff] [blame] | 42 | #else |
| 43 | #define HEAD_INIT() /* Nothing */ |
| 44 | #define HEAD_LOCK() /* Nothing */ |
| 45 | #define HEAD_UNLOCK() /* Nothing */ |
| 46 | #endif |
| 47 | |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 48 | static PyInterpreterState *interp_head = NULL; |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 49 | |
Guido van Rossum | 18bc7c2 | 1998-12-21 18:27:28 +0000 | [diff] [blame] | 50 | PyThreadState *_PyThreadState_Current = NULL; |
Guido van Rossum | 6297a7a | 2003-02-19 15:53:17 +0000 | [diff] [blame] | 51 | PyThreadFrameGetter _PyThreadState_GetFrame = NULL; |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 52 | |
Michael W. Hudson | ce7da6c | 2005-09-30 08:20:24 +0000 | [diff] [blame] | 53 | #ifdef WITH_THREAD |
Michael W. Hudson | 188d436 | 2005-06-20 16:52:57 +0000 | [diff] [blame] | 54 | static void _PyGILState_NoteThreadState(PyThreadState* tstate); |
Michael W. Hudson | ce7da6c | 2005-09-30 08:20:24 +0000 | [diff] [blame] | 55 | #endif |
Michael W. Hudson | 188d436 | 2005-06-20 16:52:57 +0000 | [diff] [blame] | 56 | |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 57 | |
| 58 | PyInterpreterState * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 59 | PyInterpreterState_New(void) |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 60 | { |
Tim Peters | 8470558 | 2004-10-10 02:47:33 +0000 | [diff] [blame] | 61 | PyInterpreterState *interp = (PyInterpreterState *) |
| 62 | malloc(sizeof(PyInterpreterState)); |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 63 | |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 64 | if (interp != NULL) { |
Guido van Rossum | 1d5ad90 | 1999-06-18 14:22:24 +0000 | [diff] [blame] | 65 | HEAD_INIT(); |
Neal Norwitz | e1fdb32 | 2006-07-21 05:32:28 +0000 | [diff] [blame] | 66 | #ifdef WITH_THREAD |
| 67 | if (head_mutex == NULL) |
| 68 | Py_FatalError("Can't initialize threads for interpreter"); |
| 69 | #endif |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 70 | interp->modules = NULL; |
Collin Winter | 276887b | 2007-03-12 16:11:39 +0000 | [diff] [blame] | 71 | interp->modules_reloading = NULL; |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 72 | interp->sysdict = NULL; |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 73 | interp->builtins = NULL; |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 74 | interp->tstate_head = NULL; |
Gustavo Niemeyer | 5ddd4c3 | 2003-03-19 00:35:36 +0000 | [diff] [blame] | 75 | interp->codec_search_path = NULL; |
| 76 | interp->codec_search_cache = NULL; |
| 77 | interp->codec_error_registry = NULL; |
Martin v. Löwis | f0473d5 | 2001-07-18 16:17:16 +0000 | [diff] [blame] | 78 | #ifdef HAVE_DLOPEN |
| 79 | #ifdef RTLD_NOW |
| 80 | interp->dlopenflags = RTLD_NOW; |
| 81 | #else |
| 82 | interp->dlopenflags = RTLD_LAZY; |
| 83 | #endif |
| 84 | #endif |
Martin v. Löwis | f30d60e | 2004-06-08 08:17:44 +0000 | [diff] [blame] | 85 | #ifdef WITH_TSC |
| 86 | interp->tscdump = 0; |
| 87 | #endif |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 88 | |
Tim Peters | 412f246 | 2000-09-02 09:16:15 +0000 | [diff] [blame] | 89 | HEAD_LOCK(); |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 90 | interp->next = interp_head; |
| 91 | interp_head = interp; |
Tim Peters | 412f246 | 2000-09-02 09:16:15 +0000 | [diff] [blame] | 92 | HEAD_UNLOCK(); |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 93 | } |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 94 | |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 95 | return interp; |
| 96 | } |
| 97 | |
| 98 | |
| 99 | void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 100 | PyInterpreterState_Clear(PyInterpreterState *interp) |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 101 | { |
| 102 | PyThreadState *p; |
Guido van Rossum | 1d5ad90 | 1999-06-18 14:22:24 +0000 | [diff] [blame] | 103 | HEAD_LOCK(); |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 104 | for (p = interp->tstate_head; p != NULL; p = p->next) |
| 105 | PyThreadState_Clear(p); |
Guido van Rossum | 1d5ad90 | 1999-06-18 14:22:24 +0000 | [diff] [blame] | 106 | HEAD_UNLOCK(); |
Martin v. Löwis | ab0e284 | 2006-04-15 18:14:21 +0000 | [diff] [blame] | 107 | Py_CLEAR(interp->codec_search_path); |
| 108 | Py_CLEAR(interp->codec_search_cache); |
| 109 | Py_CLEAR(interp->codec_error_registry); |
| 110 | Py_CLEAR(interp->modules); |
Collin Winter | 276887b | 2007-03-12 16:11:39 +0000 | [diff] [blame] | 111 | Py_CLEAR(interp->modules_reloading); |
Martin v. Löwis | ab0e284 | 2006-04-15 18:14:21 +0000 | [diff] [blame] | 112 | Py_CLEAR(interp->sysdict); |
| 113 | Py_CLEAR(interp->builtins); |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | |
| 117 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 118 | zapthreads(PyInterpreterState *interp) |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 119 | { |
Guido van Rossum | 1d5ad90 | 1999-06-18 14:22:24 +0000 | [diff] [blame] | 120 | PyThreadState *p; |
| 121 | /* No need to lock the mutex here because this should only happen |
| 122 | when the threads are all really dead (XXX famous last words). */ |
| 123 | while ((p = interp->tstate_head) != NULL) { |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 124 | PyThreadState_Delete(p); |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 125 | } |
| 126 | } |
| 127 | |
| 128 | |
| 129 | void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 130 | PyInterpreterState_Delete(PyInterpreterState *interp) |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 131 | { |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 132 | PyInterpreterState **p; |
| 133 | zapthreads(interp); |
Tim Peters | 412f246 | 2000-09-02 09:16:15 +0000 | [diff] [blame] | 134 | HEAD_LOCK(); |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 135 | for (p = &interp_head; ; p = &(*p)->next) { |
| 136 | if (*p == NULL) |
| 137 | Py_FatalError( |
| 138 | "PyInterpreterState_Delete: invalid interp"); |
| 139 | if (*p == interp) |
| 140 | break; |
| 141 | } |
| 142 | if (interp->tstate_head != NULL) |
| 143 | Py_FatalError("PyInterpreterState_Delete: remaining threads"); |
| 144 | *p = interp->next; |
Tim Peters | 412f246 | 2000-09-02 09:16:15 +0000 | [diff] [blame] | 145 | HEAD_UNLOCK(); |
Tim Peters | 8470558 | 2004-10-10 02:47:33 +0000 | [diff] [blame] | 146 | free(interp); |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | |
Michael W. Hudson | 019a78e | 2002-11-08 12:53:11 +0000 | [diff] [blame] | 150 | /* Default implementation for _PyThreadState_GetFrame */ |
| 151 | static struct _frame * |
| 152 | threadstate_getframe(PyThreadState *self) |
| 153 | { |
| 154 | return self->frame; |
| 155 | } |
| 156 | |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 157 | PyThreadState * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 158 | PyThreadState_New(PyInterpreterState *interp) |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 159 | { |
Tim Peters | 8470558 | 2004-10-10 02:47:33 +0000 | [diff] [blame] | 160 | PyThreadState *tstate = (PyThreadState *)malloc(sizeof(PyThreadState)); |
| 161 | |
Michael W. Hudson | 019a78e | 2002-11-08 12:53:11 +0000 | [diff] [blame] | 162 | if (_PyThreadState_GetFrame == NULL) |
Guido van Rossum | 6297a7a | 2003-02-19 15:53:17 +0000 | [diff] [blame] | 163 | _PyThreadState_GetFrame = threadstate_getframe; |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 164 | |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 165 | if (tstate != NULL) { |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 166 | tstate->interp = interp; |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 167 | |
| 168 | tstate->frame = NULL; |
| 169 | tstate->recursion_depth = 0; |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 170 | tstate->tracing = 0; |
Fred Drake | 9e3ad78 | 2001-07-03 23:39:52 +0000 | [diff] [blame] | 171 | tstate->use_tracing = 0; |
Michael W. Hudson | 019a78e | 2002-11-08 12:53:11 +0000 | [diff] [blame] | 172 | tstate->tick_counter = 0; |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 173 | tstate->gilstate_counter = 0; |
Guido van Rossum | b8b6d0c | 2003-06-28 21:53:52 +0000 | [diff] [blame] | 174 | tstate->async_exc = NULL; |
Martin v. Löwis | f9ce67d | 2003-07-13 10:41:53 +0000 | [diff] [blame] | 175 | #ifdef WITH_THREAD |
Guido van Rossum | b8b6d0c | 2003-06-28 21:53:52 +0000 | [diff] [blame] | 176 | tstate->thread_id = PyThread_get_thread_ident(); |
Martin v. Löwis | f9ce67d | 2003-07-13 10:41:53 +0000 | [diff] [blame] | 177 | #else |
| 178 | tstate->thread_id = 0; |
| 179 | #endif |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 180 | |
Guido van Rossum | ede0439 | 1998-04-10 20:18:25 +0000 | [diff] [blame] | 181 | tstate->dict = NULL; |
| 182 | |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 183 | tstate->curexc_type = NULL; |
| 184 | tstate->curexc_value = NULL; |
| 185 | tstate->curexc_traceback = NULL; |
| 186 | |
| 187 | tstate->exc_type = NULL; |
| 188 | tstate->exc_value = NULL; |
| 189 | tstate->exc_traceback = NULL; |
| 190 | |
Fred Drake | 5755ce6 | 2001-06-27 19:19:46 +0000 | [diff] [blame] | 191 | tstate->c_profilefunc = NULL; |
| 192 | tstate->c_tracefunc = NULL; |
| 193 | tstate->c_profileobj = NULL; |
| 194 | tstate->c_traceobj = NULL; |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 195 | |
Michael W. Hudson | ce7da6c | 2005-09-30 08:20:24 +0000 | [diff] [blame] | 196 | #ifdef WITH_THREAD |
Michael W. Hudson | 188d436 | 2005-06-20 16:52:57 +0000 | [diff] [blame] | 197 | _PyGILState_NoteThreadState(tstate); |
Michael W. Hudson | ce7da6c | 2005-09-30 08:20:24 +0000 | [diff] [blame] | 198 | #endif |
Michael W. Hudson | 188d436 | 2005-06-20 16:52:57 +0000 | [diff] [blame] | 199 | |
Guido van Rossum | 1d5ad90 | 1999-06-18 14:22:24 +0000 | [diff] [blame] | 200 | HEAD_LOCK(); |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 201 | tstate->next = interp->tstate_head; |
| 202 | interp->tstate_head = tstate; |
Guido van Rossum | 1d5ad90 | 1999-06-18 14:22:24 +0000 | [diff] [blame] | 203 | HEAD_UNLOCK(); |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 204 | } |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 205 | |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 206 | return tstate; |
| 207 | } |
| 208 | |
| 209 | |
| 210 | void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 211 | PyThreadState_Clear(PyThreadState *tstate) |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 212 | { |
Guido van Rossum | 22348dc | 1997-11-03 22:08:36 +0000 | [diff] [blame] | 213 | if (Py_VerboseFlag && tstate->frame != NULL) |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 214 | fprintf(stderr, |
Guido van Rossum | 5f896a4 | 1997-08-21 02:28:19 +0000 | [diff] [blame] | 215 | "PyThreadState_Clear: warning: thread still has a frame\n"); |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 216 | |
Martin v. Löwis | ab0e284 | 2006-04-15 18:14:21 +0000 | [diff] [blame] | 217 | Py_CLEAR(tstate->frame); |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 218 | |
Martin v. Löwis | ab0e284 | 2006-04-15 18:14:21 +0000 | [diff] [blame] | 219 | Py_CLEAR(tstate->dict); |
| 220 | Py_CLEAR(tstate->async_exc); |
Guido van Rossum | ede0439 | 1998-04-10 20:18:25 +0000 | [diff] [blame] | 221 | |
Martin v. Löwis | ab0e284 | 2006-04-15 18:14:21 +0000 | [diff] [blame] | 222 | Py_CLEAR(tstate->curexc_type); |
| 223 | Py_CLEAR(tstate->curexc_value); |
| 224 | Py_CLEAR(tstate->curexc_traceback); |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 225 | |
Martin v. Löwis | ab0e284 | 2006-04-15 18:14:21 +0000 | [diff] [blame] | 226 | Py_CLEAR(tstate->exc_type); |
| 227 | Py_CLEAR(tstate->exc_value); |
| 228 | Py_CLEAR(tstate->exc_traceback); |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 229 | |
Fred Drake | 5755ce6 | 2001-06-27 19:19:46 +0000 | [diff] [blame] | 230 | tstate->c_profilefunc = NULL; |
| 231 | tstate->c_tracefunc = NULL; |
Martin v. Löwis | ab0e284 | 2006-04-15 18:14:21 +0000 | [diff] [blame] | 232 | Py_CLEAR(tstate->c_profileobj); |
| 233 | Py_CLEAR(tstate->c_traceobj); |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 234 | } |
| 235 | |
| 236 | |
Guido van Rossum | 2975786 | 2001-01-23 01:46:06 +0000 | [diff] [blame] | 237 | /* Common code for PyThreadState_Delete() and PyThreadState_DeleteCurrent() */ |
| 238 | static void |
| 239 | tstate_delete_common(PyThreadState *tstate) |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 240 | { |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 241 | PyInterpreterState *interp; |
| 242 | PyThreadState **p; |
| 243 | if (tstate == NULL) |
| 244 | Py_FatalError("PyThreadState_Delete: NULL tstate"); |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 245 | interp = tstate->interp; |
| 246 | if (interp == NULL) |
| 247 | Py_FatalError("PyThreadState_Delete: NULL interp"); |
Guido van Rossum | 1d5ad90 | 1999-06-18 14:22:24 +0000 | [diff] [blame] | 248 | HEAD_LOCK(); |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 249 | for (p = &interp->tstate_head; ; p = &(*p)->next) { |
| 250 | if (*p == NULL) |
| 251 | Py_FatalError( |
| 252 | "PyThreadState_Delete: invalid tstate"); |
| 253 | if (*p == tstate) |
| 254 | break; |
| 255 | } |
| 256 | *p = tstate->next; |
Guido van Rossum | 1d5ad90 | 1999-06-18 14:22:24 +0000 | [diff] [blame] | 257 | HEAD_UNLOCK(); |
Tim Peters | 8470558 | 2004-10-10 02:47:33 +0000 | [diff] [blame] | 258 | free(tstate); |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 259 | } |
| 260 | |
| 261 | |
Guido van Rossum | 2975786 | 2001-01-23 01:46:06 +0000 | [diff] [blame] | 262 | void |
| 263 | PyThreadState_Delete(PyThreadState *tstate) |
| 264 | { |
| 265 | if (tstate == _PyThreadState_Current) |
| 266 | Py_FatalError("PyThreadState_Delete: tstate is still current"); |
| 267 | tstate_delete_common(tstate); |
Tim Peters | f4e6928 | 2006-02-27 17:15:31 +0000 | [diff] [blame] | 268 | #ifdef WITH_THREAD |
| 269 | if (autoTLSkey && PyThread_get_key_value(autoTLSkey) == tstate) |
| 270 | PyThread_delete_key_value(autoTLSkey); |
| 271 | #endif /* WITH_THREAD */ |
Guido van Rossum | 2975786 | 2001-01-23 01:46:06 +0000 | [diff] [blame] | 272 | } |
| 273 | |
| 274 | |
| 275 | #ifdef WITH_THREAD |
| 276 | void |
| 277 | PyThreadState_DeleteCurrent() |
| 278 | { |
| 279 | PyThreadState *tstate = _PyThreadState_Current; |
| 280 | if (tstate == NULL) |
| 281 | Py_FatalError( |
| 282 | "PyThreadState_DeleteCurrent: no current tstate"); |
| 283 | _PyThreadState_Current = NULL; |
| 284 | tstate_delete_common(tstate); |
Michael W. Hudson | 188d436 | 2005-06-20 16:52:57 +0000 | [diff] [blame] | 285 | if (autoTLSkey && PyThread_get_key_value(autoTLSkey) == tstate) |
| 286 | PyThread_delete_key_value(autoTLSkey); |
Guido van Rossum | 2975786 | 2001-01-23 01:46:06 +0000 | [diff] [blame] | 287 | PyEval_ReleaseLock(); |
| 288 | } |
| 289 | #endif /* WITH_THREAD */ |
| 290 | |
| 291 | |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 292 | PyThreadState * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 293 | PyThreadState_Get(void) |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 294 | { |
Guido van Rossum | 18bc7c2 | 1998-12-21 18:27:28 +0000 | [diff] [blame] | 295 | if (_PyThreadState_Current == NULL) |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 296 | Py_FatalError("PyThreadState_Get: no current thread"); |
| 297 | |
Guido van Rossum | 18bc7c2 | 1998-12-21 18:27:28 +0000 | [diff] [blame] | 298 | return _PyThreadState_Current; |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 299 | } |
| 300 | |
| 301 | |
| 302 | PyThreadState * |
Anthony Baxter | 7b782b6 | 2006-04-11 12:01:56 +0000 | [diff] [blame] | 303 | PyThreadState_Swap(PyThreadState *newts) |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 304 | { |
Anthony Baxter | 7b782b6 | 2006-04-11 12:01:56 +0000 | [diff] [blame] | 305 | PyThreadState *oldts = _PyThreadState_Current; |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 306 | |
Anthony Baxter | 7b782b6 | 2006-04-11 12:01:56 +0000 | [diff] [blame] | 307 | _PyThreadState_Current = newts; |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 308 | /* It should not be possible for more than one thread state |
Tim Peters | 4c1f5ec | 2004-10-09 17:25:05 +0000 | [diff] [blame] | 309 | to be used for a thread. Check this the best we can in debug |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 310 | builds. |
| 311 | */ |
Martin v. Löwis | 9e29625 | 2003-05-01 05:25:29 +0000 | [diff] [blame] | 312 | #if defined(Py_DEBUG) && defined(WITH_THREAD) |
Anthony Baxter | 7b782b6 | 2006-04-11 12:01:56 +0000 | [diff] [blame] | 313 | if (newts) { |
Georg Brandl | ec6c2df | 2006-09-11 09:38:35 +0000 | [diff] [blame] | 314 | /* This can be called from PyEval_RestoreThread(). Similar |
| 315 | to it, we need to ensure errno doesn't change. |
| 316 | */ |
| 317 | int err = errno; |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 318 | PyThreadState *check = PyGILState_GetThisThreadState(); |
Anthony Baxter | 7b782b6 | 2006-04-11 12:01:56 +0000 | [diff] [blame] | 319 | if (check && check->interp == newts->interp && check != newts) |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 320 | Py_FatalError("Invalid thread state for this thread"); |
Georg Brandl | ec6c2df | 2006-09-11 09:38:35 +0000 | [diff] [blame] | 321 | errno = err; |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 322 | } |
| 323 | #endif |
Anthony Baxter | 7b782b6 | 2006-04-11 12:01:56 +0000 | [diff] [blame] | 324 | return oldts; |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 325 | } |
Guido van Rossum | ede0439 | 1998-04-10 20:18:25 +0000 | [diff] [blame] | 326 | |
| 327 | /* An extension mechanism to store arbitrary additional per-thread state. |
| 328 | PyThreadState_GetDict() returns a dictionary that can be used to hold such |
| 329 | state; the caller should pick a unique key and store its state there. If |
Guido van Rossum | 0fc8f00 | 2003-04-15 15:12:39 +0000 | [diff] [blame] | 330 | PyThreadState_GetDict() returns NULL, an exception has *not* been raised |
| 331 | and the caller should assume no per-thread state is available. */ |
Guido van Rossum | ede0439 | 1998-04-10 20:18:25 +0000 | [diff] [blame] | 332 | |
| 333 | PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 334 | PyThreadState_GetDict(void) |
Guido van Rossum | ede0439 | 1998-04-10 20:18:25 +0000 | [diff] [blame] | 335 | { |
Guido van Rossum | 18bc7c2 | 1998-12-21 18:27:28 +0000 | [diff] [blame] | 336 | if (_PyThreadState_Current == NULL) |
Guido van Rossum | 0fc8f00 | 2003-04-15 15:12:39 +0000 | [diff] [blame] | 337 | return NULL; |
Guido van Rossum | ede0439 | 1998-04-10 20:18:25 +0000 | [diff] [blame] | 338 | |
Guido van Rossum | 0fc8f00 | 2003-04-15 15:12:39 +0000 | [diff] [blame] | 339 | if (_PyThreadState_Current->dict == NULL) { |
| 340 | PyObject *d; |
| 341 | _PyThreadState_Current->dict = d = PyDict_New(); |
| 342 | if (d == NULL) |
| 343 | PyErr_Clear(); |
| 344 | } |
Guido van Rossum | 18bc7c2 | 1998-12-21 18:27:28 +0000 | [diff] [blame] | 345 | return _PyThreadState_Current->dict; |
Guido van Rossum | ede0439 | 1998-04-10 20:18:25 +0000 | [diff] [blame] | 346 | } |
Guido van Rossum | f5df46d | 2001-07-19 12:19:27 +0000 | [diff] [blame] | 347 | |
| 348 | |
Guido van Rossum | b8b6d0c | 2003-06-28 21:53:52 +0000 | [diff] [blame] | 349 | /* Asynchronously raise an exception in a thread. |
| 350 | Requested by Just van Rossum and Alex Martelli. |
Guido van Rossum | 0f1f63c | 2005-02-08 02:07:57 +0000 | [diff] [blame] | 351 | To prevent naive misuse, you must write your own extension |
Tim Peters | 4643c2f | 2006-08-10 22:45:34 +0000 | [diff] [blame] | 352 | to call this, or use ctypes. Must be called with the GIL held. |
| 353 | Returns the number of tstates modified (normally 1, but 0 if `id` didn't |
| 354 | match any known thread id). Can be called with exc=NULL to clear an |
| 355 | existing async exception. This raises no exceptions. */ |
Guido van Rossum | b8b6d0c | 2003-06-28 21:53:52 +0000 | [diff] [blame] | 356 | |
| 357 | int |
| 358 | PyThreadState_SetAsyncExc(long id, PyObject *exc) { |
Nicholas Bastin | e5662ae | 2004-03-24 22:22:12 +0000 | [diff] [blame] | 359 | PyThreadState *tstate = PyThreadState_GET(); |
Guido van Rossum | b8b6d0c | 2003-06-28 21:53:52 +0000 | [diff] [blame] | 360 | PyInterpreterState *interp = tstate->interp; |
| 361 | PyThreadState *p; |
Tim Peters | 4643c2f | 2006-08-10 22:45:34 +0000 | [diff] [blame] | 362 | |
| 363 | /* Although the GIL is held, a few C API functions can be called |
| 364 | * without the GIL held, and in particular some that create and |
| 365 | * destroy thread and interpreter states. Those can mutate the |
| 366 | * list of thread states we're traversing, so to prevent that we lock |
| 367 | * head_mutex for the duration. |
| 368 | */ |
Guido van Rossum | 0f1f63c | 2005-02-08 02:07:57 +0000 | [diff] [blame] | 369 | HEAD_LOCK(); |
Guido van Rossum | b8b6d0c | 2003-06-28 21:53:52 +0000 | [diff] [blame] | 370 | for (p = interp->tstate_head; p != NULL; p = p->next) { |
Tim Peters | 4643c2f | 2006-08-10 22:45:34 +0000 | [diff] [blame] | 371 | if (p->thread_id == id) { |
| 372 | /* Tricky: we need to decref the current value |
| 373 | * (if any) in p->async_exc, but that can in turn |
| 374 | * allow arbitrary Python code to run, including |
| 375 | * perhaps calls to this function. To prevent |
| 376 | * deadlock, we need to release head_mutex before |
| 377 | * the decref. |
| 378 | */ |
| 379 | PyObject *old_exc = p->async_exc; |
| 380 | Py_XINCREF(exc); |
| 381 | p->async_exc = exc; |
| 382 | HEAD_UNLOCK(); |
| 383 | Py_XDECREF(old_exc); |
| 384 | return 1; |
| 385 | } |
Guido van Rossum | b8b6d0c | 2003-06-28 21:53:52 +0000 | [diff] [blame] | 386 | } |
Guido van Rossum | 0f1f63c | 2005-02-08 02:07:57 +0000 | [diff] [blame] | 387 | HEAD_UNLOCK(); |
Tim Peters | 4643c2f | 2006-08-10 22:45:34 +0000 | [diff] [blame] | 388 | return 0; |
Guido van Rossum | b8b6d0c | 2003-06-28 21:53:52 +0000 | [diff] [blame] | 389 | } |
| 390 | |
| 391 | |
Guido van Rossum | f5df46d | 2001-07-19 12:19:27 +0000 | [diff] [blame] | 392 | /* Routines for advanced debuggers, requested by David Beazley. |
| 393 | Don't use unless you know what you are doing! */ |
| 394 | |
| 395 | PyInterpreterState * |
| 396 | PyInterpreterState_Head(void) |
| 397 | { |
| 398 | return interp_head; |
| 399 | } |
| 400 | |
| 401 | PyInterpreterState * |
| 402 | PyInterpreterState_Next(PyInterpreterState *interp) { |
| 403 | return interp->next; |
| 404 | } |
| 405 | |
| 406 | PyThreadState * |
| 407 | PyInterpreterState_ThreadHead(PyInterpreterState *interp) { |
| 408 | return interp->tstate_head; |
| 409 | } |
| 410 | |
| 411 | PyThreadState * |
| 412 | PyThreadState_Next(PyThreadState *tstate) { |
| 413 | return tstate->next; |
| 414 | } |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 415 | |
Tim Peters | 112aad3 | 2006-07-19 00:03:19 +0000 | [diff] [blame] | 416 | /* The implementation of sys._current_frames(). This is intended to be |
| 417 | called with the GIL held, as it will be when called via |
| 418 | sys._current_frames(). It's possible it would work fine even without |
| 419 | the GIL held, but haven't thought enough about that. |
| 420 | */ |
| 421 | PyObject * |
| 422 | _PyThread_CurrentFrames(void) |
| 423 | { |
| 424 | PyObject *result; |
| 425 | PyInterpreterState *i; |
| 426 | |
| 427 | result = PyDict_New(); |
| 428 | if (result == NULL) |
| 429 | return NULL; |
| 430 | |
| 431 | /* for i in all interpreters: |
| 432 | * for t in all of i's thread states: |
| 433 | * if t's frame isn't NULL, map t's id to its frame |
| 434 | * Because these lists can mutute even when the GIL is held, we |
| 435 | * need to grab head_mutex for the duration. |
| 436 | */ |
| 437 | HEAD_LOCK(); |
| 438 | for (i = interp_head; i != NULL; i = i->next) { |
| 439 | PyThreadState *t; |
| 440 | for (t = i->tstate_head; t != NULL; t = t->next) { |
| 441 | PyObject *id; |
| 442 | int stat; |
| 443 | struct _frame *frame = t->frame; |
| 444 | if (frame == NULL) |
| 445 | continue; |
| 446 | id = PyInt_FromLong(t->thread_id); |
| 447 | if (id == NULL) |
| 448 | goto Fail; |
| 449 | stat = PyDict_SetItem(result, id, (PyObject *)frame); |
| 450 | Py_DECREF(id); |
| 451 | if (stat < 0) |
| 452 | goto Fail; |
| 453 | } |
| 454 | } |
| 455 | HEAD_UNLOCK(); |
| 456 | return result; |
| 457 | |
| 458 | Fail: |
| 459 | HEAD_UNLOCK(); |
| 460 | Py_DECREF(result); |
| 461 | return NULL; |
| 462 | } |
Guido van Rossum | b8b6d0c | 2003-06-28 21:53:52 +0000 | [diff] [blame] | 463 | |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 464 | /* Python "auto thread state" API. */ |
| 465 | #ifdef WITH_THREAD |
| 466 | |
| 467 | /* Keep this as a static, as it is not reliable! It can only |
| 468 | ever be compared to the state for the *current* thread. |
| 469 | * If not equal, then it doesn't matter that the actual |
| 470 | value may change immediately after comparison, as it can't |
| 471 | possibly change to the current thread's state. |
| 472 | * If equal, then the current thread holds the lock, so the value can't |
| 473 | change until we yield the lock. |
| 474 | */ |
| 475 | static int |
| 476 | PyThreadState_IsCurrent(PyThreadState *tstate) |
| 477 | { |
| 478 | /* Must be the tstate for this thread */ |
| 479 | assert(PyGILState_GetThisThreadState()==tstate); |
| 480 | /* On Windows at least, simple reads and writes to 32 bit values |
| 481 | are atomic. |
| 482 | */ |
| 483 | return tstate == _PyThreadState_Current; |
| 484 | } |
| 485 | |
Tim Peters | 4c1f5ec | 2004-10-09 17:25:05 +0000 | [diff] [blame] | 486 | /* Internal initialization/finalization functions called by |
| 487 | Py_Initialize/Py_Finalize |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 488 | */ |
Tim Peters | 19717fa | 2004-10-09 17:38:29 +0000 | [diff] [blame] | 489 | void |
| 490 | _PyGILState_Init(PyInterpreterState *i, PyThreadState *t) |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 491 | { |
Tim Peters | 19717fa | 2004-10-09 17:38:29 +0000 | [diff] [blame] | 492 | assert(i && t); /* must init with valid states */ |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 493 | autoTLSkey = PyThread_create_key(); |
| 494 | autoInterpreterState = i; |
Tim Peters | f9becec | 2004-10-09 22:47:13 +0000 | [diff] [blame] | 495 | assert(PyThread_get_key_value(autoTLSkey) == NULL); |
Michael W. Hudson | 188d436 | 2005-06-20 16:52:57 +0000 | [diff] [blame] | 496 | assert(t->gilstate_counter == 0); |
| 497 | |
| 498 | _PyGILState_NoteThreadState(t); |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 499 | } |
| 500 | |
Tim Peters | 19717fa | 2004-10-09 17:38:29 +0000 | [diff] [blame] | 501 | void |
| 502 | _PyGILState_Fini(void) |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 503 | { |
| 504 | PyThread_delete_key(autoTLSkey); |
| 505 | autoTLSkey = 0; |
Neal Norwitz | d3f9190 | 2006-09-23 04:11:38 +0000 | [diff] [blame] | 506 | autoInterpreterState = NULL; |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 507 | } |
| 508 | |
Michael W. Hudson | 188d436 | 2005-06-20 16:52:57 +0000 | [diff] [blame] | 509 | /* When a thread state is created for a thread by some mechanism other than |
| 510 | PyGILState_Ensure, it's important that the GILState machinery knows about |
| 511 | it so it doesn't try to create another thread state for the thread (this is |
| 512 | a better fix for SF bug #1010677 than the first one attempted). |
| 513 | */ |
Georg Brandl | ec6c2df | 2006-09-11 09:38:35 +0000 | [diff] [blame] | 514 | static void |
Michael W. Hudson | 188d436 | 2005-06-20 16:52:57 +0000 | [diff] [blame] | 515 | _PyGILState_NoteThreadState(PyThreadState* tstate) |
| 516 | { |
| 517 | /* If autoTLSkey is 0, this must be the very first threadstate created |
| 518 | in Py_Initialize(). Don't do anything for now (we'll be back here |
| 519 | when _PyGILState_Init is called). */ |
Tim Peters | 32a8361 | 2006-07-10 21:08:24 +0000 | [diff] [blame] | 520 | if (!autoTLSkey) |
Michael W. Hudson | 188d436 | 2005-06-20 16:52:57 +0000 | [diff] [blame] | 521 | return; |
Tim Peters | 32a8361 | 2006-07-10 21:08:24 +0000 | [diff] [blame] | 522 | |
Michael W. Hudson | 188d436 | 2005-06-20 16:52:57 +0000 | [diff] [blame] | 523 | /* Stick the thread state for this thread in thread local storage. |
| 524 | |
| 525 | The only situation where you can legitimately have more than one |
| 526 | thread state for an OS level thread is when there are multiple |
| 527 | interpreters, when: |
Tim Peters | 32a8361 | 2006-07-10 21:08:24 +0000 | [diff] [blame] | 528 | |
Michael W. Hudson | 188d436 | 2005-06-20 16:52:57 +0000 | [diff] [blame] | 529 | a) You shouldn't really be using the PyGILState_ APIs anyway, |
| 530 | and: |
| 531 | |
| 532 | b) The slightly odd way PyThread_set_key_value works (see |
| 533 | comments by its implementation) means that the first thread |
| 534 | state created for that given OS level thread will "win", |
| 535 | which seems reasonable behaviour. |
| 536 | */ |
| 537 | if (PyThread_set_key_value(autoTLSkey, (void *)tstate) < 0) |
| 538 | Py_FatalError("Couldn't create autoTLSkey mapping"); |
| 539 | |
| 540 | /* PyGILState_Release must not try to delete this thread state. */ |
| 541 | tstate->gilstate_counter = 1; |
| 542 | } |
| 543 | |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 544 | /* The public functions */ |
Tim Peters | 19717fa | 2004-10-09 17:38:29 +0000 | [diff] [blame] | 545 | PyThreadState * |
| 546 | PyGILState_GetThisThreadState(void) |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 547 | { |
Tim Peters | 19717fa | 2004-10-09 17:38:29 +0000 | [diff] [blame] | 548 | if (autoInterpreterState == NULL || autoTLSkey == 0) |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 549 | return NULL; |
Tim Peters | 19717fa | 2004-10-09 17:38:29 +0000 | [diff] [blame] | 550 | return (PyThreadState *)PyThread_get_key_value(autoTLSkey); |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 551 | } |
| 552 | |
Tim Peters | 19717fa | 2004-10-09 17:38:29 +0000 | [diff] [blame] | 553 | PyGILState_STATE |
| 554 | PyGILState_Ensure(void) |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 555 | { |
| 556 | int current; |
| 557 | PyThreadState *tcur; |
Tim Peters | 4c1f5ec | 2004-10-09 17:25:05 +0000 | [diff] [blame] | 558 | /* Note that we do not auto-init Python here - apart from |
| 559 | potential races with 2 threads auto-initializing, pep-311 |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 560 | spells out other issues. Embedders are expected to have |
| 561 | called Py_Initialize() and usually PyEval_InitThreads(). |
| 562 | */ |
| 563 | assert(autoInterpreterState); /* Py_Initialize() hasn't been called! */ |
Anthony Baxter | 7b782b6 | 2006-04-11 12:01:56 +0000 | [diff] [blame] | 564 | tcur = (PyThreadState *)PyThread_get_key_value(autoTLSkey); |
Tim Peters | 19717fa | 2004-10-09 17:38:29 +0000 | [diff] [blame] | 565 | if (tcur == NULL) { |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 566 | /* Create a new thread state for this thread */ |
| 567 | tcur = PyThreadState_New(autoInterpreterState); |
Tim Peters | 19717fa | 2004-10-09 17:38:29 +0000 | [diff] [blame] | 568 | if (tcur == NULL) |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 569 | Py_FatalError("Couldn't create thread-state for new thread"); |
Michael W. Hudson | 188d436 | 2005-06-20 16:52:57 +0000 | [diff] [blame] | 570 | /* This is our thread state! We'll need to delete it in the |
| 571 | matching call to PyGILState_Release(). */ |
| 572 | tcur->gilstate_counter = 0; |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 573 | current = 0; /* new thread state is never current */ |
Tim Peters | 19717fa | 2004-10-09 17:38:29 +0000 | [diff] [blame] | 574 | } |
| 575 | else |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 576 | current = PyThreadState_IsCurrent(tcur); |
Tim Peters | 19717fa | 2004-10-09 17:38:29 +0000 | [diff] [blame] | 577 | if (current == 0) |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 578 | PyEval_RestoreThread(tcur); |
| 579 | /* Update our counter in the thread-state - no need for locks: |
| 580 | - tcur will remain valid as we hold the GIL. |
Tim Peters | 4c1f5ec | 2004-10-09 17:25:05 +0000 | [diff] [blame] | 581 | - the counter is safe as we are the only thread "allowed" |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 582 | to modify this value |
| 583 | */ |
Tim Peters | 19717fa | 2004-10-09 17:38:29 +0000 | [diff] [blame] | 584 | ++tcur->gilstate_counter; |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 585 | return current ? PyGILState_LOCKED : PyGILState_UNLOCKED; |
| 586 | } |
| 587 | |
Tim Peters | 19717fa | 2004-10-09 17:38:29 +0000 | [diff] [blame] | 588 | void |
| 589 | PyGILState_Release(PyGILState_STATE oldstate) |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 590 | { |
Anthony Baxter | 7b782b6 | 2006-04-11 12:01:56 +0000 | [diff] [blame] | 591 | PyThreadState *tcur = (PyThreadState *)PyThread_get_key_value( |
| 592 | autoTLSkey); |
Tim Peters | 19717fa | 2004-10-09 17:38:29 +0000 | [diff] [blame] | 593 | if (tcur == NULL) |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 594 | Py_FatalError("auto-releasing thread-state, " |
| 595 | "but no thread-state for this thread"); |
| 596 | /* We must hold the GIL and have our thread state current */ |
| 597 | /* XXX - remove the check - the assert should be fine, |
Tim Peters | 4c1f5ec | 2004-10-09 17:25:05 +0000 | [diff] [blame] | 598 | but while this is very new (April 2003), the extra check |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 599 | by release-only users can't hurt. |
| 600 | */ |
Tim Peters | 19717fa | 2004-10-09 17:38:29 +0000 | [diff] [blame] | 601 | if (! PyThreadState_IsCurrent(tcur)) |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 602 | Py_FatalError("This thread state must be current when releasing"); |
Tim Peters | 19717fa | 2004-10-09 17:38:29 +0000 | [diff] [blame] | 603 | assert(PyThreadState_IsCurrent(tcur)); |
| 604 | --tcur->gilstate_counter; |
| 605 | assert(tcur->gilstate_counter >= 0); /* illegal counter value */ |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 606 | |
Tim Peters | fb1ffb0 | 2004-11-08 04:30:21 +0000 | [diff] [blame] | 607 | /* If we're going to destroy this thread-state, we must |
| 608 | * clear it while the GIL is held, as destructors may run. |
| 609 | */ |
Tim Peters | 19717fa | 2004-10-09 17:38:29 +0000 | [diff] [blame] | 610 | if (tcur->gilstate_counter == 0) { |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 611 | /* can't have been locked when we created it */ |
Tim Peters | 19717fa | 2004-10-09 17:38:29 +0000 | [diff] [blame] | 612 | assert(oldstate == PyGILState_UNLOCKED); |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 613 | PyThreadState_Clear(tcur); |
Tim Peters | fb1ffb0 | 2004-11-08 04:30:21 +0000 | [diff] [blame] | 614 | /* Delete the thread-state. Note this releases the GIL too! |
| 615 | * It's vital that the GIL be held here, to avoid shutdown |
| 616 | * races; see bugs 225673 and 1061968 (that nasty bug has a |
| 617 | * habit of coming back). |
| 618 | */ |
| 619 | PyThreadState_DeleteCurrent(); |
Tim Peters | 89c0ec9 | 2004-10-10 05:30:40 +0000 | [diff] [blame] | 620 | } |
Tim Peters | fb1ffb0 | 2004-11-08 04:30:21 +0000 | [diff] [blame] | 621 | /* Release the lock if necessary */ |
| 622 | else if (oldstate == PyGILState_UNLOCKED) |
Michael W. Hudson | 774479c | 2005-04-18 08:46:17 +0000 | [diff] [blame] | 623 | PyEval_SaveThread(); |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 624 | } |
Anthony Baxter | ac6bd46 | 2006-04-13 02:06:09 +0000 | [diff] [blame] | 625 | |
| 626 | #ifdef __cplusplus |
| 627 | } |
| 628 | #endif |
| 629 | |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 630 | #endif /* WITH_THREAD */ |
Anthony Baxter | ac6bd46 | 2006-04-13 02:06:09 +0000 | [diff] [blame] | 631 | |
| 632 | |