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