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