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