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