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