blob: 83f15fd6714a3e1fa66ac6eaedc8dcd31c10e626 [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
Victor Stinnerbfd316e2016-01-20 11:12:38 +01006#ifndef Py_BUILD_CORE
7/* ensure that PyThreadState_GET() is a macro, not an alias to
8 * PyThreadState_Get() */
9# error "pystate.c must be compiled with Py_BUILD_CORE defined"
10#endif
11
Tim Peters84705582004-10-10 02:47:33 +000012/* --------------------------------------------------------------------------
13CAUTION
14
Victor Stinner1a7425f2013-07-07 16:25:15 +020015Always use PyMem_RawMalloc() and PyMem_RawFree() directly in this file. A
16number of these functions are advertised as safe to call when the GIL isn't
17held, and in a debug build Python redirects (e.g.) PyMem_NEW (etc) to Python's
18debugging obmalloc functions. Those aren't thread-safe (they rely on the GIL
19to avoid the expense of doing their own locking).
Tim Peters84705582004-10-10 02:47:33 +000020-------------------------------------------------------------------------- */
21
Martin v. Löwisf0473d52001-07-18 16:17:16 +000022#ifdef HAVE_DLOPEN
23#ifdef HAVE_DLFCN_H
24#include <dlfcn.h>
25#endif
26#ifndef RTLD_LAZY
27#define RTLD_LAZY 1
28#endif
29#endif
30
Benjamin Peterson43162b82012-04-13 11:58:27 -040031#ifdef __cplusplus
32extern "C" {
33#endif
Martin v. Löwisf0473d52001-07-18 16:17:16 +000034
Guido van Rossum1d5ad901999-06-18 14:22:24 +000035#ifdef WITH_THREAD
36#include "pythread.h"
37static PyThread_type_lock head_mutex = NULL; /* Protects interp->tstate_head */
Moshe Zadka9fb6af92000-08-04 21:27:47 +000038#define HEAD_INIT() (void)(head_mutex || (head_mutex = PyThread_allocate_lock()))
Guido van Rossum1d5ad901999-06-18 14:22:24 +000039#define HEAD_LOCK() PyThread_acquire_lock(head_mutex, WAIT_LOCK)
40#define HEAD_UNLOCK() PyThread_release_lock(head_mutex)
Michael W. Hudson188d4362005-06-20 16:52:57 +000041
42/* The single PyInterpreterState used by this process'
43 GILState implementation
44*/
45static PyInterpreterState *autoInterpreterState = NULL;
46static int autoTLSkey = 0;
Guido van Rossum1d5ad901999-06-18 14:22:24 +000047#else
48#define HEAD_INIT() /* Nothing */
49#define HEAD_LOCK() /* Nothing */
50#define HEAD_UNLOCK() /* Nothing */
51#endif
52
Guido van Rossum25ce5661997-08-02 03:10:38 +000053static PyInterpreterState *interp_head = NULL;
Guido van Rossuma027efa1997-05-05 20:56:21 +000054
Jeffrey Yasskin39370832010-05-03 19:29:34 +000055/* Assuming the current thread holds the GIL, this is the
56 PyThreadState for the current thread. */
57_Py_atomic_address _PyThreadState_Current = {NULL};
Guido van Rossum6297a7a2003-02-19 15:53:17 +000058PyThreadFrameGetter _PyThreadState_GetFrame = NULL;
Guido van Rossuma027efa1997-05-05 20:56:21 +000059
Michael W. Hudsonce7da6c2005-09-30 08:20:24 +000060#ifdef WITH_THREAD
Michael W. Hudson188d4362005-06-20 16:52:57 +000061static void _PyGILState_NoteThreadState(PyThreadState* tstate);
Michael W. Hudsonce7da6c2005-09-30 08:20:24 +000062#endif
Michael W. Hudson188d4362005-06-20 16:52:57 +000063
Guido van Rossuma027efa1997-05-05 20:56:21 +000064
65PyInterpreterState *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000066PyInterpreterState_New(void)
Guido van Rossuma027efa1997-05-05 20:56:21 +000067{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000068 PyInterpreterState *interp = (PyInterpreterState *)
Victor Stinner1a7425f2013-07-07 16:25:15 +020069 PyMem_RawMalloc(sizeof(PyInterpreterState));
Guido van Rossum25ce5661997-08-02 03:10:38 +000070
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000071 if (interp != NULL) {
72 HEAD_INIT();
Thomas Wouters0e3f5912006-08-11 14:57:12 +000073#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000074 if (head_mutex == NULL)
75 Py_FatalError("Can't initialize threads for interpreter");
Thomas Wouters0e3f5912006-08-11 14:57:12 +000076#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000077 interp->modules = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000078 interp->modules_by_index = NULL;
79 interp->sysdict = NULL;
80 interp->builtins = NULL;
Serhiy Storchaka87a5c512014-02-10 18:21:34 +020081 interp->builtins_copy = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000082 interp->tstate_head = NULL;
83 interp->codec_search_path = NULL;
84 interp->codec_search_cache = NULL;
85 interp->codec_error_registry = NULL;
86 interp->codecs_initialized = 0;
Victor Stinner3cbf14b2011-04-27 00:24:21 +020087 interp->fscodec_initialized = 0;
Brett Cannonfd074152012-04-14 14:10:13 -040088 interp->importlib = NULL;
Martin v. Löwisf0473d52001-07-18 16:17:16 +000089#ifdef HAVE_DLOPEN
90#ifdef RTLD_NOW
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000091 interp->dlopenflags = RTLD_NOW;
Martin v. Löwisf0473d52001-07-18 16:17:16 +000092#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000093 interp->dlopenflags = RTLD_LAZY;
Martin v. Löwisf0473d52001-07-18 16:17:16 +000094#endif
95#endif
Martin v. Löwisf30d60e2004-06-08 08:17:44 +000096#ifdef WITH_TSC
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000097 interp->tscdump = 0;
Martin v. Löwisf30d60e2004-06-08 08:17:44 +000098#endif
Guido van Rossum25ce5661997-08-02 03:10:38 +000099
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000100 HEAD_LOCK();
101 interp->next = interp_head;
102 interp_head = interp;
103 HEAD_UNLOCK();
104 }
Guido van Rossum25ce5661997-08-02 03:10:38 +0000105
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000106 return interp;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000107}
108
109
110void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000111PyInterpreterState_Clear(PyInterpreterState *interp)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000112{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000113 PyThreadState *p;
114 HEAD_LOCK();
115 for (p = interp->tstate_head; p != NULL; p = p->next)
116 PyThreadState_Clear(p);
117 HEAD_UNLOCK();
118 Py_CLEAR(interp->codec_search_path);
119 Py_CLEAR(interp->codec_search_cache);
120 Py_CLEAR(interp->codec_error_registry);
121 Py_CLEAR(interp->modules);
122 Py_CLEAR(interp->modules_by_index);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000123 Py_CLEAR(interp->sysdict);
124 Py_CLEAR(interp->builtins);
Serhiy Storchaka87a5c512014-02-10 18:21:34 +0200125 Py_CLEAR(interp->builtins_copy);
Brett Cannonfd074152012-04-14 14:10:13 -0400126 Py_CLEAR(interp->importlib);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000127}
128
129
130static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000131zapthreads(PyInterpreterState *interp)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000132{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000133 PyThreadState *p;
134 /* No need to lock the mutex here because this should only happen
135 when the threads are all really dead (XXX famous last words). */
136 while ((p = interp->tstate_head) != NULL) {
137 PyThreadState_Delete(p);
138 }
Guido van Rossum25ce5661997-08-02 03:10:38 +0000139}
140
141
142void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000143PyInterpreterState_Delete(PyInterpreterState *interp)
Guido van Rossuma027efa1997-05-05 20:56:21 +0000144{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000145 PyInterpreterState **p;
146 zapthreads(interp);
147 HEAD_LOCK();
148 for (p = &interp_head; ; p = &(*p)->next) {
149 if (*p == NULL)
150 Py_FatalError(
151 "PyInterpreterState_Delete: invalid interp");
152 if (*p == interp)
153 break;
154 }
155 if (interp->tstate_head != NULL)
156 Py_FatalError("PyInterpreterState_Delete: remaining threads");
157 *p = interp->next;
158 HEAD_UNLOCK();
Victor Stinner1a7425f2013-07-07 16:25:15 +0200159 PyMem_RawFree(interp);
Antoine Pitrou8db076c2011-10-30 19:13:55 +0100160#ifdef WITH_THREAD
161 if (interp_head == NULL && head_mutex != NULL) {
162 PyThread_free_lock(head_mutex);
163 head_mutex = NULL;
164 }
165#endif
Guido van Rossuma027efa1997-05-05 20:56:21 +0000166}
167
168
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000169/* Default implementation for _PyThreadState_GetFrame */
170static struct _frame *
171threadstate_getframe(PyThreadState *self)
172{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000173 return self->frame;
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000174}
175
Victor Stinner45b9be52010-03-03 23:28:07 +0000176static PyThreadState *
177new_threadstate(PyInterpreterState *interp, int init)
Guido van Rossuma027efa1997-05-05 20:56:21 +0000178{
Victor Stinner1a7425f2013-07-07 16:25:15 +0200179 PyThreadState *tstate = (PyThreadState *)PyMem_RawMalloc(sizeof(PyThreadState));
Tim Peters84705582004-10-10 02:47:33 +0000180
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000181 if (_PyThreadState_GetFrame == NULL)
182 _PyThreadState_GetFrame = threadstate_getframe;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000183
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000184 if (tstate != NULL) {
185 tstate->interp = interp;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000186
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000187 tstate->frame = NULL;
188 tstate->recursion_depth = 0;
189 tstate->overflowed = 0;
190 tstate->recursion_critical = 0;
191 tstate->tracing = 0;
192 tstate->use_tracing = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000193 tstate->gilstate_counter = 0;
194 tstate->async_exc = NULL;
Martin v. Löwisf9ce67d2003-07-13 10:41:53 +0000195#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000196 tstate->thread_id = PyThread_get_thread_ident();
Martin v. Löwisf9ce67d2003-07-13 10:41:53 +0000197#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000198 tstate->thread_id = 0;
Martin v. Löwisf9ce67d2003-07-13 10:41:53 +0000199#endif
Guido van Rossuma027efa1997-05-05 20:56:21 +0000200
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000201 tstate->dict = NULL;
Guido van Rossumede04391998-04-10 20:18:25 +0000202
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000203 tstate->curexc_type = NULL;
204 tstate->curexc_value = NULL;
205 tstate->curexc_traceback = NULL;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000206
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000207 tstate->exc_type = NULL;
208 tstate->exc_value = NULL;
209 tstate->exc_traceback = NULL;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000210
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000211 tstate->c_profilefunc = NULL;
212 tstate->c_tracefunc = NULL;
213 tstate->c_profileobj = NULL;
214 tstate->c_traceobj = NULL;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000215
Antoine Pitrou2b0218a2012-09-06 00:59:49 +0200216 tstate->trash_delete_nesting = 0;
217 tstate->trash_delete_later = NULL;
Antoine Pitrou7b476992013-09-07 23:38:37 +0200218 tstate->on_delete = NULL;
219 tstate->on_delete_data = NULL;
Antoine Pitrou2b0218a2012-09-06 00:59:49 +0200220
Yury Selivanov75445082015-05-11 22:57:16 -0400221 tstate->coroutine_wrapper = NULL;
Yury Selivanovaab3c4a2015-06-02 18:43:51 -0400222 tstate->in_coroutine_wrapper = 0;
Yury Selivanov75445082015-05-11 22:57:16 -0400223
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000224 if (init)
225 _PyThreadState_Init(tstate);
Michael W. Hudson188d4362005-06-20 16:52:57 +0000226
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000227 HEAD_LOCK();
Charles-Francois Natalif28dfdd2013-05-08 21:09:52 +0200228 tstate->prev = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000229 tstate->next = interp->tstate_head;
Charles-Francois Natalif28dfdd2013-05-08 21:09:52 +0200230 if (tstate->next)
231 tstate->next->prev = tstate;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000232 interp->tstate_head = tstate;
233 HEAD_UNLOCK();
234 }
Guido van Rossum25ce5661997-08-02 03:10:38 +0000235
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000236 return tstate;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000237}
238
Victor Stinner45b9be52010-03-03 23:28:07 +0000239PyThreadState *
240PyThreadState_New(PyInterpreterState *interp)
241{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000242 return new_threadstate(interp, 1);
Victor Stinner45b9be52010-03-03 23:28:07 +0000243}
244
245PyThreadState *
246_PyThreadState_Prealloc(PyInterpreterState *interp)
247{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000248 return new_threadstate(interp, 0);
Victor Stinner45b9be52010-03-03 23:28:07 +0000249}
250
251void
252_PyThreadState_Init(PyThreadState *tstate)
253{
254#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000255 _PyGILState_NoteThreadState(tstate);
Victor Stinner45b9be52010-03-03 23:28:07 +0000256#endif
257}
258
Martin v. Löwis1a214512008-06-11 05:26:20 +0000259PyObject*
Martin v. Löwis7800f752012-06-22 12:20:55 +0200260PyState_FindModule(struct PyModuleDef* module)
Martin v. Löwis1a214512008-06-11 05:26:20 +0000261{
Martin v. Löwis7800f752012-06-22 12:20:55 +0200262 Py_ssize_t index = module->m_base.m_index;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000263 PyInterpreterState *state = PyThreadState_GET()->interp;
264 PyObject *res;
Nick Coghland5cacbb2015-05-23 22:24:10 +1000265 if (module->m_slots) {
266 return NULL;
267 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000268 if (index == 0)
269 return NULL;
270 if (state->modules_by_index == NULL)
271 return NULL;
Antoine Pitrou75506e82012-08-20 19:30:46 +0200272 if (index >= PyList_GET_SIZE(state->modules_by_index))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000273 return NULL;
274 res = PyList_GET_ITEM(state->modules_by_index, index);
275 return res==Py_None ? NULL : res;
Martin v. Löwis1a214512008-06-11 05:26:20 +0000276}
277
278int
279_PyState_AddModule(PyObject* module, struct PyModuleDef* def)
280{
Nick Coghland5cacbb2015-05-23 22:24:10 +1000281 PyInterpreterState *state;
282 if (def->m_slots) {
283 PyErr_SetString(PyExc_SystemError,
284 "PyState_AddModule called on module with slots");
285 return -1;
286 }
287 state = PyThreadState_GET()->interp;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000288 if (!def)
289 return -1;
290 if (!state->modules_by_index) {
291 state->modules_by_index = PyList_New(0);
292 if (!state->modules_by_index)
293 return -1;
294 }
295 while(PyList_GET_SIZE(state->modules_by_index) <= def->m_base.m_index)
296 if (PyList_Append(state->modules_by_index, Py_None) < 0)
297 return -1;
298 Py_INCREF(module);
299 return PyList_SetItem(state->modules_by_index,
300 def->m_base.m_index, module);
Martin v. Löwis1a214512008-06-11 05:26:20 +0000301}
Guido van Rossuma027efa1997-05-05 20:56:21 +0000302
Martin v. Löwis7800f752012-06-22 12:20:55 +0200303int
304PyState_AddModule(PyObject* module, struct PyModuleDef* def)
305{
306 Py_ssize_t index;
307 PyInterpreterState *state = PyThreadState_GET()->interp;
308 if (!def) {
309 Py_FatalError("PyState_AddModule: Module Definition is NULL");
310 return -1;
311 }
312 index = def->m_base.m_index;
313 if (state->modules_by_index) {
314 if(PyList_GET_SIZE(state->modules_by_index) >= index) {
315 if(module == PyList_GET_ITEM(state->modules_by_index, index)) {
316 Py_FatalError("PyState_AddModule: Module already added!");
317 return -1;
318 }
319 }
320 }
321 return _PyState_AddModule(module, def);
322}
323
324int
325PyState_RemoveModule(struct PyModuleDef* def)
326{
Nick Coghland5cacbb2015-05-23 22:24:10 +1000327 PyInterpreterState *state;
Martin v. Löwis7800f752012-06-22 12:20:55 +0200328 Py_ssize_t index = def->m_base.m_index;
Nick Coghland5cacbb2015-05-23 22:24:10 +1000329 if (def->m_slots) {
330 PyErr_SetString(PyExc_SystemError,
331 "PyState_RemoveModule called on module with slots");
332 return -1;
333 }
334 state = PyThreadState_GET()->interp;
Martin v. Löwis7800f752012-06-22 12:20:55 +0200335 if (index == 0) {
336 Py_FatalError("PyState_RemoveModule: Module index invalid.");
337 return -1;
338 }
339 if (state->modules_by_index == NULL) {
340 Py_FatalError("PyState_RemoveModule: Interpreters module-list not acessible.");
341 return -1;
342 }
343 if (index > PyList_GET_SIZE(state->modules_by_index)) {
344 Py_FatalError("PyState_RemoveModule: Module index out of bounds.");
345 return -1;
346 }
347 return PyList_SetItem(state->modules_by_index, index, Py_None);
348}
349
Antoine Pitrou40322e62013-08-11 00:30:09 +0200350/* used by import.c:PyImport_Cleanup */
351void
352_PyState_ClearModules(void)
353{
354 PyInterpreterState *state = PyThreadState_GET()->interp;
355 if (state->modules_by_index) {
356 Py_ssize_t i;
357 for (i = 0; i < PyList_GET_SIZE(state->modules_by_index); i++) {
358 PyObject *m = PyList_GET_ITEM(state->modules_by_index, i);
359 if (PyModule_Check(m)) {
360 /* cleanup the saved copy of module dicts */
361 PyModuleDef *md = PyModule_GetDef(m);
362 if (md)
363 Py_CLEAR(md->m_base.m_copy);
364 }
365 }
366 /* Setting modules_by_index to NULL could be dangerous, so we
367 clear the list instead. */
368 if (PyList_SetSlice(state->modules_by_index,
369 0, PyList_GET_SIZE(state->modules_by_index),
370 NULL))
371 PyErr_WriteUnraisable(state->modules_by_index);
372 }
373}
374
Guido van Rossuma027efa1997-05-05 20:56:21 +0000375void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000376PyThreadState_Clear(PyThreadState *tstate)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000377{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000378 if (Py_VerboseFlag && tstate->frame != NULL)
379 fprintf(stderr,
380 "PyThreadState_Clear: warning: thread still has a frame\n");
Guido van Rossum25ce5661997-08-02 03:10:38 +0000381
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000382 Py_CLEAR(tstate->frame);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000383
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000384 Py_CLEAR(tstate->dict);
385 Py_CLEAR(tstate->async_exc);
Guido van Rossumede04391998-04-10 20:18:25 +0000386
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000387 Py_CLEAR(tstate->curexc_type);
388 Py_CLEAR(tstate->curexc_value);
389 Py_CLEAR(tstate->curexc_traceback);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000390
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000391 Py_CLEAR(tstate->exc_type);
392 Py_CLEAR(tstate->exc_value);
393 Py_CLEAR(tstate->exc_traceback);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000394
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000395 tstate->c_profilefunc = NULL;
396 tstate->c_tracefunc = NULL;
397 Py_CLEAR(tstate->c_profileobj);
398 Py_CLEAR(tstate->c_traceobj);
Yury Selivanov75445082015-05-11 22:57:16 -0400399
400 Py_CLEAR(tstate->coroutine_wrapper);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000401}
402
403
Guido van Rossum29757862001-01-23 01:46:06 +0000404/* Common code for PyThreadState_Delete() and PyThreadState_DeleteCurrent() */
405static void
406tstate_delete_common(PyThreadState *tstate)
Guido van Rossuma027efa1997-05-05 20:56:21 +0000407{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000408 PyInterpreterState *interp;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000409 if (tstate == NULL)
410 Py_FatalError("PyThreadState_Delete: NULL tstate");
411 interp = tstate->interp;
412 if (interp == NULL)
413 Py_FatalError("PyThreadState_Delete: NULL interp");
414 HEAD_LOCK();
Charles-Francois Natalif28dfdd2013-05-08 21:09:52 +0200415 if (tstate->prev)
416 tstate->prev->next = tstate->next;
417 else
418 interp->tstate_head = tstate->next;
419 if (tstate->next)
420 tstate->next->prev = tstate->prev;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000421 HEAD_UNLOCK();
Antoine Pitrou7b476992013-09-07 23:38:37 +0200422 if (tstate->on_delete != NULL) {
423 tstate->on_delete(tstate->on_delete_data);
424 }
Victor Stinner1a7425f2013-07-07 16:25:15 +0200425 PyMem_RawFree(tstate);
Guido van Rossuma027efa1997-05-05 20:56:21 +0000426}
427
428
Guido van Rossum29757862001-01-23 01:46:06 +0000429void
430PyThreadState_Delete(PyThreadState *tstate)
431{
Victor Stinnerbfd316e2016-01-20 11:12:38 +0100432 if (tstate == PyThreadState_GET())
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000433 Py_FatalError("PyThreadState_Delete: tstate is still current");
Tim Petersf4e69282006-02-27 17:15:31 +0000434#ifdef WITH_THREAD
Antoine Pitrou079ce542010-09-08 12:37:10 +0000435 if (autoInterpreterState && PyThread_get_key_value(autoTLSkey) == tstate)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000436 PyThread_delete_key_value(autoTLSkey);
Tim Petersf4e69282006-02-27 17:15:31 +0000437#endif /* WITH_THREAD */
Christian Heimesb9dbc7d2013-07-01 23:42:28 +0200438 tstate_delete_common(tstate);
Guido van Rossum29757862001-01-23 01:46:06 +0000439}
440
441
442#ifdef WITH_THREAD
443void
444PyThreadState_DeleteCurrent()
445{
Victor Stinnerbfd316e2016-01-20 11:12:38 +0100446 PyThreadState *tstate = PyThreadState_GET();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000447 if (tstate == NULL)
448 Py_FatalError(
449 "PyThreadState_DeleteCurrent: no current tstate");
450 _Py_atomic_store_relaxed(&_PyThreadState_Current, NULL);
Antoine Pitrou079ce542010-09-08 12:37:10 +0000451 if (autoInterpreterState && PyThread_get_key_value(autoTLSkey) == tstate)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000452 PyThread_delete_key_value(autoTLSkey);
Christian Heimesb9dbc7d2013-07-01 23:42:28 +0200453 tstate_delete_common(tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000454 PyEval_ReleaseLock();
Guido van Rossum29757862001-01-23 01:46:06 +0000455}
456#endif /* WITH_THREAD */
457
458
Antoine Pitrou8408cea2013-05-05 23:47:09 +0200459/*
460 * Delete all thread states except the one passed as argument.
461 * Note that, if there is a current thread state, it *must* be the one
462 * passed as argument. Also, this won't touch any other interpreters
463 * than the current one, since we don't know which thread state should
464 * be kept in those other interpreteres.
465 */
466void
467_PyThreadState_DeleteExcept(PyThreadState *tstate)
468{
469 PyInterpreterState *interp = tstate->interp;
470 PyThreadState *p, *next, *garbage;
471 HEAD_LOCK();
472 /* Remove all thread states, except tstate, from the linked list of
473 thread states. This will allow calling PyThreadState_Clear()
Charles-Francois Natalif28dfdd2013-05-08 21:09:52 +0200474 without holding the lock. */
Antoine Pitrou8408cea2013-05-05 23:47:09 +0200475 garbage = interp->tstate_head;
Charles-Francois Natalif28dfdd2013-05-08 21:09:52 +0200476 if (garbage == tstate)
477 garbage = tstate->next;
478 if (tstate->prev)
479 tstate->prev->next = tstate->next;
480 if (tstate->next)
481 tstate->next->prev = tstate->prev;
482 tstate->prev = tstate->next = NULL;
Antoine Pitrou8408cea2013-05-05 23:47:09 +0200483 interp->tstate_head = tstate;
Antoine Pitrou8408cea2013-05-05 23:47:09 +0200484 HEAD_UNLOCK();
485 /* Clear and deallocate all stale thread states. Even if this
486 executes Python code, we should be safe since it executes
487 in the current thread, not one of the stale threads. */
488 for (p = garbage; p; p = next) {
489 next = p->next;
490 PyThreadState_Clear(p);
Victor Stinner1a7425f2013-07-07 16:25:15 +0200491 PyMem_RawFree(p);
Antoine Pitrou8408cea2013-05-05 23:47:09 +0200492 }
493}
494
495
Guido van Rossuma027efa1997-05-05 20:56:21 +0000496PyThreadState *
Victor Stinnerbfd316e2016-01-20 11:12:38 +0100497_PyThreadState_UncheckedGet(void)
498{
499 return PyThreadState_GET();
500}
501
502
503PyThreadState *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000504PyThreadState_Get(void)
Guido van Rossuma027efa1997-05-05 20:56:21 +0000505{
Victor Stinnerbfd316e2016-01-20 11:12:38 +0100506 PyThreadState *tstate = PyThreadState_GET();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000507 if (tstate == NULL)
508 Py_FatalError("PyThreadState_Get: no current thread");
Guido van Rossum25ce5661997-08-02 03:10:38 +0000509
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000510 return tstate;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000511}
512
513
514PyThreadState *
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000515PyThreadState_Swap(PyThreadState *newts)
Guido van Rossuma027efa1997-05-05 20:56:21 +0000516{
Victor Stinnerbfd316e2016-01-20 11:12:38 +0100517 PyThreadState *oldts = PyThreadState_GET();
Guido van Rossum25ce5661997-08-02 03:10:38 +0000518
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000519 _Py_atomic_store_relaxed(&_PyThreadState_Current, newts);
520 /* It should not be possible for more than one thread state
521 to be used for a thread. Check this the best we can in debug
522 builds.
523 */
Martin v. Löwis9e296252003-05-01 05:25:29 +0000524#if defined(Py_DEBUG) && defined(WITH_THREAD)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000525 if (newts) {
526 /* This can be called from PyEval_RestoreThread(). Similar
527 to it, we need to ensure errno doesn't change.
528 */
529 int err = errno;
530 PyThreadState *check = PyGILState_GetThisThreadState();
531 if (check && check->interp == newts->interp && check != newts)
532 Py_FatalError("Invalid thread state for this thread");
533 errno = err;
534 }
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000535#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000536 return oldts;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000537}
Guido van Rossumede04391998-04-10 20:18:25 +0000538
539/* An extension mechanism to store arbitrary additional per-thread state.
540 PyThreadState_GetDict() returns a dictionary that can be used to hold such
541 state; the caller should pick a unique key and store its state there. If
Guido van Rossum0fc8f002003-04-15 15:12:39 +0000542 PyThreadState_GetDict() returns NULL, an exception has *not* been raised
543 and the caller should assume no per-thread state is available. */
Guido van Rossumede04391998-04-10 20:18:25 +0000544
545PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000546PyThreadState_GetDict(void)
Guido van Rossumede04391998-04-10 20:18:25 +0000547{
Victor Stinnerbfd316e2016-01-20 11:12:38 +0100548 PyThreadState *tstate = PyThreadState_GET();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000549 if (tstate == NULL)
550 return NULL;
Guido van Rossumede04391998-04-10 20:18:25 +0000551
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000552 if (tstate->dict == NULL) {
553 PyObject *d;
554 tstate->dict = d = PyDict_New();
555 if (d == NULL)
556 PyErr_Clear();
557 }
558 return tstate->dict;
Guido van Rossumede04391998-04-10 20:18:25 +0000559}
Guido van Rossumf5df46d2001-07-19 12:19:27 +0000560
561
Guido van Rossumb8b6d0c2003-06-28 21:53:52 +0000562/* Asynchronously raise an exception in a thread.
563 Requested by Just van Rossum and Alex Martelli.
Guido van Rossum0f1f63c2005-02-08 02:07:57 +0000564 To prevent naive misuse, you must write your own extension
Thomas Wouters00ee7ba2006-08-21 19:07:27 +0000565 to call this, or use ctypes. Must be called with the GIL held.
566 Returns the number of tstates modified (normally 1, but 0 if `id` didn't
567 match any known thread id). Can be called with exc=NULL to clear an
568 existing async exception. This raises no exceptions. */
Guido van Rossumb8b6d0c2003-06-28 21:53:52 +0000569
570int
571PyThreadState_SetAsyncExc(long id, PyObject *exc) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000572 PyThreadState *tstate = PyThreadState_GET();
573 PyInterpreterState *interp = tstate->interp;
574 PyThreadState *p;
Thomas Wouters00ee7ba2006-08-21 19:07:27 +0000575
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000576 /* Although the GIL is held, a few C API functions can be called
577 * without the GIL held, and in particular some that create and
578 * destroy thread and interpreter states. Those can mutate the
579 * list of thread states we're traversing, so to prevent that we lock
580 * head_mutex for the duration.
581 */
582 HEAD_LOCK();
583 for (p = interp->tstate_head; p != NULL; p = p->next) {
584 if (p->thread_id == id) {
585 /* Tricky: we need to decref the current value
586 * (if any) in p->async_exc, but that can in turn
587 * allow arbitrary Python code to run, including
588 * perhaps calls to this function. To prevent
589 * deadlock, we need to release head_mutex before
590 * the decref.
591 */
592 PyObject *old_exc = p->async_exc;
593 Py_XINCREF(exc);
594 p->async_exc = exc;
595 HEAD_UNLOCK();
596 Py_XDECREF(old_exc);
597 _PyEval_SignalAsyncExc();
598 return 1;
599 }
600 }
601 HEAD_UNLOCK();
602 return 0;
Guido van Rossumb8b6d0c2003-06-28 21:53:52 +0000603}
604
605
Guido van Rossumf5df46d2001-07-19 12:19:27 +0000606/* Routines for advanced debuggers, requested by David Beazley.
607 Don't use unless you know what you are doing! */
608
609PyInterpreterState *
610PyInterpreterState_Head(void)
611{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000612 return interp_head;
Guido van Rossumf5df46d2001-07-19 12:19:27 +0000613}
614
615PyInterpreterState *
616PyInterpreterState_Next(PyInterpreterState *interp) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000617 return interp->next;
Guido van Rossumf5df46d2001-07-19 12:19:27 +0000618}
619
620PyThreadState *
621PyInterpreterState_ThreadHead(PyInterpreterState *interp) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000622 return interp->tstate_head;
Guido van Rossumf5df46d2001-07-19 12:19:27 +0000623}
624
625PyThreadState *
626PyThreadState_Next(PyThreadState *tstate) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000627 return tstate->next;
Guido van Rossumf5df46d2001-07-19 12:19:27 +0000628}
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000629
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000630/* The implementation of sys._current_frames(). This is intended to be
631 called with the GIL held, as it will be when called via
632 sys._current_frames(). It's possible it would work fine even without
633 the GIL held, but haven't thought enough about that.
634*/
635PyObject *
636_PyThread_CurrentFrames(void)
637{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000638 PyObject *result;
639 PyInterpreterState *i;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000640
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000641 result = PyDict_New();
642 if (result == NULL)
643 return NULL;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000644
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000645 /* for i in all interpreters:
646 * for t in all of i's thread states:
647 * if t's frame isn't NULL, map t's id to its frame
Ezio Melotti13925002011-03-16 11:05:33 +0200648 * Because these lists can mutate even when the GIL is held, we
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000649 * need to grab head_mutex for the duration.
650 */
651 HEAD_LOCK();
652 for (i = interp_head; i != NULL; i = i->next) {
653 PyThreadState *t;
654 for (t = i->tstate_head; t != NULL; t = t->next) {
655 PyObject *id;
656 int stat;
657 struct _frame *frame = t->frame;
658 if (frame == NULL)
659 continue;
660 id = PyLong_FromLong(t->thread_id);
661 if (id == NULL)
662 goto Fail;
663 stat = PyDict_SetItem(result, id, (PyObject *)frame);
664 Py_DECREF(id);
665 if (stat < 0)
666 goto Fail;
667 }
668 }
669 HEAD_UNLOCK();
670 return result;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000671
672 Fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000673 HEAD_UNLOCK();
674 Py_DECREF(result);
675 return NULL;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000676}
Guido van Rossumb8b6d0c2003-06-28 21:53:52 +0000677
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000678/* Python "auto thread state" API. */
679#ifdef WITH_THREAD
680
681/* Keep this as a static, as it is not reliable! It can only
682 ever be compared to the state for the *current* thread.
683 * If not equal, then it doesn't matter that the actual
684 value may change immediately after comparison, as it can't
685 possibly change to the current thread's state.
686 * If equal, then the current thread holds the lock, so the value can't
687 change until we yield the lock.
688*/
689static int
690PyThreadState_IsCurrent(PyThreadState *tstate)
691{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000692 /* Must be the tstate for this thread */
693 assert(PyGILState_GetThisThreadState()==tstate);
Victor Stinnerbfd316e2016-01-20 11:12:38 +0100694 return tstate == PyThreadState_GET();
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000695}
696
Tim Peters4c1f5ec2004-10-09 17:25:05 +0000697/* Internal initialization/finalization functions called by
698 Py_Initialize/Py_Finalize
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000699*/
Tim Peters19717fa2004-10-09 17:38:29 +0000700void
701_PyGILState_Init(PyInterpreterState *i, PyThreadState *t)
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000702{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000703 assert(i && t); /* must init with valid states */
704 autoTLSkey = PyThread_create_key();
Kristján Valur Jónsson2fea9b92010-09-20 02:11:49 +0000705 if (autoTLSkey == -1)
706 Py_FatalError("Could not allocate TLS entry");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000707 autoInterpreterState = i;
708 assert(PyThread_get_key_value(autoTLSkey) == NULL);
709 assert(t->gilstate_counter == 0);
Michael W. Hudson188d4362005-06-20 16:52:57 +0000710
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000711 _PyGILState_NoteThreadState(t);
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000712}
713
Tim Peters19717fa2004-10-09 17:38:29 +0000714void
715_PyGILState_Fini(void)
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000716{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000717 PyThread_delete_key(autoTLSkey);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000718 autoInterpreterState = NULL;
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000719}
720
Charles-François Natalia233df82011-11-22 19:49:51 +0100721/* Reset the TLS key - called by PyOS_AfterFork().
Antoine Pitrou0c759fe2011-04-27 19:28:05 +0200722 * This should not be necessary, but some - buggy - pthread implementations
Charles-François Natalia233df82011-11-22 19:49:51 +0100723 * don't reset TLS upon fork(), see issue #10517.
Antoine Pitrou0c759fe2011-04-27 19:28:05 +0200724 */
725void
726_PyGILState_Reinit(void)
727{
728 PyThreadState *tstate = PyGILState_GetThisThreadState();
729 PyThread_delete_key(autoTLSkey);
730 if ((autoTLSkey = PyThread_create_key()) == -1)
731 Py_FatalError("Could not allocate TLS entry");
732
Charles-François Natalia233df82011-11-22 19:49:51 +0100733 /* If the thread had an associated auto thread state, reassociate it with
734 * the new key. */
735 if (tstate && PyThread_set_key_value(autoTLSkey, (void *)tstate) < 0)
Antoine Pitrou0c759fe2011-04-27 19:28:05 +0200736 Py_FatalError("Couldn't create autoTLSkey mapping");
737}
738
Michael W. Hudson188d4362005-06-20 16:52:57 +0000739/* When a thread state is created for a thread by some mechanism other than
740 PyGILState_Ensure, it's important that the GILState machinery knows about
741 it so it doesn't try to create another thread state for the thread (this is
742 a better fix for SF bug #1010677 than the first one attempted).
743*/
Thomas Wouters89f507f2006-12-13 04:49:30 +0000744static void
Michael W. Hudson188d4362005-06-20 16:52:57 +0000745_PyGILState_NoteThreadState(PyThreadState* tstate)
746{
Antoine Pitrou079ce542010-09-08 12:37:10 +0000747 /* If autoTLSkey isn't initialized, this must be the very first
748 threadstate created in Py_Initialize(). Don't do anything for now
749 (we'll be back here when _PyGILState_Init is called). */
750 if (!autoInterpreterState)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000751 return;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000752
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000753 /* Stick the thread state for this thread in thread local storage.
Michael W. Hudson188d4362005-06-20 16:52:57 +0000754
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000755 The only situation where you can legitimately have more than one
756 thread state for an OS level thread is when there are multiple
Victor Stinner590cebe2013-12-13 11:08:56 +0100757 interpreters.
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000758
Victor Stinner590cebe2013-12-13 11:08:56 +0100759 You shouldn't really be using the PyGILState_ APIs anyway (see issues
760 #10915 and #15751).
Michael W. Hudson188d4362005-06-20 16:52:57 +0000761
Victor Stinner590cebe2013-12-13 11:08:56 +0100762 The first thread state created for that given OS level thread will
763 "win", which seems reasonable behaviour.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000764 */
Victor Stinner590cebe2013-12-13 11:08:56 +0100765 if (PyThread_get_key_value(autoTLSkey) == NULL) {
766 if (PyThread_set_key_value(autoTLSkey, (void *)tstate) < 0)
767 Py_FatalError("Couldn't create autoTLSkey mapping");
768 }
Michael W. Hudson188d4362005-06-20 16:52:57 +0000769
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000770 /* PyGILState_Release must not try to delete this thread state. */
771 tstate->gilstate_counter = 1;
Michael W. Hudson188d4362005-06-20 16:52:57 +0000772}
773
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000774/* The public functions */
Tim Peters19717fa2004-10-09 17:38:29 +0000775PyThreadState *
776PyGILState_GetThisThreadState(void)
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000777{
Antoine Pitrou079ce542010-09-08 12:37:10 +0000778 if (autoInterpreterState == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000779 return NULL;
780 return (PyThreadState *)PyThread_get_key_value(autoTLSkey);
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000781}
782
Kristján Valur Jónsson684cd0e2013-03-23 03:36:16 -0700783int
784PyGILState_Check(void)
785{
Victor Stinnerbfd316e2016-01-20 11:12:38 +0100786 PyThreadState *tstate = PyThreadState_GET();
Kristján Valur Jónsson684cd0e2013-03-23 03:36:16 -0700787 return tstate && (tstate == PyGILState_GetThisThreadState());
788}
789
Tim Peters19717fa2004-10-09 17:38:29 +0000790PyGILState_STATE
791PyGILState_Ensure(void)
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000792{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000793 int current;
794 PyThreadState *tcur;
795 /* Note that we do not auto-init Python here - apart from
796 potential races with 2 threads auto-initializing, pep-311
797 spells out other issues. Embedders are expected to have
798 called Py_Initialize() and usually PyEval_InitThreads().
799 */
800 assert(autoInterpreterState); /* Py_Initialize() hasn't been called! */
801 tcur = (PyThreadState *)PyThread_get_key_value(autoTLSkey);
802 if (tcur == NULL) {
Victor Stinner62ca1002013-12-13 01:46:43 +0100803 /* At startup, Python has no concrete GIL. If PyGILState_Ensure() is
804 called from a new thread for the first time, we need the create the
805 GIL. */
806 PyEval_InitThreads();
807
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000808 /* Create a new thread state for this thread */
809 tcur = PyThreadState_New(autoInterpreterState);
810 if (tcur == NULL)
811 Py_FatalError("Couldn't create thread-state for new thread");
812 /* This is our thread state! We'll need to delete it in the
813 matching call to PyGILState_Release(). */
814 tcur->gilstate_counter = 0;
815 current = 0; /* new thread state is never current */
816 }
817 else
818 current = PyThreadState_IsCurrent(tcur);
819 if (current == 0)
820 PyEval_RestoreThread(tcur);
821 /* Update our counter in the thread-state - no need for locks:
822 - tcur will remain valid as we hold the GIL.
823 - the counter is safe as we are the only thread "allowed"
824 to modify this value
825 */
826 ++tcur->gilstate_counter;
827 return current ? PyGILState_LOCKED : PyGILState_UNLOCKED;
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000828}
829
Tim Peters19717fa2004-10-09 17:38:29 +0000830void
831PyGILState_Release(PyGILState_STATE oldstate)
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000832{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000833 PyThreadState *tcur = (PyThreadState *)PyThread_get_key_value(
834 autoTLSkey);
835 if (tcur == NULL)
836 Py_FatalError("auto-releasing thread-state, "
837 "but no thread-state for this thread");
838 /* We must hold the GIL and have our thread state current */
839 /* XXX - remove the check - the assert should be fine,
840 but while this is very new (April 2003), the extra check
841 by release-only users can't hurt.
842 */
843 if (! PyThreadState_IsCurrent(tcur))
844 Py_FatalError("This thread state must be current when releasing");
845 assert(PyThreadState_IsCurrent(tcur));
846 --tcur->gilstate_counter;
847 assert(tcur->gilstate_counter >= 0); /* illegal counter value */
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000848
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000849 /* If we're going to destroy this thread-state, we must
850 * clear it while the GIL is held, as destructors may run.
851 */
852 if (tcur->gilstate_counter == 0) {
853 /* can't have been locked when we created it */
854 assert(oldstate == PyGILState_UNLOCKED);
855 PyThreadState_Clear(tcur);
856 /* Delete the thread-state. Note this releases the GIL too!
857 * It's vital that the GIL be held here, to avoid shutdown
858 * races; see bugs 225673 and 1061968 (that nasty bug has a
859 * habit of coming back).
860 */
861 PyThreadState_DeleteCurrent();
862 }
863 /* Release the lock if necessary */
864 else if (oldstate == PyGILState_UNLOCKED)
865 PyEval_SaveThread();
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000866}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000867
Benjamin Peterson3bf01752012-04-13 18:06:36 -0400868#endif /* WITH_THREAD */
869
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000870#ifdef __cplusplus
871}
872#endif
873
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000874