blob: 65f9b7ea05dd79c71538dacaa6d024b6af8205e7 [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) \
Benjamin Petersonca470632016-09-06 13:47:26 -07009 _Py_atomic_store_relaxed(&_PyThreadState_Current, (uintptr_t)(value))
Victor Stinnerb02ef712016-01-22 14:09:55 +010010#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
Serhiy Storchakac2f7d872016-05-04 09:44:44 +030028#if !HAVE_DECL_RTLD_LAZY
Martin v. Löwisf0473d52001-07-18 16:17:16 +000029#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
Victor Stinner8a1be612016-03-14 22:07:55 +010037int _PyGILState_check_enabled = 1;
38
Guido van Rossum1d5ad901999-06-18 14:22:24 +000039#ifdef WITH_THREAD
40#include "pythread.h"
41static PyThread_type_lock head_mutex = NULL; /* Protects interp->tstate_head */
Moshe Zadka9fb6af92000-08-04 21:27:47 +000042#define HEAD_INIT() (void)(head_mutex || (head_mutex = PyThread_allocate_lock()))
Guido van Rossum1d5ad901999-06-18 14:22:24 +000043#define HEAD_LOCK() PyThread_acquire_lock(head_mutex, WAIT_LOCK)
44#define HEAD_UNLOCK() PyThread_release_lock(head_mutex)
Michael W. Hudson188d4362005-06-20 16:52:57 +000045
46/* The single PyInterpreterState used by this process'
47 GILState implementation
48*/
49static PyInterpreterState *autoInterpreterState = NULL;
Victor Stinner8a1be612016-03-14 22:07:55 +010050static int autoTLSkey = -1;
Guido van Rossum1d5ad901999-06-18 14:22:24 +000051#else
52#define HEAD_INIT() /* Nothing */
53#define HEAD_LOCK() /* Nothing */
54#define HEAD_UNLOCK() /* Nothing */
55#endif
56
Guido van Rossum25ce5661997-08-02 03:10:38 +000057static PyInterpreterState *interp_head = NULL;
Dino Viehland2997fec2017-06-12 18:46:35 -070058static __PyCodeExtraState *coextra_head = NULL;
Guido van Rossuma027efa1997-05-05 20:56:21 +000059
Jeffrey Yasskin39370832010-05-03 19:29:34 +000060/* Assuming the current thread holds the GIL, this is the
61 PyThreadState for the current thread. */
Victor Stinnerb02ef712016-01-22 14:09:55 +010062_Py_atomic_address _PyThreadState_Current = {0};
Guido van Rossum6297a7a2003-02-19 15:53:17 +000063PyThreadFrameGetter _PyThreadState_GetFrame = NULL;
Guido van Rossuma027efa1997-05-05 20:56:21 +000064
Michael W. Hudsonce7da6c2005-09-30 08:20:24 +000065#ifdef WITH_THREAD
Michael W. Hudson188d4362005-06-20 16:52:57 +000066static void _PyGILState_NoteThreadState(PyThreadState* tstate);
Michael W. Hudsonce7da6c2005-09-30 08:20:24 +000067#endif
Michael W. Hudson188d4362005-06-20 16:52:57 +000068
Guido van Rossuma027efa1997-05-05 20:56:21 +000069
70PyInterpreterState *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000071PyInterpreterState_New(void)
Guido van Rossuma027efa1997-05-05 20:56:21 +000072{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000073 PyInterpreterState *interp = (PyInterpreterState *)
Victor Stinner1a7425f2013-07-07 16:25:15 +020074 PyMem_RawMalloc(sizeof(PyInterpreterState));
Guido van Rossum25ce5661997-08-02 03:10:38 +000075
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000076 if (interp != NULL) {
Dino Viehland2997fec2017-06-12 18:46:35 -070077 __PyCodeExtraState* coextra = PyMem_RawMalloc(sizeof(__PyCodeExtraState));
78 if (coextra == NULL) {
79 PyMem_RawFree(interp);
80 return NULL;
81 }
82
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000083 HEAD_INIT();
Thomas Wouters0e3f5912006-08-11 14:57:12 +000084#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000085 if (head_mutex == NULL)
86 Py_FatalError("Can't initialize threads for interpreter");
Thomas Wouters0e3f5912006-08-11 14:57:12 +000087#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000088 interp->modules = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000089 interp->modules_by_index = NULL;
90 interp->sysdict = NULL;
91 interp->builtins = NULL;
Serhiy Storchaka87a5c512014-02-10 18:21:34 +020092 interp->builtins_copy = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000093 interp->tstate_head = NULL;
94 interp->codec_search_path = NULL;
95 interp->codec_search_cache = NULL;
96 interp->codec_error_registry = NULL;
97 interp->codecs_initialized = 0;
Victor Stinner3cbf14b2011-04-27 00:24:21 +020098 interp->fscodec_initialized = 0;
Brett Cannonfd074152012-04-14 14:10:13 -040099 interp->importlib = NULL;
Serhiy Storchaka133138a2016-08-02 22:51:21 +0300100 interp->import_func = NULL;
Brett Cannon3cebf932016-09-05 15:33:46 -0700101 interp->eval_frame = _PyEval_EvalFrameDefault;
Dino Viehland2997fec2017-06-12 18:46:35 -0700102 coextra->co_extra_user_count = 0;
103 coextra->interp = interp;
Martin v. Löwisf0473d52001-07-18 16:17:16 +0000104#ifdef HAVE_DLOPEN
Serhiy Storchakac2f7d872016-05-04 09:44:44 +0300105#if HAVE_DECL_RTLD_NOW
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000106 interp->dlopenflags = RTLD_NOW;
Martin v. Löwisf0473d52001-07-18 16:17:16 +0000107#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000108 interp->dlopenflags = RTLD_LAZY;
Martin v. Löwisf0473d52001-07-18 16:17:16 +0000109#endif
110#endif
Guido van Rossum25ce5661997-08-02 03:10:38 +0000111
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000112 HEAD_LOCK();
113 interp->next = interp_head;
114 interp_head = interp;
Dino Viehland2997fec2017-06-12 18:46:35 -0700115 coextra->next = coextra_head;
116 coextra_head = coextra;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000117 HEAD_UNLOCK();
118 }
Guido van Rossum25ce5661997-08-02 03:10:38 +0000119
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000120 return interp;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000121}
122
123
124void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000125PyInterpreterState_Clear(PyInterpreterState *interp)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000126{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000127 PyThreadState *p;
128 HEAD_LOCK();
129 for (p = interp->tstate_head; p != NULL; p = p->next)
130 PyThreadState_Clear(p);
131 HEAD_UNLOCK();
132 Py_CLEAR(interp->codec_search_path);
133 Py_CLEAR(interp->codec_search_cache);
134 Py_CLEAR(interp->codec_error_registry);
135 Py_CLEAR(interp->modules);
136 Py_CLEAR(interp->modules_by_index);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000137 Py_CLEAR(interp->sysdict);
138 Py_CLEAR(interp->builtins);
Serhiy Storchaka87a5c512014-02-10 18:21:34 +0200139 Py_CLEAR(interp->builtins_copy);
Brett Cannonfd074152012-04-14 14:10:13 -0400140 Py_CLEAR(interp->importlib);
Serhiy Storchaka133138a2016-08-02 22:51:21 +0300141 Py_CLEAR(interp->import_func);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000142}
143
144
145static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000146zapthreads(PyInterpreterState *interp)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000147{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000148 PyThreadState *p;
149 /* No need to lock the mutex here because this should only happen
150 when the threads are all really dead (XXX famous last words). */
151 while ((p = interp->tstate_head) != NULL) {
152 PyThreadState_Delete(p);
153 }
Guido van Rossum25ce5661997-08-02 03:10:38 +0000154}
155
156
157void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000158PyInterpreterState_Delete(PyInterpreterState *interp)
Guido van Rossuma027efa1997-05-05 20:56:21 +0000159{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000160 PyInterpreterState **p;
Dino Viehland2997fec2017-06-12 18:46:35 -0700161 __PyCodeExtraState **pextra;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000162 zapthreads(interp);
163 HEAD_LOCK();
Dino Viehland2997fec2017-06-12 18:46:35 -0700164 for (p = &interp_head; /* N/A */; p = &(*p)->next) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000165 if (*p == NULL)
166 Py_FatalError(
167 "PyInterpreterState_Delete: invalid interp");
168 if (*p == interp)
169 break;
170 }
171 if (interp->tstate_head != NULL)
172 Py_FatalError("PyInterpreterState_Delete: remaining threads");
173 *p = interp->next;
Dino Viehland2997fec2017-06-12 18:46:35 -0700174
175 for (pextra = &coextra_head; ; pextra = &(*pextra)->next) {
176 if (*pextra == NULL)
177 Py_FatalError(
178 "PyInterpreterState_Delete: invalid extra");
179 __PyCodeExtraState* extra = *pextra;
180 if (extra->interp == interp) {
181 *pextra = extra->next;
182 PyMem_RawFree(extra);
183 break;
184 }
185 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000186 HEAD_UNLOCK();
Victor Stinner1a7425f2013-07-07 16:25:15 +0200187 PyMem_RawFree(interp);
Antoine Pitrou8db076c2011-10-30 19:13:55 +0100188#ifdef WITH_THREAD
189 if (interp_head == NULL && head_mutex != NULL) {
190 PyThread_free_lock(head_mutex);
191 head_mutex = NULL;
192 }
193#endif
Guido van Rossuma027efa1997-05-05 20:56:21 +0000194}
195
196
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000197/* Default implementation for _PyThreadState_GetFrame */
198static struct _frame *
199threadstate_getframe(PyThreadState *self)
200{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000201 return self->frame;
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000202}
203
Victor Stinner45b9be52010-03-03 23:28:07 +0000204static PyThreadState *
205new_threadstate(PyInterpreterState *interp, int init)
Guido van Rossuma027efa1997-05-05 20:56:21 +0000206{
Victor Stinner1a7425f2013-07-07 16:25:15 +0200207 PyThreadState *tstate = (PyThreadState *)PyMem_RawMalloc(sizeof(PyThreadState));
Tim Peters84705582004-10-10 02:47:33 +0000208
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000209 if (_PyThreadState_GetFrame == NULL)
210 _PyThreadState_GetFrame = threadstate_getframe;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000211
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000212 if (tstate != NULL) {
213 tstate->interp = interp;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000214
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000215 tstate->frame = NULL;
216 tstate->recursion_depth = 0;
217 tstate->overflowed = 0;
218 tstate->recursion_critical = 0;
219 tstate->tracing = 0;
220 tstate->use_tracing = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000221 tstate->gilstate_counter = 0;
222 tstate->async_exc = NULL;
Martin v. Löwisf9ce67d2003-07-13 10:41:53 +0000223#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000224 tstate->thread_id = PyThread_get_thread_ident();
Martin v. Löwisf9ce67d2003-07-13 10:41:53 +0000225#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000226 tstate->thread_id = 0;
Martin v. Löwisf9ce67d2003-07-13 10:41:53 +0000227#endif
Guido van Rossuma027efa1997-05-05 20:56:21 +0000228
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000229 tstate->dict = NULL;
Guido van Rossumede04391998-04-10 20:18:25 +0000230
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000231 tstate->curexc_type = NULL;
232 tstate->curexc_value = NULL;
233 tstate->curexc_traceback = NULL;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000234
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000235 tstate->exc_type = NULL;
236 tstate->exc_value = NULL;
237 tstate->exc_traceback = NULL;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000238
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000239 tstate->c_profilefunc = NULL;
240 tstate->c_tracefunc = NULL;
241 tstate->c_profileobj = NULL;
242 tstate->c_traceobj = NULL;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000243
Antoine Pitrou2b0218a2012-09-06 00:59:49 +0200244 tstate->trash_delete_nesting = 0;
245 tstate->trash_delete_later = NULL;
Antoine Pitrou7b476992013-09-07 23:38:37 +0200246 tstate->on_delete = NULL;
247 tstate->on_delete_data = NULL;
Antoine Pitrou2b0218a2012-09-06 00:59:49 +0200248
Yury Selivanov75445082015-05-11 22:57:16 -0400249 tstate->coroutine_wrapper = NULL;
Yury Selivanovaab3c4a2015-06-02 18:43:51 -0400250 tstate->in_coroutine_wrapper = 0;
Yury Selivanov75445082015-05-11 22:57:16 -0400251
Yury Selivanoveb636452016-09-08 22:01:51 -0700252 tstate->async_gen_firstiter = NULL;
253 tstate->async_gen_finalizer = NULL;
254
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000255 if (init)
256 _PyThreadState_Init(tstate);
Michael W. Hudson188d4362005-06-20 16:52:57 +0000257
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000258 HEAD_LOCK();
Charles-Francois Natalif28dfdd2013-05-08 21:09:52 +0200259 tstate->prev = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000260 tstate->next = interp->tstate_head;
Charles-Francois Natalif28dfdd2013-05-08 21:09:52 +0200261 if (tstate->next)
262 tstate->next->prev = tstate;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000263 interp->tstate_head = tstate;
264 HEAD_UNLOCK();
265 }
Guido van Rossum25ce5661997-08-02 03:10:38 +0000266
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000267 return tstate;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000268}
269
Victor Stinner45b9be52010-03-03 23:28:07 +0000270PyThreadState *
271PyThreadState_New(PyInterpreterState *interp)
272{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000273 return new_threadstate(interp, 1);
Victor Stinner45b9be52010-03-03 23:28:07 +0000274}
275
276PyThreadState *
277_PyThreadState_Prealloc(PyInterpreterState *interp)
278{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000279 return new_threadstate(interp, 0);
Victor Stinner45b9be52010-03-03 23:28:07 +0000280}
281
282void
283_PyThreadState_Init(PyThreadState *tstate)
284{
285#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000286 _PyGILState_NoteThreadState(tstate);
Victor Stinner45b9be52010-03-03 23:28:07 +0000287#endif
288}
289
Martin v. Löwis1a214512008-06-11 05:26:20 +0000290PyObject*
Martin v. Löwis7800f752012-06-22 12:20:55 +0200291PyState_FindModule(struct PyModuleDef* module)
Martin v. Löwis1a214512008-06-11 05:26:20 +0000292{
Martin v. Löwis7800f752012-06-22 12:20:55 +0200293 Py_ssize_t index = module->m_base.m_index;
Victor Stinnerb02ef712016-01-22 14:09:55 +0100294 PyInterpreterState *state = GET_INTERP_STATE();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000295 PyObject *res;
Nick Coghland5cacbb2015-05-23 22:24:10 +1000296 if (module->m_slots) {
297 return NULL;
298 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000299 if (index == 0)
300 return NULL;
301 if (state->modules_by_index == NULL)
302 return NULL;
Antoine Pitrou75506e82012-08-20 19:30:46 +0200303 if (index >= PyList_GET_SIZE(state->modules_by_index))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000304 return NULL;
305 res = PyList_GET_ITEM(state->modules_by_index, index);
306 return res==Py_None ? NULL : res;
Martin v. Löwis1a214512008-06-11 05:26:20 +0000307}
308
309int
310_PyState_AddModule(PyObject* module, struct PyModuleDef* def)
311{
Nick Coghland5cacbb2015-05-23 22:24:10 +1000312 PyInterpreterState *state;
Berker Peksag4b7b5652016-08-22 18:05:56 +0300313 if (!def) {
314 assert(PyErr_Occurred());
315 return -1;
316 }
Nick Coghland5cacbb2015-05-23 22:24:10 +1000317 if (def->m_slots) {
318 PyErr_SetString(PyExc_SystemError,
319 "PyState_AddModule called on module with slots");
320 return -1;
321 }
Victor Stinnerb02ef712016-01-22 14:09:55 +0100322 state = GET_INTERP_STATE();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000323 if (!state->modules_by_index) {
324 state->modules_by_index = PyList_New(0);
325 if (!state->modules_by_index)
326 return -1;
327 }
328 while(PyList_GET_SIZE(state->modules_by_index) <= def->m_base.m_index)
329 if (PyList_Append(state->modules_by_index, Py_None) < 0)
330 return -1;
331 Py_INCREF(module);
332 return PyList_SetItem(state->modules_by_index,
333 def->m_base.m_index, module);
Martin v. Löwis1a214512008-06-11 05:26:20 +0000334}
Guido van Rossuma027efa1997-05-05 20:56:21 +0000335
Martin v. Löwis7800f752012-06-22 12:20:55 +0200336int
337PyState_AddModule(PyObject* module, struct PyModuleDef* def)
338{
339 Py_ssize_t index;
Victor Stinnerb02ef712016-01-22 14:09:55 +0100340 PyInterpreterState *state = GET_INTERP_STATE();
Martin v. Löwis7800f752012-06-22 12:20:55 +0200341 if (!def) {
342 Py_FatalError("PyState_AddModule: Module Definition is NULL");
343 return -1;
344 }
345 index = def->m_base.m_index;
346 if (state->modules_by_index) {
347 if(PyList_GET_SIZE(state->modules_by_index) >= index) {
348 if(module == PyList_GET_ITEM(state->modules_by_index, index)) {
349 Py_FatalError("PyState_AddModule: Module already added!");
350 return -1;
351 }
352 }
353 }
354 return _PyState_AddModule(module, def);
355}
356
357int
358PyState_RemoveModule(struct PyModuleDef* def)
359{
Nick Coghland5cacbb2015-05-23 22:24:10 +1000360 PyInterpreterState *state;
Martin v. Löwis7800f752012-06-22 12:20:55 +0200361 Py_ssize_t index = def->m_base.m_index;
Nick Coghland5cacbb2015-05-23 22:24:10 +1000362 if (def->m_slots) {
363 PyErr_SetString(PyExc_SystemError,
364 "PyState_RemoveModule called on module with slots");
365 return -1;
366 }
Victor Stinnerb02ef712016-01-22 14:09:55 +0100367 state = GET_INTERP_STATE();
Martin v. Löwis7800f752012-06-22 12:20:55 +0200368 if (index == 0) {
369 Py_FatalError("PyState_RemoveModule: Module index invalid.");
370 return -1;
371 }
372 if (state->modules_by_index == NULL) {
373 Py_FatalError("PyState_RemoveModule: Interpreters module-list not acessible.");
374 return -1;
375 }
376 if (index > PyList_GET_SIZE(state->modules_by_index)) {
377 Py_FatalError("PyState_RemoveModule: Module index out of bounds.");
378 return -1;
379 }
380 return PyList_SetItem(state->modules_by_index, index, Py_None);
381}
382
Antoine Pitrou40322e62013-08-11 00:30:09 +0200383/* used by import.c:PyImport_Cleanup */
384void
385_PyState_ClearModules(void)
386{
Victor Stinnerb02ef712016-01-22 14:09:55 +0100387 PyInterpreterState *state = GET_INTERP_STATE();
Antoine Pitrou40322e62013-08-11 00:30:09 +0200388 if (state->modules_by_index) {
389 Py_ssize_t i;
390 for (i = 0; i < PyList_GET_SIZE(state->modules_by_index); i++) {
391 PyObject *m = PyList_GET_ITEM(state->modules_by_index, i);
392 if (PyModule_Check(m)) {
393 /* cleanup the saved copy of module dicts */
394 PyModuleDef *md = PyModule_GetDef(m);
395 if (md)
396 Py_CLEAR(md->m_base.m_copy);
397 }
398 }
399 /* Setting modules_by_index to NULL could be dangerous, so we
400 clear the list instead. */
401 if (PyList_SetSlice(state->modules_by_index,
402 0, PyList_GET_SIZE(state->modules_by_index),
403 NULL))
404 PyErr_WriteUnraisable(state->modules_by_index);
405 }
406}
407
Guido van Rossuma027efa1997-05-05 20:56:21 +0000408void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000409PyThreadState_Clear(PyThreadState *tstate)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000410{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000411 if (Py_VerboseFlag && tstate->frame != NULL)
412 fprintf(stderr,
413 "PyThreadState_Clear: warning: thread still has a frame\n");
Guido van Rossum25ce5661997-08-02 03:10:38 +0000414
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000415 Py_CLEAR(tstate->frame);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000416
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000417 Py_CLEAR(tstate->dict);
418 Py_CLEAR(tstate->async_exc);
Guido van Rossumede04391998-04-10 20:18:25 +0000419
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000420 Py_CLEAR(tstate->curexc_type);
421 Py_CLEAR(tstate->curexc_value);
422 Py_CLEAR(tstate->curexc_traceback);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000423
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000424 Py_CLEAR(tstate->exc_type);
425 Py_CLEAR(tstate->exc_value);
426 Py_CLEAR(tstate->exc_traceback);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000427
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000428 tstate->c_profilefunc = NULL;
429 tstate->c_tracefunc = NULL;
430 Py_CLEAR(tstate->c_profileobj);
431 Py_CLEAR(tstate->c_traceobj);
Yury Selivanov75445082015-05-11 22:57:16 -0400432
433 Py_CLEAR(tstate->coroutine_wrapper);
Yury Selivanoveb636452016-09-08 22:01:51 -0700434 Py_CLEAR(tstate->async_gen_firstiter);
435 Py_CLEAR(tstate->async_gen_finalizer);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000436}
437
438
Guido van Rossum29757862001-01-23 01:46:06 +0000439/* Common code for PyThreadState_Delete() and PyThreadState_DeleteCurrent() */
440static void
441tstate_delete_common(PyThreadState *tstate)
Guido van Rossuma027efa1997-05-05 20:56:21 +0000442{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000443 PyInterpreterState *interp;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000444 if (tstate == NULL)
445 Py_FatalError("PyThreadState_Delete: NULL tstate");
446 interp = tstate->interp;
447 if (interp == NULL)
448 Py_FatalError("PyThreadState_Delete: NULL interp");
449 HEAD_LOCK();
Charles-Francois Natalif28dfdd2013-05-08 21:09:52 +0200450 if (tstate->prev)
451 tstate->prev->next = tstate->next;
452 else
453 interp->tstate_head = tstate->next;
454 if (tstate->next)
455 tstate->next->prev = tstate->prev;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000456 HEAD_UNLOCK();
Antoine Pitrou7b476992013-09-07 23:38:37 +0200457 if (tstate->on_delete != NULL) {
458 tstate->on_delete(tstate->on_delete_data);
459 }
Victor Stinner1a7425f2013-07-07 16:25:15 +0200460 PyMem_RawFree(tstate);
Guido van Rossuma027efa1997-05-05 20:56:21 +0000461}
462
463
Guido van Rossum29757862001-01-23 01:46:06 +0000464void
465PyThreadState_Delete(PyThreadState *tstate)
466{
Victor Stinnerb02ef712016-01-22 14:09:55 +0100467 if (tstate == GET_TSTATE())
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000468 Py_FatalError("PyThreadState_Delete: tstate is still current");
Tim Petersf4e69282006-02-27 17:15:31 +0000469#ifdef WITH_THREAD
Antoine Pitrou079ce542010-09-08 12:37:10 +0000470 if (autoInterpreterState && PyThread_get_key_value(autoTLSkey) == tstate)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000471 PyThread_delete_key_value(autoTLSkey);
Tim Petersf4e69282006-02-27 17:15:31 +0000472#endif /* WITH_THREAD */
Christian Heimesb9dbc7d2013-07-01 23:42:28 +0200473 tstate_delete_common(tstate);
Guido van Rossum29757862001-01-23 01:46:06 +0000474}
475
476
477#ifdef WITH_THREAD
478void
479PyThreadState_DeleteCurrent()
480{
Victor Stinnerb02ef712016-01-22 14:09:55 +0100481 PyThreadState *tstate = GET_TSTATE();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000482 if (tstate == NULL)
483 Py_FatalError(
484 "PyThreadState_DeleteCurrent: no current tstate");
Victor Stinner8a1be612016-03-14 22:07:55 +0100485 tstate_delete_common(tstate);
Antoine Pitrou079ce542010-09-08 12:37:10 +0000486 if (autoInterpreterState && PyThread_get_key_value(autoTLSkey) == tstate)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000487 PyThread_delete_key_value(autoTLSkey);
Victor Stinner8a1be612016-03-14 22:07:55 +0100488 SET_TSTATE(NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000489 PyEval_ReleaseLock();
Guido van Rossum29757862001-01-23 01:46:06 +0000490}
491#endif /* WITH_THREAD */
492
493
Antoine Pitrou8408cea2013-05-05 23:47:09 +0200494/*
495 * Delete all thread states except the one passed as argument.
496 * Note that, if there is a current thread state, it *must* be the one
497 * passed as argument. Also, this won't touch any other interpreters
498 * than the current one, since we don't know which thread state should
499 * be kept in those other interpreteres.
500 */
501void
502_PyThreadState_DeleteExcept(PyThreadState *tstate)
503{
504 PyInterpreterState *interp = tstate->interp;
505 PyThreadState *p, *next, *garbage;
506 HEAD_LOCK();
507 /* Remove all thread states, except tstate, from the linked list of
508 thread states. This will allow calling PyThreadState_Clear()
Charles-Francois Natalif28dfdd2013-05-08 21:09:52 +0200509 without holding the lock. */
Antoine Pitrou8408cea2013-05-05 23:47:09 +0200510 garbage = interp->tstate_head;
Charles-Francois Natalif28dfdd2013-05-08 21:09:52 +0200511 if (garbage == tstate)
512 garbage = tstate->next;
513 if (tstate->prev)
514 tstate->prev->next = tstate->next;
515 if (tstate->next)
516 tstate->next->prev = tstate->prev;
517 tstate->prev = tstate->next = NULL;
Antoine Pitrou8408cea2013-05-05 23:47:09 +0200518 interp->tstate_head = tstate;
Antoine Pitrou8408cea2013-05-05 23:47:09 +0200519 HEAD_UNLOCK();
520 /* Clear and deallocate all stale thread states. Even if this
521 executes Python code, we should be safe since it executes
522 in the current thread, not one of the stale threads. */
523 for (p = garbage; p; p = next) {
524 next = p->next;
525 PyThreadState_Clear(p);
Victor Stinner1a7425f2013-07-07 16:25:15 +0200526 PyMem_RawFree(p);
Antoine Pitrou8408cea2013-05-05 23:47:09 +0200527 }
528}
529
530
Guido van Rossuma027efa1997-05-05 20:56:21 +0000531PyThreadState *
Victor Stinnerbfd316e2016-01-20 11:12:38 +0100532_PyThreadState_UncheckedGet(void)
533{
Victor Stinnerb02ef712016-01-22 14:09:55 +0100534 return GET_TSTATE();
Victor Stinnerbfd316e2016-01-20 11:12:38 +0100535}
536
537
538PyThreadState *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000539PyThreadState_Get(void)
Guido van Rossuma027efa1997-05-05 20:56:21 +0000540{
Victor Stinnerb02ef712016-01-22 14:09:55 +0100541 PyThreadState *tstate = GET_TSTATE();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000542 if (tstate == NULL)
543 Py_FatalError("PyThreadState_Get: no current thread");
Guido van Rossum25ce5661997-08-02 03:10:38 +0000544
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000545 return tstate;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000546}
547
548
549PyThreadState *
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000550PyThreadState_Swap(PyThreadState *newts)
Guido van Rossuma027efa1997-05-05 20:56:21 +0000551{
Victor Stinnerb02ef712016-01-22 14:09:55 +0100552 PyThreadState *oldts = GET_TSTATE();
Guido van Rossum25ce5661997-08-02 03:10:38 +0000553
Victor Stinnerb02ef712016-01-22 14:09:55 +0100554 SET_TSTATE(newts);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000555 /* It should not be possible for more than one thread state
556 to be used for a thread. Check this the best we can in debug
557 builds.
558 */
Martin v. Löwis9e296252003-05-01 05:25:29 +0000559#if defined(Py_DEBUG) && defined(WITH_THREAD)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000560 if (newts) {
561 /* This can be called from PyEval_RestoreThread(). Similar
562 to it, we need to ensure errno doesn't change.
563 */
564 int err = errno;
565 PyThreadState *check = PyGILState_GetThisThreadState();
566 if (check && check->interp == newts->interp && check != newts)
567 Py_FatalError("Invalid thread state for this thread");
568 errno = err;
569 }
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000570#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000571 return oldts;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000572}
Guido van Rossumede04391998-04-10 20:18:25 +0000573
Victor Stinner932946c2017-06-13 10:39:30 +0200574__PyCodeExtraState*
575__PyCodeExtraState_Get(void) {
Dino Viehland2997fec2017-06-12 18:46:35 -0700576 PyInterpreterState* interp = PyThreadState_Get()->interp;
577
578 HEAD_LOCK();
579 for (__PyCodeExtraState* cur = coextra_head; cur != NULL; cur = cur->next) {
580 if (cur->interp == interp) {
581 HEAD_UNLOCK();
582 return cur;
583 }
584 }
585 HEAD_UNLOCK();
586
587 Py_FatalError("__PyCodeExtraState_Get: no code state for interpreter");
588 return NULL;
589}
590
Guido van Rossumede04391998-04-10 20:18:25 +0000591/* An extension mechanism to store arbitrary additional per-thread state.
592 PyThreadState_GetDict() returns a dictionary that can be used to hold such
593 state; the caller should pick a unique key and store its state there. If
Guido van Rossum0fc8f002003-04-15 15:12:39 +0000594 PyThreadState_GetDict() returns NULL, an exception has *not* been raised
595 and the caller should assume no per-thread state is available. */
Guido van Rossumede04391998-04-10 20:18:25 +0000596
597PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000598PyThreadState_GetDict(void)
Guido van Rossumede04391998-04-10 20:18:25 +0000599{
Victor Stinnerb02ef712016-01-22 14:09:55 +0100600 PyThreadState *tstate = GET_TSTATE();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000601 if (tstate == NULL)
602 return NULL;
Guido van Rossumede04391998-04-10 20:18:25 +0000603
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000604 if (tstate->dict == NULL) {
605 PyObject *d;
606 tstate->dict = d = PyDict_New();
607 if (d == NULL)
608 PyErr_Clear();
609 }
610 return tstate->dict;
Guido van Rossumede04391998-04-10 20:18:25 +0000611}
Guido van Rossumf5df46d2001-07-19 12:19:27 +0000612
613
Guido van Rossumb8b6d0c2003-06-28 21:53:52 +0000614/* Asynchronously raise an exception in a thread.
615 Requested by Just van Rossum and Alex Martelli.
Guido van Rossum0f1f63c2005-02-08 02:07:57 +0000616 To prevent naive misuse, you must write your own extension
Thomas Wouters00ee7ba2006-08-21 19:07:27 +0000617 to call this, or use ctypes. Must be called with the GIL held.
618 Returns the number of tstates modified (normally 1, but 0 if `id` didn't
619 match any known thread id). Can be called with exc=NULL to clear an
620 existing async exception. This raises no exceptions. */
Guido van Rossumb8b6d0c2003-06-28 21:53:52 +0000621
622int
623PyThreadState_SetAsyncExc(long id, PyObject *exc) {
Victor Stinnerb02ef712016-01-22 14:09:55 +0100624 PyInterpreterState *interp = GET_INTERP_STATE();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000625 PyThreadState *p;
Thomas Wouters00ee7ba2006-08-21 19:07:27 +0000626
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000627 /* Although the GIL is held, a few C API functions can be called
628 * without the GIL held, and in particular some that create and
629 * destroy thread and interpreter states. Those can mutate the
630 * list of thread states we're traversing, so to prevent that we lock
631 * head_mutex for the duration.
632 */
633 HEAD_LOCK();
634 for (p = interp->tstate_head; p != NULL; p = p->next) {
635 if (p->thread_id == id) {
636 /* Tricky: we need to decref the current value
637 * (if any) in p->async_exc, but that can in turn
638 * allow arbitrary Python code to run, including
639 * perhaps calls to this function. To prevent
640 * deadlock, we need to release head_mutex before
641 * the decref.
642 */
643 PyObject *old_exc = p->async_exc;
644 Py_XINCREF(exc);
645 p->async_exc = exc;
646 HEAD_UNLOCK();
647 Py_XDECREF(old_exc);
648 _PyEval_SignalAsyncExc();
649 return 1;
650 }
651 }
652 HEAD_UNLOCK();
653 return 0;
Guido van Rossumb8b6d0c2003-06-28 21:53:52 +0000654}
655
656
Guido van Rossumf5df46d2001-07-19 12:19:27 +0000657/* Routines for advanced debuggers, requested by David Beazley.
658 Don't use unless you know what you are doing! */
659
660PyInterpreterState *
661PyInterpreterState_Head(void)
662{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000663 return interp_head;
Guido van Rossumf5df46d2001-07-19 12:19:27 +0000664}
665
666PyInterpreterState *
667PyInterpreterState_Next(PyInterpreterState *interp) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000668 return interp->next;
Guido van Rossumf5df46d2001-07-19 12:19:27 +0000669}
670
671PyThreadState *
672PyInterpreterState_ThreadHead(PyInterpreterState *interp) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000673 return interp->tstate_head;
Guido van Rossumf5df46d2001-07-19 12:19:27 +0000674}
675
676PyThreadState *
677PyThreadState_Next(PyThreadState *tstate) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000678 return tstate->next;
Guido van Rossumf5df46d2001-07-19 12:19:27 +0000679}
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000680
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000681/* The implementation of sys._current_frames(). This is intended to be
682 called with the GIL held, as it will be when called via
683 sys._current_frames(). It's possible it would work fine even without
684 the GIL held, but haven't thought enough about that.
685*/
686PyObject *
687_PyThread_CurrentFrames(void)
688{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000689 PyObject *result;
690 PyInterpreterState *i;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000691
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000692 result = PyDict_New();
693 if (result == NULL)
694 return NULL;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000695
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000696 /* for i in all interpreters:
697 * for t in all of i's thread states:
698 * if t's frame isn't NULL, map t's id to its frame
Ezio Melotti13925002011-03-16 11:05:33 +0200699 * Because these lists can mutate even when the GIL is held, we
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000700 * need to grab head_mutex for the duration.
701 */
702 HEAD_LOCK();
703 for (i = interp_head; i != NULL; i = i->next) {
704 PyThreadState *t;
705 for (t = i->tstate_head; t != NULL; t = t->next) {
706 PyObject *id;
707 int stat;
708 struct _frame *frame = t->frame;
709 if (frame == NULL)
710 continue;
711 id = PyLong_FromLong(t->thread_id);
712 if (id == NULL)
713 goto Fail;
714 stat = PyDict_SetItem(result, id, (PyObject *)frame);
715 Py_DECREF(id);
716 if (stat < 0)
717 goto Fail;
718 }
719 }
720 HEAD_UNLOCK();
721 return result;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000722
723 Fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000724 HEAD_UNLOCK();
725 Py_DECREF(result);
726 return NULL;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000727}
Guido van Rossumb8b6d0c2003-06-28 21:53:52 +0000728
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000729/* Python "auto thread state" API. */
730#ifdef WITH_THREAD
731
732/* Keep this as a static, as it is not reliable! It can only
733 ever be compared to the state for the *current* thread.
734 * If not equal, then it doesn't matter that the actual
735 value may change immediately after comparison, as it can't
736 possibly change to the current thread's state.
737 * If equal, then the current thread holds the lock, so the value can't
738 change until we yield the lock.
739*/
740static int
741PyThreadState_IsCurrent(PyThreadState *tstate)
742{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000743 /* Must be the tstate for this thread */
744 assert(PyGILState_GetThisThreadState()==tstate);
Victor Stinnerb02ef712016-01-22 14:09:55 +0100745 return tstate == GET_TSTATE();
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000746}
747
Tim Peters4c1f5ec2004-10-09 17:25:05 +0000748/* Internal initialization/finalization functions called by
Martin Panterb4ce1fc2015-11-30 03:18:29 +0000749 Py_Initialize/Py_FinalizeEx
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000750*/
Tim Peters19717fa2004-10-09 17:38:29 +0000751void
752_PyGILState_Init(PyInterpreterState *i, PyThreadState *t)
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000753{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000754 assert(i && t); /* must init with valid states */
755 autoTLSkey = PyThread_create_key();
Kristján Valur Jónsson2fea9b92010-09-20 02:11:49 +0000756 if (autoTLSkey == -1)
757 Py_FatalError("Could not allocate TLS entry");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000758 autoInterpreterState = i;
759 assert(PyThread_get_key_value(autoTLSkey) == NULL);
760 assert(t->gilstate_counter == 0);
Michael W. Hudson188d4362005-06-20 16:52:57 +0000761
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000762 _PyGILState_NoteThreadState(t);
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000763}
764
Victor Stinner861d9ab2016-03-16 22:45:24 +0100765PyInterpreterState *
766_PyGILState_GetInterpreterStateUnsafe(void)
767{
768 return autoInterpreterState;
769}
770
Tim Peters19717fa2004-10-09 17:38:29 +0000771void
772_PyGILState_Fini(void)
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000773{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000774 PyThread_delete_key(autoTLSkey);
Victor Stinner8a1be612016-03-14 22:07:55 +0100775 autoTLSkey = -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000776 autoInterpreterState = NULL;
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000777}
778
Charles-François Natalia233df82011-11-22 19:49:51 +0100779/* Reset the TLS key - called by PyOS_AfterFork().
Antoine Pitrou0c759fe2011-04-27 19:28:05 +0200780 * This should not be necessary, but some - buggy - pthread implementations
Charles-François Natalia233df82011-11-22 19:49:51 +0100781 * don't reset TLS upon fork(), see issue #10517.
Antoine Pitrou0c759fe2011-04-27 19:28:05 +0200782 */
783void
784_PyGILState_Reinit(void)
785{
Łukasz Langad29fecc2017-05-22 22:23:05 -0700786#ifdef WITH_THREAD
787 head_mutex = NULL;
788 HEAD_INIT();
789#endif
Antoine Pitrou0c759fe2011-04-27 19:28:05 +0200790 PyThreadState *tstate = PyGILState_GetThisThreadState();
791 PyThread_delete_key(autoTLSkey);
792 if ((autoTLSkey = PyThread_create_key()) == -1)
793 Py_FatalError("Could not allocate TLS entry");
794
Charles-François Natalia233df82011-11-22 19:49:51 +0100795 /* If the thread had an associated auto thread state, reassociate it with
796 * the new key. */
797 if (tstate && PyThread_set_key_value(autoTLSkey, (void *)tstate) < 0)
Antoine Pitrou0c759fe2011-04-27 19:28:05 +0200798 Py_FatalError("Couldn't create autoTLSkey mapping");
799}
800
Michael W. Hudson188d4362005-06-20 16:52:57 +0000801/* When a thread state is created for a thread by some mechanism other than
802 PyGILState_Ensure, it's important that the GILState machinery knows about
803 it so it doesn't try to create another thread state for the thread (this is
804 a better fix for SF bug #1010677 than the first one attempted).
805*/
Thomas Wouters89f507f2006-12-13 04:49:30 +0000806static void
Michael W. Hudson188d4362005-06-20 16:52:57 +0000807_PyGILState_NoteThreadState(PyThreadState* tstate)
808{
Antoine Pitrou079ce542010-09-08 12:37:10 +0000809 /* If autoTLSkey isn't initialized, this must be the very first
810 threadstate created in Py_Initialize(). Don't do anything for now
811 (we'll be back here when _PyGILState_Init is called). */
812 if (!autoInterpreterState)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000813 return;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000814
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000815 /* Stick the thread state for this thread in thread local storage.
Michael W. Hudson188d4362005-06-20 16:52:57 +0000816
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000817 The only situation where you can legitimately have more than one
818 thread state for an OS level thread is when there are multiple
Victor Stinner590cebe2013-12-13 11:08:56 +0100819 interpreters.
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000820
Victor Stinner590cebe2013-12-13 11:08:56 +0100821 You shouldn't really be using the PyGILState_ APIs anyway (see issues
822 #10915 and #15751).
Michael W. Hudson188d4362005-06-20 16:52:57 +0000823
Victor Stinner590cebe2013-12-13 11:08:56 +0100824 The first thread state created for that given OS level thread will
825 "win", which seems reasonable behaviour.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000826 */
Victor Stinner590cebe2013-12-13 11:08:56 +0100827 if (PyThread_get_key_value(autoTLSkey) == NULL) {
828 if (PyThread_set_key_value(autoTLSkey, (void *)tstate) < 0)
829 Py_FatalError("Couldn't create autoTLSkey mapping");
830 }
Michael W. Hudson188d4362005-06-20 16:52:57 +0000831
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000832 /* PyGILState_Release must not try to delete this thread state. */
833 tstate->gilstate_counter = 1;
Michael W. Hudson188d4362005-06-20 16:52:57 +0000834}
835
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000836/* The public functions */
Tim Peters19717fa2004-10-09 17:38:29 +0000837PyThreadState *
838PyGILState_GetThisThreadState(void)
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000839{
Antoine Pitrou079ce542010-09-08 12:37:10 +0000840 if (autoInterpreterState == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000841 return NULL;
842 return (PyThreadState *)PyThread_get_key_value(autoTLSkey);
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000843}
844
Kristján Valur Jónsson684cd0e2013-03-23 03:36:16 -0700845int
846PyGILState_Check(void)
847{
Victor Stinner8a1be612016-03-14 22:07:55 +0100848 PyThreadState *tstate;
849
850 if (!_PyGILState_check_enabled)
851 return 1;
852
853 if (autoTLSkey == -1)
854 return 1;
855
856 tstate = GET_TSTATE();
857 if (tstate == NULL)
858 return 0;
859
860 return (tstate == PyGILState_GetThisThreadState());
Kristján Valur Jónsson684cd0e2013-03-23 03:36:16 -0700861}
862
Tim Peters19717fa2004-10-09 17:38:29 +0000863PyGILState_STATE
864PyGILState_Ensure(void)
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000865{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000866 int current;
867 PyThreadState *tcur;
868 /* Note that we do not auto-init Python here - apart from
869 potential races with 2 threads auto-initializing, pep-311
870 spells out other issues. Embedders are expected to have
871 called Py_Initialize() and usually PyEval_InitThreads().
872 */
873 assert(autoInterpreterState); /* Py_Initialize() hasn't been called! */
874 tcur = (PyThreadState *)PyThread_get_key_value(autoTLSkey);
875 if (tcur == NULL) {
Victor Stinner62ca1002013-12-13 01:46:43 +0100876 /* At startup, Python has no concrete GIL. If PyGILState_Ensure() is
877 called from a new thread for the first time, we need the create the
878 GIL. */
879 PyEval_InitThreads();
880
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000881 /* Create a new thread state for this thread */
882 tcur = PyThreadState_New(autoInterpreterState);
883 if (tcur == NULL)
884 Py_FatalError("Couldn't create thread-state for new thread");
885 /* This is our thread state! We'll need to delete it in the
886 matching call to PyGILState_Release(). */
887 tcur->gilstate_counter = 0;
888 current = 0; /* new thread state is never current */
889 }
890 else
891 current = PyThreadState_IsCurrent(tcur);
892 if (current == 0)
893 PyEval_RestoreThread(tcur);
894 /* Update our counter in the thread-state - no need for locks:
895 - tcur will remain valid as we hold the GIL.
896 - the counter is safe as we are the only thread "allowed"
897 to modify this value
898 */
899 ++tcur->gilstate_counter;
900 return current ? PyGILState_LOCKED : PyGILState_UNLOCKED;
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000901}
902
Tim Peters19717fa2004-10-09 17:38:29 +0000903void
904PyGILState_Release(PyGILState_STATE oldstate)
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000905{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000906 PyThreadState *tcur = (PyThreadState *)PyThread_get_key_value(
907 autoTLSkey);
908 if (tcur == NULL)
909 Py_FatalError("auto-releasing thread-state, "
910 "but no thread-state for this thread");
911 /* We must hold the GIL and have our thread state current */
912 /* XXX - remove the check - the assert should be fine,
913 but while this is very new (April 2003), the extra check
914 by release-only users can't hurt.
915 */
916 if (! PyThreadState_IsCurrent(tcur))
917 Py_FatalError("This thread state must be current when releasing");
918 assert(PyThreadState_IsCurrent(tcur));
919 --tcur->gilstate_counter;
920 assert(tcur->gilstate_counter >= 0); /* illegal counter value */
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000921
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000922 /* If we're going to destroy this thread-state, we must
923 * clear it while the GIL is held, as destructors may run.
924 */
925 if (tcur->gilstate_counter == 0) {
926 /* can't have been locked when we created it */
927 assert(oldstate == PyGILState_UNLOCKED);
928 PyThreadState_Clear(tcur);
929 /* Delete the thread-state. Note this releases the GIL too!
930 * It's vital that the GIL be held here, to avoid shutdown
931 * races; see bugs 225673 and 1061968 (that nasty bug has a
932 * habit of coming back).
933 */
934 PyThreadState_DeleteCurrent();
935 }
936 /* Release the lock if necessary */
937 else if (oldstate == PyGILState_UNLOCKED)
938 PyEval_SaveThread();
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000939}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000940
Benjamin Peterson3bf01752012-04-13 18:06:36 -0400941#endif /* WITH_THREAD */
942
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000943#ifdef __cplusplus
944}
945#endif
946
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000947