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