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