blob: 853e5c746d01e00e8fe4553a090d5203afcbd7b4 [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 Stinnerb02ef712016-01-22 14:09:55 +01006#define GET_TSTATE() \
7 ((PyThreadState*)_Py_atomic_load_relaxed(&_PyThreadState_Current))
8#define SET_TSTATE(value) \
9 _Py_atomic_store_relaxed(&_PyThreadState_Current, (Py_uintptr_t)(value))
10#define GET_INTERP_STATE() \
11 (GET_TSTATE()->interp)
12
Victor Stinnerbfd316e2016-01-20 11:12:38 +010013
Tim Peters84705582004-10-10 02:47:33 +000014/* --------------------------------------------------------------------------
15CAUTION
16
Victor Stinner1a7425f2013-07-07 16:25:15 +020017Always use PyMem_RawMalloc() and PyMem_RawFree() directly in this file. A
18number of these functions are advertised as safe to call when the GIL isn't
19held, and in a debug build Python redirects (e.g.) PyMem_NEW (etc) to Python's
20debugging obmalloc functions. Those aren't thread-safe (they rely on the GIL
21to avoid the expense of doing their own locking).
Tim Peters84705582004-10-10 02:47:33 +000022-------------------------------------------------------------------------- */
23
Martin v. Löwisf0473d52001-07-18 16:17:16 +000024#ifdef HAVE_DLOPEN
25#ifdef HAVE_DLFCN_H
26#include <dlfcn.h>
27#endif
28#ifndef RTLD_LAZY
29#define RTLD_LAZY 1
30#endif
31#endif
32
Benjamin Peterson43162b82012-04-13 11:58:27 -040033#ifdef __cplusplus
34extern "C" {
35#endif
Martin v. Löwisf0473d52001-07-18 16:17:16 +000036
Guido van Rossum1d5ad901999-06-18 14:22:24 +000037#ifdef WITH_THREAD
38#include "pythread.h"
39static PyThread_type_lock head_mutex = NULL; /* Protects interp->tstate_head */
Moshe Zadka9fb6af92000-08-04 21:27:47 +000040#define HEAD_INIT() (void)(head_mutex || (head_mutex = PyThread_allocate_lock()))
Guido van Rossum1d5ad901999-06-18 14:22:24 +000041#define HEAD_LOCK() PyThread_acquire_lock(head_mutex, WAIT_LOCK)
42#define HEAD_UNLOCK() PyThread_release_lock(head_mutex)
Michael W. Hudson188d4362005-06-20 16:52:57 +000043
44/* The single PyInterpreterState used by this process'
45 GILState implementation
46*/
47static PyInterpreterState *autoInterpreterState = NULL;
48static int autoTLSkey = 0;
Guido van Rossum1d5ad901999-06-18 14:22:24 +000049#else
50#define HEAD_INIT() /* Nothing */
51#define HEAD_LOCK() /* Nothing */
52#define HEAD_UNLOCK() /* Nothing */
53#endif
54
Guido van Rossum25ce5661997-08-02 03:10:38 +000055static PyInterpreterState *interp_head = NULL;
Guido van Rossuma027efa1997-05-05 20:56:21 +000056
Jeffrey Yasskin39370832010-05-03 19:29:34 +000057/* Assuming the current thread holds the GIL, this is the
58 PyThreadState for the current thread. */
Victor Stinnerb02ef712016-01-22 14:09:55 +010059_Py_atomic_address _PyThreadState_Current = {0};
Guido van Rossum6297a7a2003-02-19 15:53:17 +000060PyThreadFrameGetter _PyThreadState_GetFrame = NULL;
Guido van Rossuma027efa1997-05-05 20:56:21 +000061
Michael W. Hudsonce7da6c2005-09-30 08:20:24 +000062#ifdef WITH_THREAD
Michael W. Hudson188d4362005-06-20 16:52:57 +000063static void _PyGILState_NoteThreadState(PyThreadState* tstate);
Michael W. Hudsonce7da6c2005-09-30 08:20:24 +000064#endif
Michael W. Hudson188d4362005-06-20 16:52:57 +000065
Guido van Rossuma027efa1997-05-05 20:56:21 +000066
67PyInterpreterState *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000068PyInterpreterState_New(void)
Guido van Rossuma027efa1997-05-05 20:56:21 +000069{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000070 PyInterpreterState *interp = (PyInterpreterState *)
Victor Stinner1a7425f2013-07-07 16:25:15 +020071 PyMem_RawMalloc(sizeof(PyInterpreterState));
Guido van Rossum25ce5661997-08-02 03:10:38 +000072
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000073 if (interp != NULL) {
74 HEAD_INIT();
Thomas Wouters0e3f5912006-08-11 14:57:12 +000075#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000076 if (head_mutex == NULL)
77 Py_FatalError("Can't initialize threads for interpreter");
Thomas Wouters0e3f5912006-08-11 14:57:12 +000078#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000079 interp->modules = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000080 interp->modules_by_index = NULL;
81 interp->sysdict = NULL;
82 interp->builtins = NULL;
Serhiy Storchaka87a5c512014-02-10 18:21:34 +020083 interp->builtins_copy = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000084 interp->tstate_head = NULL;
85 interp->codec_search_path = NULL;
86 interp->codec_search_cache = NULL;
87 interp->codec_error_registry = NULL;
88 interp->codecs_initialized = 0;
Victor Stinner3cbf14b2011-04-27 00:24:21 +020089 interp->fscodec_initialized = 0;
Brett Cannonfd074152012-04-14 14:10:13 -040090 interp->importlib = NULL;
Martin v. Löwisf0473d52001-07-18 16:17:16 +000091#ifdef HAVE_DLOPEN
92#ifdef RTLD_NOW
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000093 interp->dlopenflags = RTLD_NOW;
Martin v. Löwisf0473d52001-07-18 16:17:16 +000094#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000095 interp->dlopenflags = RTLD_LAZY;
Martin v. Löwisf0473d52001-07-18 16:17:16 +000096#endif
97#endif
Martin v. Löwisf30d60e2004-06-08 08:17:44 +000098#ifdef WITH_TSC
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000099 interp->tscdump = 0;
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000100#endif
Guido van Rossum25ce5661997-08-02 03:10:38 +0000101
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000102 HEAD_LOCK();
103 interp->next = interp_head;
104 interp_head = interp;
105 HEAD_UNLOCK();
106 }
Guido van Rossum25ce5661997-08-02 03:10:38 +0000107
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000108 return interp;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000109}
110
111
112void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000113PyInterpreterState_Clear(PyInterpreterState *interp)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000114{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000115 PyThreadState *p;
116 HEAD_LOCK();
117 for (p = interp->tstate_head; p != NULL; p = p->next)
118 PyThreadState_Clear(p);
119 HEAD_UNLOCK();
120 Py_CLEAR(interp->codec_search_path);
121 Py_CLEAR(interp->codec_search_cache);
122 Py_CLEAR(interp->codec_error_registry);
123 Py_CLEAR(interp->modules);
124 Py_CLEAR(interp->modules_by_index);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000125 Py_CLEAR(interp->sysdict);
126 Py_CLEAR(interp->builtins);
Serhiy Storchaka87a5c512014-02-10 18:21:34 +0200127 Py_CLEAR(interp->builtins_copy);
Brett Cannonfd074152012-04-14 14:10:13 -0400128 Py_CLEAR(interp->importlib);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000129}
130
131
132static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000133zapthreads(PyInterpreterState *interp)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000134{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000135 PyThreadState *p;
136 /* No need to lock the mutex here because this should only happen
137 when the threads are all really dead (XXX famous last words). */
138 while ((p = interp->tstate_head) != NULL) {
139 PyThreadState_Delete(p);
140 }
Guido van Rossum25ce5661997-08-02 03:10:38 +0000141}
142
143
144void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000145PyInterpreterState_Delete(PyInterpreterState *interp)
Guido van Rossuma027efa1997-05-05 20:56:21 +0000146{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000147 PyInterpreterState **p;
148 zapthreads(interp);
149 HEAD_LOCK();
150 for (p = &interp_head; ; p = &(*p)->next) {
151 if (*p == NULL)
152 Py_FatalError(
153 "PyInterpreterState_Delete: invalid interp");
154 if (*p == interp)
155 break;
156 }
157 if (interp->tstate_head != NULL)
158 Py_FatalError("PyInterpreterState_Delete: remaining threads");
159 *p = interp->next;
160 HEAD_UNLOCK();
Victor Stinner1a7425f2013-07-07 16:25:15 +0200161 PyMem_RawFree(interp);
Antoine Pitrou8db076c2011-10-30 19:13:55 +0100162#ifdef WITH_THREAD
163 if (interp_head == NULL && head_mutex != NULL) {
164 PyThread_free_lock(head_mutex);
165 head_mutex = NULL;
166 }
167#endif
Guido van Rossuma027efa1997-05-05 20:56:21 +0000168}
169
170
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000171/* Default implementation for _PyThreadState_GetFrame */
172static struct _frame *
173threadstate_getframe(PyThreadState *self)
174{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000175 return self->frame;
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000176}
177
Victor Stinner45b9be52010-03-03 23:28:07 +0000178static PyThreadState *
179new_threadstate(PyInterpreterState *interp, int init)
Guido van Rossuma027efa1997-05-05 20:56:21 +0000180{
Victor Stinner1a7425f2013-07-07 16:25:15 +0200181 PyThreadState *tstate = (PyThreadState *)PyMem_RawMalloc(sizeof(PyThreadState));
Tim Peters84705582004-10-10 02:47:33 +0000182
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000183 if (_PyThreadState_GetFrame == NULL)
184 _PyThreadState_GetFrame = threadstate_getframe;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000185
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000186 if (tstate != NULL) {
187 tstate->interp = interp;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000188
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000189 tstate->frame = NULL;
190 tstate->recursion_depth = 0;
191 tstate->overflowed = 0;
192 tstate->recursion_critical = 0;
193 tstate->tracing = 0;
194 tstate->use_tracing = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000195 tstate->gilstate_counter = 0;
196 tstate->async_exc = NULL;
Martin v. Löwisf9ce67d2003-07-13 10:41:53 +0000197#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000198 tstate->thread_id = PyThread_get_thread_ident();
Martin v. Löwisf9ce67d2003-07-13 10:41:53 +0000199#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000200 tstate->thread_id = 0;
Martin v. Löwisf9ce67d2003-07-13 10:41:53 +0000201#endif
Guido van Rossuma027efa1997-05-05 20:56:21 +0000202
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000203 tstate->dict = NULL;
Guido van Rossumede04391998-04-10 20:18:25 +0000204
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000205 tstate->curexc_type = NULL;
206 tstate->curexc_value = NULL;
207 tstate->curexc_traceback = NULL;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000208
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000209 tstate->exc_type = NULL;
210 tstate->exc_value = NULL;
211 tstate->exc_traceback = NULL;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000212
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000213 tstate->c_profilefunc = NULL;
214 tstate->c_tracefunc = NULL;
215 tstate->c_profileobj = NULL;
216 tstate->c_traceobj = NULL;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000217
Antoine Pitrou2b0218a2012-09-06 00:59:49 +0200218 tstate->trash_delete_nesting = 0;
219 tstate->trash_delete_later = NULL;
Antoine Pitrou7b476992013-09-07 23:38:37 +0200220 tstate->on_delete = NULL;
221 tstate->on_delete_data = NULL;
Antoine Pitrou2b0218a2012-09-06 00:59:49 +0200222
Yury Selivanov75445082015-05-11 22:57:16 -0400223 tstate->coroutine_wrapper = NULL;
Yury Selivanovaab3c4a2015-06-02 18:43:51 -0400224 tstate->in_coroutine_wrapper = 0;
Yury Selivanov75445082015-05-11 22:57:16 -0400225
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000226 if (init)
227 _PyThreadState_Init(tstate);
Michael W. Hudson188d4362005-06-20 16:52:57 +0000228
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000229 HEAD_LOCK();
Charles-Francois Natalif28dfdd2013-05-08 21:09:52 +0200230 tstate->prev = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000231 tstate->next = interp->tstate_head;
Charles-Francois Natalif28dfdd2013-05-08 21:09:52 +0200232 if (tstate->next)
233 tstate->next->prev = tstate;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000234 interp->tstate_head = tstate;
235 HEAD_UNLOCK();
236 }
Guido van Rossum25ce5661997-08-02 03:10:38 +0000237
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000238 return tstate;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000239}
240
Victor Stinner45b9be52010-03-03 23:28:07 +0000241PyThreadState *
242PyThreadState_New(PyInterpreterState *interp)
243{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000244 return new_threadstate(interp, 1);
Victor Stinner45b9be52010-03-03 23:28:07 +0000245}
246
247PyThreadState *
248_PyThreadState_Prealloc(PyInterpreterState *interp)
249{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000250 return new_threadstate(interp, 0);
Victor Stinner45b9be52010-03-03 23:28:07 +0000251}
252
253void
254_PyThreadState_Init(PyThreadState *tstate)
255{
256#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000257 _PyGILState_NoteThreadState(tstate);
Victor Stinner45b9be52010-03-03 23:28:07 +0000258#endif
259}
260
Martin v. Löwis1a214512008-06-11 05:26:20 +0000261PyObject*
Martin v. Löwis7800f752012-06-22 12:20:55 +0200262PyState_FindModule(struct PyModuleDef* module)
Martin v. Löwis1a214512008-06-11 05:26:20 +0000263{
Martin v. Löwis7800f752012-06-22 12:20:55 +0200264 Py_ssize_t index = module->m_base.m_index;
Victor Stinnerb02ef712016-01-22 14:09:55 +0100265 PyInterpreterState *state = GET_INTERP_STATE();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000266 PyObject *res;
Nick Coghland5cacbb2015-05-23 22:24:10 +1000267 if (module->m_slots) {
268 return NULL;
269 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000270 if (index == 0)
271 return NULL;
272 if (state->modules_by_index == NULL)
273 return NULL;
Antoine Pitrou75506e82012-08-20 19:30:46 +0200274 if (index >= PyList_GET_SIZE(state->modules_by_index))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000275 return NULL;
276 res = PyList_GET_ITEM(state->modules_by_index, index);
277 return res==Py_None ? NULL : res;
Martin v. Löwis1a214512008-06-11 05:26:20 +0000278}
279
280int
281_PyState_AddModule(PyObject* module, struct PyModuleDef* def)
282{
Nick Coghland5cacbb2015-05-23 22:24:10 +1000283 PyInterpreterState *state;
284 if (def->m_slots) {
285 PyErr_SetString(PyExc_SystemError,
286 "PyState_AddModule called on module with slots");
287 return -1;
288 }
Victor Stinnerb02ef712016-01-22 14:09:55 +0100289 state = GET_INTERP_STATE();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000290 if (!def)
291 return -1;
292 if (!state->modules_by_index) {
293 state->modules_by_index = PyList_New(0);
294 if (!state->modules_by_index)
295 return -1;
296 }
297 while(PyList_GET_SIZE(state->modules_by_index) <= def->m_base.m_index)
298 if (PyList_Append(state->modules_by_index, Py_None) < 0)
299 return -1;
300 Py_INCREF(module);
301 return PyList_SetItem(state->modules_by_index,
302 def->m_base.m_index, module);
Martin v. Löwis1a214512008-06-11 05:26:20 +0000303}
Guido van Rossuma027efa1997-05-05 20:56:21 +0000304
Martin v. Löwis7800f752012-06-22 12:20:55 +0200305int
306PyState_AddModule(PyObject* module, struct PyModuleDef* def)
307{
308 Py_ssize_t index;
Victor Stinnerb02ef712016-01-22 14:09:55 +0100309 PyInterpreterState *state = GET_INTERP_STATE();
Martin v. Löwis7800f752012-06-22 12:20:55 +0200310 if (!def) {
311 Py_FatalError("PyState_AddModule: Module Definition is NULL");
312 return -1;
313 }
314 index = def->m_base.m_index;
315 if (state->modules_by_index) {
316 if(PyList_GET_SIZE(state->modules_by_index) >= index) {
317 if(module == PyList_GET_ITEM(state->modules_by_index, index)) {
318 Py_FatalError("PyState_AddModule: Module already added!");
319 return -1;
320 }
321 }
322 }
323 return _PyState_AddModule(module, def);
324}
325
326int
327PyState_RemoveModule(struct PyModuleDef* def)
328{
Nick Coghland5cacbb2015-05-23 22:24:10 +1000329 PyInterpreterState *state;
Martin v. Löwis7800f752012-06-22 12:20:55 +0200330 Py_ssize_t index = def->m_base.m_index;
Nick Coghland5cacbb2015-05-23 22:24:10 +1000331 if (def->m_slots) {
332 PyErr_SetString(PyExc_SystemError,
333 "PyState_RemoveModule called on module with slots");
334 return -1;
335 }
Victor Stinnerb02ef712016-01-22 14:09:55 +0100336 state = GET_INTERP_STATE();
Martin v. Löwis7800f752012-06-22 12:20:55 +0200337 if (index == 0) {
338 Py_FatalError("PyState_RemoveModule: Module index invalid.");
339 return -1;
340 }
341 if (state->modules_by_index == NULL) {
342 Py_FatalError("PyState_RemoveModule: Interpreters module-list not acessible.");
343 return -1;
344 }
345 if (index > PyList_GET_SIZE(state->modules_by_index)) {
346 Py_FatalError("PyState_RemoveModule: Module index out of bounds.");
347 return -1;
348 }
349 return PyList_SetItem(state->modules_by_index, index, Py_None);
350}
351
Antoine Pitrou40322e62013-08-11 00:30:09 +0200352/* used by import.c:PyImport_Cleanup */
353void
354_PyState_ClearModules(void)
355{
Victor Stinnerb02ef712016-01-22 14:09:55 +0100356 PyInterpreterState *state = GET_INTERP_STATE();
Antoine Pitrou40322e62013-08-11 00:30:09 +0200357 if (state->modules_by_index) {
358 Py_ssize_t i;
359 for (i = 0; i < PyList_GET_SIZE(state->modules_by_index); i++) {
360 PyObject *m = PyList_GET_ITEM(state->modules_by_index, i);
361 if (PyModule_Check(m)) {
362 /* cleanup the saved copy of module dicts */
363 PyModuleDef *md = PyModule_GetDef(m);
364 if (md)
365 Py_CLEAR(md->m_base.m_copy);
366 }
367 }
368 /* Setting modules_by_index to NULL could be dangerous, so we
369 clear the list instead. */
370 if (PyList_SetSlice(state->modules_by_index,
371 0, PyList_GET_SIZE(state->modules_by_index),
372 NULL))
373 PyErr_WriteUnraisable(state->modules_by_index);
374 }
375}
376
Guido van Rossuma027efa1997-05-05 20:56:21 +0000377void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000378PyThreadState_Clear(PyThreadState *tstate)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000379{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000380 if (Py_VerboseFlag && tstate->frame != NULL)
381 fprintf(stderr,
382 "PyThreadState_Clear: warning: thread still has a frame\n");
Guido van Rossum25ce5661997-08-02 03:10:38 +0000383
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000384 Py_CLEAR(tstate->frame);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000385
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000386 Py_CLEAR(tstate->dict);
387 Py_CLEAR(tstate->async_exc);
Guido van Rossumede04391998-04-10 20:18:25 +0000388
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000389 Py_CLEAR(tstate->curexc_type);
390 Py_CLEAR(tstate->curexc_value);
391 Py_CLEAR(tstate->curexc_traceback);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000392
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000393 Py_CLEAR(tstate->exc_type);
394 Py_CLEAR(tstate->exc_value);
395 Py_CLEAR(tstate->exc_traceback);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000396
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000397 tstate->c_profilefunc = NULL;
398 tstate->c_tracefunc = NULL;
399 Py_CLEAR(tstate->c_profileobj);
400 Py_CLEAR(tstate->c_traceobj);
Yury Selivanov75445082015-05-11 22:57:16 -0400401
402 Py_CLEAR(tstate->coroutine_wrapper);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000403}
404
405
Guido van Rossum29757862001-01-23 01:46:06 +0000406/* Common code for PyThreadState_Delete() and PyThreadState_DeleteCurrent() */
407static void
408tstate_delete_common(PyThreadState *tstate)
Guido van Rossuma027efa1997-05-05 20:56:21 +0000409{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000410 PyInterpreterState *interp;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000411 if (tstate == NULL)
412 Py_FatalError("PyThreadState_Delete: NULL tstate");
413 interp = tstate->interp;
414 if (interp == NULL)
415 Py_FatalError("PyThreadState_Delete: NULL interp");
416 HEAD_LOCK();
Charles-Francois Natalif28dfdd2013-05-08 21:09:52 +0200417 if (tstate->prev)
418 tstate->prev->next = tstate->next;
419 else
420 interp->tstate_head = tstate->next;
421 if (tstate->next)
422 tstate->next->prev = tstate->prev;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000423 HEAD_UNLOCK();
Antoine Pitrou7b476992013-09-07 23:38:37 +0200424 if (tstate->on_delete != NULL) {
425 tstate->on_delete(tstate->on_delete_data);
426 }
Victor Stinner1a7425f2013-07-07 16:25:15 +0200427 PyMem_RawFree(tstate);
Guido van Rossuma027efa1997-05-05 20:56:21 +0000428}
429
430
Guido van Rossum29757862001-01-23 01:46:06 +0000431void
432PyThreadState_Delete(PyThreadState *tstate)
433{
Victor Stinnerb02ef712016-01-22 14:09:55 +0100434 if (tstate == GET_TSTATE())
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000435 Py_FatalError("PyThreadState_Delete: tstate is still current");
Tim Petersf4e69282006-02-27 17:15:31 +0000436#ifdef WITH_THREAD
Antoine Pitrou079ce542010-09-08 12:37:10 +0000437 if (autoInterpreterState && PyThread_get_key_value(autoTLSkey) == tstate)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000438 PyThread_delete_key_value(autoTLSkey);
Tim Petersf4e69282006-02-27 17:15:31 +0000439#endif /* WITH_THREAD */
Christian Heimesb9dbc7d2013-07-01 23:42:28 +0200440 tstate_delete_common(tstate);
Guido van Rossum29757862001-01-23 01:46:06 +0000441}
442
443
444#ifdef WITH_THREAD
445void
446PyThreadState_DeleteCurrent()
447{
Victor Stinnerb02ef712016-01-22 14:09:55 +0100448 PyThreadState *tstate = GET_TSTATE();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000449 if (tstate == NULL)
450 Py_FatalError(
451 "PyThreadState_DeleteCurrent: no current tstate");
Victor Stinnerb02ef712016-01-22 14:09:55 +0100452 SET_TSTATE(NULL);
Antoine Pitrou079ce542010-09-08 12:37:10 +0000453 if (autoInterpreterState && PyThread_get_key_value(autoTLSkey) == tstate)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000454 PyThread_delete_key_value(autoTLSkey);
Christian Heimesb9dbc7d2013-07-01 23:42:28 +0200455 tstate_delete_common(tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000456 PyEval_ReleaseLock();
Guido van Rossum29757862001-01-23 01:46:06 +0000457}
458#endif /* WITH_THREAD */
459
460
Antoine Pitrou8408cea2013-05-05 23:47:09 +0200461/*
462 * Delete all thread states except the one passed as argument.
463 * Note that, if there is a current thread state, it *must* be the one
464 * passed as argument. Also, this won't touch any other interpreters
465 * than the current one, since we don't know which thread state should
466 * be kept in those other interpreteres.
467 */
468void
469_PyThreadState_DeleteExcept(PyThreadState *tstate)
470{
471 PyInterpreterState *interp = tstate->interp;
472 PyThreadState *p, *next, *garbage;
473 HEAD_LOCK();
474 /* Remove all thread states, except tstate, from the linked list of
475 thread states. This will allow calling PyThreadState_Clear()
Charles-Francois Natalif28dfdd2013-05-08 21:09:52 +0200476 without holding the lock. */
Antoine Pitrou8408cea2013-05-05 23:47:09 +0200477 garbage = interp->tstate_head;
Charles-Francois Natalif28dfdd2013-05-08 21:09:52 +0200478 if (garbage == tstate)
479 garbage = tstate->next;
480 if (tstate->prev)
481 tstate->prev->next = tstate->next;
482 if (tstate->next)
483 tstate->next->prev = tstate->prev;
484 tstate->prev = tstate->next = NULL;
Antoine Pitrou8408cea2013-05-05 23:47:09 +0200485 interp->tstate_head = tstate;
Antoine Pitrou8408cea2013-05-05 23:47:09 +0200486 HEAD_UNLOCK();
487 /* Clear and deallocate all stale thread states. Even if this
488 executes Python code, we should be safe since it executes
489 in the current thread, not one of the stale threads. */
490 for (p = garbage; p; p = next) {
491 next = p->next;
492 PyThreadState_Clear(p);
Victor Stinner1a7425f2013-07-07 16:25:15 +0200493 PyMem_RawFree(p);
Antoine Pitrou8408cea2013-05-05 23:47:09 +0200494 }
495}
496
497
Guido van Rossuma027efa1997-05-05 20:56:21 +0000498PyThreadState *
Victor Stinnerbfd316e2016-01-20 11:12:38 +0100499_PyThreadState_UncheckedGet(void)
500{
Victor Stinnerb02ef712016-01-22 14:09:55 +0100501 return GET_TSTATE();
Victor Stinnerbfd316e2016-01-20 11:12:38 +0100502}
503
504
505PyThreadState *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000506PyThreadState_Get(void)
Guido van Rossuma027efa1997-05-05 20:56:21 +0000507{
Victor Stinnerb02ef712016-01-22 14:09:55 +0100508 PyThreadState *tstate = GET_TSTATE();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000509 if (tstate == NULL)
510 Py_FatalError("PyThreadState_Get: no current thread");
Guido van Rossum25ce5661997-08-02 03:10:38 +0000511
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000512 return tstate;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000513}
514
515
516PyThreadState *
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000517PyThreadState_Swap(PyThreadState *newts)
Guido van Rossuma027efa1997-05-05 20:56:21 +0000518{
Victor Stinnerb02ef712016-01-22 14:09:55 +0100519 PyThreadState *oldts = GET_TSTATE();
Guido van Rossum25ce5661997-08-02 03:10:38 +0000520
Victor Stinnerb02ef712016-01-22 14:09:55 +0100521 SET_TSTATE(newts);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000522 /* It should not be possible for more than one thread state
523 to be used for a thread. Check this the best we can in debug
524 builds.
525 */
Martin v. Löwis9e296252003-05-01 05:25:29 +0000526#if defined(Py_DEBUG) && defined(WITH_THREAD)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000527 if (newts) {
528 /* This can be called from PyEval_RestoreThread(). Similar
529 to it, we need to ensure errno doesn't change.
530 */
531 int err = errno;
532 PyThreadState *check = PyGILState_GetThisThreadState();
533 if (check && check->interp == newts->interp && check != newts)
534 Py_FatalError("Invalid thread state for this thread");
535 errno = err;
536 }
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000537#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000538 return oldts;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000539}
Guido van Rossumede04391998-04-10 20:18:25 +0000540
541/* An extension mechanism to store arbitrary additional per-thread state.
542 PyThreadState_GetDict() returns a dictionary that can be used to hold such
543 state; the caller should pick a unique key and store its state there. If
Guido van Rossum0fc8f002003-04-15 15:12:39 +0000544 PyThreadState_GetDict() returns NULL, an exception has *not* been raised
545 and the caller should assume no per-thread state is available. */
Guido van Rossumede04391998-04-10 20:18:25 +0000546
547PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000548PyThreadState_GetDict(void)
Guido van Rossumede04391998-04-10 20:18:25 +0000549{
Victor Stinnerb02ef712016-01-22 14:09:55 +0100550 PyThreadState *tstate = GET_TSTATE();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000551 if (tstate == NULL)
552 return NULL;
Guido van Rossumede04391998-04-10 20:18:25 +0000553
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000554 if (tstate->dict == NULL) {
555 PyObject *d;
556 tstate->dict = d = PyDict_New();
557 if (d == NULL)
558 PyErr_Clear();
559 }
560 return tstate->dict;
Guido van Rossumede04391998-04-10 20:18:25 +0000561}
Guido van Rossumf5df46d2001-07-19 12:19:27 +0000562
563
Guido van Rossumb8b6d0c2003-06-28 21:53:52 +0000564/* Asynchronously raise an exception in a thread.
565 Requested by Just van Rossum and Alex Martelli.
Guido van Rossum0f1f63c2005-02-08 02:07:57 +0000566 To prevent naive misuse, you must write your own extension
Thomas Wouters00ee7ba2006-08-21 19:07:27 +0000567 to call this, or use ctypes. Must be called with the GIL held.
568 Returns the number of tstates modified (normally 1, but 0 if `id` didn't
569 match any known thread id). Can be called with exc=NULL to clear an
570 existing async exception. This raises no exceptions. */
Guido van Rossumb8b6d0c2003-06-28 21:53:52 +0000571
572int
573PyThreadState_SetAsyncExc(long id, PyObject *exc) {
Victor Stinnerb02ef712016-01-22 14:09:55 +0100574 PyInterpreterState *interp = GET_INTERP_STATE();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000575 PyThreadState *p;
Thomas Wouters00ee7ba2006-08-21 19:07:27 +0000576
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000577 /* Although the GIL is held, a few C API functions can be called
578 * without the GIL held, and in particular some that create and
579 * destroy thread and interpreter states. Those can mutate the
580 * list of thread states we're traversing, so to prevent that we lock
581 * head_mutex for the duration.
582 */
583 HEAD_LOCK();
584 for (p = interp->tstate_head; p != NULL; p = p->next) {
585 if (p->thread_id == id) {
586 /* Tricky: we need to decref the current value
587 * (if any) in p->async_exc, but that can in turn
588 * allow arbitrary Python code to run, including
589 * perhaps calls to this function. To prevent
590 * deadlock, we need to release head_mutex before
591 * the decref.
592 */
593 PyObject *old_exc = p->async_exc;
594 Py_XINCREF(exc);
595 p->async_exc = exc;
596 HEAD_UNLOCK();
597 Py_XDECREF(old_exc);
598 _PyEval_SignalAsyncExc();
599 return 1;
600 }
601 }
602 HEAD_UNLOCK();
603 return 0;
Guido van Rossumb8b6d0c2003-06-28 21:53:52 +0000604}
605
606
Guido van Rossumf5df46d2001-07-19 12:19:27 +0000607/* Routines for advanced debuggers, requested by David Beazley.
608 Don't use unless you know what you are doing! */
609
610PyInterpreterState *
611PyInterpreterState_Head(void)
612{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000613 return interp_head;
Guido van Rossumf5df46d2001-07-19 12:19:27 +0000614}
615
616PyInterpreterState *
617PyInterpreterState_Next(PyInterpreterState *interp) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000618 return interp->next;
Guido van Rossumf5df46d2001-07-19 12:19:27 +0000619}
620
621PyThreadState *
622PyInterpreterState_ThreadHead(PyInterpreterState *interp) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000623 return interp->tstate_head;
Guido van Rossumf5df46d2001-07-19 12:19:27 +0000624}
625
626PyThreadState *
627PyThreadState_Next(PyThreadState *tstate) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000628 return tstate->next;
Guido van Rossumf5df46d2001-07-19 12:19:27 +0000629}
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000630
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000631/* The implementation of sys._current_frames(). This is intended to be
632 called with the GIL held, as it will be when called via
633 sys._current_frames(). It's possible it would work fine even without
634 the GIL held, but haven't thought enough about that.
635*/
636PyObject *
637_PyThread_CurrentFrames(void)
638{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000639 PyObject *result;
640 PyInterpreterState *i;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000641
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000642 result = PyDict_New();
643 if (result == NULL)
644 return NULL;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000645
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000646 /* for i in all interpreters:
647 * for t in all of i's thread states:
648 * if t's frame isn't NULL, map t's id to its frame
Ezio Melotti13925002011-03-16 11:05:33 +0200649 * Because these lists can mutate even when the GIL is held, we
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000650 * need to grab head_mutex for the duration.
651 */
652 HEAD_LOCK();
653 for (i = interp_head; i != NULL; i = i->next) {
654 PyThreadState *t;
655 for (t = i->tstate_head; t != NULL; t = t->next) {
656 PyObject *id;
657 int stat;
658 struct _frame *frame = t->frame;
659 if (frame == NULL)
660 continue;
661 id = PyLong_FromLong(t->thread_id);
662 if (id == NULL)
663 goto Fail;
664 stat = PyDict_SetItem(result, id, (PyObject *)frame);
665 Py_DECREF(id);
666 if (stat < 0)
667 goto Fail;
668 }
669 }
670 HEAD_UNLOCK();
671 return result;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000672
673 Fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000674 HEAD_UNLOCK();
675 Py_DECREF(result);
676 return NULL;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000677}
Guido van Rossumb8b6d0c2003-06-28 21:53:52 +0000678
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000679/* Python "auto thread state" API. */
680#ifdef WITH_THREAD
681
682/* Keep this as a static, as it is not reliable! It can only
683 ever be compared to the state for the *current* thread.
684 * If not equal, then it doesn't matter that the actual
685 value may change immediately after comparison, as it can't
686 possibly change to the current thread's state.
687 * If equal, then the current thread holds the lock, so the value can't
688 change until we yield the lock.
689*/
690static int
691PyThreadState_IsCurrent(PyThreadState *tstate)
692{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000693 /* Must be the tstate for this thread */
694 assert(PyGILState_GetThisThreadState()==tstate);
Victor Stinnerb02ef712016-01-22 14:09:55 +0100695 return tstate == GET_TSTATE();
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000696}
697
Tim Peters4c1f5ec2004-10-09 17:25:05 +0000698/* Internal initialization/finalization functions called by
Martin Panterb4ce1fc2015-11-30 03:18:29 +0000699 Py_Initialize/Py_FinalizeEx
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000700*/
Tim Peters19717fa2004-10-09 17:38:29 +0000701void
702_PyGILState_Init(PyInterpreterState *i, PyThreadState *t)
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000703{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000704 assert(i && t); /* must init with valid states */
705 autoTLSkey = PyThread_create_key();
Kristján Valur Jónsson2fea9b92010-09-20 02:11:49 +0000706 if (autoTLSkey == -1)
707 Py_FatalError("Could not allocate TLS entry");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000708 autoInterpreterState = i;
709 assert(PyThread_get_key_value(autoTLSkey) == NULL);
710 assert(t->gilstate_counter == 0);
Michael W. Hudson188d4362005-06-20 16:52:57 +0000711
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000712 _PyGILState_NoteThreadState(t);
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000713}
714
Tim Peters19717fa2004-10-09 17:38:29 +0000715void
716_PyGILState_Fini(void)
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000717{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000718 PyThread_delete_key(autoTLSkey);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000719 autoInterpreterState = NULL;
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000720}
721
Charles-François Natalia233df82011-11-22 19:49:51 +0100722/* Reset the TLS key - called by PyOS_AfterFork().
Antoine Pitrou0c759fe2011-04-27 19:28:05 +0200723 * This should not be necessary, but some - buggy - pthread implementations
Charles-François Natalia233df82011-11-22 19:49:51 +0100724 * don't reset TLS upon fork(), see issue #10517.
Antoine Pitrou0c759fe2011-04-27 19:28:05 +0200725 */
726void
727_PyGILState_Reinit(void)
728{
729 PyThreadState *tstate = PyGILState_GetThisThreadState();
730 PyThread_delete_key(autoTLSkey);
731 if ((autoTLSkey = PyThread_create_key()) == -1)
732 Py_FatalError("Could not allocate TLS entry");
733
Charles-François Natalia233df82011-11-22 19:49:51 +0100734 /* If the thread had an associated auto thread state, reassociate it with
735 * the new key. */
736 if (tstate && PyThread_set_key_value(autoTLSkey, (void *)tstate) < 0)
Antoine Pitrou0c759fe2011-04-27 19:28:05 +0200737 Py_FatalError("Couldn't create autoTLSkey mapping");
738}
739
Michael W. Hudson188d4362005-06-20 16:52:57 +0000740/* When a thread state is created for a thread by some mechanism other than
741 PyGILState_Ensure, it's important that the GILState machinery knows about
742 it so it doesn't try to create another thread state for the thread (this is
743 a better fix for SF bug #1010677 than the first one attempted).
744*/
Thomas Wouters89f507f2006-12-13 04:49:30 +0000745static void
Michael W. Hudson188d4362005-06-20 16:52:57 +0000746_PyGILState_NoteThreadState(PyThreadState* tstate)
747{
Antoine Pitrou079ce542010-09-08 12:37:10 +0000748 /* If autoTLSkey isn't initialized, this must be the very first
749 threadstate created in Py_Initialize(). Don't do anything for now
750 (we'll be back here when _PyGILState_Init is called). */
751 if (!autoInterpreterState)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000752 return;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000753
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000754 /* Stick the thread state for this thread in thread local storage.
Michael W. Hudson188d4362005-06-20 16:52:57 +0000755
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000756 The only situation where you can legitimately have more than one
757 thread state for an OS level thread is when there are multiple
Victor Stinner590cebe2013-12-13 11:08:56 +0100758 interpreters.
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000759
Victor Stinner590cebe2013-12-13 11:08:56 +0100760 You shouldn't really be using the PyGILState_ APIs anyway (see issues
761 #10915 and #15751).
Michael W. Hudson188d4362005-06-20 16:52:57 +0000762
Victor Stinner590cebe2013-12-13 11:08:56 +0100763 The first thread state created for that given OS level thread will
764 "win", which seems reasonable behaviour.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000765 */
Victor Stinner590cebe2013-12-13 11:08:56 +0100766 if (PyThread_get_key_value(autoTLSkey) == NULL) {
767 if (PyThread_set_key_value(autoTLSkey, (void *)tstate) < 0)
768 Py_FatalError("Couldn't create autoTLSkey mapping");
769 }
Michael W. Hudson188d4362005-06-20 16:52:57 +0000770
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000771 /* PyGILState_Release must not try to delete this thread state. */
772 tstate->gilstate_counter = 1;
Michael W. Hudson188d4362005-06-20 16:52:57 +0000773}
774
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000775/* The public functions */
Tim Peters19717fa2004-10-09 17:38:29 +0000776PyThreadState *
777PyGILState_GetThisThreadState(void)
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000778{
Antoine Pitrou079ce542010-09-08 12:37:10 +0000779 if (autoInterpreterState == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000780 return NULL;
781 return (PyThreadState *)PyThread_get_key_value(autoTLSkey);
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000782}
783
Kristján Valur Jónsson684cd0e2013-03-23 03:36:16 -0700784int
785PyGILState_Check(void)
786{
Victor Stinnerb02ef712016-01-22 14:09:55 +0100787 PyThreadState *tstate = GET_TSTATE();
Kristján Valur Jónsson684cd0e2013-03-23 03:36:16 -0700788 return tstate && (tstate == PyGILState_GetThisThreadState());
789}
790
Tim Peters19717fa2004-10-09 17:38:29 +0000791PyGILState_STATE
792PyGILState_Ensure(void)
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000793{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000794 int current;
795 PyThreadState *tcur;
796 /* Note that we do not auto-init Python here - apart from
797 potential races with 2 threads auto-initializing, pep-311
798 spells out other issues. Embedders are expected to have
799 called Py_Initialize() and usually PyEval_InitThreads().
800 */
801 assert(autoInterpreterState); /* Py_Initialize() hasn't been called! */
802 tcur = (PyThreadState *)PyThread_get_key_value(autoTLSkey);
803 if (tcur == NULL) {
Victor Stinner62ca1002013-12-13 01:46:43 +0100804 /* At startup, Python has no concrete GIL. If PyGILState_Ensure() is
805 called from a new thread for the first time, we need the create the
806 GIL. */
807 PyEval_InitThreads();
808
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000809 /* Create a new thread state for this thread */
810 tcur = PyThreadState_New(autoInterpreterState);
811 if (tcur == NULL)
812 Py_FatalError("Couldn't create thread-state for new thread");
813 /* This is our thread state! We'll need to delete it in the
814 matching call to PyGILState_Release(). */
815 tcur->gilstate_counter = 0;
816 current = 0; /* new thread state is never current */
817 }
818 else
819 current = PyThreadState_IsCurrent(tcur);
820 if (current == 0)
821 PyEval_RestoreThread(tcur);
822 /* Update our counter in the thread-state - no need for locks:
823 - tcur will remain valid as we hold the GIL.
824 - the counter is safe as we are the only thread "allowed"
825 to modify this value
826 */
827 ++tcur->gilstate_counter;
828 return current ? PyGILState_LOCKED : PyGILState_UNLOCKED;
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000829}
830
Tim Peters19717fa2004-10-09 17:38:29 +0000831void
832PyGILState_Release(PyGILState_STATE oldstate)
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000833{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000834 PyThreadState *tcur = (PyThreadState *)PyThread_get_key_value(
835 autoTLSkey);
836 if (tcur == NULL)
837 Py_FatalError("auto-releasing thread-state, "
838 "but no thread-state for this thread");
839 /* We must hold the GIL and have our thread state current */
840 /* XXX - remove the check - the assert should be fine,
841 but while this is very new (April 2003), the extra check
842 by release-only users can't hurt.
843 */
844 if (! PyThreadState_IsCurrent(tcur))
845 Py_FatalError("This thread state must be current when releasing");
846 assert(PyThreadState_IsCurrent(tcur));
847 --tcur->gilstate_counter;
848 assert(tcur->gilstate_counter >= 0); /* illegal counter value */
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000849
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000850 /* If we're going to destroy this thread-state, we must
851 * clear it while the GIL is held, as destructors may run.
852 */
853 if (tcur->gilstate_counter == 0) {
854 /* can't have been locked when we created it */
855 assert(oldstate == PyGILState_UNLOCKED);
856 PyThreadState_Clear(tcur);
857 /* Delete the thread-state. Note this releases the GIL too!
858 * It's vital that the GIL be held here, to avoid shutdown
859 * races; see bugs 225673 and 1061968 (that nasty bug has a
860 * habit of coming back).
861 */
862 PyThreadState_DeleteCurrent();
863 }
864 /* Release the lock if necessary */
865 else if (oldstate == PyGILState_UNLOCKED)
866 PyEval_SaveThread();
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000867}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000868
Benjamin Peterson3bf01752012-04-13 18:06:36 -0400869#endif /* WITH_THREAD */
870
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000871#ifdef __cplusplus
872}
873#endif
874
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000875