blob: 3d3207702f8accbb9b1a0e374b20b6d4c0ed08c2 [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
Eric Snow76d5abc2017-09-05 18:26:16 -070037void
38_PyRuntimeState_Init(_PyRuntimeState *runtime)
39{
40 _PyRuntimeState initial = {};
41 *runtime = initial;
42
43 _PyObject_Initialize(&runtime->obj);
44 _PyMem_Initialize(&runtime->mem);
45 _PyGC_Initialize(&runtime->gc);
46 _PyEval_Initialize(&runtime->ceval);
47
48 runtime->gilstate.check_enabled = 1;
49 runtime->gilstate.autoTLSkey = -1;
Victor Stinner8a1be612016-03-14 22:07:55 +010050
Guido van Rossum1d5ad901999-06-18 14:22:24 +000051#ifdef WITH_THREAD
Eric Snow76d5abc2017-09-05 18:26:16 -070052 runtime->interpreters.mutex = PyThread_allocate_lock();
53 if (runtime->interpreters.mutex == NULL)
54 Py_FatalError("Can't initialize threads for interpreter");
55#endif
56 runtime->interpreters.next_id = -1;
57}
Michael W. Hudson188d4362005-06-20 16:52:57 +000058
Eric Snow76d5abc2017-09-05 18:26:16 -070059void
60_PyRuntimeState_Fini(_PyRuntimeState *runtime)
61{
62#ifdef WITH_THREAD
63 if (runtime->interpreters.mutex != NULL) {
64 PyThread_free_lock(runtime->interpreters.mutex);
65 runtime->interpreters.mutex = NULL;
66 }
67#endif
68}
69
70#ifdef WITH_THREAD
71#define HEAD_LOCK() PyThread_acquire_lock(_PyRuntime.interpreters.mutex, \
72 WAIT_LOCK)
73#define HEAD_UNLOCK() PyThread_release_lock(_PyRuntime.interpreters.mutex)
Guido van Rossum1d5ad901999-06-18 14:22:24 +000074#else
Guido van Rossum1d5ad901999-06-18 14:22:24 +000075#define HEAD_LOCK() /* Nothing */
76#define HEAD_UNLOCK() /* Nothing */
77#endif
78
Michael W. Hudsonce7da6c2005-09-30 08:20:24 +000079#ifdef WITH_THREAD
Michael W. Hudson188d4362005-06-20 16:52:57 +000080static void _PyGILState_NoteThreadState(PyThreadState* tstate);
Michael W. Hudsonce7da6c2005-09-30 08:20:24 +000081#endif
Michael W. Hudson188d4362005-06-20 16:52:57 +000082
Eric Snowe3774162017-05-22 19:46:40 -070083void
Eric Snow76d5abc2017-09-05 18:26:16 -070084_PyInterpreterState_Enable(_PyRuntimeState *runtime)
Eric Snowe3774162017-05-22 19:46:40 -070085{
Eric Snow76d5abc2017-09-05 18:26:16 -070086 runtime->interpreters.next_id = 0;
87#ifdef WITH_THREAD
88 /* Since we only call _PyRuntimeState_Init() once per process
89 (see _PyRuntime_Initialize()), we make sure the mutex is
90 initialized here. */
91 if (runtime->interpreters.mutex == NULL) {
92 runtime->interpreters.mutex = PyThread_allocate_lock();
93 if (runtime->interpreters.mutex == NULL)
94 Py_FatalError("Can't initialize threads for interpreter");
95 }
96#endif
Eric Snowe3774162017-05-22 19:46:40 -070097}
Guido van Rossuma027efa1997-05-05 20:56:21 +000098
99PyInterpreterState *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000100PyInterpreterState_New(void)
Guido van Rossuma027efa1997-05-05 20:56:21 +0000101{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000102 PyInterpreterState *interp = (PyInterpreterState *)
Victor Stinner1a7425f2013-07-07 16:25:15 +0200103 PyMem_RawMalloc(sizeof(PyInterpreterState));
Guido van Rossum25ce5661997-08-02 03:10:38 +0000104
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000105 if (interp != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000106 interp->modules_by_index = NULL;
107 interp->sysdict = NULL;
108 interp->builtins = NULL;
Serhiy Storchaka87a5c512014-02-10 18:21:34 +0200109 interp->builtins_copy = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000110 interp->tstate_head = NULL;
Eric Snow76d5abc2017-09-05 18:26:16 -0700111 interp->check_interval = 100;
112 interp->warnoptions = NULL;
113 interp->xoptions = NULL;
114 interp->num_threads = 0;
115 interp->pythread_stacksize = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000116 interp->codec_search_path = NULL;
117 interp->codec_search_cache = NULL;
118 interp->codec_error_registry = NULL;
119 interp->codecs_initialized = 0;
Victor Stinner3cbf14b2011-04-27 00:24:21 +0200120 interp->fscodec_initialized = 0;
Brett Cannonfd074152012-04-14 14:10:13 -0400121 interp->importlib = NULL;
Serhiy Storchaka133138a2016-08-02 22:51:21 +0300122 interp->import_func = NULL;
Brett Cannon3cebf932016-09-05 15:33:46 -0700123 interp->eval_frame = _PyEval_EvalFrameDefault;
Dino Viehlandf3cffd22017-06-21 14:44:36 -0700124 interp->co_extra_user_count = 0;
Martin v. Löwisf0473d52001-07-18 16:17:16 +0000125#ifdef HAVE_DLOPEN
Serhiy Storchakac2f7d872016-05-04 09:44:44 +0300126#if HAVE_DECL_RTLD_NOW
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000127 interp->dlopenflags = RTLD_NOW;
Martin v. Löwisf0473d52001-07-18 16:17:16 +0000128#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000129 interp->dlopenflags = RTLD_LAZY;
Martin v. Löwisf0473d52001-07-18 16:17:16 +0000130#endif
131#endif
Antoine Pitrou346cbd32017-05-27 17:50:54 +0200132#ifdef HAVE_FORK
133 interp->before_forkers = NULL;
134 interp->after_forkers_parent = NULL;
135 interp->after_forkers_child = NULL;
136#endif
Guido van Rossum25ce5661997-08-02 03:10:38 +0000137
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000138 HEAD_LOCK();
Eric Snow76d5abc2017-09-05 18:26:16 -0700139 interp->next = _PyRuntime.interpreters.head;
140 if (_PyRuntime.interpreters.main == NULL) {
141 _PyRuntime.interpreters.main = interp;
Eric Snow6b4be192017-05-22 21:36:03 -0700142 }
Eric Snow76d5abc2017-09-05 18:26:16 -0700143 _PyRuntime.interpreters.head = interp;
144 if (_PyRuntime.interpreters.next_id < 0) {
Eric Snowe3774162017-05-22 19:46:40 -0700145 /* overflow or Py_Initialize() not called! */
146 PyErr_SetString(PyExc_RuntimeError,
147 "failed to get an interpreter ID");
148 interp = NULL;
149 } else {
Eric Snow76d5abc2017-09-05 18:26:16 -0700150 interp->id = _PyRuntime.interpreters.next_id;
151 _PyRuntime.interpreters.next_id += 1;
Eric Snowe3774162017-05-22 19:46:40 -0700152 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000153 HEAD_UNLOCK();
154 }
Guido van Rossum25ce5661997-08-02 03:10:38 +0000155
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000156 return interp;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000157}
158
159
160void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000161PyInterpreterState_Clear(PyInterpreterState *interp)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000162{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000163 PyThreadState *p;
164 HEAD_LOCK();
165 for (p = interp->tstate_head; p != NULL; p = p->next)
166 PyThreadState_Clear(p);
167 HEAD_UNLOCK();
168 Py_CLEAR(interp->codec_search_path);
169 Py_CLEAR(interp->codec_search_cache);
170 Py_CLEAR(interp->codec_error_registry);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000171 Py_CLEAR(interp->modules_by_index);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000172 Py_CLEAR(interp->sysdict);
173 Py_CLEAR(interp->builtins);
Serhiy Storchaka87a5c512014-02-10 18:21:34 +0200174 Py_CLEAR(interp->builtins_copy);
Brett Cannonfd074152012-04-14 14:10:13 -0400175 Py_CLEAR(interp->importlib);
Serhiy Storchaka133138a2016-08-02 22:51:21 +0300176 Py_CLEAR(interp->import_func);
Antoine Pitrou346cbd32017-05-27 17:50:54 +0200177#ifdef HAVE_FORK
178 Py_CLEAR(interp->before_forkers);
179 Py_CLEAR(interp->after_forkers_parent);
180 Py_CLEAR(interp->after_forkers_child);
181#endif
Guido van Rossum25ce5661997-08-02 03:10:38 +0000182}
183
184
185static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000186zapthreads(PyInterpreterState *interp)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000187{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000188 PyThreadState *p;
189 /* No need to lock the mutex here because this should only happen
190 when the threads are all really dead (XXX famous last words). */
191 while ((p = interp->tstate_head) != NULL) {
192 PyThreadState_Delete(p);
193 }
Guido van Rossum25ce5661997-08-02 03:10:38 +0000194}
195
196
197void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000198PyInterpreterState_Delete(PyInterpreterState *interp)
Guido van Rossuma027efa1997-05-05 20:56:21 +0000199{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000200 PyInterpreterState **p;
201 zapthreads(interp);
202 HEAD_LOCK();
Eric Snow76d5abc2017-09-05 18:26:16 -0700203 for (p = &_PyRuntime.interpreters.head; ; p = &(*p)->next) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000204 if (*p == NULL)
205 Py_FatalError(
206 "PyInterpreterState_Delete: invalid interp");
207 if (*p == interp)
208 break;
209 }
210 if (interp->tstate_head != NULL)
211 Py_FatalError("PyInterpreterState_Delete: remaining threads");
212 *p = interp->next;
Eric Snow76d5abc2017-09-05 18:26:16 -0700213 if (_PyRuntime.interpreters.main == interp) {
214 _PyRuntime.interpreters.main = NULL;
215 if (_PyRuntime.interpreters.head != NULL)
Eric Snow6b4be192017-05-22 21:36:03 -0700216 Py_FatalError("PyInterpreterState_Delete: remaining subinterpreters");
217 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000218 HEAD_UNLOCK();
Victor Stinner1a7425f2013-07-07 16:25:15 +0200219 PyMem_RawFree(interp);
Guido van Rossuma027efa1997-05-05 20:56:21 +0000220}
221
222
Eric Snowe3774162017-05-22 19:46:40 -0700223int64_t
224PyInterpreterState_GetID(PyInterpreterState *interp)
225{
226 if (interp == NULL) {
227 PyErr_SetString(PyExc_RuntimeError, "no interpreter provided");
228 return -1;
229 }
230 return interp->id;
231}
232
233
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000234/* Default implementation for _PyThreadState_GetFrame */
235static struct _frame *
236threadstate_getframe(PyThreadState *self)
237{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000238 return self->frame;
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000239}
240
Victor Stinner45b9be52010-03-03 23:28:07 +0000241static PyThreadState *
242new_threadstate(PyInterpreterState *interp, int init)
Guido van Rossuma027efa1997-05-05 20:56:21 +0000243{
Victor Stinner1a7425f2013-07-07 16:25:15 +0200244 PyThreadState *tstate = (PyThreadState *)PyMem_RawMalloc(sizeof(PyThreadState));
Tim Peters84705582004-10-10 02:47:33 +0000245
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000246 if (_PyThreadState_GetFrame == NULL)
247 _PyThreadState_GetFrame = threadstate_getframe;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000248
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000249 if (tstate != NULL) {
250 tstate->interp = interp;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000251
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000252 tstate->frame = NULL;
253 tstate->recursion_depth = 0;
254 tstate->overflowed = 0;
255 tstate->recursion_critical = 0;
256 tstate->tracing = 0;
257 tstate->use_tracing = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000258 tstate->gilstate_counter = 0;
259 tstate->async_exc = NULL;
Martin v. Löwisf9ce67d2003-07-13 10:41:53 +0000260#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000261 tstate->thread_id = PyThread_get_thread_ident();
Martin v. Löwisf9ce67d2003-07-13 10:41:53 +0000262#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000263 tstate->thread_id = 0;
Martin v. Löwisf9ce67d2003-07-13 10:41:53 +0000264#endif
Guido van Rossuma027efa1997-05-05 20:56:21 +0000265
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000266 tstate->dict = NULL;
Guido van Rossumede04391998-04-10 20:18:25 +0000267
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000268 tstate->curexc_type = NULL;
269 tstate->curexc_value = NULL;
270 tstate->curexc_traceback = NULL;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000271
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000272 tstate->exc_type = NULL;
273 tstate->exc_value = NULL;
274 tstate->exc_traceback = NULL;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000275
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000276 tstate->c_profilefunc = NULL;
277 tstate->c_tracefunc = NULL;
278 tstate->c_profileobj = NULL;
279 tstate->c_traceobj = NULL;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000280
Antoine Pitrou2b0218a2012-09-06 00:59:49 +0200281 tstate->trash_delete_nesting = 0;
282 tstate->trash_delete_later = NULL;
Antoine Pitrou7b476992013-09-07 23:38:37 +0200283 tstate->on_delete = NULL;
284 tstate->on_delete_data = NULL;
Antoine Pitrou2b0218a2012-09-06 00:59:49 +0200285
Yury Selivanov75445082015-05-11 22:57:16 -0400286 tstate->coroutine_wrapper = NULL;
Yury Selivanovaab3c4a2015-06-02 18:43:51 -0400287 tstate->in_coroutine_wrapper = 0;
Yury Selivanov75445082015-05-11 22:57:16 -0400288
Yury Selivanoveb636452016-09-08 22:01:51 -0700289 tstate->async_gen_firstiter = NULL;
290 tstate->async_gen_finalizer = NULL;
291
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000292 if (init)
293 _PyThreadState_Init(tstate);
Michael W. Hudson188d4362005-06-20 16:52:57 +0000294
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000295 HEAD_LOCK();
Charles-Francois Natalif28dfdd2013-05-08 21:09:52 +0200296 tstate->prev = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000297 tstate->next = interp->tstate_head;
Charles-Francois Natalif28dfdd2013-05-08 21:09:52 +0200298 if (tstate->next)
299 tstate->next->prev = tstate;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000300 interp->tstate_head = tstate;
301 HEAD_UNLOCK();
302 }
Guido van Rossum25ce5661997-08-02 03:10:38 +0000303
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000304 return tstate;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000305}
306
Victor Stinner45b9be52010-03-03 23:28:07 +0000307PyThreadState *
308PyThreadState_New(PyInterpreterState *interp)
309{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000310 return new_threadstate(interp, 1);
Victor Stinner45b9be52010-03-03 23:28:07 +0000311}
312
313PyThreadState *
314_PyThreadState_Prealloc(PyInterpreterState *interp)
315{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000316 return new_threadstate(interp, 0);
Victor Stinner45b9be52010-03-03 23:28:07 +0000317}
318
319void
320_PyThreadState_Init(PyThreadState *tstate)
321{
322#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000323 _PyGILState_NoteThreadState(tstate);
Victor Stinner45b9be52010-03-03 23:28:07 +0000324#endif
325}
326
Martin v. Löwis1a214512008-06-11 05:26:20 +0000327PyObject*
Martin v. Löwis7800f752012-06-22 12:20:55 +0200328PyState_FindModule(struct PyModuleDef* module)
Martin v. Löwis1a214512008-06-11 05:26:20 +0000329{
Martin v. Löwis7800f752012-06-22 12:20:55 +0200330 Py_ssize_t index = module->m_base.m_index;
Victor Stinnerb02ef712016-01-22 14:09:55 +0100331 PyInterpreterState *state = GET_INTERP_STATE();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000332 PyObject *res;
Nick Coghland5cacbb2015-05-23 22:24:10 +1000333 if (module->m_slots) {
334 return NULL;
335 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000336 if (index == 0)
337 return NULL;
338 if (state->modules_by_index == NULL)
339 return NULL;
Antoine Pitrou75506e82012-08-20 19:30:46 +0200340 if (index >= PyList_GET_SIZE(state->modules_by_index))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000341 return NULL;
342 res = PyList_GET_ITEM(state->modules_by_index, index);
343 return res==Py_None ? NULL : res;
Martin v. Löwis1a214512008-06-11 05:26:20 +0000344}
345
346int
347_PyState_AddModule(PyObject* module, struct PyModuleDef* def)
348{
Nick Coghland5cacbb2015-05-23 22:24:10 +1000349 PyInterpreterState *state;
Berker Peksag4b7b5652016-08-22 18:05:56 +0300350 if (!def) {
351 assert(PyErr_Occurred());
352 return -1;
353 }
Nick Coghland5cacbb2015-05-23 22:24:10 +1000354 if (def->m_slots) {
355 PyErr_SetString(PyExc_SystemError,
356 "PyState_AddModule called on module with slots");
357 return -1;
358 }
Victor Stinnerb02ef712016-01-22 14:09:55 +0100359 state = GET_INTERP_STATE();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000360 if (!state->modules_by_index) {
361 state->modules_by_index = PyList_New(0);
362 if (!state->modules_by_index)
363 return -1;
364 }
365 while(PyList_GET_SIZE(state->modules_by_index) <= def->m_base.m_index)
366 if (PyList_Append(state->modules_by_index, Py_None) < 0)
367 return -1;
368 Py_INCREF(module);
369 return PyList_SetItem(state->modules_by_index,
370 def->m_base.m_index, module);
Martin v. Löwis1a214512008-06-11 05:26:20 +0000371}
Guido van Rossuma027efa1997-05-05 20:56:21 +0000372
Martin v. Löwis7800f752012-06-22 12:20:55 +0200373int
374PyState_AddModule(PyObject* module, struct PyModuleDef* def)
375{
376 Py_ssize_t index;
Victor Stinnerb02ef712016-01-22 14:09:55 +0100377 PyInterpreterState *state = GET_INTERP_STATE();
Martin v. Löwis7800f752012-06-22 12:20:55 +0200378 if (!def) {
379 Py_FatalError("PyState_AddModule: Module Definition is NULL");
380 return -1;
381 }
382 index = def->m_base.m_index;
383 if (state->modules_by_index) {
384 if(PyList_GET_SIZE(state->modules_by_index) >= index) {
385 if(module == PyList_GET_ITEM(state->modules_by_index, index)) {
386 Py_FatalError("PyState_AddModule: Module already added!");
387 return -1;
388 }
389 }
390 }
391 return _PyState_AddModule(module, def);
392}
393
394int
395PyState_RemoveModule(struct PyModuleDef* def)
396{
Nick Coghland5cacbb2015-05-23 22:24:10 +1000397 PyInterpreterState *state;
Martin v. Löwis7800f752012-06-22 12:20:55 +0200398 Py_ssize_t index = def->m_base.m_index;
Nick Coghland5cacbb2015-05-23 22:24:10 +1000399 if (def->m_slots) {
400 PyErr_SetString(PyExc_SystemError,
401 "PyState_RemoveModule called on module with slots");
402 return -1;
403 }
Victor Stinnerb02ef712016-01-22 14:09:55 +0100404 state = GET_INTERP_STATE();
Martin v. Löwis7800f752012-06-22 12:20:55 +0200405 if (index == 0) {
406 Py_FatalError("PyState_RemoveModule: Module index invalid.");
407 return -1;
408 }
409 if (state->modules_by_index == NULL) {
410 Py_FatalError("PyState_RemoveModule: Interpreters module-list not acessible.");
411 return -1;
412 }
413 if (index > PyList_GET_SIZE(state->modules_by_index)) {
414 Py_FatalError("PyState_RemoveModule: Module index out of bounds.");
415 return -1;
416 }
417 return PyList_SetItem(state->modules_by_index, index, Py_None);
418}
419
Antoine Pitrou40322e62013-08-11 00:30:09 +0200420/* used by import.c:PyImport_Cleanup */
421void
422_PyState_ClearModules(void)
423{
Victor Stinnerb02ef712016-01-22 14:09:55 +0100424 PyInterpreterState *state = GET_INTERP_STATE();
Antoine Pitrou40322e62013-08-11 00:30:09 +0200425 if (state->modules_by_index) {
426 Py_ssize_t i;
427 for (i = 0; i < PyList_GET_SIZE(state->modules_by_index); i++) {
428 PyObject *m = PyList_GET_ITEM(state->modules_by_index, i);
429 if (PyModule_Check(m)) {
430 /* cleanup the saved copy of module dicts */
431 PyModuleDef *md = PyModule_GetDef(m);
432 if (md)
433 Py_CLEAR(md->m_base.m_copy);
434 }
435 }
436 /* Setting modules_by_index to NULL could be dangerous, so we
437 clear the list instead. */
438 if (PyList_SetSlice(state->modules_by_index,
439 0, PyList_GET_SIZE(state->modules_by_index),
440 NULL))
441 PyErr_WriteUnraisable(state->modules_by_index);
442 }
443}
444
Guido van Rossuma027efa1997-05-05 20:56:21 +0000445void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000446PyThreadState_Clear(PyThreadState *tstate)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000447{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000448 if (Py_VerboseFlag && tstate->frame != NULL)
449 fprintf(stderr,
450 "PyThreadState_Clear: warning: thread still has a frame\n");
Guido van Rossum25ce5661997-08-02 03:10:38 +0000451
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000452 Py_CLEAR(tstate->frame);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000453
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000454 Py_CLEAR(tstate->dict);
455 Py_CLEAR(tstate->async_exc);
Guido van Rossumede04391998-04-10 20:18:25 +0000456
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000457 Py_CLEAR(tstate->curexc_type);
458 Py_CLEAR(tstate->curexc_value);
459 Py_CLEAR(tstate->curexc_traceback);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000460
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000461 Py_CLEAR(tstate->exc_type);
462 Py_CLEAR(tstate->exc_value);
463 Py_CLEAR(tstate->exc_traceback);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000464
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000465 tstate->c_profilefunc = NULL;
466 tstate->c_tracefunc = NULL;
467 Py_CLEAR(tstate->c_profileobj);
468 Py_CLEAR(tstate->c_traceobj);
Yury Selivanov75445082015-05-11 22:57:16 -0400469
470 Py_CLEAR(tstate->coroutine_wrapper);
Yury Selivanoveb636452016-09-08 22:01:51 -0700471 Py_CLEAR(tstate->async_gen_firstiter);
472 Py_CLEAR(tstate->async_gen_finalizer);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000473}
474
475
Guido van Rossum29757862001-01-23 01:46:06 +0000476/* Common code for PyThreadState_Delete() and PyThreadState_DeleteCurrent() */
477static void
478tstate_delete_common(PyThreadState *tstate)
Guido van Rossuma027efa1997-05-05 20:56:21 +0000479{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000480 PyInterpreterState *interp;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000481 if (tstate == NULL)
482 Py_FatalError("PyThreadState_Delete: NULL tstate");
483 interp = tstate->interp;
484 if (interp == NULL)
485 Py_FatalError("PyThreadState_Delete: NULL interp");
486 HEAD_LOCK();
Charles-Francois Natalif28dfdd2013-05-08 21:09:52 +0200487 if (tstate->prev)
488 tstate->prev->next = tstate->next;
489 else
490 interp->tstate_head = tstate->next;
491 if (tstate->next)
492 tstate->next->prev = tstate->prev;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000493 HEAD_UNLOCK();
Antoine Pitrou7b476992013-09-07 23:38:37 +0200494 if (tstate->on_delete != NULL) {
495 tstate->on_delete(tstate->on_delete_data);
496 }
Victor Stinner1a7425f2013-07-07 16:25:15 +0200497 PyMem_RawFree(tstate);
Guido van Rossuma027efa1997-05-05 20:56:21 +0000498}
499
500
Guido van Rossum29757862001-01-23 01:46:06 +0000501void
502PyThreadState_Delete(PyThreadState *tstate)
503{
Victor Stinnerb02ef712016-01-22 14:09:55 +0100504 if (tstate == GET_TSTATE())
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000505 Py_FatalError("PyThreadState_Delete: tstate is still current");
Tim Petersf4e69282006-02-27 17:15:31 +0000506#ifdef WITH_THREAD
Eric Snow76d5abc2017-09-05 18:26:16 -0700507 if (_PyRuntime.gilstate.autoInterpreterState &&
508 PyThread_get_key_value(_PyRuntime.gilstate.autoTLSkey) == tstate)
509 {
510 PyThread_delete_key_value(_PyRuntime.gilstate.autoTLSkey);
511 }
Tim Petersf4e69282006-02-27 17:15:31 +0000512#endif /* WITH_THREAD */
Christian Heimesb9dbc7d2013-07-01 23:42:28 +0200513 tstate_delete_common(tstate);
Guido van Rossum29757862001-01-23 01:46:06 +0000514}
515
516
517#ifdef WITH_THREAD
518void
519PyThreadState_DeleteCurrent()
520{
Victor Stinnerb02ef712016-01-22 14:09:55 +0100521 PyThreadState *tstate = GET_TSTATE();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000522 if (tstate == NULL)
523 Py_FatalError(
524 "PyThreadState_DeleteCurrent: no current tstate");
Victor Stinner8a1be612016-03-14 22:07:55 +0100525 tstate_delete_common(tstate);
Eric Snow76d5abc2017-09-05 18:26:16 -0700526 if (_PyRuntime.gilstate.autoInterpreterState &&
527 PyThread_get_key_value(_PyRuntime.gilstate.autoTLSkey) == tstate)
528 {
529 PyThread_delete_key_value(_PyRuntime.gilstate.autoTLSkey);
530 }
Victor Stinner8a1be612016-03-14 22:07:55 +0100531 SET_TSTATE(NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000532 PyEval_ReleaseLock();
Guido van Rossum29757862001-01-23 01:46:06 +0000533}
534#endif /* WITH_THREAD */
535
536
Antoine Pitrou8408cea2013-05-05 23:47:09 +0200537/*
538 * Delete all thread states except the one passed as argument.
539 * Note that, if there is a current thread state, it *must* be the one
540 * passed as argument. Also, this won't touch any other interpreters
541 * than the current one, since we don't know which thread state should
542 * be kept in those other interpreteres.
543 */
544void
545_PyThreadState_DeleteExcept(PyThreadState *tstate)
546{
547 PyInterpreterState *interp = tstate->interp;
548 PyThreadState *p, *next, *garbage;
549 HEAD_LOCK();
550 /* Remove all thread states, except tstate, from the linked list of
551 thread states. This will allow calling PyThreadState_Clear()
Charles-Francois Natalif28dfdd2013-05-08 21:09:52 +0200552 without holding the lock. */
Antoine Pitrou8408cea2013-05-05 23:47:09 +0200553 garbage = interp->tstate_head;
Charles-Francois Natalif28dfdd2013-05-08 21:09:52 +0200554 if (garbage == tstate)
555 garbage = tstate->next;
556 if (tstate->prev)
557 tstate->prev->next = tstate->next;
558 if (tstate->next)
559 tstate->next->prev = tstate->prev;
560 tstate->prev = tstate->next = NULL;
Antoine Pitrou8408cea2013-05-05 23:47:09 +0200561 interp->tstate_head = tstate;
Antoine Pitrou8408cea2013-05-05 23:47:09 +0200562 HEAD_UNLOCK();
563 /* Clear and deallocate all stale thread states. Even if this
564 executes Python code, we should be safe since it executes
565 in the current thread, not one of the stale threads. */
566 for (p = garbage; p; p = next) {
567 next = p->next;
568 PyThreadState_Clear(p);
Victor Stinner1a7425f2013-07-07 16:25:15 +0200569 PyMem_RawFree(p);
Antoine Pitrou8408cea2013-05-05 23:47:09 +0200570 }
571}
572
573
Guido van Rossuma027efa1997-05-05 20:56:21 +0000574PyThreadState *
Victor Stinnerbfd316e2016-01-20 11:12:38 +0100575_PyThreadState_UncheckedGet(void)
576{
Victor Stinnerb02ef712016-01-22 14:09:55 +0100577 return GET_TSTATE();
Victor Stinnerbfd316e2016-01-20 11:12:38 +0100578}
579
580
581PyThreadState *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000582PyThreadState_Get(void)
Guido van Rossuma027efa1997-05-05 20:56:21 +0000583{
Victor Stinnerb02ef712016-01-22 14:09:55 +0100584 PyThreadState *tstate = GET_TSTATE();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000585 if (tstate == NULL)
586 Py_FatalError("PyThreadState_Get: no current thread");
Guido van Rossum25ce5661997-08-02 03:10:38 +0000587
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000588 return tstate;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000589}
590
591
592PyThreadState *
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000593PyThreadState_Swap(PyThreadState *newts)
Guido van Rossuma027efa1997-05-05 20:56:21 +0000594{
Victor Stinnerb02ef712016-01-22 14:09:55 +0100595 PyThreadState *oldts = GET_TSTATE();
Guido van Rossum25ce5661997-08-02 03:10:38 +0000596
Victor Stinnerb02ef712016-01-22 14:09:55 +0100597 SET_TSTATE(newts);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000598 /* It should not be possible for more than one thread state
599 to be used for a thread. Check this the best we can in debug
600 builds.
601 */
Martin v. Löwis9e296252003-05-01 05:25:29 +0000602#if defined(Py_DEBUG) && defined(WITH_THREAD)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000603 if (newts) {
604 /* This can be called from PyEval_RestoreThread(). Similar
605 to it, we need to ensure errno doesn't change.
606 */
607 int err = errno;
608 PyThreadState *check = PyGILState_GetThisThreadState();
609 if (check && check->interp == newts->interp && check != newts)
610 Py_FatalError("Invalid thread state for this thread");
611 errno = err;
612 }
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000613#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000614 return oldts;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000615}
Guido van Rossumede04391998-04-10 20:18:25 +0000616
617/* An extension mechanism to store arbitrary additional per-thread state.
618 PyThreadState_GetDict() returns a dictionary that can be used to hold such
619 state; the caller should pick a unique key and store its state there. If
Guido van Rossum0fc8f002003-04-15 15:12:39 +0000620 PyThreadState_GetDict() returns NULL, an exception has *not* been raised
621 and the caller should assume no per-thread state is available. */
Guido van Rossumede04391998-04-10 20:18:25 +0000622
623PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000624PyThreadState_GetDict(void)
Guido van Rossumede04391998-04-10 20:18:25 +0000625{
Victor Stinnerb02ef712016-01-22 14:09:55 +0100626 PyThreadState *tstate = GET_TSTATE();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000627 if (tstate == NULL)
628 return NULL;
Guido van Rossumede04391998-04-10 20:18:25 +0000629
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000630 if (tstate->dict == NULL) {
631 PyObject *d;
632 tstate->dict = d = PyDict_New();
633 if (d == NULL)
634 PyErr_Clear();
635 }
636 return tstate->dict;
Guido van Rossumede04391998-04-10 20:18:25 +0000637}
Guido van Rossumf5df46d2001-07-19 12:19:27 +0000638
639
Guido van Rossumb8b6d0c2003-06-28 21:53:52 +0000640/* Asynchronously raise an exception in a thread.
641 Requested by Just van Rossum and Alex Martelli.
Guido van Rossum0f1f63c2005-02-08 02:07:57 +0000642 To prevent naive misuse, you must write your own extension
Thomas Wouters00ee7ba2006-08-21 19:07:27 +0000643 to call this, or use ctypes. Must be called with the GIL held.
644 Returns the number of tstates modified (normally 1, but 0 if `id` didn't
645 match any known thread id). Can be called with exc=NULL to clear an
646 existing async exception. This raises no exceptions. */
Guido van Rossumb8b6d0c2003-06-28 21:53:52 +0000647
648int
Serhiy Storchakaaefa7eb2017-03-23 15:48:39 +0200649PyThreadState_SetAsyncExc(unsigned long id, PyObject *exc)
650{
Victor Stinnerb02ef712016-01-22 14:09:55 +0100651 PyInterpreterState *interp = GET_INTERP_STATE();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000652 PyThreadState *p;
Thomas Wouters00ee7ba2006-08-21 19:07:27 +0000653
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000654 /* Although the GIL is held, a few C API functions can be called
655 * without the GIL held, and in particular some that create and
656 * destroy thread and interpreter states. Those can mutate the
657 * list of thread states we're traversing, so to prevent that we lock
658 * head_mutex for the duration.
659 */
660 HEAD_LOCK();
661 for (p = interp->tstate_head; p != NULL; p = p->next) {
662 if (p->thread_id == id) {
663 /* Tricky: we need to decref the current value
664 * (if any) in p->async_exc, but that can in turn
665 * allow arbitrary Python code to run, including
666 * perhaps calls to this function. To prevent
667 * deadlock, we need to release head_mutex before
668 * the decref.
669 */
670 PyObject *old_exc = p->async_exc;
671 Py_XINCREF(exc);
672 p->async_exc = exc;
673 HEAD_UNLOCK();
674 Py_XDECREF(old_exc);
675 _PyEval_SignalAsyncExc();
676 return 1;
677 }
678 }
679 HEAD_UNLOCK();
680 return 0;
Guido van Rossumb8b6d0c2003-06-28 21:53:52 +0000681}
682
683
Guido van Rossumf5df46d2001-07-19 12:19:27 +0000684/* Routines for advanced debuggers, requested by David Beazley.
685 Don't use unless you know what you are doing! */
686
687PyInterpreterState *
688PyInterpreterState_Head(void)
689{
Eric Snow76d5abc2017-09-05 18:26:16 -0700690 return _PyRuntime.interpreters.head;
Guido van Rossumf5df46d2001-07-19 12:19:27 +0000691}
692
693PyInterpreterState *
Eric Snow6b4be192017-05-22 21:36:03 -0700694PyInterpreterState_Main(void)
695{
Eric Snow76d5abc2017-09-05 18:26:16 -0700696 return _PyRuntime.interpreters.main;
Eric Snow6b4be192017-05-22 21:36:03 -0700697}
698
699PyInterpreterState *
Guido van Rossumf5df46d2001-07-19 12:19:27 +0000700PyInterpreterState_Next(PyInterpreterState *interp) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000701 return interp->next;
Guido van Rossumf5df46d2001-07-19 12:19:27 +0000702}
703
704PyThreadState *
705PyInterpreterState_ThreadHead(PyInterpreterState *interp) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000706 return interp->tstate_head;
Guido van Rossumf5df46d2001-07-19 12:19:27 +0000707}
708
709PyThreadState *
710PyThreadState_Next(PyThreadState *tstate) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000711 return tstate->next;
Guido van Rossumf5df46d2001-07-19 12:19:27 +0000712}
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000713
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000714/* The implementation of sys._current_frames(). This is intended to be
715 called with the GIL held, as it will be when called via
716 sys._current_frames(). It's possible it would work fine even without
717 the GIL held, but haven't thought enough about that.
718*/
719PyObject *
720_PyThread_CurrentFrames(void)
721{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000722 PyObject *result;
723 PyInterpreterState *i;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000724
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000725 result = PyDict_New();
726 if (result == NULL)
727 return NULL;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000728
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000729 /* for i in all interpreters:
730 * for t in all of i's thread states:
731 * if t's frame isn't NULL, map t's id to its frame
Ezio Melotti13925002011-03-16 11:05:33 +0200732 * Because these lists can mutate even when the GIL is held, we
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000733 * need to grab head_mutex for the duration.
734 */
735 HEAD_LOCK();
Eric Snow76d5abc2017-09-05 18:26:16 -0700736 for (i = _PyRuntime.interpreters.head; i != NULL; i = i->next) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000737 PyThreadState *t;
738 for (t = i->tstate_head; t != NULL; t = t->next) {
739 PyObject *id;
740 int stat;
741 struct _frame *frame = t->frame;
742 if (frame == NULL)
743 continue;
Serhiy Storchakaaefa7eb2017-03-23 15:48:39 +0200744 id = PyLong_FromUnsignedLong(t->thread_id);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000745 if (id == NULL)
746 goto Fail;
747 stat = PyDict_SetItem(result, id, (PyObject *)frame);
748 Py_DECREF(id);
749 if (stat < 0)
750 goto Fail;
751 }
752 }
753 HEAD_UNLOCK();
754 return result;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000755
756 Fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000757 HEAD_UNLOCK();
758 Py_DECREF(result);
759 return NULL;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000760}
Guido van Rossumb8b6d0c2003-06-28 21:53:52 +0000761
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000762/* Python "auto thread state" API. */
763#ifdef WITH_THREAD
764
765/* Keep this as a static, as it is not reliable! It can only
766 ever be compared to the state for the *current* thread.
767 * If not equal, then it doesn't matter that the actual
768 value may change immediately after comparison, as it can't
769 possibly change to the current thread's state.
770 * If equal, then the current thread holds the lock, so the value can't
771 change until we yield the lock.
772*/
773static int
774PyThreadState_IsCurrent(PyThreadState *tstate)
775{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000776 /* Must be the tstate for this thread */
777 assert(PyGILState_GetThisThreadState()==tstate);
Victor Stinnerb02ef712016-01-22 14:09:55 +0100778 return tstate == GET_TSTATE();
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000779}
780
Tim Peters4c1f5ec2004-10-09 17:25:05 +0000781/* Internal initialization/finalization functions called by
Martin Panterb4ce1fc2015-11-30 03:18:29 +0000782 Py_Initialize/Py_FinalizeEx
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000783*/
Tim Peters19717fa2004-10-09 17:38:29 +0000784void
785_PyGILState_Init(PyInterpreterState *i, PyThreadState *t)
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000786{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000787 assert(i && t); /* must init with valid states */
Eric Snow76d5abc2017-09-05 18:26:16 -0700788 _PyRuntime.gilstate.autoTLSkey = PyThread_create_key();
789 if (_PyRuntime.gilstate.autoTLSkey == -1)
Kristján Valur Jónsson2fea9b92010-09-20 02:11:49 +0000790 Py_FatalError("Could not allocate TLS entry");
Eric Snow76d5abc2017-09-05 18:26:16 -0700791 _PyRuntime.gilstate.autoInterpreterState = i;
792 assert(PyThread_get_key_value(_PyRuntime.gilstate.autoTLSkey) == NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000793 assert(t->gilstate_counter == 0);
Michael W. Hudson188d4362005-06-20 16:52:57 +0000794
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000795 _PyGILState_NoteThreadState(t);
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000796}
797
Victor Stinner861d9ab2016-03-16 22:45:24 +0100798PyInterpreterState *
799_PyGILState_GetInterpreterStateUnsafe(void)
800{
Eric Snow76d5abc2017-09-05 18:26:16 -0700801 return _PyRuntime.gilstate.autoInterpreterState;
Victor Stinner861d9ab2016-03-16 22:45:24 +0100802}
803
Tim Peters19717fa2004-10-09 17:38:29 +0000804void
805_PyGILState_Fini(void)
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000806{
Eric Snow76d5abc2017-09-05 18:26:16 -0700807 PyThread_delete_key(_PyRuntime.gilstate.autoTLSkey);
808 _PyRuntime.gilstate.autoTLSkey = -1;
809 _PyRuntime.gilstate.autoInterpreterState = NULL;
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000810}
811
Antoine Pitrouf7ecfac2017-05-28 11:35:14 +0200812/* Reset the TLS key - called by PyOS_AfterFork_Child().
Antoine Pitrou0c759fe2011-04-27 19:28:05 +0200813 * This should not be necessary, but some - buggy - pthread implementations
Charles-François Natalia233df82011-11-22 19:49:51 +0100814 * don't reset TLS upon fork(), see issue #10517.
Antoine Pitrou0c759fe2011-04-27 19:28:05 +0200815 */
816void
817_PyGILState_Reinit(void)
818{
Jason Friedf82c9512017-05-22 16:58:55 -0700819#ifdef WITH_THREAD
Eric Snow76d5abc2017-09-05 18:26:16 -0700820 _PyRuntime.interpreters.mutex = PyThread_allocate_lock();
821 if (_PyRuntime.interpreters.mutex == NULL)
822 Py_FatalError("Can't initialize threads for interpreter");
Jason Friedf82c9512017-05-22 16:58:55 -0700823#endif
Antoine Pitrou0c759fe2011-04-27 19:28:05 +0200824 PyThreadState *tstate = PyGILState_GetThisThreadState();
Eric Snow76d5abc2017-09-05 18:26:16 -0700825 PyThread_delete_key(_PyRuntime.gilstate.autoTLSkey);
826 if ((_PyRuntime.gilstate.autoTLSkey = PyThread_create_key()) == -1)
Antoine Pitrou0c759fe2011-04-27 19:28:05 +0200827 Py_FatalError("Could not allocate TLS entry");
828
Charles-François Natalia233df82011-11-22 19:49:51 +0100829 /* If the thread had an associated auto thread state, reassociate it with
830 * the new key. */
Eric Snow76d5abc2017-09-05 18:26:16 -0700831 if (tstate && PyThread_set_key_value(_PyRuntime.gilstate.autoTLSkey,
832 (void *)tstate) < 0)
Antoine Pitrou0c759fe2011-04-27 19:28:05 +0200833 Py_FatalError("Couldn't create autoTLSkey mapping");
834}
835
Michael W. Hudson188d4362005-06-20 16:52:57 +0000836/* When a thread state is created for a thread by some mechanism other than
837 PyGILState_Ensure, it's important that the GILState machinery knows about
838 it so it doesn't try to create another thread state for the thread (this is
839 a better fix for SF bug #1010677 than the first one attempted).
840*/
Thomas Wouters89f507f2006-12-13 04:49:30 +0000841static void
Michael W. Hudson188d4362005-06-20 16:52:57 +0000842_PyGILState_NoteThreadState(PyThreadState* tstate)
843{
Antoine Pitrou079ce542010-09-08 12:37:10 +0000844 /* If autoTLSkey isn't initialized, this must be the very first
845 threadstate created in Py_Initialize(). Don't do anything for now
846 (we'll be back here when _PyGILState_Init is called). */
Eric Snow76d5abc2017-09-05 18:26:16 -0700847 if (!_PyRuntime.gilstate.autoInterpreterState)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000848 return;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000849
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000850 /* Stick the thread state for this thread in thread local storage.
Michael W. Hudson188d4362005-06-20 16:52:57 +0000851
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000852 The only situation where you can legitimately have more than one
853 thread state for an OS level thread is when there are multiple
Victor Stinner590cebe2013-12-13 11:08:56 +0100854 interpreters.
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000855
Victor Stinner590cebe2013-12-13 11:08:56 +0100856 You shouldn't really be using the PyGILState_ APIs anyway (see issues
857 #10915 and #15751).
Michael W. Hudson188d4362005-06-20 16:52:57 +0000858
Victor Stinner590cebe2013-12-13 11:08:56 +0100859 The first thread state created for that given OS level thread will
860 "win", which seems reasonable behaviour.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000861 */
Eric Snow76d5abc2017-09-05 18:26:16 -0700862 if (PyThread_get_key_value(_PyRuntime.gilstate.autoTLSkey) == NULL) {
863 if ((PyThread_set_key_value(_PyRuntime.gilstate.autoTLSkey,
864 (void *)tstate)
865 ) < 0)
866 {
Victor Stinner590cebe2013-12-13 11:08:56 +0100867 Py_FatalError("Couldn't create autoTLSkey mapping");
Eric Snow76d5abc2017-09-05 18:26:16 -0700868 }
Victor Stinner590cebe2013-12-13 11:08:56 +0100869 }
Michael W. Hudson188d4362005-06-20 16:52:57 +0000870
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000871 /* PyGILState_Release must not try to delete this thread state. */
872 tstate->gilstate_counter = 1;
Michael W. Hudson188d4362005-06-20 16:52:57 +0000873}
874
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000875/* The public functions */
Tim Peters19717fa2004-10-09 17:38:29 +0000876PyThreadState *
877PyGILState_GetThisThreadState(void)
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000878{
Eric Snow76d5abc2017-09-05 18:26:16 -0700879 if (_PyRuntime.gilstate.autoInterpreterState == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000880 return NULL;
Eric Snow76d5abc2017-09-05 18:26:16 -0700881 return (PyThreadState *)PyThread_get_key_value(
882 _PyRuntime.gilstate.autoTLSkey);
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000883}
884
Kristján Valur Jónsson684cd0e2013-03-23 03:36:16 -0700885int
886PyGILState_Check(void)
887{
Victor Stinner8a1be612016-03-14 22:07:55 +0100888 PyThreadState *tstate;
889
890 if (!_PyGILState_check_enabled)
891 return 1;
892
Eric Snow76d5abc2017-09-05 18:26:16 -0700893 if (_PyRuntime.gilstate.autoTLSkey == -1)
Victor Stinner8a1be612016-03-14 22:07:55 +0100894 return 1;
895
896 tstate = GET_TSTATE();
897 if (tstate == NULL)
898 return 0;
899
900 return (tstate == PyGILState_GetThisThreadState());
Kristján Valur Jónsson684cd0e2013-03-23 03:36:16 -0700901}
902
Tim Peters19717fa2004-10-09 17:38:29 +0000903PyGILState_STATE
904PyGILState_Ensure(void)
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000905{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000906 int current;
907 PyThreadState *tcur;
908 /* Note that we do not auto-init Python here - apart from
909 potential races with 2 threads auto-initializing, pep-311
910 spells out other issues. Embedders are expected to have
911 called Py_Initialize() and usually PyEval_InitThreads().
912 */
Eric Snow76d5abc2017-09-05 18:26:16 -0700913 /* Py_Initialize() hasn't been called! */
914 assert(_PyRuntime.gilstate.autoInterpreterState);
915 tcur = (PyThreadState *)PyThread_get_key_value(
916 _PyRuntime.gilstate.autoTLSkey);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000917 if (tcur == NULL) {
Victor Stinner62ca1002013-12-13 01:46:43 +0100918 /* At startup, Python has no concrete GIL. If PyGILState_Ensure() is
919 called from a new thread for the first time, we need the create the
920 GIL. */
921 PyEval_InitThreads();
922
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000923 /* Create a new thread state for this thread */
Eric Snow76d5abc2017-09-05 18:26:16 -0700924 tcur = PyThreadState_New(_PyRuntime.gilstate.autoInterpreterState);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000925 if (tcur == NULL)
926 Py_FatalError("Couldn't create thread-state for new thread");
927 /* This is our thread state! We'll need to delete it in the
928 matching call to PyGILState_Release(). */
929 tcur->gilstate_counter = 0;
930 current = 0; /* new thread state is never current */
931 }
932 else
933 current = PyThreadState_IsCurrent(tcur);
934 if (current == 0)
935 PyEval_RestoreThread(tcur);
936 /* Update our counter in the thread-state - no need for locks:
937 - tcur will remain valid as we hold the GIL.
938 - the counter is safe as we are the only thread "allowed"
939 to modify this value
940 */
941 ++tcur->gilstate_counter;
942 return current ? PyGILState_LOCKED : PyGILState_UNLOCKED;
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000943}
944
Tim Peters19717fa2004-10-09 17:38:29 +0000945void
946PyGILState_Release(PyGILState_STATE oldstate)
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000947{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000948 PyThreadState *tcur = (PyThreadState *)PyThread_get_key_value(
Eric Snow76d5abc2017-09-05 18:26:16 -0700949 _PyRuntime.gilstate.autoTLSkey);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000950 if (tcur == NULL)
951 Py_FatalError("auto-releasing thread-state, "
952 "but no thread-state for this thread");
953 /* We must hold the GIL and have our thread state current */
954 /* XXX - remove the check - the assert should be fine,
955 but while this is very new (April 2003), the extra check
956 by release-only users can't hurt.
957 */
958 if (! PyThreadState_IsCurrent(tcur))
959 Py_FatalError("This thread state must be current when releasing");
960 assert(PyThreadState_IsCurrent(tcur));
961 --tcur->gilstate_counter;
962 assert(tcur->gilstate_counter >= 0); /* illegal counter value */
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000963
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000964 /* If we're going to destroy this thread-state, we must
965 * clear it while the GIL is held, as destructors may run.
966 */
967 if (tcur->gilstate_counter == 0) {
968 /* can't have been locked when we created it */
969 assert(oldstate == PyGILState_UNLOCKED);
970 PyThreadState_Clear(tcur);
971 /* Delete the thread-state. Note this releases the GIL too!
972 * It's vital that the GIL be held here, to avoid shutdown
973 * races; see bugs 225673 and 1061968 (that nasty bug has a
974 * habit of coming back).
975 */
976 PyThreadState_DeleteCurrent();
977 }
978 /* Release the lock if necessary */
979 else if (oldstate == PyGILState_UNLOCKED)
980 PyEval_SaveThread();
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000981}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000982
Benjamin Peterson3bf01752012-04-13 18:06:36 -0400983#endif /* WITH_THREAD */
984
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000985#ifdef __cplusplus
986}
987#endif
988
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000989