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" |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 5 | #include "internal/pystate.h" |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 6 | |
Victor Stinner | b02ef71 | 2016-01-22 14:09:55 +0100 | [diff] [blame] | 7 | #define GET_TSTATE() \ |
| 8 | ((PyThreadState*)_Py_atomic_load_relaxed(&_PyThreadState_Current)) |
| 9 | #define SET_TSTATE(value) \ |
Benjamin Peterson | ca47063 | 2016-09-06 13:47:26 -0700 | [diff] [blame] | 10 | _Py_atomic_store_relaxed(&_PyThreadState_Current, (uintptr_t)(value)) |
Victor Stinner | b02ef71 | 2016-01-22 14:09:55 +0100 | [diff] [blame] | 11 | #define GET_INTERP_STATE() \ |
| 12 | (GET_TSTATE()->interp) |
| 13 | |
Victor Stinner | bfd316e | 2016-01-20 11:12:38 +0100 | [diff] [blame] | 14 | |
Tim Peters | 8470558 | 2004-10-10 02:47:33 +0000 | [diff] [blame] | 15 | /* -------------------------------------------------------------------------- |
| 16 | CAUTION |
| 17 | |
Victor Stinner | 1a7425f | 2013-07-07 16:25:15 +0200 | [diff] [blame] | 18 | Always use PyMem_RawMalloc() and PyMem_RawFree() directly in this file. A |
| 19 | number of these functions are advertised as safe to call when the GIL isn't |
| 20 | held, and in a debug build Python redirects (e.g.) PyMem_NEW (etc) to Python's |
| 21 | debugging obmalloc functions. Those aren't thread-safe (they rely on the GIL |
| 22 | to avoid the expense of doing their own locking). |
Tim Peters | 8470558 | 2004-10-10 02:47:33 +0000 | [diff] [blame] | 23 | -------------------------------------------------------------------------- */ |
| 24 | |
Martin v. Löwis | f0473d5 | 2001-07-18 16:17:16 +0000 | [diff] [blame] | 25 | #ifdef HAVE_DLOPEN |
| 26 | #ifdef HAVE_DLFCN_H |
| 27 | #include <dlfcn.h> |
| 28 | #endif |
Serhiy Storchaka | c2f7d87 | 2016-05-04 09:44:44 +0300 | [diff] [blame] | 29 | #if !HAVE_DECL_RTLD_LAZY |
Martin v. Löwis | f0473d5 | 2001-07-18 16:17:16 +0000 | [diff] [blame] | 30 | #define RTLD_LAZY 1 |
| 31 | #endif |
| 32 | #endif |
| 33 | |
Benjamin Peterson | 43162b8 | 2012-04-13 11:58:27 -0400 | [diff] [blame] | 34 | #ifdef __cplusplus |
| 35 | extern "C" { |
| 36 | #endif |
Martin v. Löwis | f0473d5 | 2001-07-18 16:17:16 +0000 | [diff] [blame] | 37 | |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 38 | void |
| 39 | _PyRuntimeState_Init(_PyRuntimeState *runtime) |
| 40 | { |
| 41 | memset(runtime, 0, sizeof(*runtime)); |
Victor Stinner | 8a1be61 | 2016-03-14 22:07:55 +0100 | [diff] [blame] | 42 | |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 43 | _PyObject_Initialize(&runtime->obj); |
| 44 | _PyMem_Initialize(&runtime->mem); |
| 45 | _PyGC_Initialize(&runtime->gc); |
| 46 | _PyEval_Initialize(&runtime->ceval); |
Michael W. Hudson | 188d436 | 2005-06-20 16:52:57 +0000 | [diff] [blame] | 47 | |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 48 | runtime->gilstate.check_enabled = 1; |
| 49 | runtime->gilstate.autoTLSkey = -1; |
Guido van Rossum | 1d5ad90 | 1999-06-18 14:22:24 +0000 | [diff] [blame] | 50 | |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 51 | runtime->interpreters.mutex = PyThread_allocate_lock(); |
| 52 | if (runtime->interpreters.mutex == NULL) |
| 53 | Py_FatalError("Can't initialize threads for interpreter"); |
| 54 | runtime->interpreters.next_id = -1; |
| 55 | } |
Eric Snow | 05351c1 | 2017-09-05 21:43:08 -0700 | [diff] [blame] | 56 | |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 57 | void |
| 58 | _PyRuntimeState_Fini(_PyRuntimeState *runtime) |
| 59 | { |
| 60 | if (runtime->interpreters.mutex != NULL) { |
| 61 | PyThread_free_lock(runtime->interpreters.mutex); |
| 62 | runtime->interpreters.mutex = NULL; |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | #define HEAD_LOCK() PyThread_acquire_lock(_PyRuntime.interpreters.mutex, \ |
| 67 | WAIT_LOCK) |
| 68 | #define HEAD_UNLOCK() PyThread_release_lock(_PyRuntime.interpreters.mutex) |
Eric Snow | 05351c1 | 2017-09-05 21:43:08 -0700 | [diff] [blame] | 69 | |
Michael W. Hudson | 188d436 | 2005-06-20 16:52:57 +0000 | [diff] [blame] | 70 | static void _PyGILState_NoteThreadState(PyThreadState* tstate); |
| 71 | |
Eric Snow | e377416 | 2017-05-22 19:46:40 -0700 | [diff] [blame] | 72 | void |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 73 | _PyInterpreterState_Enable(_PyRuntimeState *runtime) |
Eric Snow | e377416 | 2017-05-22 19:46:40 -0700 | [diff] [blame] | 74 | { |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 75 | runtime->interpreters.next_id = 0; |
| 76 | /* Since we only call _PyRuntimeState_Init() once per process |
| 77 | (see _PyRuntime_Initialize()), we make sure the mutex is |
| 78 | initialized here. */ |
| 79 | if (runtime->interpreters.mutex == NULL) { |
| 80 | runtime->interpreters.mutex = PyThread_allocate_lock(); |
| 81 | if (runtime->interpreters.mutex == NULL) |
| 82 | Py_FatalError("Can't initialize threads for interpreter"); |
| 83 | } |
Eric Snow | e377416 | 2017-05-22 19:46:40 -0700 | [diff] [blame] | 84 | } |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 85 | |
| 86 | PyInterpreterState * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 87 | PyInterpreterState_New(void) |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 88 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 89 | PyInterpreterState *interp = (PyInterpreterState *) |
Victor Stinner | 1a7425f | 2013-07-07 16:25:15 +0200 | [diff] [blame] | 90 | PyMem_RawMalloc(sizeof(PyInterpreterState)); |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 91 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 92 | if (interp != NULL) { |
Eric Snow | 93c92f7 | 2017-09-13 23:46:04 -0700 | [diff] [blame] | 93 | interp->modules = NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 94 | interp->modules_by_index = NULL; |
| 95 | interp->sysdict = NULL; |
| 96 | interp->builtins = NULL; |
Serhiy Storchaka | 87a5c51 | 2014-02-10 18:21:34 +0200 | [diff] [blame] | 97 | interp->builtins_copy = NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 98 | interp->tstate_head = NULL; |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 99 | interp->check_interval = 100; |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 100 | interp->num_threads = 0; |
| 101 | interp->pythread_stacksize = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 102 | interp->codec_search_path = NULL; |
| 103 | interp->codec_search_cache = NULL; |
| 104 | interp->codec_error_registry = NULL; |
| 105 | interp->codecs_initialized = 0; |
Victor Stinner | 3cbf14b | 2011-04-27 00:24:21 +0200 | [diff] [blame] | 106 | interp->fscodec_initialized = 0; |
Brett Cannon | fd07415 | 2012-04-14 14:10:13 -0400 | [diff] [blame] | 107 | interp->importlib = NULL; |
Serhiy Storchaka | 133138a | 2016-08-02 22:51:21 +0300 | [diff] [blame] | 108 | interp->import_func = NULL; |
Brett Cannon | 3cebf93 | 2016-09-05 15:33:46 -0700 | [diff] [blame] | 109 | interp->eval_frame = _PyEval_EvalFrameDefault; |
Dino Viehland | f3cffd2 | 2017-06-21 14:44:36 -0700 | [diff] [blame] | 110 | interp->co_extra_user_count = 0; |
Martin v. Löwis | f0473d5 | 2001-07-18 16:17:16 +0000 | [diff] [blame] | 111 | #ifdef HAVE_DLOPEN |
Serhiy Storchaka | c2f7d87 | 2016-05-04 09:44:44 +0300 | [diff] [blame] | 112 | #if HAVE_DECL_RTLD_NOW |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 113 | interp->dlopenflags = RTLD_NOW; |
Martin v. Löwis | f0473d5 | 2001-07-18 16:17:16 +0000 | [diff] [blame] | 114 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 115 | interp->dlopenflags = RTLD_LAZY; |
Martin v. Löwis | f0473d5 | 2001-07-18 16:17:16 +0000 | [diff] [blame] | 116 | #endif |
| 117 | #endif |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 118 | #ifdef HAVE_FORK |
| 119 | interp->before_forkers = NULL; |
| 120 | interp->after_forkers_parent = NULL; |
| 121 | interp->after_forkers_child = NULL; |
| 122 | #endif |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 123 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 124 | HEAD_LOCK(); |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 125 | interp->next = _PyRuntime.interpreters.head; |
| 126 | if (_PyRuntime.interpreters.main == NULL) { |
| 127 | _PyRuntime.interpreters.main = interp; |
Eric Snow | 6b4be19 | 2017-05-22 21:36:03 -0700 | [diff] [blame] | 128 | } |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 129 | _PyRuntime.interpreters.head = interp; |
| 130 | if (_PyRuntime.interpreters.next_id < 0) { |
Eric Snow | e377416 | 2017-05-22 19:46:40 -0700 | [diff] [blame] | 131 | /* overflow or Py_Initialize() not called! */ |
| 132 | PyErr_SetString(PyExc_RuntimeError, |
| 133 | "failed to get an interpreter ID"); |
| 134 | interp = NULL; |
| 135 | } else { |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 136 | interp->id = _PyRuntime.interpreters.next_id; |
| 137 | _PyRuntime.interpreters.next_id += 1; |
Eric Snow | e377416 | 2017-05-22 19:46:40 -0700 | [diff] [blame] | 138 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 139 | HEAD_UNLOCK(); |
| 140 | } |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 141 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 142 | return interp; |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | |
| 146 | void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 147 | PyInterpreterState_Clear(PyInterpreterState *interp) |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 148 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 149 | PyThreadState *p; |
| 150 | HEAD_LOCK(); |
| 151 | for (p = interp->tstate_head; p != NULL; p = p->next) |
| 152 | PyThreadState_Clear(p); |
| 153 | HEAD_UNLOCK(); |
| 154 | Py_CLEAR(interp->codec_search_path); |
| 155 | Py_CLEAR(interp->codec_search_cache); |
| 156 | Py_CLEAR(interp->codec_error_registry); |
Eric Snow | 93c92f7 | 2017-09-13 23:46:04 -0700 | [diff] [blame] | 157 | Py_CLEAR(interp->modules); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 158 | Py_CLEAR(interp->modules_by_index); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 159 | Py_CLEAR(interp->sysdict); |
| 160 | Py_CLEAR(interp->builtins); |
Serhiy Storchaka | 87a5c51 | 2014-02-10 18:21:34 +0200 | [diff] [blame] | 161 | Py_CLEAR(interp->builtins_copy); |
Brett Cannon | fd07415 | 2012-04-14 14:10:13 -0400 | [diff] [blame] | 162 | Py_CLEAR(interp->importlib); |
Serhiy Storchaka | 133138a | 2016-08-02 22:51:21 +0300 | [diff] [blame] | 163 | Py_CLEAR(interp->import_func); |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 164 | #ifdef HAVE_FORK |
| 165 | Py_CLEAR(interp->before_forkers); |
| 166 | Py_CLEAR(interp->after_forkers_parent); |
| 167 | Py_CLEAR(interp->after_forkers_child); |
| 168 | #endif |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | |
| 172 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 173 | zapthreads(PyInterpreterState *interp) |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 174 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 175 | PyThreadState *p; |
| 176 | /* No need to lock the mutex here because this should only happen |
| 177 | when the threads are all really dead (XXX famous last words). */ |
| 178 | while ((p = interp->tstate_head) != NULL) { |
| 179 | PyThreadState_Delete(p); |
| 180 | } |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 181 | } |
| 182 | |
| 183 | |
| 184 | void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 185 | PyInterpreterState_Delete(PyInterpreterState *interp) |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 186 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 187 | PyInterpreterState **p; |
| 188 | zapthreads(interp); |
| 189 | HEAD_LOCK(); |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 190 | for (p = &_PyRuntime.interpreters.head; ; p = &(*p)->next) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 191 | if (*p == NULL) |
| 192 | Py_FatalError( |
| 193 | "PyInterpreterState_Delete: invalid interp"); |
| 194 | if (*p == interp) |
| 195 | break; |
| 196 | } |
| 197 | if (interp->tstate_head != NULL) |
| 198 | Py_FatalError("PyInterpreterState_Delete: remaining threads"); |
| 199 | *p = interp->next; |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 200 | if (_PyRuntime.interpreters.main == interp) { |
| 201 | _PyRuntime.interpreters.main = NULL; |
| 202 | if (_PyRuntime.interpreters.head != NULL) |
Eric Snow | 6b4be19 | 2017-05-22 21:36:03 -0700 | [diff] [blame] | 203 | Py_FatalError("PyInterpreterState_Delete: remaining subinterpreters"); |
| 204 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 205 | HEAD_UNLOCK(); |
Victor Stinner | 1a7425f | 2013-07-07 16:25:15 +0200 | [diff] [blame] | 206 | PyMem_RawFree(interp); |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 207 | } |
| 208 | |
| 209 | |
Eric Snow | e377416 | 2017-05-22 19:46:40 -0700 | [diff] [blame] | 210 | int64_t |
| 211 | PyInterpreterState_GetID(PyInterpreterState *interp) |
| 212 | { |
| 213 | if (interp == NULL) { |
| 214 | PyErr_SetString(PyExc_RuntimeError, "no interpreter provided"); |
| 215 | return -1; |
| 216 | } |
| 217 | return interp->id; |
| 218 | } |
| 219 | |
| 220 | |
Michael W. Hudson | 019a78e | 2002-11-08 12:53:11 +0000 | [diff] [blame] | 221 | /* Default implementation for _PyThreadState_GetFrame */ |
| 222 | static struct _frame * |
| 223 | threadstate_getframe(PyThreadState *self) |
| 224 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 225 | return self->frame; |
Michael W. Hudson | 019a78e | 2002-11-08 12:53:11 +0000 | [diff] [blame] | 226 | } |
| 227 | |
Victor Stinner | 45b9be5 | 2010-03-03 23:28:07 +0000 | [diff] [blame] | 228 | static PyThreadState * |
| 229 | new_threadstate(PyInterpreterState *interp, int init) |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 230 | { |
Victor Stinner | 1a7425f | 2013-07-07 16:25:15 +0200 | [diff] [blame] | 231 | PyThreadState *tstate = (PyThreadState *)PyMem_RawMalloc(sizeof(PyThreadState)); |
Tim Peters | 8470558 | 2004-10-10 02:47:33 +0000 | [diff] [blame] | 232 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 233 | if (_PyThreadState_GetFrame == NULL) |
| 234 | _PyThreadState_GetFrame = threadstate_getframe; |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 235 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 236 | if (tstate != NULL) { |
| 237 | tstate->interp = interp; |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 238 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 239 | tstate->frame = NULL; |
| 240 | tstate->recursion_depth = 0; |
| 241 | tstate->overflowed = 0; |
| 242 | tstate->recursion_critical = 0; |
| 243 | tstate->tracing = 0; |
| 244 | tstate->use_tracing = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 245 | tstate->gilstate_counter = 0; |
| 246 | tstate->async_exc = NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 247 | tstate->thread_id = PyThread_get_thread_ident(); |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 248 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 249 | tstate->dict = NULL; |
Guido van Rossum | ede0439 | 1998-04-10 20:18:25 +0000 | [diff] [blame] | 250 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 251 | tstate->curexc_type = NULL; |
| 252 | tstate->curexc_value = NULL; |
| 253 | tstate->curexc_traceback = NULL; |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 254 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 255 | tstate->exc_type = NULL; |
| 256 | tstate->exc_value = NULL; |
| 257 | tstate->exc_traceback = NULL; |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 258 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 259 | tstate->c_profilefunc = NULL; |
| 260 | tstate->c_tracefunc = NULL; |
| 261 | tstate->c_profileobj = NULL; |
| 262 | tstate->c_traceobj = NULL; |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 263 | |
Antoine Pitrou | 2b0218a | 2012-09-06 00:59:49 +0200 | [diff] [blame] | 264 | tstate->trash_delete_nesting = 0; |
| 265 | tstate->trash_delete_later = NULL; |
Antoine Pitrou | 7b47699 | 2013-09-07 23:38:37 +0200 | [diff] [blame] | 266 | tstate->on_delete = NULL; |
| 267 | tstate->on_delete_data = NULL; |
Antoine Pitrou | 2b0218a | 2012-09-06 00:59:49 +0200 | [diff] [blame] | 268 | |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 269 | tstate->coroutine_wrapper = NULL; |
Yury Selivanov | aab3c4a | 2015-06-02 18:43:51 -0400 | [diff] [blame] | 270 | tstate->in_coroutine_wrapper = 0; |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 271 | |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 272 | tstate->async_gen_firstiter = NULL; |
| 273 | tstate->async_gen_finalizer = NULL; |
| 274 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 275 | if (init) |
| 276 | _PyThreadState_Init(tstate); |
Michael W. Hudson | 188d436 | 2005-06-20 16:52:57 +0000 | [diff] [blame] | 277 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 278 | HEAD_LOCK(); |
Charles-Francois Natali | f28dfdd | 2013-05-08 21:09:52 +0200 | [diff] [blame] | 279 | tstate->prev = NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 280 | tstate->next = interp->tstate_head; |
Charles-Francois Natali | f28dfdd | 2013-05-08 21:09:52 +0200 | [diff] [blame] | 281 | if (tstate->next) |
| 282 | tstate->next->prev = tstate; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 283 | interp->tstate_head = tstate; |
| 284 | HEAD_UNLOCK(); |
| 285 | } |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 286 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 287 | return tstate; |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 288 | } |
| 289 | |
Victor Stinner | 45b9be5 | 2010-03-03 23:28:07 +0000 | [diff] [blame] | 290 | PyThreadState * |
| 291 | PyThreadState_New(PyInterpreterState *interp) |
| 292 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 293 | return new_threadstate(interp, 1); |
Victor Stinner | 45b9be5 | 2010-03-03 23:28:07 +0000 | [diff] [blame] | 294 | } |
| 295 | |
| 296 | PyThreadState * |
| 297 | _PyThreadState_Prealloc(PyInterpreterState *interp) |
| 298 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 299 | return new_threadstate(interp, 0); |
Victor Stinner | 45b9be5 | 2010-03-03 23:28:07 +0000 | [diff] [blame] | 300 | } |
| 301 | |
| 302 | void |
| 303 | _PyThreadState_Init(PyThreadState *tstate) |
| 304 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 305 | _PyGILState_NoteThreadState(tstate); |
Victor Stinner | 45b9be5 | 2010-03-03 23:28:07 +0000 | [diff] [blame] | 306 | } |
| 307 | |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 308 | PyObject* |
Martin v. Löwis | 7800f75 | 2012-06-22 12:20:55 +0200 | [diff] [blame] | 309 | PyState_FindModule(struct PyModuleDef* module) |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 310 | { |
Martin v. Löwis | 7800f75 | 2012-06-22 12:20:55 +0200 | [diff] [blame] | 311 | Py_ssize_t index = module->m_base.m_index; |
Victor Stinner | b02ef71 | 2016-01-22 14:09:55 +0100 | [diff] [blame] | 312 | PyInterpreterState *state = GET_INTERP_STATE(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 313 | PyObject *res; |
Nick Coghlan | d5cacbb | 2015-05-23 22:24:10 +1000 | [diff] [blame] | 314 | if (module->m_slots) { |
| 315 | return NULL; |
| 316 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 317 | if (index == 0) |
| 318 | return NULL; |
| 319 | if (state->modules_by_index == NULL) |
| 320 | return NULL; |
Antoine Pitrou | 75506e8 | 2012-08-20 19:30:46 +0200 | [diff] [blame] | 321 | if (index >= PyList_GET_SIZE(state->modules_by_index)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 322 | return NULL; |
| 323 | res = PyList_GET_ITEM(state->modules_by_index, index); |
| 324 | return res==Py_None ? NULL : res; |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 325 | } |
| 326 | |
| 327 | int |
| 328 | _PyState_AddModule(PyObject* module, struct PyModuleDef* def) |
| 329 | { |
Nick Coghlan | d5cacbb | 2015-05-23 22:24:10 +1000 | [diff] [blame] | 330 | PyInterpreterState *state; |
Berker Peksag | 4b7b565 | 2016-08-22 18:05:56 +0300 | [diff] [blame] | 331 | if (!def) { |
| 332 | assert(PyErr_Occurred()); |
| 333 | return -1; |
| 334 | } |
Nick Coghlan | d5cacbb | 2015-05-23 22:24:10 +1000 | [diff] [blame] | 335 | if (def->m_slots) { |
| 336 | PyErr_SetString(PyExc_SystemError, |
| 337 | "PyState_AddModule called on module with slots"); |
| 338 | return -1; |
| 339 | } |
Victor Stinner | b02ef71 | 2016-01-22 14:09:55 +0100 | [diff] [blame] | 340 | state = GET_INTERP_STATE(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 341 | if (!state->modules_by_index) { |
| 342 | state->modules_by_index = PyList_New(0); |
| 343 | if (!state->modules_by_index) |
| 344 | return -1; |
| 345 | } |
| 346 | while(PyList_GET_SIZE(state->modules_by_index) <= def->m_base.m_index) |
| 347 | if (PyList_Append(state->modules_by_index, Py_None) < 0) |
| 348 | return -1; |
| 349 | Py_INCREF(module); |
| 350 | return PyList_SetItem(state->modules_by_index, |
| 351 | def->m_base.m_index, module); |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 352 | } |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 353 | |
Martin v. Löwis | 7800f75 | 2012-06-22 12:20:55 +0200 | [diff] [blame] | 354 | int |
| 355 | PyState_AddModule(PyObject* module, struct PyModuleDef* def) |
| 356 | { |
| 357 | Py_ssize_t index; |
Victor Stinner | b02ef71 | 2016-01-22 14:09:55 +0100 | [diff] [blame] | 358 | PyInterpreterState *state = GET_INTERP_STATE(); |
Martin v. Löwis | 7800f75 | 2012-06-22 12:20:55 +0200 | [diff] [blame] | 359 | if (!def) { |
| 360 | Py_FatalError("PyState_AddModule: Module Definition is NULL"); |
| 361 | return -1; |
| 362 | } |
| 363 | index = def->m_base.m_index; |
| 364 | if (state->modules_by_index) { |
| 365 | if(PyList_GET_SIZE(state->modules_by_index) >= index) { |
| 366 | if(module == PyList_GET_ITEM(state->modules_by_index, index)) { |
| 367 | Py_FatalError("PyState_AddModule: Module already added!"); |
| 368 | return -1; |
| 369 | } |
| 370 | } |
| 371 | } |
| 372 | return _PyState_AddModule(module, def); |
| 373 | } |
| 374 | |
| 375 | int |
| 376 | PyState_RemoveModule(struct PyModuleDef* def) |
| 377 | { |
Nick Coghlan | d5cacbb | 2015-05-23 22:24:10 +1000 | [diff] [blame] | 378 | PyInterpreterState *state; |
Martin v. Löwis | 7800f75 | 2012-06-22 12:20:55 +0200 | [diff] [blame] | 379 | Py_ssize_t index = def->m_base.m_index; |
Nick Coghlan | d5cacbb | 2015-05-23 22:24:10 +1000 | [diff] [blame] | 380 | if (def->m_slots) { |
| 381 | PyErr_SetString(PyExc_SystemError, |
| 382 | "PyState_RemoveModule called on module with slots"); |
| 383 | return -1; |
| 384 | } |
Victor Stinner | b02ef71 | 2016-01-22 14:09:55 +0100 | [diff] [blame] | 385 | state = GET_INTERP_STATE(); |
Martin v. Löwis | 7800f75 | 2012-06-22 12:20:55 +0200 | [diff] [blame] | 386 | if (index == 0) { |
| 387 | Py_FatalError("PyState_RemoveModule: Module index invalid."); |
| 388 | return -1; |
| 389 | } |
| 390 | if (state->modules_by_index == NULL) { |
| 391 | Py_FatalError("PyState_RemoveModule: Interpreters module-list not acessible."); |
| 392 | return -1; |
| 393 | } |
| 394 | if (index > PyList_GET_SIZE(state->modules_by_index)) { |
| 395 | Py_FatalError("PyState_RemoveModule: Module index out of bounds."); |
| 396 | return -1; |
| 397 | } |
| 398 | return PyList_SetItem(state->modules_by_index, index, Py_None); |
| 399 | } |
| 400 | |
Antoine Pitrou | 40322e6 | 2013-08-11 00:30:09 +0200 | [diff] [blame] | 401 | /* used by import.c:PyImport_Cleanup */ |
| 402 | void |
| 403 | _PyState_ClearModules(void) |
| 404 | { |
Victor Stinner | b02ef71 | 2016-01-22 14:09:55 +0100 | [diff] [blame] | 405 | PyInterpreterState *state = GET_INTERP_STATE(); |
Antoine Pitrou | 40322e6 | 2013-08-11 00:30:09 +0200 | [diff] [blame] | 406 | if (state->modules_by_index) { |
| 407 | Py_ssize_t i; |
| 408 | for (i = 0; i < PyList_GET_SIZE(state->modules_by_index); i++) { |
| 409 | PyObject *m = PyList_GET_ITEM(state->modules_by_index, i); |
| 410 | if (PyModule_Check(m)) { |
| 411 | /* cleanup the saved copy of module dicts */ |
| 412 | PyModuleDef *md = PyModule_GetDef(m); |
| 413 | if (md) |
| 414 | Py_CLEAR(md->m_base.m_copy); |
| 415 | } |
| 416 | } |
| 417 | /* Setting modules_by_index to NULL could be dangerous, so we |
| 418 | clear the list instead. */ |
| 419 | if (PyList_SetSlice(state->modules_by_index, |
| 420 | 0, PyList_GET_SIZE(state->modules_by_index), |
| 421 | NULL)) |
| 422 | PyErr_WriteUnraisable(state->modules_by_index); |
| 423 | } |
| 424 | } |
| 425 | |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 426 | void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 427 | PyThreadState_Clear(PyThreadState *tstate) |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 428 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 429 | if (Py_VerboseFlag && tstate->frame != NULL) |
| 430 | fprintf(stderr, |
| 431 | "PyThreadState_Clear: warning: thread still has a frame\n"); |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 432 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 433 | Py_CLEAR(tstate->frame); |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 434 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 435 | Py_CLEAR(tstate->dict); |
| 436 | Py_CLEAR(tstate->async_exc); |
Guido van Rossum | ede0439 | 1998-04-10 20:18:25 +0000 | [diff] [blame] | 437 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 438 | Py_CLEAR(tstate->curexc_type); |
| 439 | Py_CLEAR(tstate->curexc_value); |
| 440 | Py_CLEAR(tstate->curexc_traceback); |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 441 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 442 | Py_CLEAR(tstate->exc_type); |
| 443 | Py_CLEAR(tstate->exc_value); |
| 444 | Py_CLEAR(tstate->exc_traceback); |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 445 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 446 | tstate->c_profilefunc = NULL; |
| 447 | tstate->c_tracefunc = NULL; |
| 448 | Py_CLEAR(tstate->c_profileobj); |
| 449 | Py_CLEAR(tstate->c_traceobj); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 450 | |
| 451 | Py_CLEAR(tstate->coroutine_wrapper); |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 452 | Py_CLEAR(tstate->async_gen_firstiter); |
| 453 | Py_CLEAR(tstate->async_gen_finalizer); |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 454 | } |
| 455 | |
| 456 | |
Guido van Rossum | 2975786 | 2001-01-23 01:46:06 +0000 | [diff] [blame] | 457 | /* Common code for PyThreadState_Delete() and PyThreadState_DeleteCurrent() */ |
| 458 | static void |
| 459 | tstate_delete_common(PyThreadState *tstate) |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 460 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 461 | PyInterpreterState *interp; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 462 | if (tstate == NULL) |
| 463 | Py_FatalError("PyThreadState_Delete: NULL tstate"); |
| 464 | interp = tstate->interp; |
| 465 | if (interp == NULL) |
| 466 | Py_FatalError("PyThreadState_Delete: NULL interp"); |
| 467 | HEAD_LOCK(); |
Charles-Francois Natali | f28dfdd | 2013-05-08 21:09:52 +0200 | [diff] [blame] | 468 | if (tstate->prev) |
| 469 | tstate->prev->next = tstate->next; |
| 470 | else |
| 471 | interp->tstate_head = tstate->next; |
| 472 | if (tstate->next) |
| 473 | tstate->next->prev = tstate->prev; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 474 | HEAD_UNLOCK(); |
Antoine Pitrou | 7b47699 | 2013-09-07 23:38:37 +0200 | [diff] [blame] | 475 | if (tstate->on_delete != NULL) { |
| 476 | tstate->on_delete(tstate->on_delete_data); |
| 477 | } |
Victor Stinner | 1a7425f | 2013-07-07 16:25:15 +0200 | [diff] [blame] | 478 | PyMem_RawFree(tstate); |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 479 | } |
| 480 | |
| 481 | |
Guido van Rossum | 2975786 | 2001-01-23 01:46:06 +0000 | [diff] [blame] | 482 | void |
| 483 | PyThreadState_Delete(PyThreadState *tstate) |
| 484 | { |
Victor Stinner | b02ef71 | 2016-01-22 14:09:55 +0100 | [diff] [blame] | 485 | if (tstate == GET_TSTATE()) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 486 | Py_FatalError("PyThreadState_Delete: tstate is still current"); |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 487 | if (_PyRuntime.gilstate.autoInterpreterState && |
| 488 | PyThread_get_key_value(_PyRuntime.gilstate.autoTLSkey) == tstate) |
| 489 | { |
| 490 | PyThread_delete_key_value(_PyRuntime.gilstate.autoTLSkey); |
| 491 | } |
Christian Heimes | b9dbc7d | 2013-07-01 23:42:28 +0200 | [diff] [blame] | 492 | tstate_delete_common(tstate); |
Guido van Rossum | 2975786 | 2001-01-23 01:46:06 +0000 | [diff] [blame] | 493 | } |
| 494 | |
| 495 | |
Guido van Rossum | 2975786 | 2001-01-23 01:46:06 +0000 | [diff] [blame] | 496 | void |
| 497 | PyThreadState_DeleteCurrent() |
| 498 | { |
Victor Stinner | b02ef71 | 2016-01-22 14:09:55 +0100 | [diff] [blame] | 499 | PyThreadState *tstate = GET_TSTATE(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 500 | if (tstate == NULL) |
| 501 | Py_FatalError( |
| 502 | "PyThreadState_DeleteCurrent: no current tstate"); |
Victor Stinner | 8a1be61 | 2016-03-14 22:07:55 +0100 | [diff] [blame] | 503 | tstate_delete_common(tstate); |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 504 | if (_PyRuntime.gilstate.autoInterpreterState && |
| 505 | PyThread_get_key_value(_PyRuntime.gilstate.autoTLSkey) == tstate) |
| 506 | { |
| 507 | PyThread_delete_key_value(_PyRuntime.gilstate.autoTLSkey); |
| 508 | } |
Victor Stinner | 8a1be61 | 2016-03-14 22:07:55 +0100 | [diff] [blame] | 509 | SET_TSTATE(NULL); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 510 | PyEval_ReleaseLock(); |
Guido van Rossum | 2975786 | 2001-01-23 01:46:06 +0000 | [diff] [blame] | 511 | } |
Guido van Rossum | 2975786 | 2001-01-23 01:46:06 +0000 | [diff] [blame] | 512 | |
| 513 | |
Antoine Pitrou | 8408cea | 2013-05-05 23:47:09 +0200 | [diff] [blame] | 514 | /* |
| 515 | * Delete all thread states except the one passed as argument. |
| 516 | * Note that, if there is a current thread state, it *must* be the one |
| 517 | * passed as argument. Also, this won't touch any other interpreters |
| 518 | * than the current one, since we don't know which thread state should |
| 519 | * be kept in those other interpreteres. |
| 520 | */ |
| 521 | void |
| 522 | _PyThreadState_DeleteExcept(PyThreadState *tstate) |
| 523 | { |
| 524 | PyInterpreterState *interp = tstate->interp; |
| 525 | PyThreadState *p, *next, *garbage; |
| 526 | HEAD_LOCK(); |
| 527 | /* Remove all thread states, except tstate, from the linked list of |
| 528 | thread states. This will allow calling PyThreadState_Clear() |
Charles-Francois Natali | f28dfdd | 2013-05-08 21:09:52 +0200 | [diff] [blame] | 529 | without holding the lock. */ |
Antoine Pitrou | 8408cea | 2013-05-05 23:47:09 +0200 | [diff] [blame] | 530 | garbage = interp->tstate_head; |
Charles-Francois Natali | f28dfdd | 2013-05-08 21:09:52 +0200 | [diff] [blame] | 531 | if (garbage == tstate) |
| 532 | garbage = tstate->next; |
| 533 | if (tstate->prev) |
| 534 | tstate->prev->next = tstate->next; |
| 535 | if (tstate->next) |
| 536 | tstate->next->prev = tstate->prev; |
| 537 | tstate->prev = tstate->next = NULL; |
Antoine Pitrou | 8408cea | 2013-05-05 23:47:09 +0200 | [diff] [blame] | 538 | interp->tstate_head = tstate; |
Antoine Pitrou | 8408cea | 2013-05-05 23:47:09 +0200 | [diff] [blame] | 539 | HEAD_UNLOCK(); |
| 540 | /* Clear and deallocate all stale thread states. Even if this |
| 541 | executes Python code, we should be safe since it executes |
| 542 | in the current thread, not one of the stale threads. */ |
| 543 | for (p = garbage; p; p = next) { |
| 544 | next = p->next; |
| 545 | PyThreadState_Clear(p); |
Victor Stinner | 1a7425f | 2013-07-07 16:25:15 +0200 | [diff] [blame] | 546 | PyMem_RawFree(p); |
Antoine Pitrou | 8408cea | 2013-05-05 23:47:09 +0200 | [diff] [blame] | 547 | } |
| 548 | } |
| 549 | |
| 550 | |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 551 | PyThreadState * |
Victor Stinner | bfd316e | 2016-01-20 11:12:38 +0100 | [diff] [blame] | 552 | _PyThreadState_UncheckedGet(void) |
| 553 | { |
Victor Stinner | b02ef71 | 2016-01-22 14:09:55 +0100 | [diff] [blame] | 554 | return GET_TSTATE(); |
Victor Stinner | bfd316e | 2016-01-20 11:12:38 +0100 | [diff] [blame] | 555 | } |
| 556 | |
| 557 | |
| 558 | PyThreadState * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 559 | PyThreadState_Get(void) |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 560 | { |
Victor Stinner | b02ef71 | 2016-01-22 14:09:55 +0100 | [diff] [blame] | 561 | PyThreadState *tstate = GET_TSTATE(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 562 | if (tstate == NULL) |
| 563 | Py_FatalError("PyThreadState_Get: no current thread"); |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 564 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 565 | return tstate; |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 566 | } |
| 567 | |
| 568 | |
| 569 | PyThreadState * |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 570 | PyThreadState_Swap(PyThreadState *newts) |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 571 | { |
Victor Stinner | b02ef71 | 2016-01-22 14:09:55 +0100 | [diff] [blame] | 572 | PyThreadState *oldts = GET_TSTATE(); |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 573 | |
Victor Stinner | b02ef71 | 2016-01-22 14:09:55 +0100 | [diff] [blame] | 574 | SET_TSTATE(newts); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 575 | /* It should not be possible for more than one thread state |
| 576 | to be used for a thread. Check this the best we can in debug |
| 577 | builds. |
| 578 | */ |
Antoine Pitrou | a6a4dc8 | 2017-09-07 18:56:24 +0200 | [diff] [blame] | 579 | #if defined(Py_DEBUG) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 580 | if (newts) { |
| 581 | /* This can be called from PyEval_RestoreThread(). Similar |
| 582 | to it, we need to ensure errno doesn't change. |
| 583 | */ |
| 584 | int err = errno; |
| 585 | PyThreadState *check = PyGILState_GetThisThreadState(); |
| 586 | if (check && check->interp == newts->interp && check != newts) |
| 587 | Py_FatalError("Invalid thread state for this thread"); |
| 588 | errno = err; |
| 589 | } |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 590 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 591 | return oldts; |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 592 | } |
Guido van Rossum | ede0439 | 1998-04-10 20:18:25 +0000 | [diff] [blame] | 593 | |
| 594 | /* An extension mechanism to store arbitrary additional per-thread state. |
| 595 | PyThreadState_GetDict() returns a dictionary that can be used to hold such |
| 596 | 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] | 597 | PyThreadState_GetDict() returns NULL, an exception has *not* been raised |
| 598 | and the caller should assume no per-thread state is available. */ |
Guido van Rossum | ede0439 | 1998-04-10 20:18:25 +0000 | [diff] [blame] | 599 | |
| 600 | PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 601 | PyThreadState_GetDict(void) |
Guido van Rossum | ede0439 | 1998-04-10 20:18:25 +0000 | [diff] [blame] | 602 | { |
Victor Stinner | b02ef71 | 2016-01-22 14:09:55 +0100 | [diff] [blame] | 603 | PyThreadState *tstate = GET_TSTATE(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 604 | if (tstate == NULL) |
| 605 | return NULL; |
Guido van Rossum | ede0439 | 1998-04-10 20:18:25 +0000 | [diff] [blame] | 606 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 607 | if (tstate->dict == NULL) { |
| 608 | PyObject *d; |
| 609 | tstate->dict = d = PyDict_New(); |
| 610 | if (d == NULL) |
| 611 | PyErr_Clear(); |
| 612 | } |
| 613 | return tstate->dict; |
Guido van Rossum | ede0439 | 1998-04-10 20:18:25 +0000 | [diff] [blame] | 614 | } |
Guido van Rossum | f5df46d | 2001-07-19 12:19:27 +0000 | [diff] [blame] | 615 | |
| 616 | |
Guido van Rossum | b8b6d0c | 2003-06-28 21:53:52 +0000 | [diff] [blame] | 617 | /* Asynchronously raise an exception in a thread. |
| 618 | Requested by Just van Rossum and Alex Martelli. |
Guido van Rossum | 0f1f63c | 2005-02-08 02:07:57 +0000 | [diff] [blame] | 619 | To prevent naive misuse, you must write your own extension |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 620 | to call this, or use ctypes. Must be called with the GIL held. |
| 621 | Returns the number of tstates modified (normally 1, but 0 if `id` didn't |
| 622 | match any known thread id). Can be called with exc=NULL to clear an |
| 623 | existing async exception. This raises no exceptions. */ |
Guido van Rossum | b8b6d0c | 2003-06-28 21:53:52 +0000 | [diff] [blame] | 624 | |
| 625 | int |
Serhiy Storchaka | aefa7eb | 2017-03-23 15:48:39 +0200 | [diff] [blame] | 626 | PyThreadState_SetAsyncExc(unsigned long id, PyObject *exc) |
| 627 | { |
Victor Stinner | b02ef71 | 2016-01-22 14:09:55 +0100 | [diff] [blame] | 628 | PyInterpreterState *interp = GET_INTERP_STATE(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 629 | PyThreadState *p; |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 630 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 631 | /* Although the GIL is held, a few C API functions can be called |
| 632 | * without the GIL held, and in particular some that create and |
| 633 | * destroy thread and interpreter states. Those can mutate the |
| 634 | * list of thread states we're traversing, so to prevent that we lock |
| 635 | * head_mutex for the duration. |
| 636 | */ |
| 637 | HEAD_LOCK(); |
| 638 | for (p = interp->tstate_head; p != NULL; p = p->next) { |
| 639 | if (p->thread_id == id) { |
| 640 | /* Tricky: we need to decref the current value |
| 641 | * (if any) in p->async_exc, but that can in turn |
| 642 | * allow arbitrary Python code to run, including |
| 643 | * perhaps calls to this function. To prevent |
| 644 | * deadlock, we need to release head_mutex before |
| 645 | * the decref. |
| 646 | */ |
| 647 | PyObject *old_exc = p->async_exc; |
| 648 | Py_XINCREF(exc); |
| 649 | p->async_exc = exc; |
| 650 | HEAD_UNLOCK(); |
| 651 | Py_XDECREF(old_exc); |
| 652 | _PyEval_SignalAsyncExc(); |
| 653 | return 1; |
| 654 | } |
| 655 | } |
| 656 | HEAD_UNLOCK(); |
| 657 | return 0; |
Guido van Rossum | b8b6d0c | 2003-06-28 21:53:52 +0000 | [diff] [blame] | 658 | } |
| 659 | |
| 660 | |
Guido van Rossum | f5df46d | 2001-07-19 12:19:27 +0000 | [diff] [blame] | 661 | /* Routines for advanced debuggers, requested by David Beazley. |
| 662 | Don't use unless you know what you are doing! */ |
| 663 | |
| 664 | PyInterpreterState * |
| 665 | PyInterpreterState_Head(void) |
| 666 | { |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 667 | return _PyRuntime.interpreters.head; |
Guido van Rossum | f5df46d | 2001-07-19 12:19:27 +0000 | [diff] [blame] | 668 | } |
| 669 | |
| 670 | PyInterpreterState * |
Eric Snow | 6b4be19 | 2017-05-22 21:36:03 -0700 | [diff] [blame] | 671 | PyInterpreterState_Main(void) |
| 672 | { |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 673 | return _PyRuntime.interpreters.main; |
Eric Snow | 6b4be19 | 2017-05-22 21:36:03 -0700 | [diff] [blame] | 674 | } |
| 675 | |
| 676 | PyInterpreterState * |
Guido van Rossum | f5df46d | 2001-07-19 12:19:27 +0000 | [diff] [blame] | 677 | PyInterpreterState_Next(PyInterpreterState *interp) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 678 | return interp->next; |
Guido van Rossum | f5df46d | 2001-07-19 12:19:27 +0000 | [diff] [blame] | 679 | } |
| 680 | |
| 681 | PyThreadState * |
| 682 | PyInterpreterState_ThreadHead(PyInterpreterState *interp) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 683 | return interp->tstate_head; |
Guido van Rossum | f5df46d | 2001-07-19 12:19:27 +0000 | [diff] [blame] | 684 | } |
| 685 | |
| 686 | PyThreadState * |
| 687 | PyThreadState_Next(PyThreadState *tstate) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 688 | return tstate->next; |
Guido van Rossum | f5df46d | 2001-07-19 12:19:27 +0000 | [diff] [blame] | 689 | } |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 690 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 691 | /* The implementation of sys._current_frames(). This is intended to be |
| 692 | called with the GIL held, as it will be when called via |
| 693 | sys._current_frames(). It's possible it would work fine even without |
| 694 | the GIL held, but haven't thought enough about that. |
| 695 | */ |
| 696 | PyObject * |
| 697 | _PyThread_CurrentFrames(void) |
| 698 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 699 | PyObject *result; |
| 700 | PyInterpreterState *i; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 701 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 702 | result = PyDict_New(); |
| 703 | if (result == NULL) |
| 704 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 705 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 706 | /* for i in all interpreters: |
| 707 | * for t in all of i's thread states: |
| 708 | * if t's frame isn't NULL, map t's id to its frame |
Ezio Melotti | 1392500 | 2011-03-16 11:05:33 +0200 | [diff] [blame] | 709 | * Because these lists can mutate even when the GIL is held, we |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 710 | * need to grab head_mutex for the duration. |
| 711 | */ |
| 712 | HEAD_LOCK(); |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 713 | for (i = _PyRuntime.interpreters.head; i != NULL; i = i->next) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 714 | PyThreadState *t; |
| 715 | for (t = i->tstate_head; t != NULL; t = t->next) { |
| 716 | PyObject *id; |
| 717 | int stat; |
| 718 | struct _frame *frame = t->frame; |
| 719 | if (frame == NULL) |
| 720 | continue; |
Serhiy Storchaka | aefa7eb | 2017-03-23 15:48:39 +0200 | [diff] [blame] | 721 | id = PyLong_FromUnsignedLong(t->thread_id); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 722 | if (id == NULL) |
| 723 | goto Fail; |
| 724 | stat = PyDict_SetItem(result, id, (PyObject *)frame); |
| 725 | Py_DECREF(id); |
| 726 | if (stat < 0) |
| 727 | goto Fail; |
| 728 | } |
| 729 | } |
| 730 | HEAD_UNLOCK(); |
| 731 | return result; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 732 | |
| 733 | Fail: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 734 | HEAD_UNLOCK(); |
| 735 | Py_DECREF(result); |
| 736 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 737 | } |
Guido van Rossum | b8b6d0c | 2003-06-28 21:53:52 +0000 | [diff] [blame] | 738 | |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 739 | /* Python "auto thread state" API. */ |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 740 | |
| 741 | /* Keep this as a static, as it is not reliable! It can only |
| 742 | ever be compared to the state for the *current* thread. |
| 743 | * If not equal, then it doesn't matter that the actual |
| 744 | value may change immediately after comparison, as it can't |
| 745 | possibly change to the current thread's state. |
| 746 | * If equal, then the current thread holds the lock, so the value can't |
| 747 | change until we yield the lock. |
| 748 | */ |
| 749 | static int |
| 750 | PyThreadState_IsCurrent(PyThreadState *tstate) |
| 751 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 752 | /* Must be the tstate for this thread */ |
| 753 | assert(PyGILState_GetThisThreadState()==tstate); |
Victor Stinner | b02ef71 | 2016-01-22 14:09:55 +0100 | [diff] [blame] | 754 | return tstate == GET_TSTATE(); |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 755 | } |
| 756 | |
Tim Peters | 4c1f5ec | 2004-10-09 17:25:05 +0000 | [diff] [blame] | 757 | /* Internal initialization/finalization functions called by |
Martin Panter | b4ce1fc | 2015-11-30 03:18:29 +0000 | [diff] [blame] | 758 | Py_Initialize/Py_FinalizeEx |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 759 | */ |
Tim Peters | 19717fa | 2004-10-09 17:38:29 +0000 | [diff] [blame] | 760 | void |
| 761 | _PyGILState_Init(PyInterpreterState *i, PyThreadState *t) |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 762 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 763 | assert(i && t); /* must init with valid states */ |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 764 | _PyRuntime.gilstate.autoTLSkey = PyThread_create_key(); |
| 765 | if (_PyRuntime.gilstate.autoTLSkey == -1) |
Kristján Valur Jónsson | 2fea9b9 | 2010-09-20 02:11:49 +0000 | [diff] [blame] | 766 | Py_FatalError("Could not allocate TLS entry"); |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 767 | _PyRuntime.gilstate.autoInterpreterState = i; |
| 768 | assert(PyThread_get_key_value(_PyRuntime.gilstate.autoTLSkey) == NULL); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 769 | assert(t->gilstate_counter == 0); |
Michael W. Hudson | 188d436 | 2005-06-20 16:52:57 +0000 | [diff] [blame] | 770 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 771 | _PyGILState_NoteThreadState(t); |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 772 | } |
| 773 | |
Victor Stinner | 861d9ab | 2016-03-16 22:45:24 +0100 | [diff] [blame] | 774 | PyInterpreterState * |
| 775 | _PyGILState_GetInterpreterStateUnsafe(void) |
| 776 | { |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 777 | return _PyRuntime.gilstate.autoInterpreterState; |
Victor Stinner | 861d9ab | 2016-03-16 22:45:24 +0100 | [diff] [blame] | 778 | } |
| 779 | |
Tim Peters | 19717fa | 2004-10-09 17:38:29 +0000 | [diff] [blame] | 780 | void |
| 781 | _PyGILState_Fini(void) |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 782 | { |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 783 | PyThread_delete_key(_PyRuntime.gilstate.autoTLSkey); |
| 784 | _PyRuntime.gilstate.autoTLSkey = -1; |
| 785 | _PyRuntime.gilstate.autoInterpreterState = NULL; |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 786 | } |
| 787 | |
Antoine Pitrou | f7ecfac | 2017-05-28 11:35:14 +0200 | [diff] [blame] | 788 | /* Reset the TLS key - called by PyOS_AfterFork_Child(). |
Antoine Pitrou | 0c759fe | 2011-04-27 19:28:05 +0200 | [diff] [blame] | 789 | * This should not be necessary, but some - buggy - pthread implementations |
Charles-François Natali | a233df8 | 2011-11-22 19:49:51 +0100 | [diff] [blame] | 790 | * don't reset TLS upon fork(), see issue #10517. |
Antoine Pitrou | 0c759fe | 2011-04-27 19:28:05 +0200 | [diff] [blame] | 791 | */ |
| 792 | void |
| 793 | _PyGILState_Reinit(void) |
| 794 | { |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 795 | _PyRuntime.interpreters.mutex = PyThread_allocate_lock(); |
| 796 | if (_PyRuntime.interpreters.mutex == NULL) |
| 797 | Py_FatalError("Can't initialize threads for interpreter"); |
Antoine Pitrou | 0c759fe | 2011-04-27 19:28:05 +0200 | [diff] [blame] | 798 | PyThreadState *tstate = PyGILState_GetThisThreadState(); |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 799 | PyThread_delete_key(_PyRuntime.gilstate.autoTLSkey); |
| 800 | if ((_PyRuntime.gilstate.autoTLSkey = PyThread_create_key()) == -1) |
Antoine Pitrou | 0c759fe | 2011-04-27 19:28:05 +0200 | [diff] [blame] | 801 | Py_FatalError("Could not allocate TLS entry"); |
| 802 | |
Charles-François Natali | a233df8 | 2011-11-22 19:49:51 +0100 | [diff] [blame] | 803 | /* If the thread had an associated auto thread state, reassociate it with |
| 804 | * the new key. */ |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 805 | if (tstate && PyThread_set_key_value(_PyRuntime.gilstate.autoTLSkey, |
| 806 | (void *)tstate) < 0) |
Antoine Pitrou | 0c759fe | 2011-04-27 19:28:05 +0200 | [diff] [blame] | 807 | Py_FatalError("Couldn't create autoTLSkey mapping"); |
| 808 | } |
| 809 | |
Michael W. Hudson | 188d436 | 2005-06-20 16:52:57 +0000 | [diff] [blame] | 810 | /* When a thread state is created for a thread by some mechanism other than |
| 811 | PyGILState_Ensure, it's important that the GILState machinery knows about |
| 812 | it so it doesn't try to create another thread state for the thread (this is |
| 813 | a better fix for SF bug #1010677 than the first one attempted). |
| 814 | */ |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 815 | static void |
Michael W. Hudson | 188d436 | 2005-06-20 16:52:57 +0000 | [diff] [blame] | 816 | _PyGILState_NoteThreadState(PyThreadState* tstate) |
| 817 | { |
Antoine Pitrou | 079ce54 | 2010-09-08 12:37:10 +0000 | [diff] [blame] | 818 | /* If autoTLSkey isn't initialized, this must be the very first |
| 819 | threadstate created in Py_Initialize(). Don't do anything for now |
| 820 | (we'll be back here when _PyGILState_Init is called). */ |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 821 | if (!_PyRuntime.gilstate.autoInterpreterState) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 822 | return; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 823 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 824 | /* Stick the thread state for this thread in thread local storage. |
Michael W. Hudson | 188d436 | 2005-06-20 16:52:57 +0000 | [diff] [blame] | 825 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 826 | The only situation where you can legitimately have more than one |
| 827 | thread state for an OS level thread is when there are multiple |
Victor Stinner | 590cebe | 2013-12-13 11:08:56 +0100 | [diff] [blame] | 828 | interpreters. |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 829 | |
Victor Stinner | 590cebe | 2013-12-13 11:08:56 +0100 | [diff] [blame] | 830 | You shouldn't really be using the PyGILState_ APIs anyway (see issues |
| 831 | #10915 and #15751). |
Michael W. Hudson | 188d436 | 2005-06-20 16:52:57 +0000 | [diff] [blame] | 832 | |
Victor Stinner | 590cebe | 2013-12-13 11:08:56 +0100 | [diff] [blame] | 833 | The first thread state created for that given OS level thread will |
| 834 | "win", which seems reasonable behaviour. |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 835 | */ |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 836 | if (PyThread_get_key_value(_PyRuntime.gilstate.autoTLSkey) == NULL) { |
| 837 | if ((PyThread_set_key_value(_PyRuntime.gilstate.autoTLSkey, |
| 838 | (void *)tstate) |
| 839 | ) < 0) |
| 840 | { |
Victor Stinner | 590cebe | 2013-12-13 11:08:56 +0100 | [diff] [blame] | 841 | Py_FatalError("Couldn't create autoTLSkey mapping"); |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 842 | } |
Victor Stinner | 590cebe | 2013-12-13 11:08:56 +0100 | [diff] [blame] | 843 | } |
Michael W. Hudson | 188d436 | 2005-06-20 16:52:57 +0000 | [diff] [blame] | 844 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 845 | /* PyGILState_Release must not try to delete this thread state. */ |
| 846 | tstate->gilstate_counter = 1; |
Michael W. Hudson | 188d436 | 2005-06-20 16:52:57 +0000 | [diff] [blame] | 847 | } |
| 848 | |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 849 | /* The public functions */ |
Tim Peters | 19717fa | 2004-10-09 17:38:29 +0000 | [diff] [blame] | 850 | PyThreadState * |
| 851 | PyGILState_GetThisThreadState(void) |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 852 | { |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 853 | if (_PyRuntime.gilstate.autoInterpreterState == NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 854 | return NULL; |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 855 | return (PyThreadState *)PyThread_get_key_value( |
| 856 | _PyRuntime.gilstate.autoTLSkey); |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 857 | } |
| 858 | |
Kristján Valur Jónsson | 684cd0e | 2013-03-23 03:36:16 -0700 | [diff] [blame] | 859 | int |
| 860 | PyGILState_Check(void) |
| 861 | { |
Victor Stinner | 8a1be61 | 2016-03-14 22:07:55 +0100 | [diff] [blame] | 862 | PyThreadState *tstate; |
| 863 | |
| 864 | if (!_PyGILState_check_enabled) |
| 865 | return 1; |
| 866 | |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 867 | if (_PyRuntime.gilstate.autoTLSkey == -1) |
Victor Stinner | 8a1be61 | 2016-03-14 22:07:55 +0100 | [diff] [blame] | 868 | return 1; |
| 869 | |
| 870 | tstate = GET_TSTATE(); |
| 871 | if (tstate == NULL) |
| 872 | return 0; |
| 873 | |
| 874 | return (tstate == PyGILState_GetThisThreadState()); |
Kristján Valur Jónsson | 684cd0e | 2013-03-23 03:36:16 -0700 | [diff] [blame] | 875 | } |
| 876 | |
Tim Peters | 19717fa | 2004-10-09 17:38:29 +0000 | [diff] [blame] | 877 | PyGILState_STATE |
| 878 | PyGILState_Ensure(void) |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 879 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 880 | int current; |
| 881 | PyThreadState *tcur; |
| 882 | /* Note that we do not auto-init Python here - apart from |
| 883 | potential races with 2 threads auto-initializing, pep-311 |
| 884 | spells out other issues. Embedders are expected to have |
| 885 | called Py_Initialize() and usually PyEval_InitThreads(). |
| 886 | */ |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 887 | /* Py_Initialize() hasn't been called! */ |
| 888 | assert(_PyRuntime.gilstate.autoInterpreterState); |
| 889 | tcur = (PyThreadState *)PyThread_get_key_value( |
| 890 | _PyRuntime.gilstate.autoTLSkey); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 891 | if (tcur == NULL) { |
Victor Stinner | 62ca100 | 2013-12-13 01:46:43 +0100 | [diff] [blame] | 892 | /* At startup, Python has no concrete GIL. If PyGILState_Ensure() is |
| 893 | called from a new thread for the first time, we need the create the |
| 894 | GIL. */ |
| 895 | PyEval_InitThreads(); |
| 896 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 897 | /* Create a new thread state for this thread */ |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 898 | tcur = PyThreadState_New(_PyRuntime.gilstate.autoInterpreterState); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 899 | if (tcur == NULL) |
| 900 | Py_FatalError("Couldn't create thread-state for new thread"); |
| 901 | /* This is our thread state! We'll need to delete it in the |
| 902 | matching call to PyGILState_Release(). */ |
| 903 | tcur->gilstate_counter = 0; |
| 904 | current = 0; /* new thread state is never current */ |
| 905 | } |
| 906 | else |
| 907 | current = PyThreadState_IsCurrent(tcur); |
| 908 | if (current == 0) |
| 909 | PyEval_RestoreThread(tcur); |
| 910 | /* Update our counter in the thread-state - no need for locks: |
| 911 | - tcur will remain valid as we hold the GIL. |
| 912 | - the counter is safe as we are the only thread "allowed" |
| 913 | to modify this value |
| 914 | */ |
| 915 | ++tcur->gilstate_counter; |
| 916 | return current ? PyGILState_LOCKED : PyGILState_UNLOCKED; |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 917 | } |
| 918 | |
Tim Peters | 19717fa | 2004-10-09 17:38:29 +0000 | [diff] [blame] | 919 | void |
| 920 | PyGILState_Release(PyGILState_STATE oldstate) |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 921 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 922 | PyThreadState *tcur = (PyThreadState *)PyThread_get_key_value( |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 923 | _PyRuntime.gilstate.autoTLSkey); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 924 | if (tcur == NULL) |
| 925 | Py_FatalError("auto-releasing thread-state, " |
| 926 | "but no thread-state for this thread"); |
| 927 | /* We must hold the GIL and have our thread state current */ |
| 928 | /* XXX - remove the check - the assert should be fine, |
| 929 | but while this is very new (April 2003), the extra check |
| 930 | by release-only users can't hurt. |
| 931 | */ |
| 932 | if (! PyThreadState_IsCurrent(tcur)) |
| 933 | Py_FatalError("This thread state must be current when releasing"); |
| 934 | assert(PyThreadState_IsCurrent(tcur)); |
| 935 | --tcur->gilstate_counter; |
| 936 | assert(tcur->gilstate_counter >= 0); /* illegal counter value */ |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 937 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 938 | /* If we're going to destroy this thread-state, we must |
| 939 | * clear it while the GIL is held, as destructors may run. |
| 940 | */ |
| 941 | if (tcur->gilstate_counter == 0) { |
| 942 | /* can't have been locked when we created it */ |
| 943 | assert(oldstate == PyGILState_UNLOCKED); |
| 944 | PyThreadState_Clear(tcur); |
| 945 | /* Delete the thread-state. Note this releases the GIL too! |
| 946 | * It's vital that the GIL be held here, to avoid shutdown |
| 947 | * races; see bugs 225673 and 1061968 (that nasty bug has a |
| 948 | * habit of coming back). |
| 949 | */ |
| 950 | PyThreadState_DeleteCurrent(); |
| 951 | } |
| 952 | /* Release the lock if necessary */ |
| 953 | else if (oldstate == PyGILState_UNLOCKED) |
| 954 | PyEval_SaveThread(); |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 955 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 956 | |
Benjamin Peterson | 3bf0175 | 2012-04-13 18:06:36 -0400 | [diff] [blame] | 957 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 958 | #ifdef __cplusplus |
| 959 | } |
| 960 | #endif |