blob: 4ac05d6625686e688e87a943f722a7feb55c07d7 [file] [log] [blame]
Guido van Rossuma027efa1997-05-05 20:56:21 +00001
2/* Thread and interpreter state structures and their interfaces */
3
4#include "Python.h"
5
Tim Peters84705582004-10-10 02:47:33 +00006/* --------------------------------------------------------------------------
7CAUTION
8
Victor Stinner1a7425f2013-07-07 16:25:15 +02009Always use PyMem_RawMalloc() and PyMem_RawFree() directly in this file. A
10number of these functions are advertised as safe to call when the GIL isn't
11held, and in a debug build Python redirects (e.g.) PyMem_NEW (etc) to Python's
12debugging obmalloc functions. Those aren't thread-safe (they rely on the GIL
13to avoid the expense of doing their own locking).
Tim Peters84705582004-10-10 02:47:33 +000014-------------------------------------------------------------------------- */
15
Martin v. Löwisf0473d52001-07-18 16:17:16 +000016#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 Peterson43162b82012-04-13 11:58:27 -040025#ifdef __cplusplus
26extern "C" {
27#endif
Martin v. Löwisf0473d52001-07-18 16:17:16 +000028
Guido van Rossum1d5ad901999-06-18 14:22:24 +000029#ifdef WITH_THREAD
30#include "pythread.h"
31static PyThread_type_lock head_mutex = NULL; /* Protects interp->tstate_head */
Moshe Zadka9fb6af92000-08-04 21:27:47 +000032#define HEAD_INIT() (void)(head_mutex || (head_mutex = PyThread_allocate_lock()))
Guido van Rossum1d5ad901999-06-18 14:22:24 +000033#define HEAD_LOCK() PyThread_acquire_lock(head_mutex, WAIT_LOCK)
34#define HEAD_UNLOCK() PyThread_release_lock(head_mutex)
Michael W. Hudson188d4362005-06-20 16:52:57 +000035
36/* The single PyInterpreterState used by this process'
37 GILState implementation
38*/
39static PyInterpreterState *autoInterpreterState = NULL;
40static int autoTLSkey = 0;
Guido van Rossum1d5ad901999-06-18 14:22:24 +000041#else
42#define HEAD_INIT() /* Nothing */
43#define HEAD_LOCK() /* Nothing */
44#define HEAD_UNLOCK() /* Nothing */
45#endif
46
Guido van Rossum25ce5661997-08-02 03:10:38 +000047static PyInterpreterState *interp_head = NULL;
Guido van Rossuma027efa1997-05-05 20:56:21 +000048
Jeffrey Yasskin39370832010-05-03 19:29:34 +000049/* 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 Rossum6297a7a2003-02-19 15:53:17 +000052PyThreadFrameGetter _PyThreadState_GetFrame = NULL;
Guido van Rossuma027efa1997-05-05 20:56:21 +000053
Michael W. Hudsonce7da6c2005-09-30 08:20:24 +000054#ifdef WITH_THREAD
Michael W. Hudson188d4362005-06-20 16:52:57 +000055static void _PyGILState_NoteThreadState(PyThreadState* tstate);
Michael W. Hudsonce7da6c2005-09-30 08:20:24 +000056#endif
Michael W. Hudson188d4362005-06-20 16:52:57 +000057
Guido van Rossuma027efa1997-05-05 20:56:21 +000058
59PyInterpreterState *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000060PyInterpreterState_New(void)
Guido van Rossuma027efa1997-05-05 20:56:21 +000061{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000062 PyInterpreterState *interp = (PyInterpreterState *)
Victor Stinner1a7425f2013-07-07 16:25:15 +020063 PyMem_RawMalloc(sizeof(PyInterpreterState));
Guido van Rossum25ce5661997-08-02 03:10:38 +000064
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000065 if (interp != NULL) {
66 HEAD_INIT();
Thomas Wouters0e3f5912006-08-11 14:57:12 +000067#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000068 if (head_mutex == NULL)
69 Py_FatalError("Can't initialize threads for interpreter");
Thomas Wouters0e3f5912006-08-11 14:57:12 +000070#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000071 interp->modules = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000072 interp->modules_by_index = NULL;
73 interp->sysdict = NULL;
74 interp->builtins = NULL;
Serhiy Storchaka87a5c512014-02-10 18:21:34 +020075 interp->builtins_copy = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000076 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 Stinner3cbf14b2011-04-27 00:24:21 +020081 interp->fscodec_initialized = 0;
Brett Cannonfd074152012-04-14 14:10:13 -040082 interp->importlib = NULL;
Martin v. Löwisf0473d52001-07-18 16:17:16 +000083#ifdef HAVE_DLOPEN
84#ifdef RTLD_NOW
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000085 interp->dlopenflags = RTLD_NOW;
Martin v. Löwisf0473d52001-07-18 16:17:16 +000086#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000087 interp->dlopenflags = RTLD_LAZY;
Martin v. Löwisf0473d52001-07-18 16:17:16 +000088#endif
89#endif
Martin v. Löwisf30d60e2004-06-08 08:17:44 +000090#ifdef WITH_TSC
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000091 interp->tscdump = 0;
Martin v. Löwisf30d60e2004-06-08 08:17:44 +000092#endif
Guido van Rossum25ce5661997-08-02 03:10:38 +000093
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000094 HEAD_LOCK();
95 interp->next = interp_head;
96 interp_head = interp;
97 HEAD_UNLOCK();
98 }
Guido van Rossum25ce5661997-08-02 03:10:38 +000099
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000100 return interp;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000101}
102
103
104void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000105PyInterpreterState_Clear(PyInterpreterState *interp)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000106{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000107 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);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000117 Py_CLEAR(interp->sysdict);
118 Py_CLEAR(interp->builtins);
Serhiy Storchaka87a5c512014-02-10 18:21:34 +0200119 Py_CLEAR(interp->builtins_copy);
Brett Cannonfd074152012-04-14 14:10:13 -0400120 Py_CLEAR(interp->importlib);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000121}
122
123
124static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000125zapthreads(PyInterpreterState *interp)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000126{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000127 PyThreadState *p;
128 /* No need to lock the mutex here because this should only happen
129 when the threads are all really dead (XXX famous last words). */
130 while ((p = interp->tstate_head) != NULL) {
131 PyThreadState_Delete(p);
132 }
Guido van Rossum25ce5661997-08-02 03:10:38 +0000133}
134
135
136void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000137PyInterpreterState_Delete(PyInterpreterState *interp)
Guido van Rossuma027efa1997-05-05 20:56:21 +0000138{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000139 PyInterpreterState **p;
140 zapthreads(interp);
141 HEAD_LOCK();
142 for (p = &interp_head; ; p = &(*p)->next) {
143 if (*p == NULL)
144 Py_FatalError(
145 "PyInterpreterState_Delete: invalid interp");
146 if (*p == interp)
147 break;
148 }
149 if (interp->tstate_head != NULL)
150 Py_FatalError("PyInterpreterState_Delete: remaining threads");
151 *p = interp->next;
152 HEAD_UNLOCK();
Victor Stinner1a7425f2013-07-07 16:25:15 +0200153 PyMem_RawFree(interp);
Antoine Pitrou8db076c2011-10-30 19:13:55 +0100154#ifdef WITH_THREAD
155 if (interp_head == NULL && head_mutex != NULL) {
156 PyThread_free_lock(head_mutex);
157 head_mutex = NULL;
158 }
159#endif
Guido van Rossuma027efa1997-05-05 20:56:21 +0000160}
161
162
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000163/* Default implementation for _PyThreadState_GetFrame */
164static struct _frame *
165threadstate_getframe(PyThreadState *self)
166{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000167 return self->frame;
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000168}
169
Victor Stinner45b9be52010-03-03 23:28:07 +0000170static PyThreadState *
171new_threadstate(PyInterpreterState *interp, int init)
Guido van Rossuma027efa1997-05-05 20:56:21 +0000172{
Victor Stinner1a7425f2013-07-07 16:25:15 +0200173 PyThreadState *tstate = (PyThreadState *)PyMem_RawMalloc(sizeof(PyThreadState));
Tim Peters84705582004-10-10 02:47:33 +0000174
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000175 if (_PyThreadState_GetFrame == NULL)
176 _PyThreadState_GetFrame = threadstate_getframe;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000177
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000178 if (tstate != NULL) {
179 tstate->interp = interp;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000180
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000181 tstate->frame = NULL;
182 tstate->recursion_depth = 0;
183 tstate->overflowed = 0;
184 tstate->recursion_critical = 0;
185 tstate->tracing = 0;
186 tstate->use_tracing = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000187 tstate->gilstate_counter = 0;
188 tstate->async_exc = NULL;
Martin v. Löwisf9ce67d2003-07-13 10:41:53 +0000189#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000190 tstate->thread_id = PyThread_get_thread_ident();
Martin v. Löwisf9ce67d2003-07-13 10:41:53 +0000191#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000192 tstate->thread_id = 0;
Martin v. Löwisf9ce67d2003-07-13 10:41:53 +0000193#endif
Guido van Rossuma027efa1997-05-05 20:56:21 +0000194
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000195 tstate->dict = NULL;
Guido van Rossumede04391998-04-10 20:18:25 +0000196
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000197 tstate->curexc_type = NULL;
198 tstate->curexc_value = NULL;
199 tstate->curexc_traceback = NULL;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000200
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000201 tstate->exc_type = NULL;
202 tstate->exc_value = NULL;
203 tstate->exc_traceback = NULL;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000204
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000205 tstate->c_profilefunc = NULL;
206 tstate->c_tracefunc = NULL;
207 tstate->c_profileobj = NULL;
208 tstate->c_traceobj = NULL;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000209
Antoine Pitrou2b0218a2012-09-06 00:59:49 +0200210 tstate->trash_delete_nesting = 0;
211 tstate->trash_delete_later = NULL;
Antoine Pitrou7b476992013-09-07 23:38:37 +0200212 tstate->on_delete = NULL;
213 tstate->on_delete_data = NULL;
Antoine Pitrou2b0218a2012-09-06 00:59:49 +0200214
Yury Selivanov75445082015-05-11 22:57:16 -0400215 tstate->coroutine_wrapper = NULL;
216
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000217 if (init)
218 _PyThreadState_Init(tstate);
Michael W. Hudson188d4362005-06-20 16:52:57 +0000219
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000220 HEAD_LOCK();
Charles-Francois Natalif28dfdd2013-05-08 21:09:52 +0200221 tstate->prev = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000222 tstate->next = interp->tstate_head;
Charles-Francois Natalif28dfdd2013-05-08 21:09:52 +0200223 if (tstate->next)
224 tstate->next->prev = tstate;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000225 interp->tstate_head = tstate;
226 HEAD_UNLOCK();
227 }
Guido van Rossum25ce5661997-08-02 03:10:38 +0000228
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000229 return tstate;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000230}
231
Victor Stinner45b9be52010-03-03 23:28:07 +0000232PyThreadState *
233PyThreadState_New(PyInterpreterState *interp)
234{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000235 return new_threadstate(interp, 1);
Victor Stinner45b9be52010-03-03 23:28:07 +0000236}
237
238PyThreadState *
239_PyThreadState_Prealloc(PyInterpreterState *interp)
240{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000241 return new_threadstate(interp, 0);
Victor Stinner45b9be52010-03-03 23:28:07 +0000242}
243
244void
245_PyThreadState_Init(PyThreadState *tstate)
246{
247#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000248 _PyGILState_NoteThreadState(tstate);
Victor Stinner45b9be52010-03-03 23:28:07 +0000249#endif
250}
251
Martin v. Löwis1a214512008-06-11 05:26:20 +0000252PyObject*
Martin v. Löwis7800f752012-06-22 12:20:55 +0200253PyState_FindModule(struct PyModuleDef* module)
Martin v. Löwis1a214512008-06-11 05:26:20 +0000254{
Martin v. Löwis7800f752012-06-22 12:20:55 +0200255 Py_ssize_t index = module->m_base.m_index;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000256 PyInterpreterState *state = PyThreadState_GET()->interp;
257 PyObject *res;
Nick Coghland5cacbb2015-05-23 22:24:10 +1000258 if (module->m_slots) {
259 return NULL;
260 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000261 if (index == 0)
262 return NULL;
263 if (state->modules_by_index == NULL)
264 return NULL;
Antoine Pitrou75506e82012-08-20 19:30:46 +0200265 if (index >= PyList_GET_SIZE(state->modules_by_index))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000266 return NULL;
267 res = PyList_GET_ITEM(state->modules_by_index, index);
268 return res==Py_None ? NULL : res;
Martin v. Löwis1a214512008-06-11 05:26:20 +0000269}
270
271int
272_PyState_AddModule(PyObject* module, struct PyModuleDef* def)
273{
Nick Coghland5cacbb2015-05-23 22:24:10 +1000274 PyInterpreterState *state;
275 if (def->m_slots) {
276 PyErr_SetString(PyExc_SystemError,
277 "PyState_AddModule called on module with slots");
278 return -1;
279 }
280 state = PyThreadState_GET()->interp;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000281 if (!def)
282 return -1;
283 if (!state->modules_by_index) {
284 state->modules_by_index = PyList_New(0);
285 if (!state->modules_by_index)
286 return -1;
287 }
288 while(PyList_GET_SIZE(state->modules_by_index) <= def->m_base.m_index)
289 if (PyList_Append(state->modules_by_index, Py_None) < 0)
290 return -1;
291 Py_INCREF(module);
292 return PyList_SetItem(state->modules_by_index,
293 def->m_base.m_index, module);
Martin v. Löwis1a214512008-06-11 05:26:20 +0000294}
Guido van Rossuma027efa1997-05-05 20:56:21 +0000295
Martin v. Löwis7800f752012-06-22 12:20:55 +0200296int
297PyState_AddModule(PyObject* module, struct PyModuleDef* def)
298{
299 Py_ssize_t index;
300 PyInterpreterState *state = PyThreadState_GET()->interp;
301 if (!def) {
302 Py_FatalError("PyState_AddModule: Module Definition is NULL");
303 return -1;
304 }
305 index = def->m_base.m_index;
306 if (state->modules_by_index) {
307 if(PyList_GET_SIZE(state->modules_by_index) >= index) {
308 if(module == PyList_GET_ITEM(state->modules_by_index, index)) {
309 Py_FatalError("PyState_AddModule: Module already added!");
310 return -1;
311 }
312 }
313 }
314 return _PyState_AddModule(module, def);
315}
316
317int
318PyState_RemoveModule(struct PyModuleDef* def)
319{
Nick Coghland5cacbb2015-05-23 22:24:10 +1000320 PyInterpreterState *state;
Martin v. Löwis7800f752012-06-22 12:20:55 +0200321 Py_ssize_t index = def->m_base.m_index;
Nick Coghland5cacbb2015-05-23 22:24:10 +1000322 if (def->m_slots) {
323 PyErr_SetString(PyExc_SystemError,
324 "PyState_RemoveModule called on module with slots");
325 return -1;
326 }
327 state = PyThreadState_GET()->interp;
Martin v. Löwis7800f752012-06-22 12:20:55 +0200328 if (index == 0) {
329 Py_FatalError("PyState_RemoveModule: Module index invalid.");
330 return -1;
331 }
332 if (state->modules_by_index == NULL) {
333 Py_FatalError("PyState_RemoveModule: Interpreters module-list not acessible.");
334 return -1;
335 }
336 if (index > PyList_GET_SIZE(state->modules_by_index)) {
337 Py_FatalError("PyState_RemoveModule: Module index out of bounds.");
338 return -1;
339 }
340 return PyList_SetItem(state->modules_by_index, index, Py_None);
341}
342
Antoine Pitrou40322e62013-08-11 00:30:09 +0200343/* used by import.c:PyImport_Cleanup */
344void
345_PyState_ClearModules(void)
346{
347 PyInterpreterState *state = PyThreadState_GET()->interp;
348 if (state->modules_by_index) {
349 Py_ssize_t i;
350 for (i = 0; i < PyList_GET_SIZE(state->modules_by_index); i++) {
351 PyObject *m = PyList_GET_ITEM(state->modules_by_index, i);
352 if (PyModule_Check(m)) {
353 /* cleanup the saved copy of module dicts */
354 PyModuleDef *md = PyModule_GetDef(m);
355 if (md)
356 Py_CLEAR(md->m_base.m_copy);
357 }
358 }
359 /* Setting modules_by_index to NULL could be dangerous, so we
360 clear the list instead. */
361 if (PyList_SetSlice(state->modules_by_index,
362 0, PyList_GET_SIZE(state->modules_by_index),
363 NULL))
364 PyErr_WriteUnraisable(state->modules_by_index);
365 }
366}
367
Guido van Rossuma027efa1997-05-05 20:56:21 +0000368void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000369PyThreadState_Clear(PyThreadState *tstate)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000370{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000371 if (Py_VerboseFlag && tstate->frame != NULL)
372 fprintf(stderr,
373 "PyThreadState_Clear: warning: thread still has a frame\n");
Guido van Rossum25ce5661997-08-02 03:10:38 +0000374
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000375 Py_CLEAR(tstate->frame);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000376
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000377 Py_CLEAR(tstate->dict);
378 Py_CLEAR(tstate->async_exc);
Guido van Rossumede04391998-04-10 20:18:25 +0000379
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000380 Py_CLEAR(tstate->curexc_type);
381 Py_CLEAR(tstate->curexc_value);
382 Py_CLEAR(tstate->curexc_traceback);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000383
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000384 Py_CLEAR(tstate->exc_type);
385 Py_CLEAR(tstate->exc_value);
386 Py_CLEAR(tstate->exc_traceback);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000387
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000388 tstate->c_profilefunc = NULL;
389 tstate->c_tracefunc = NULL;
390 Py_CLEAR(tstate->c_profileobj);
391 Py_CLEAR(tstate->c_traceobj);
Yury Selivanov75445082015-05-11 22:57:16 -0400392
393 Py_CLEAR(tstate->coroutine_wrapper);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000394}
395
396
Guido van Rossum29757862001-01-23 01:46:06 +0000397/* Common code for PyThreadState_Delete() and PyThreadState_DeleteCurrent() */
398static void
399tstate_delete_common(PyThreadState *tstate)
Guido van Rossuma027efa1997-05-05 20:56:21 +0000400{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000401 PyInterpreterState *interp;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000402 if (tstate == NULL)
403 Py_FatalError("PyThreadState_Delete: NULL tstate");
404 interp = tstate->interp;
405 if (interp == NULL)
406 Py_FatalError("PyThreadState_Delete: NULL interp");
407 HEAD_LOCK();
Charles-Francois Natalif28dfdd2013-05-08 21:09:52 +0200408 if (tstate->prev)
409 tstate->prev->next = tstate->next;
410 else
411 interp->tstate_head = tstate->next;
412 if (tstate->next)
413 tstate->next->prev = tstate->prev;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000414 HEAD_UNLOCK();
Antoine Pitrou7b476992013-09-07 23:38:37 +0200415 if (tstate->on_delete != NULL) {
416 tstate->on_delete(tstate->on_delete_data);
417 }
Victor Stinner1a7425f2013-07-07 16:25:15 +0200418 PyMem_RawFree(tstate);
Guido van Rossuma027efa1997-05-05 20:56:21 +0000419}
420
421
Guido van Rossum29757862001-01-23 01:46:06 +0000422void
423PyThreadState_Delete(PyThreadState *tstate)
424{
Serhiy Storchaka53fa8b22015-02-16 09:40:12 +0200425 if (tstate == (PyThreadState*)_Py_atomic_load_relaxed(&_PyThreadState_Current))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000426 Py_FatalError("PyThreadState_Delete: tstate is still current");
Tim Petersf4e69282006-02-27 17:15:31 +0000427#ifdef WITH_THREAD
Antoine Pitrou079ce542010-09-08 12:37:10 +0000428 if (autoInterpreterState && PyThread_get_key_value(autoTLSkey) == tstate)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000429 PyThread_delete_key_value(autoTLSkey);
Tim Petersf4e69282006-02-27 17:15:31 +0000430#endif /* WITH_THREAD */
Christian Heimesb9dbc7d2013-07-01 23:42:28 +0200431 tstate_delete_common(tstate);
Guido van Rossum29757862001-01-23 01:46:06 +0000432}
433
434
435#ifdef WITH_THREAD
436void
437PyThreadState_DeleteCurrent()
438{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000439 PyThreadState *tstate = (PyThreadState*)_Py_atomic_load_relaxed(
440 &_PyThreadState_Current);
441 if (tstate == NULL)
442 Py_FatalError(
443 "PyThreadState_DeleteCurrent: no current tstate");
444 _Py_atomic_store_relaxed(&_PyThreadState_Current, NULL);
Antoine Pitrou079ce542010-09-08 12:37:10 +0000445 if (autoInterpreterState && PyThread_get_key_value(autoTLSkey) == tstate)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000446 PyThread_delete_key_value(autoTLSkey);
Christian Heimesb9dbc7d2013-07-01 23:42:28 +0200447 tstate_delete_common(tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000448 PyEval_ReleaseLock();
Guido van Rossum29757862001-01-23 01:46:06 +0000449}
450#endif /* WITH_THREAD */
451
452
Antoine Pitrou8408cea2013-05-05 23:47:09 +0200453/*
454 * Delete all thread states except the one passed as argument.
455 * Note that, if there is a current thread state, it *must* be the one
456 * passed as argument. Also, this won't touch any other interpreters
457 * than the current one, since we don't know which thread state should
458 * be kept in those other interpreteres.
459 */
460void
461_PyThreadState_DeleteExcept(PyThreadState *tstate)
462{
463 PyInterpreterState *interp = tstate->interp;
464 PyThreadState *p, *next, *garbage;
465 HEAD_LOCK();
466 /* Remove all thread states, except tstate, from the linked list of
467 thread states. This will allow calling PyThreadState_Clear()
Charles-Francois Natalif28dfdd2013-05-08 21:09:52 +0200468 without holding the lock. */
Antoine Pitrou8408cea2013-05-05 23:47:09 +0200469 garbage = interp->tstate_head;
Charles-Francois Natalif28dfdd2013-05-08 21:09:52 +0200470 if (garbage == tstate)
471 garbage = tstate->next;
472 if (tstate->prev)
473 tstate->prev->next = tstate->next;
474 if (tstate->next)
475 tstate->next->prev = tstate->prev;
476 tstate->prev = tstate->next = NULL;
Antoine Pitrou8408cea2013-05-05 23:47:09 +0200477 interp->tstate_head = tstate;
Antoine Pitrou8408cea2013-05-05 23:47:09 +0200478 HEAD_UNLOCK();
479 /* Clear and deallocate all stale thread states. Even if this
480 executes Python code, we should be safe since it executes
481 in the current thread, not one of the stale threads. */
482 for (p = garbage; p; p = next) {
483 next = p->next;
484 PyThreadState_Clear(p);
Victor Stinner1a7425f2013-07-07 16:25:15 +0200485 PyMem_RawFree(p);
Antoine Pitrou8408cea2013-05-05 23:47:09 +0200486 }
487}
488
489
Guido van Rossuma027efa1997-05-05 20:56:21 +0000490PyThreadState *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000491PyThreadState_Get(void)
Guido van Rossuma027efa1997-05-05 20:56:21 +0000492{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000493 PyThreadState *tstate = (PyThreadState*)_Py_atomic_load_relaxed(
494 &_PyThreadState_Current);
495 if (tstate == NULL)
496 Py_FatalError("PyThreadState_Get: no current thread");
Guido van Rossum25ce5661997-08-02 03:10:38 +0000497
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000498 return tstate;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000499}
500
501
502PyThreadState *
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000503PyThreadState_Swap(PyThreadState *newts)
Guido van Rossuma027efa1997-05-05 20:56:21 +0000504{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000505 PyThreadState *oldts = (PyThreadState*)_Py_atomic_load_relaxed(
506 &_PyThreadState_Current);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000507
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000508 _Py_atomic_store_relaxed(&_PyThreadState_Current, newts);
509 /* It should not be possible for more than one thread state
510 to be used for a thread. Check this the best we can in debug
511 builds.
512 */
Martin v. Löwis9e296252003-05-01 05:25:29 +0000513#if defined(Py_DEBUG) && defined(WITH_THREAD)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000514 if (newts) {
515 /* This can be called from PyEval_RestoreThread(). Similar
516 to it, we need to ensure errno doesn't change.
517 */
518 int err = errno;
519 PyThreadState *check = PyGILState_GetThisThreadState();
520 if (check && check->interp == newts->interp && check != newts)
521 Py_FatalError("Invalid thread state for this thread");
522 errno = err;
523 }
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000524#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000525 return oldts;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000526}
Guido van Rossumede04391998-04-10 20:18:25 +0000527
528/* An extension mechanism to store arbitrary additional per-thread state.
529 PyThreadState_GetDict() returns a dictionary that can be used to hold such
530 state; the caller should pick a unique key and store its state there. If
Guido van Rossum0fc8f002003-04-15 15:12:39 +0000531 PyThreadState_GetDict() returns NULL, an exception has *not* been raised
532 and the caller should assume no per-thread state is available. */
Guido van Rossumede04391998-04-10 20:18:25 +0000533
534PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000535PyThreadState_GetDict(void)
Guido van Rossumede04391998-04-10 20:18:25 +0000536{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000537 PyThreadState *tstate = (PyThreadState*)_Py_atomic_load_relaxed(
538 &_PyThreadState_Current);
539 if (tstate == NULL)
540 return NULL;
Guido van Rossumede04391998-04-10 20:18:25 +0000541
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000542 if (tstate->dict == NULL) {
543 PyObject *d;
544 tstate->dict = d = PyDict_New();
545 if (d == NULL)
546 PyErr_Clear();
547 }
548 return tstate->dict;
Guido van Rossumede04391998-04-10 20:18:25 +0000549}
Guido van Rossumf5df46d2001-07-19 12:19:27 +0000550
551
Guido van Rossumb8b6d0c2003-06-28 21:53:52 +0000552/* Asynchronously raise an exception in a thread.
553 Requested by Just van Rossum and Alex Martelli.
Guido van Rossum0f1f63c2005-02-08 02:07:57 +0000554 To prevent naive misuse, you must write your own extension
Thomas Wouters00ee7ba2006-08-21 19:07:27 +0000555 to call this, or use ctypes. Must be called with the GIL held.
556 Returns the number of tstates modified (normally 1, but 0 if `id` didn't
557 match any known thread id). Can be called with exc=NULL to clear an
558 existing async exception. This raises no exceptions. */
Guido van Rossumb8b6d0c2003-06-28 21:53:52 +0000559
560int
561PyThreadState_SetAsyncExc(long id, PyObject *exc) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000562 PyThreadState *tstate = PyThreadState_GET();
563 PyInterpreterState *interp = tstate->interp;
564 PyThreadState *p;
Thomas Wouters00ee7ba2006-08-21 19:07:27 +0000565
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000566 /* Although the GIL is held, a few C API functions can be called
567 * without the GIL held, and in particular some that create and
568 * destroy thread and interpreter states. Those can mutate the
569 * list of thread states we're traversing, so to prevent that we lock
570 * head_mutex for the duration.
571 */
572 HEAD_LOCK();
573 for (p = interp->tstate_head; p != NULL; p = p->next) {
574 if (p->thread_id == id) {
575 /* Tricky: we need to decref the current value
576 * (if any) in p->async_exc, but that can in turn
577 * allow arbitrary Python code to run, including
578 * perhaps calls to this function. To prevent
579 * deadlock, we need to release head_mutex before
580 * the decref.
581 */
582 PyObject *old_exc = p->async_exc;
583 Py_XINCREF(exc);
584 p->async_exc = exc;
585 HEAD_UNLOCK();
586 Py_XDECREF(old_exc);
587 _PyEval_SignalAsyncExc();
588 return 1;
589 }
590 }
591 HEAD_UNLOCK();
592 return 0;
Guido van Rossumb8b6d0c2003-06-28 21:53:52 +0000593}
594
595
Guido van Rossumf5df46d2001-07-19 12:19:27 +0000596/* Routines for advanced debuggers, requested by David Beazley.
597 Don't use unless you know what you are doing! */
598
599PyInterpreterState *
600PyInterpreterState_Head(void)
601{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000602 return interp_head;
Guido van Rossumf5df46d2001-07-19 12:19:27 +0000603}
604
605PyInterpreterState *
606PyInterpreterState_Next(PyInterpreterState *interp) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000607 return interp->next;
Guido van Rossumf5df46d2001-07-19 12:19:27 +0000608}
609
610PyThreadState *
611PyInterpreterState_ThreadHead(PyInterpreterState *interp) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000612 return interp->tstate_head;
Guido van Rossumf5df46d2001-07-19 12:19:27 +0000613}
614
615PyThreadState *
616PyThreadState_Next(PyThreadState *tstate) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000617 return tstate->next;
Guido van Rossumf5df46d2001-07-19 12:19:27 +0000618}
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000619
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000620/* The implementation of sys._current_frames(). This is intended to be
621 called with the GIL held, as it will be when called via
622 sys._current_frames(). It's possible it would work fine even without
623 the GIL held, but haven't thought enough about that.
624*/
625PyObject *
626_PyThread_CurrentFrames(void)
627{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000628 PyObject *result;
629 PyInterpreterState *i;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000630
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000631 result = PyDict_New();
632 if (result == NULL)
633 return NULL;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000634
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000635 /* for i in all interpreters:
636 * for t in all of i's thread states:
637 * if t's frame isn't NULL, map t's id to its frame
Ezio Melotti13925002011-03-16 11:05:33 +0200638 * Because these lists can mutate even when the GIL is held, we
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000639 * need to grab head_mutex for the duration.
640 */
641 HEAD_LOCK();
642 for (i = interp_head; i != NULL; i = i->next) {
643 PyThreadState *t;
644 for (t = i->tstate_head; t != NULL; t = t->next) {
645 PyObject *id;
646 int stat;
647 struct _frame *frame = t->frame;
648 if (frame == NULL)
649 continue;
650 id = PyLong_FromLong(t->thread_id);
651 if (id == NULL)
652 goto Fail;
653 stat = PyDict_SetItem(result, id, (PyObject *)frame);
654 Py_DECREF(id);
655 if (stat < 0)
656 goto Fail;
657 }
658 }
659 HEAD_UNLOCK();
660 return result;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000661
662 Fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000663 HEAD_UNLOCK();
664 Py_DECREF(result);
665 return NULL;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000666}
Guido van Rossumb8b6d0c2003-06-28 21:53:52 +0000667
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000668/* Python "auto thread state" API. */
669#ifdef WITH_THREAD
670
671/* Keep this as a static, as it is not reliable! It can only
672 ever be compared to the state for the *current* thread.
673 * If not equal, then it doesn't matter that the actual
674 value may change immediately after comparison, as it can't
675 possibly change to the current thread's state.
676 * If equal, then the current thread holds the lock, so the value can't
677 change until we yield the lock.
678*/
679static int
680PyThreadState_IsCurrent(PyThreadState *tstate)
681{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000682 /* Must be the tstate for this thread */
683 assert(PyGILState_GetThisThreadState()==tstate);
Serhiy Storchaka53fa8b22015-02-16 09:40:12 +0200684 return tstate == (PyThreadState*)_Py_atomic_load_relaxed(&_PyThreadState_Current);
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000685}
686
Tim Peters4c1f5ec2004-10-09 17:25:05 +0000687/* Internal initialization/finalization functions called by
688 Py_Initialize/Py_Finalize
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000689*/
Tim Peters19717fa2004-10-09 17:38:29 +0000690void
691_PyGILState_Init(PyInterpreterState *i, PyThreadState *t)
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000692{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000693 assert(i && t); /* must init with valid states */
694 autoTLSkey = PyThread_create_key();
Kristján Valur Jónsson2fea9b92010-09-20 02:11:49 +0000695 if (autoTLSkey == -1)
696 Py_FatalError("Could not allocate TLS entry");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000697 autoInterpreterState = i;
698 assert(PyThread_get_key_value(autoTLSkey) == NULL);
699 assert(t->gilstate_counter == 0);
Michael W. Hudson188d4362005-06-20 16:52:57 +0000700
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000701 _PyGILState_NoteThreadState(t);
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000702}
703
Tim Peters19717fa2004-10-09 17:38:29 +0000704void
705_PyGILState_Fini(void)
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000706{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000707 PyThread_delete_key(autoTLSkey);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000708 autoInterpreterState = NULL;
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000709}
710
Charles-François Natalia233df82011-11-22 19:49:51 +0100711/* Reset the TLS key - called by PyOS_AfterFork().
Antoine Pitrou0c759fe2011-04-27 19:28:05 +0200712 * This should not be necessary, but some - buggy - pthread implementations
Charles-François Natalia233df82011-11-22 19:49:51 +0100713 * don't reset TLS upon fork(), see issue #10517.
Antoine Pitrou0c759fe2011-04-27 19:28:05 +0200714 */
715void
716_PyGILState_Reinit(void)
717{
718 PyThreadState *tstate = PyGILState_GetThisThreadState();
719 PyThread_delete_key(autoTLSkey);
720 if ((autoTLSkey = PyThread_create_key()) == -1)
721 Py_FatalError("Could not allocate TLS entry");
722
Charles-François Natalia233df82011-11-22 19:49:51 +0100723 /* If the thread had an associated auto thread state, reassociate it with
724 * the new key. */
725 if (tstate && PyThread_set_key_value(autoTLSkey, (void *)tstate) < 0)
Antoine Pitrou0c759fe2011-04-27 19:28:05 +0200726 Py_FatalError("Couldn't create autoTLSkey mapping");
727}
728
Michael W. Hudson188d4362005-06-20 16:52:57 +0000729/* When a thread state is created for a thread by some mechanism other than
730 PyGILState_Ensure, it's important that the GILState machinery knows about
731 it so it doesn't try to create another thread state for the thread (this is
732 a better fix for SF bug #1010677 than the first one attempted).
733*/
Thomas Wouters89f507f2006-12-13 04:49:30 +0000734static void
Michael W. Hudson188d4362005-06-20 16:52:57 +0000735_PyGILState_NoteThreadState(PyThreadState* tstate)
736{
Antoine Pitrou079ce542010-09-08 12:37:10 +0000737 /* If autoTLSkey isn't initialized, this must be the very first
738 threadstate created in Py_Initialize(). Don't do anything for now
739 (we'll be back here when _PyGILState_Init is called). */
740 if (!autoInterpreterState)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000741 return;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000742
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000743 /* Stick the thread state for this thread in thread local storage.
Michael W. Hudson188d4362005-06-20 16:52:57 +0000744
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000745 The only situation where you can legitimately have more than one
746 thread state for an OS level thread is when there are multiple
Victor Stinner590cebe2013-12-13 11:08:56 +0100747 interpreters.
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000748
Victor Stinner590cebe2013-12-13 11:08:56 +0100749 You shouldn't really be using the PyGILState_ APIs anyway (see issues
750 #10915 and #15751).
Michael W. Hudson188d4362005-06-20 16:52:57 +0000751
Victor Stinner590cebe2013-12-13 11:08:56 +0100752 The first thread state created for that given OS level thread will
753 "win", which seems reasonable behaviour.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000754 */
Victor Stinner590cebe2013-12-13 11:08:56 +0100755 if (PyThread_get_key_value(autoTLSkey) == NULL) {
756 if (PyThread_set_key_value(autoTLSkey, (void *)tstate) < 0)
757 Py_FatalError("Couldn't create autoTLSkey mapping");
758 }
Michael W. Hudson188d4362005-06-20 16:52:57 +0000759
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000760 /* PyGILState_Release must not try to delete this thread state. */
761 tstate->gilstate_counter = 1;
Michael W. Hudson188d4362005-06-20 16:52:57 +0000762}
763
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000764/* The public functions */
Tim Peters19717fa2004-10-09 17:38:29 +0000765PyThreadState *
766PyGILState_GetThisThreadState(void)
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000767{
Antoine Pitrou079ce542010-09-08 12:37:10 +0000768 if (autoInterpreterState == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000769 return NULL;
770 return (PyThreadState *)PyThread_get_key_value(autoTLSkey);
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000771}
772
Kristján Valur Jónsson684cd0e2013-03-23 03:36:16 -0700773int
774PyGILState_Check(void)
775{
776 /* can't use PyThreadState_Get() since it will assert that it has the GIL */
777 PyThreadState *tstate = (PyThreadState*)_Py_atomic_load_relaxed(
778 &_PyThreadState_Current);
779 return tstate && (tstate == PyGILState_GetThisThreadState());
780}
781
Tim Peters19717fa2004-10-09 17:38:29 +0000782PyGILState_STATE
783PyGILState_Ensure(void)
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000784{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000785 int current;
786 PyThreadState *tcur;
787 /* Note that we do not auto-init Python here - apart from
788 potential races with 2 threads auto-initializing, pep-311
789 spells out other issues. Embedders are expected to have
790 called Py_Initialize() and usually PyEval_InitThreads().
791 */
792 assert(autoInterpreterState); /* Py_Initialize() hasn't been called! */
793 tcur = (PyThreadState *)PyThread_get_key_value(autoTLSkey);
794 if (tcur == NULL) {
Victor Stinner62ca1002013-12-13 01:46:43 +0100795 /* At startup, Python has no concrete GIL. If PyGILState_Ensure() is
796 called from a new thread for the first time, we need the create the
797 GIL. */
798 PyEval_InitThreads();
799
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000800 /* Create a new thread state for this thread */
801 tcur = PyThreadState_New(autoInterpreterState);
802 if (tcur == NULL)
803 Py_FatalError("Couldn't create thread-state for new thread");
804 /* This is our thread state! We'll need to delete it in the
805 matching call to PyGILState_Release(). */
806 tcur->gilstate_counter = 0;
807 current = 0; /* new thread state is never current */
808 }
809 else
810 current = PyThreadState_IsCurrent(tcur);
811 if (current == 0)
812 PyEval_RestoreThread(tcur);
813 /* Update our counter in the thread-state - no need for locks:
814 - tcur will remain valid as we hold the GIL.
815 - the counter is safe as we are the only thread "allowed"
816 to modify this value
817 */
818 ++tcur->gilstate_counter;
819 return current ? PyGILState_LOCKED : PyGILState_UNLOCKED;
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000820}
821
Tim Peters19717fa2004-10-09 17:38:29 +0000822void
823PyGILState_Release(PyGILState_STATE oldstate)
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000824{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000825 PyThreadState *tcur = (PyThreadState *)PyThread_get_key_value(
826 autoTLSkey);
827 if (tcur == NULL)
828 Py_FatalError("auto-releasing thread-state, "
829 "but no thread-state for this thread");
830 /* We must hold the GIL and have our thread state current */
831 /* XXX - remove the check - the assert should be fine,
832 but while this is very new (April 2003), the extra check
833 by release-only users can't hurt.
834 */
835 if (! PyThreadState_IsCurrent(tcur))
836 Py_FatalError("This thread state must be current when releasing");
837 assert(PyThreadState_IsCurrent(tcur));
838 --tcur->gilstate_counter;
839 assert(tcur->gilstate_counter >= 0); /* illegal counter value */
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000840
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000841 /* If we're going to destroy this thread-state, we must
842 * clear it while the GIL is held, as destructors may run.
843 */
844 if (tcur->gilstate_counter == 0) {
845 /* can't have been locked when we created it */
846 assert(oldstate == PyGILState_UNLOCKED);
847 PyThreadState_Clear(tcur);
848 /* Delete the thread-state. Note this releases the GIL too!
849 * It's vital that the GIL be held here, to avoid shutdown
850 * races; see bugs 225673 and 1061968 (that nasty bug has a
851 * habit of coming back).
852 */
853 PyThreadState_DeleteCurrent();
854 }
855 /* Release the lock if necessary */
856 else if (oldstate == PyGILState_UNLOCKED)
857 PyEval_SaveThread();
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000858}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000859
Benjamin Peterson3bf01752012-04-13 18:06:36 -0400860#endif /* WITH_THREAD */
861
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000862#ifdef __cplusplus
863}
864#endif
865
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000866