1
2/* Thread and interpreter state structures and their interfaces */
3
4#include "Python.h"
5#include "pycore_ceval.h"
6#include "pycore_initconfig.h"
7#include "pycore_object.h" // _PyType_InitCache()
8#include "pycore_pyerrors.h"
9#include "pycore_pylifecycle.h"
10#include "pycore_pymem.h" // _PyMem_SetDefaultAllocator()
11#include "pycore_pystate.h" // _PyThreadState_GET()
12#include "pycore_sysmodule.h"
13
14/* --------------------------------------------------------------------------
15CAUTION
16
17Always 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).
22-------------------------------------------------------------------------- */
23
24#ifdef HAVE_DLOPEN
25#ifdef HAVE_DLFCN_H
26#include <dlfcn.h>
27#endif
28#if !HAVE_DECL_RTLD_LAZY
29#define RTLD_LAZY 1
30#endif
31#endif
32
33#ifdef __cplusplus
34extern "C" {
35#endif
36
37#define _PyRuntimeGILState_GetThreadState(gilstate) \
38 ((PyThreadState*)_Py_atomic_load_relaxed(&(gilstate)->tstate_current))
39#define _PyRuntimeGILState_SetThreadState(gilstate, value) \
40 _Py_atomic_store_relaxed(&(gilstate)->tstate_current, \
41 (uintptr_t)(value))
42
43/* Forward declarations */
44static PyThreadState *_PyGILState_GetThisThreadState(struct _gilstate_runtime_state *gilstate);
45static void _PyThreadState_Delete(PyThreadState *tstate, int check_current);
46
47
48static PyStatus
49_PyRuntimeState_Init_impl(_PyRuntimeState *runtime)
50{
51 /* We preserve the hook across init, because there is
52 currently no public API to set it between runtime
53 initialization and interpreter initialization. */
54 void *open_code_hook = runtime->open_code_hook;
55 void *open_code_userdata = runtime->open_code_userdata;
56 _Py_AuditHookEntry *audit_hook_head = runtime->audit_hook_head;
57 // bpo-42882: Preserve next_index value if Py_Initialize()/Py_Finalize()
58 // is called multiple times.
59 Py_ssize_t unicode_next_index = runtime->unicode_ids.next_index;
60
61 memset(runtime, 0, sizeof(*runtime));
62
63 runtime->open_code_hook = open_code_hook;
64 runtime->open_code_userdata = open_code_userdata;
65 runtime->audit_hook_head = audit_hook_head;
66
67 _PyEval_InitRuntimeState(&runtime->ceval);
68
69 PyPreConfig_InitPythonConfig(&runtime->preconfig);
70
71 runtime->gilstate.check_enabled = 1;
72
73 /* A TSS key must be initialized with Py_tss_NEEDS_INIT
74 in accordance with the specification. */
75 Py_tss_t initial = Py_tss_NEEDS_INIT;
76 runtime->gilstate.autoTSSkey = initial;
77
78 runtime->interpreters.mutex = PyThread_allocate_lock();
79 if (runtime->interpreters.mutex == NULL) {
80 return _PyStatus_NO_MEMORY();
81 }
82 runtime->interpreters.next_id = -1;
83
84 runtime->xidregistry.mutex = PyThread_allocate_lock();
85 if (runtime->xidregistry.mutex == NULL) {
86 return _PyStatus_NO_MEMORY();
87 }
88
89 // Set it to the ID of the main thread of the main interpreter.
90 runtime->main_thread = PyThread_get_thread_ident();
91
92 runtime->unicode_ids.lock = PyThread_allocate_lock();
93 if (runtime->unicode_ids.lock == NULL) {
94 return _PyStatus_NO_MEMORY();
95 }
96 runtime->unicode_ids.next_index = unicode_next_index;
97
98 return _PyStatus_OK();
99}
100
101PyStatus
102_PyRuntimeState_Init(_PyRuntimeState *runtime)
103{
104 /* Force default allocator, since _PyRuntimeState_Fini() must
105 use the same allocator than this function. */
106 PyMemAllocatorEx old_alloc;
107 _PyMem_SetDefaultAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
108
109 PyStatus status = _PyRuntimeState_Init_impl(runtime);
110
111 PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
112 return status;
113}
114
115void
116_PyRuntimeState_Fini(_PyRuntimeState *runtime)
117{
118 /* Force the allocator used by _PyRuntimeState_Init(). */
119 PyMemAllocatorEx old_alloc;
120 _PyMem_SetDefaultAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
121#define FREE_LOCK(LOCK) \
122 if (LOCK != NULL) { \
123 PyThread_free_lock(LOCK); \
124 LOCK = NULL; \
125 }
126
127 FREE_LOCK(runtime->interpreters.mutex);
128 FREE_LOCK(runtime->xidregistry.mutex);
129 FREE_LOCK(runtime->unicode_ids.lock);
130
131#undef FREE_LOCK
132 PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
133}
134
135#ifdef HAVE_FORK
136/* This function is called from PyOS_AfterFork_Child to ensure that
137 newly created child processes do not share locks with the parent. */
138PyStatus
139_PyRuntimeState_ReInitThreads(_PyRuntimeState *runtime)
140{
141 // This was initially set in _PyRuntimeState_Init().
142 runtime->main_thread = PyThread_get_thread_ident();
143
144 /* Force default allocator, since _PyRuntimeState_Fini() must
145 use the same allocator than this function. */
146 PyMemAllocatorEx old_alloc;
147 _PyMem_SetDefaultAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
148
149 int reinit_interp = _PyThread_at_fork_reinit(&runtime->interpreters.mutex);
150 int reinit_xidregistry = _PyThread_at_fork_reinit(&runtime->xidregistry.mutex);
151 int reinit_unicode_ids = _PyThread_at_fork_reinit(&runtime->unicode_ids.lock);
152
153 PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
154
155 /* bpo-42540: id_mutex is freed by _PyInterpreterState_Delete, which does
156 * not force the default allocator. */
157 int reinit_main_id = _PyThread_at_fork_reinit(&runtime->interpreters.main->id_mutex);
158
159 if (reinit_interp < 0
160 || reinit_main_id < 0
161 || reinit_xidregistry < 0
162 || reinit_unicode_ids < 0)
163 {
164 return _PyStatus_ERR("Failed to reinitialize runtime locks");
165
166 }
167 return _PyStatus_OK();
168}
169#endif
170
171#define HEAD_LOCK(runtime) \
172 PyThread_acquire_lock((runtime)->interpreters.mutex, WAIT_LOCK)
173#define HEAD_UNLOCK(runtime) \
174 PyThread_release_lock((runtime)->interpreters.mutex)
175
176/* Forward declaration */
177static void _PyGILState_NoteThreadState(
178 struct _gilstate_runtime_state *gilstate, PyThreadState* tstate);
179
180PyStatus
181_PyInterpreterState_Enable(_PyRuntimeState *runtime)
182{
183 struct pyinterpreters *interpreters = &runtime->interpreters;
184 interpreters->next_id = 0;
185
186 /* Py_Finalize() calls _PyRuntimeState_Fini() which clears the mutex.
187 Create a new mutex if needed. */
188 if (interpreters->mutex == NULL) {
189 /* Force default allocator, since _PyRuntimeState_Fini() must
190 use the same allocator than this function. */
191 PyMemAllocatorEx old_alloc;
192 _PyMem_SetDefaultAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
193
194 interpreters->mutex = PyThread_allocate_lock();
195
196 PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
197
198 if (interpreters->mutex == NULL) {
199 return _PyStatus_ERR("Can't initialize threads for interpreter");
200 }
201 }
202
203 return _PyStatus_OK();
204}
205
206PyInterpreterState *
207PyInterpreterState_New(void)
208{
209 PyThreadState *tstate = _PyThreadState_GET();
210 /* tstate is NULL when Py_InitializeFromConfig() calls
211 PyInterpreterState_New() to create the main interpreter. */
212 if (_PySys_Audit(tstate, "cpython.PyInterpreterState_New", NULL) < 0) {
213 return NULL;
214 }
215
216 PyInterpreterState *interp = PyMem_RawCalloc(1, sizeof(PyInterpreterState));
217 if (interp == NULL) {
218 return NULL;
219 }
220
221 interp->id_refcount = -1;
222
223 /* Don't get runtime from tstate since tstate can be NULL */
224 _PyRuntimeState *runtime = &_PyRuntime;
225 interp->runtime = runtime;
226
227 if (_PyEval_InitState(&interp->ceval) < 0) {
228 goto out_of_memory;
229 }
230
231 _PyGC_InitState(&interp->gc);
232 PyConfig_InitPythonConfig(&interp->config);
233 _PyType_InitCache(interp);
234
235 interp->eval_frame = _PyEval_EvalFrameDefault;
236#ifdef HAVE_DLOPEN
237#if HAVE_DECL_RTLD_NOW
238 interp->dlopenflags = RTLD_NOW;
239#else
240 interp->dlopenflags = RTLD_LAZY;
241#endif
242#endif
243
244 struct pyinterpreters *interpreters = &runtime->interpreters;
245
246 HEAD_LOCK(runtime);
247 if (interpreters->next_id < 0) {
248 /* overflow or Py_Initialize() not called! */
249 if (tstate != NULL) {
250 _PyErr_SetString(tstate, PyExc_RuntimeError,
251 "failed to get an interpreter ID");
252 }
253 PyMem_RawFree(interp);
254 interp = NULL;
255 }
256 else {
257 interp->id = interpreters->next_id;
258 interpreters->next_id += 1;
259 interp->next = interpreters->head;
260 if (interpreters->main == NULL) {
261 interpreters->main = interp;
262 }
263 interpreters->head = interp;
264 }
265 HEAD_UNLOCK(runtime);
266
267 if (interp == NULL) {
268 return NULL;
269 }
270
271 interp->tstate_next_unique_id = 0;
272
273 interp->audit_hooks = NULL;
274
275 return interp;
276
277out_of_memory:
278 if (tstate != NULL) {
279 _PyErr_NoMemory(tstate);
280 }
281
282 PyMem_RawFree(interp);
283 return NULL;
284}
285
286
287static void
288interpreter_clear(PyInterpreterState *interp, PyThreadState *tstate)
289{
290 _PyRuntimeState *runtime = interp->runtime;
291
292 if (_PySys_Audit(tstate, "cpython.PyInterpreterState_Clear", NULL) < 0) {
293 _PyErr_Clear(tstate);
294 }
295
296 HEAD_LOCK(runtime);
297 for (PyThreadState *p = interp->tstate_head; p != NULL; p = p->next) {
298 PyThreadState_Clear(p);
299 }
300 HEAD_UNLOCK(runtime);
301
302 Py_CLEAR(interp->audit_hooks);
303
304 PyConfig_Clear(&interp->config);
305 Py_CLEAR(interp->codec_search_path);
306 Py_CLEAR(interp->codec_search_cache);
307 Py_CLEAR(interp->codec_error_registry);
308 Py_CLEAR(interp->modules);
309 Py_CLEAR(interp->modules_by_index);
310 Py_CLEAR(interp->builtins_copy);
311 Py_CLEAR(interp->importlib);
312 Py_CLEAR(interp->import_func);
313 Py_CLEAR(interp->dict);
314#ifdef HAVE_FORK
315 Py_CLEAR(interp->before_forkers);
316 Py_CLEAR(interp->after_forkers_parent);
317 Py_CLEAR(interp->after_forkers_child);
318#endif
319
320 _PyAST_Fini(interp);
321 _PyWarnings_Fini(interp);
322 _PyAtExit_Fini(interp);
323
324 // All Python types must be destroyed before the last GC collection. Python
325 // types create a reference cycle to themselves in their in their
326 // PyTypeObject.tp_mro member (the tuple contains the type).
327
328 /* Last garbage collection on this interpreter */
329 _PyGC_CollectNoFail(tstate);
330 _PyGC_Fini(interp);
331
332 /* We don't clear sysdict and builtins until the end of this function.
333 Because clearing other attributes can execute arbitrary Python code
334 which requires sysdict and builtins. */
335 PyDict_Clear(interp->sysdict);
336 PyDict_Clear(interp->builtins);
337 Py_CLEAR(interp->sysdict);
338 Py_CLEAR(interp->builtins);
339
340 // XXX Once we have one allocator per interpreter (i.e.
341 // per-interpreter GC) we must ensure that all of the interpreter's
342 // objects have been cleaned up at the point.
343}
344
345
346void
347PyInterpreterState_Clear(PyInterpreterState *interp)
348{
349 // Use the current Python thread state to call audit hooks and to collect
350 // garbage. It can be different than the current Python thread state
351 // of 'interp'.
352 PyThreadState *current_tstate = _PyThreadState_GET();
353
354 interpreter_clear(interp, current_tstate);
355}
356
357
358void
359_PyInterpreterState_Clear(PyThreadState *tstate)
360{
361 interpreter_clear(tstate->interp, tstate);
362}
363
364
365static void
366zapthreads(PyInterpreterState *interp, int check_current)
367{
368 PyThreadState *tstate;
369 /* No need to lock the mutex here because this should only happen
370 when the threads are all really dead (XXX famous last words). */
371 while ((tstate = interp->tstate_head) != NULL) {
372 _PyThreadState_Delete(tstate, check_current);
373 }
374}
375
376
377void
378PyInterpreterState_Delete(PyInterpreterState *interp)
379{
380 _PyRuntimeState *runtime = interp->runtime;
381 struct pyinterpreters *interpreters = &runtime->interpreters;
382 zapthreads(interp, 0);
383
384 _PyEval_FiniState(&interp->ceval);
385
386 /* Delete current thread. After this, many C API calls become crashy. */
387 _PyThreadState_Swap(&runtime->gilstate, NULL);
388
389 HEAD_LOCK(runtime);
390 PyInterpreterState **p;
391 for (p = &interpreters->head; ; p = &(*p)->next) {
392 if (*p == NULL) {
393 Py_FatalError("NULL interpreter");
394 }
395 if (*p == interp) {
396 break;
397 }
398 }
399 if (interp->tstate_head != NULL) {
400 Py_FatalError("remaining threads");
401 }
402 *p = interp->next;
403
404 if (interpreters->main == interp) {
405 interpreters->main = NULL;
406 if (interpreters->head != NULL) {
407 Py_FatalError("remaining subinterpreters");
408 }
409 }
410 HEAD_UNLOCK(runtime);
411
412 if (interp->id_mutex != NULL) {
413 PyThread_free_lock(interp->id_mutex);
414 }
415 PyMem_RawFree(interp);
416}
417
418
419#ifdef HAVE_FORK
420/*
421 * Delete all interpreter states except the main interpreter. If there
422 * is a current interpreter state, it *must* be the main interpreter.
423 */
424PyStatus
425_PyInterpreterState_DeleteExceptMain(_PyRuntimeState *runtime)
426{
427 struct _gilstate_runtime_state *gilstate = &runtime->gilstate;
428 struct pyinterpreters *interpreters = &runtime->interpreters;
429
430 PyThreadState *tstate = _PyThreadState_Swap(gilstate, NULL);
431 if (tstate != NULL && tstate->interp != interpreters->main) {
432 return _PyStatus_ERR("not main interpreter");
433 }
434
435 HEAD_LOCK(runtime);
436 PyInterpreterState *interp = interpreters->head;
437 interpreters->head = NULL;
438 while (interp != NULL) {
439 if (interp == interpreters->main) {
440 interpreters->main->next = NULL;
441 interpreters->head = interp;
442 interp = interp->next;
443 continue;
444 }
445
446 PyInterpreterState_Clear(interp); // XXX must activate?
447 zapthreads(interp, 1);
448 if (interp->id_mutex != NULL) {
449 PyThread_free_lock(interp->id_mutex);
450 }
451 PyInterpreterState *prev_interp = interp;
452 interp = interp->next;
453 PyMem_RawFree(prev_interp);
454 }
455 HEAD_UNLOCK(runtime);
456
457 if (interpreters->head == NULL) {
458 return _PyStatus_ERR("missing main interpreter");
459 }
460 _PyThreadState_Swap(gilstate, tstate);
461 return _PyStatus_OK();
462}
463#endif
464
465
466PyInterpreterState *
467PyInterpreterState_Get(void)
468{
469 PyThreadState *tstate = _PyThreadState_GET();
470 _Py_EnsureTstateNotNULL(tstate);
471 PyInterpreterState *interp = tstate->interp;
472 if (interp == NULL) {
473 Py_FatalError("no current interpreter");
474 }
475 return interp;
476}
477
478
479int64_t
480PyInterpreterState_GetID(PyInterpreterState *interp)
481{
482 if (interp == NULL) {
483 PyErr_SetString(PyExc_RuntimeError, "no interpreter provided");
484 return -1;
485 }
486 return interp->id;
487}
488
489
490static PyInterpreterState *
491interp_look_up_id(_PyRuntimeState *runtime, int64_t requested_id)
492{
493 PyInterpreterState *interp = runtime->interpreters.head;
494 while (interp != NULL) {
495 int64_t id = PyInterpreterState_GetID(interp);
496 if (id < 0) {
497 return NULL;
498 }
499 if (requested_id == id) {
500 return interp;
501 }
502 interp = PyInterpreterState_Next(interp);
503 }
504 return NULL;
505}
506
507PyInterpreterState *
508_PyInterpreterState_LookUpID(int64_t requested_id)
509{
510 PyInterpreterState *interp = NULL;
511 if (requested_id >= 0) {
512 _PyRuntimeState *runtime = &_PyRuntime;
513 HEAD_LOCK(runtime);
514 interp = interp_look_up_id(runtime, requested_id);
515 HEAD_UNLOCK(runtime);
516 }
517 if (interp == NULL && !PyErr_Occurred()) {
518 PyErr_Format(PyExc_RuntimeError,
519 "unrecognized interpreter ID %lld", requested_id);
520 }
521 return interp;
522}
523
524
525int
526_PyInterpreterState_IDInitref(PyInterpreterState *interp)
527{
528 if (interp->id_mutex != NULL) {
529 return 0;
530 }
531 interp->id_mutex = PyThread_allocate_lock();
532 if (interp->id_mutex == NULL) {
533 PyErr_SetString(PyExc_RuntimeError,
534 "failed to create init interpreter ID mutex");
535 return -1;
536 }
537 interp->id_refcount = 0;
538 return 0;
539}
540
541
542int
543_PyInterpreterState_IDIncref(PyInterpreterState *interp)
544{
545 if (_PyInterpreterState_IDInitref(interp) < 0) {
546 return -1;
547 }
548
549 PyThread_acquire_lock(interp->id_mutex, WAIT_LOCK);
550 interp->id_refcount += 1;
551 PyThread_release_lock(interp->id_mutex);
552 return 0;
553}
554
555
556void
557_PyInterpreterState_IDDecref(PyInterpreterState *interp)
558{
559 assert(interp->id_mutex != NULL);
560
561 struct _gilstate_runtime_state *gilstate = &_PyRuntime.gilstate;
562 PyThread_acquire_lock(interp->id_mutex, WAIT_LOCK);
563 assert(interp->id_refcount != 0);
564 interp->id_refcount -= 1;
565 int64_t refcount = interp->id_refcount;
566 PyThread_release_lock(interp->id_mutex);
567
568 if (refcount == 0 && interp->requires_idref) {
569 // XXX Using the "head" thread isn't strictly correct.
570 PyThreadState *tstate = PyInterpreterState_ThreadHead(interp);
571 // XXX Possible GILState issues?
572 PyThreadState *save_tstate = _PyThreadState_Swap(gilstate, tstate);
573 Py_EndInterpreter(tstate);
574 _PyThreadState_Swap(gilstate, save_tstate);
575 }
576}
577
578int
579_PyInterpreterState_RequiresIDRef(PyInterpreterState *interp)
580{
581 return interp->requires_idref;
582}
583
584void
585_PyInterpreterState_RequireIDRef(PyInterpreterState *interp, int required)
586{
587 interp->requires_idref = required ? 1 : 0;
588}
589
590PyObject *
591_PyInterpreterState_GetMainModule(PyInterpreterState *interp)
592{
593 if (interp->modules == NULL) {
594 PyErr_SetString(PyExc_RuntimeError, "interpreter not initialized");
595 return NULL;
596 }
597 return PyMapping_GetItemString(interp->modules, "__main__");
598}
599
600PyObject *
601PyInterpreterState_GetDict(PyInterpreterState *interp)
602{
603 if (interp->dict == NULL) {
604 interp->dict = PyDict_New();
605 if (interp->dict == NULL) {
606 PyErr_Clear();
607 }
608 }
609 /* Returning NULL means no per-interpreter dict is available. */
610 return interp->dict;
611}
612
613static PyThreadState *
614new_threadstate(PyInterpreterState *interp, int init)
615{
616 _PyRuntimeState *runtime = interp->runtime;
617 PyThreadState *tstate = (PyThreadState *)PyMem_RawMalloc(sizeof(PyThreadState));
618 if (tstate == NULL) {
619 return NULL;
620 }
621
622 tstate->interp = interp;
623
624 tstate->frame = NULL;
625 tstate->recursion_depth = 0;
626 tstate->recursion_headroom = 0;
627 tstate->stackcheck_counter = 0;
628 tstate->tracing = 0;
629 tstate->root_cframe.use_tracing = 0;
630 tstate->cframe = &tstate->root_cframe;
631 tstate->gilstate_counter = 0;
632 tstate->async_exc = NULL;
633 tstate->thread_id = PyThread_get_thread_ident();
634
635 tstate->dict = NULL;
636
637 tstate->curexc_type = NULL;
638 tstate->curexc_value = NULL;
639 tstate->curexc_traceback = NULL;
640
641 tstate->exc_state.exc_type = NULL;
642 tstate->exc_state.exc_value = NULL;
643 tstate->exc_state.exc_traceback = NULL;
644 tstate->exc_state.previous_item = NULL;
645 tstate->exc_info = &tstate->exc_state;
646
647 tstate->c_profilefunc = NULL;
648 tstate->c_tracefunc = NULL;
649 tstate->c_profileobj = NULL;
650 tstate->c_traceobj = NULL;
651
652 tstate->trash_delete_nesting = 0;
653 tstate->trash_delete_later = NULL;
654 tstate->on_delete = NULL;
655 tstate->on_delete_data = NULL;
656
657 tstate->coroutine_origin_tracking_depth = 0;
658
659 tstate->async_gen_firstiter = NULL;
660 tstate->async_gen_finalizer = NULL;
661
662 tstate->context = NULL;
663 tstate->context_ver = 1;
664
665 if (init) {
666 _PyThreadState_Init(tstate);
667 }
668
669 HEAD_LOCK(runtime);
670 tstate->id = ++interp->tstate_next_unique_id;
671 tstate->prev = NULL;
672 tstate->next = interp->tstate_head;
673 if (tstate->next)
674 tstate->next->prev = tstate;
675 interp->tstate_head = tstate;
676 HEAD_UNLOCK(runtime);
677
678 return tstate;
679}
680
681PyThreadState *
682PyThreadState_New(PyInterpreterState *interp)
683{
684 return new_threadstate(interp, 1);
685}
686
687PyThreadState *
688_PyThreadState_Prealloc(PyInterpreterState *interp)
689{
690 return new_threadstate(interp, 0);
691}
692
693void
694_PyThreadState_Init(PyThreadState *tstate)
695{
696 _PyGILState_NoteThreadState(&tstate->interp->runtime->gilstate, tstate);
697}
698
699PyObject*
700PyState_FindModule(struct PyModuleDef* module)
701{
702 Py_ssize_t index = module->m_base.m_index;
703 PyInterpreterState *state = _PyInterpreterState_GET();
704 PyObject *res;
705 if (module->m_slots) {
706 return NULL;
707 }
708 if (index == 0)
709 return NULL;
710 if (state->modules_by_index == NULL)
711 return NULL;
712 if (index >= PyList_GET_SIZE(state->modules_by_index))
713 return NULL;
714 res = PyList_GET_ITEM(state->modules_by_index, index);
715 return res==Py_None ? NULL : res;
716}
717
718int
719_PyState_AddModule(PyThreadState *tstate, PyObject* module, struct PyModuleDef* def)
720{
721 if (!def) {
722 assert(_PyErr_Occurred(tstate));
723 return -1;
724 }
725 if (def->m_slots) {
726 _PyErr_SetString(tstate,
727 PyExc_SystemError,
728 "PyState_AddModule called on module with slots");
729 return -1;
730 }
731
732 PyInterpreterState *interp = tstate->interp;
733 if (!interp->modules_by_index) {
734 interp->modules_by_index = PyList_New(0);
735 if (!interp->modules_by_index) {
736 return -1;
737 }
738 }
739
740 while (PyList_GET_SIZE(interp->modules_by_index) <= def->m_base.m_index) {
741 if (PyList_Append(interp->modules_by_index, Py_None) < 0) {
742 return -1;
743 }
744 }
745
746 Py_INCREF(module);
747 return PyList_SetItem(interp->modules_by_index,
748 def->m_base.m_index, module);
749}
750
751int
752PyState_AddModule(PyObject* module, struct PyModuleDef* def)
753{
754 if (!def) {
755 Py_FatalError("module definition is NULL");
756 return -1;
757 }
758
759 PyThreadState *tstate = _PyThreadState_GET();
760 PyInterpreterState *interp = tstate->interp;
761 Py_ssize_t index = def->m_base.m_index;
762 if (interp->modules_by_index &&
763 index < PyList_GET_SIZE(interp->modules_by_index) &&
764 module == PyList_GET_ITEM(interp->modules_by_index, index))
765 {
766 _Py_FatalErrorFormat(__func__, "module %p already added", module);
767 return -1;
768 }
769 return _PyState_AddModule(tstate, module, def);
770}
771
772int
773PyState_RemoveModule(struct PyModuleDef* def)
774{
775 PyThreadState *tstate = _PyThreadState_GET();
776 PyInterpreterState *interp = tstate->interp;
777
778 if (def->m_slots) {
779 _PyErr_SetString(tstate,
780 PyExc_SystemError,
781 "PyState_RemoveModule called on module with slots");
782 return -1;
783 }
784
785 Py_ssize_t index = def->m_base.m_index;
786 if (index == 0) {
787 Py_FatalError("invalid module index");
788 }
789 if (interp->modules_by_index == NULL) {
790 Py_FatalError("Interpreters module-list not accessible.");
791 }
792 if (index > PyList_GET_SIZE(interp->modules_by_index)) {
793 Py_FatalError("Module index out of bounds.");
794 }
795
796 Py_INCREF(Py_None);
797 return PyList_SetItem(interp->modules_by_index, index, Py_None);
798}
799
800// Used by finalize_modules()
801void
802_PyInterpreterState_ClearModules(PyInterpreterState *interp)
803{
804 if (!interp->modules_by_index) {
805 return;
806 }
807
808 Py_ssize_t i;
809 for (i = 0; i < PyList_GET_SIZE(interp->modules_by_index); i++) {
810 PyObject *m = PyList_GET_ITEM(interp->modules_by_index, i);
811 if (PyModule_Check(m)) {
812 /* cleanup the saved copy of module dicts */
813 PyModuleDef *md = PyModule_GetDef(m);
814 if (md) {
815 Py_CLEAR(md->m_base.m_copy);
816 }
817 }
818 }
819
820 /* Setting modules_by_index to NULL could be dangerous, so we
821 clear the list instead. */
822 if (PyList_SetSlice(interp->modules_by_index,
823 0, PyList_GET_SIZE(interp->modules_by_index),
824 NULL)) {
825 PyErr_WriteUnraisable(interp->modules_by_index);
826 }
827}
828
829void
830PyThreadState_Clear(PyThreadState *tstate)
831{
832 int verbose = _PyInterpreterState_GetConfig(tstate->interp)->verbose;
833
834 if (verbose && tstate->frame != NULL) {
835 /* bpo-20526: After the main thread calls
836 _PyRuntimeState_SetFinalizing() in Py_FinalizeEx(), threads must
837 exit when trying to take the GIL. If a thread exit in the middle of
838 _PyEval_EvalFrameDefault(), tstate->frame is not reset to its
839 previous value. It is more likely with daemon threads, but it can
840 happen with regular threads if threading._shutdown() fails
841 (ex: interrupted by CTRL+C). */
842 fprintf(stderr,
843 "PyThreadState_Clear: warning: thread still has a frame\n");
844 }
845
846 /* Don't clear tstate->frame: it is a borrowed reference */
847
848 Py_CLEAR(tstate->dict);
849 Py_CLEAR(tstate->async_exc);
850
851 Py_CLEAR(tstate->curexc_type);
852 Py_CLEAR(tstate->curexc_value);
853 Py_CLEAR(tstate->curexc_traceback);
854
855 Py_CLEAR(tstate->exc_state.exc_type);
856 Py_CLEAR(tstate->exc_state.exc_value);
857 Py_CLEAR(tstate->exc_state.exc_traceback);
858
859 /* The stack of exception states should contain just this thread. */
860 if (verbose && tstate->exc_info != &tstate->exc_state) {
861 fprintf(stderr,
862 "PyThreadState_Clear: warning: thread still has a generator\n");
863 }
864
865 tstate->c_profilefunc = NULL;
866 tstate->c_tracefunc = NULL;
867 Py_CLEAR(tstate->c_profileobj);
868 Py_CLEAR(tstate->c_traceobj);
869
870 Py_CLEAR(tstate->async_gen_firstiter);
871 Py_CLEAR(tstate->async_gen_finalizer);
872
873 Py_CLEAR(tstate->context);
874
875 if (tstate->on_delete != NULL) {
876 tstate->on_delete(tstate->on_delete_data);
877 }
878}
879
880
881/* Common code for PyThreadState_Delete() and PyThreadState_DeleteCurrent() */
882static void
883tstate_delete_common(PyThreadState *tstate,
884 struct _gilstate_runtime_state *gilstate)
885{
886 _Py_EnsureTstateNotNULL(tstate);
887 PyInterpreterState *interp = tstate->interp;
888 if (interp == NULL) {
889 Py_FatalError("NULL interpreter");
890 }
891 _PyRuntimeState *runtime = interp->runtime;
892
893 HEAD_LOCK(runtime);
894 if (tstate->prev) {
895 tstate->prev->next = tstate->next;
896 }
897 else {
898 interp->tstate_head = tstate->next;
899 }
900 if (tstate->next) {
901 tstate->next->prev = tstate->prev;
902 }
903 HEAD_UNLOCK(runtime);
904
905 if (gilstate->autoInterpreterState &&
906 PyThread_tss_get(&gilstate->autoTSSkey) == tstate)
907 {
908 PyThread_tss_set(&gilstate->autoTSSkey, NULL);
909 }
910}
911
912
913static void
914_PyThreadState_Delete(PyThreadState *tstate, int check_current)
915{
916 struct _gilstate_runtime_state *gilstate = &tstate->interp->runtime->gilstate;
917 if (check_current) {
918 if (tstate == _PyRuntimeGILState_GetThreadState(gilstate)) {
919 _Py_FatalErrorFormat(__func__, "tstate %p is still current", tstate);
920 }
921 }
922 tstate_delete_common(tstate, gilstate);
923 PyMem_RawFree(tstate);
924}
925
926
927void
928PyThreadState_Delete(PyThreadState *tstate)
929{
930 _PyThreadState_Delete(tstate, 1);
931}
932
933
934void
935_PyThreadState_DeleteCurrent(PyThreadState *tstate)
936{
937 _Py_EnsureTstateNotNULL(tstate);
938 struct _gilstate_runtime_state *gilstate = &tstate->interp->runtime->gilstate;
939 tstate_delete_common(tstate, gilstate);
940 _PyRuntimeGILState_SetThreadState(gilstate, NULL);
941 _PyEval_ReleaseLock(tstate);
942 PyMem_RawFree(tstate);
943}
944
945void
946PyThreadState_DeleteCurrent(void)
947{
948 struct _gilstate_runtime_state *gilstate = &_PyRuntime.gilstate;
949 PyThreadState *tstate = _PyRuntimeGILState_GetThreadState(gilstate);
950 _PyThreadState_DeleteCurrent(tstate);
951}
952
953
954/*
955 * Delete all thread states except the one passed as argument.
956 * Note that, if there is a current thread state, it *must* be the one
957 * passed as argument. Also, this won't touch any other interpreters
958 * than the current one, since we don't know which thread state should
959 * be kept in those other interpreters.
960 */
961void
962_PyThreadState_DeleteExcept(_PyRuntimeState *runtime, PyThreadState *tstate)
963{
964 PyInterpreterState *interp = tstate->interp;
965
966 HEAD_LOCK(runtime);
967 /* Remove all thread states, except tstate, from the linked list of
968 thread states. This will allow calling PyThreadState_Clear()
969 without holding the lock. */
970 PyThreadState *list = interp->tstate_head;
971 if (list == tstate) {
972 list = tstate->next;
973 }
974 if (tstate->prev) {
975 tstate->prev->next = tstate->next;
976 }
977 if (tstate->next) {
978 tstate->next->prev = tstate->prev;
979 }
980 tstate->prev = tstate->next = NULL;
981 interp->tstate_head = tstate;
982 HEAD_UNLOCK(runtime);
983
984 /* Clear and deallocate all stale thread states. Even if this
985 executes Python code, we should be safe since it executes
986 in the current thread, not one of the stale threads. */
987 PyThreadState *p, *next;
988 for (p = list; p; p = next) {
989 next = p->next;
990 PyThreadState_Clear(p);
991 PyMem_RawFree(p);
992 }
993}
994
995
996#ifdef EXPERIMENTAL_ISOLATED_SUBINTERPRETERS
997PyThreadState*
998_PyThreadState_GetTSS(void) {
999 return PyThread_tss_get(&_PyRuntime.gilstate.autoTSSkey);
1000}
1001#endif
1002
1003
1004PyThreadState *
1005_PyThreadState_UncheckedGet(void)
1006{
1007 return _PyThreadState_GET();
1008}
1009
1010
1011PyThreadState *
1012PyThreadState_Get(void)
1013{
1014 PyThreadState *tstate = _PyThreadState_GET();
1015 _Py_EnsureTstateNotNULL(tstate);
1016 return tstate;
1017}
1018
1019
1020PyThreadState *
1021_PyThreadState_Swap(struct _gilstate_runtime_state *gilstate, PyThreadState *newts)
1022{
1023#ifdef EXPERIMENTAL_ISOLATED_SUBINTERPRETERS
1024 PyThreadState *oldts = _PyThreadState_GetTSS();
1025#else
1026 PyThreadState *oldts = _PyRuntimeGILState_GetThreadState(gilstate);
1027#endif
1028
1029 _PyRuntimeGILState_SetThreadState(gilstate, newts);
1030 /* It should not be possible for more than one thread state
1031 to be used for a thread. Check this the best we can in debug
1032 builds.
1033 */
1034#if defined(Py_DEBUG)
1035 if (newts) {
1036 /* This can be called from PyEval_RestoreThread(). Similar
1037 to it, we need to ensure errno doesn't change.
1038 */
1039 int err = errno;
1040 PyThreadState *check = _PyGILState_GetThisThreadState(gilstate);
1041 if (check && check->interp == newts->interp && check != newts)
1042 Py_FatalError("Invalid thread state for this thread");
1043 errno = err;
1044 }
1045#endif
1046#ifdef EXPERIMENTAL_ISOLATED_SUBINTERPRETERS
1047 PyThread_tss_set(&gilstate->autoTSSkey, newts);
1048#endif
1049 return oldts;
1050}
1051
1052PyThreadState *
1053PyThreadState_Swap(PyThreadState *newts)
1054{
1055 return _PyThreadState_Swap(&_PyRuntime.gilstate, newts);
1056}
1057
1058/* An extension mechanism to store arbitrary additional per-thread state.
1059 PyThreadState_GetDict() returns a dictionary that can be used to hold such
1060 state; the caller should pick a unique key and store its state there. If
1061 PyThreadState_GetDict() returns NULL, an exception has *not* been raised
1062 and the caller should assume no per-thread state is available. */
1063
1064PyObject *
1065_PyThreadState_GetDict(PyThreadState *tstate)
1066{
1067 assert(tstate != NULL);
1068 if (tstate->dict == NULL) {
1069 tstate->dict = PyDict_New();
1070 if (tstate->dict == NULL) {
1071 _PyErr_Clear(tstate);
1072 }
1073 }
1074 return tstate->dict;
1075}
1076
1077
1078PyObject *
1079PyThreadState_GetDict(void)
1080{
1081 PyThreadState *tstate = _PyThreadState_GET();
1082 if (tstate == NULL) {
1083 return NULL;
1084 }
1085 return _PyThreadState_GetDict(tstate);
1086}
1087
1088
1089PyInterpreterState *
1090PyThreadState_GetInterpreter(PyThreadState *tstate)
1091{
1092 assert(tstate != NULL);
1093 return tstate->interp;
1094}
1095
1096
1097PyFrameObject*
1098PyThreadState_GetFrame(PyThreadState *tstate)
1099{
1100 assert(tstate != NULL);
1101 PyFrameObject *frame = tstate->frame;
1102 Py_XINCREF(frame);
1103 return frame;
1104}
1105
1106
1107uint64_t
1108PyThreadState_GetID(PyThreadState *tstate)
1109{
1110 assert(tstate != NULL);
1111 return tstate->id;
1112}
1113
1114
1115/* Asynchronously raise an exception in a thread.
1116 Requested by Just van Rossum and Alex Martelli.
1117 To prevent naive misuse, you must write your own extension
1118 to call this, or use ctypes. Must be called with the GIL held.
1119 Returns the number of tstates modified (normally 1, but 0 if `id` didn't
1120 match any known thread id). Can be called with exc=NULL to clear an
1121 existing async exception. This raises no exceptions. */
1122
1123int
1124PyThreadState_SetAsyncExc(unsigned long id, PyObject *exc)
1125{
1126 _PyRuntimeState *runtime = &_PyRuntime;
1127 PyInterpreterState *interp = _PyRuntimeState_GetThreadState(runtime)->interp;
1128
1129 /* Although the GIL is held, a few C API functions can be called
1130 * without the GIL held, and in particular some that create and
1131 * destroy thread and interpreter states. Those can mutate the
1132 * list of thread states we're traversing, so to prevent that we lock
1133 * head_mutex for the duration.
1134 */
1135 HEAD_LOCK(runtime);
1136 for (PyThreadState *tstate = interp->tstate_head; tstate != NULL; tstate = tstate->next) {
1137 if (tstate->thread_id != id) {
1138 continue;
1139 }
1140
1141 /* Tricky: we need to decref the current value
1142 * (if any) in tstate->async_exc, but that can in turn
1143 * allow arbitrary Python code to run, including
1144 * perhaps calls to this function. To prevent
1145 * deadlock, we need to release head_mutex before
1146 * the decref.
1147 */
1148 PyObject *old_exc = tstate->async_exc;
1149 Py_XINCREF(exc);
1150 tstate->async_exc = exc;
1151 HEAD_UNLOCK(runtime);
1152
1153 Py_XDECREF(old_exc);
1154 _PyEval_SignalAsyncExc(tstate->interp);
1155 return 1;
1156 }
1157 HEAD_UNLOCK(runtime);
1158 return 0;
1159}
1160
1161
1162/* Routines for advanced debuggers, requested by David Beazley.
1163 Don't use unless you know what you are doing! */
1164
1165PyInterpreterState *
1166PyInterpreterState_Head(void)
1167{
1168 return _PyRuntime.interpreters.head;
1169}
1170
1171PyInterpreterState *
1172PyInterpreterState_Main(void)
1173{
1174 return _PyRuntime.interpreters.main;
1175}
1176
1177PyInterpreterState *
1178PyInterpreterState_Next(PyInterpreterState *interp) {
1179 return interp->next;
1180}
1181
1182PyThreadState *
1183PyInterpreterState_ThreadHead(PyInterpreterState *interp) {
1184 return interp->tstate_head;
1185}
1186
1187PyThreadState *
1188PyThreadState_Next(PyThreadState *tstate) {
1189 return tstate->next;
1190}
1191
1192/* The implementation of sys._current_frames(). This is intended to be
1193 called with the GIL held, as it will be when called via
1194 sys._current_frames(). It's possible it would work fine even without
1195 the GIL held, but haven't thought enough about that.
1196*/
1197PyObject *
1198_PyThread_CurrentFrames(void)
1199{
1200 PyThreadState *tstate = _PyThreadState_GET();
1201 if (_PySys_Audit(tstate, "sys._current_frames", NULL) < 0) {
1202 return NULL;
1203 }
1204
1205 PyObject *result = PyDict_New();
1206 if (result == NULL) {
1207 return NULL;
1208 }
1209
1210 /* for i in all interpreters:
1211 * for t in all of i's thread states:
1212 * if t's frame isn't NULL, map t's id to its frame
1213 * Because these lists can mutate even when the GIL is held, we
1214 * need to grab head_mutex for the duration.
1215 */
1216 _PyRuntimeState *runtime = tstate->interp->runtime;
1217 HEAD_LOCK(runtime);
1218 PyInterpreterState *i;
1219 for (i = runtime->interpreters.head; i != NULL; i = i->next) {
1220 PyThreadState *t;
1221 for (t = i->tstate_head; t != NULL; t = t->next) {
1222 PyFrameObject *frame = t->frame;
1223 if (frame == NULL) {
1224 continue;
1225 }
1226 PyObject *id = PyLong_FromUnsignedLong(t->thread_id);
1227 if (id == NULL) {
1228 goto fail;
1229 }
1230 int stat = PyDict_SetItem(result, id, (PyObject *)frame);
1231 Py_DECREF(id);
1232 if (stat < 0) {
1233 goto fail;
1234 }
1235 }
1236 }
1237 goto done;
1238
1239fail:
1240 Py_CLEAR(result);
1241
1242done:
1243 HEAD_UNLOCK(runtime);
1244 return result;
1245}
1246
1247PyObject *
1248_PyThread_CurrentExceptions(void)
1249{
1250 PyThreadState *tstate = _PyThreadState_GET();
1251
1252 _Py_EnsureTstateNotNULL(tstate);
1253
1254 if (_PySys_Audit(tstate, "sys._current_exceptions", NULL) < 0) {
1255 return NULL;
1256 }
1257
1258 PyObject *result = PyDict_New();
1259 if (result == NULL) {
1260 return NULL;
1261 }
1262
1263 /* for i in all interpreters:
1264 * for t in all of i's thread states:
1265 * if t's frame isn't NULL, map t's id to its frame
1266 * Because these lists can mutate even when the GIL is held, we
1267 * need to grab head_mutex for the duration.
1268 */
1269 _PyRuntimeState *runtime = tstate->interp->runtime;
1270 HEAD_LOCK(runtime);
1271 PyInterpreterState *i;
1272 for (i = runtime->interpreters.head; i != NULL; i = i->next) {
1273 PyThreadState *t;
1274 for (t = i->tstate_head; t != NULL; t = t->next) {
1275 _PyErr_StackItem *err_info = _PyErr_GetTopmostException(t);
1276 if (err_info == NULL) {
1277 continue;
1278 }
1279 PyObject *id = PyLong_FromUnsignedLong(t->thread_id);
1280 if (id == NULL) {
1281 goto fail;
1282 }
1283 PyObject *exc_info = PyTuple_Pack(
1284 3,
1285 err_info->exc_type != NULL ? err_info->exc_type : Py_None,
1286 err_info->exc_value != NULL ? err_info->exc_value : Py_None,
1287 err_info->exc_traceback != NULL ? err_info->exc_traceback : Py_None);
1288 if (exc_info == NULL) {
1289 Py_DECREF(id);
1290 goto fail;
1291 }
1292 int stat = PyDict_SetItem(result, id, exc_info);
1293 Py_DECREF(id);
1294 Py_DECREF(exc_info);
1295 if (stat < 0) {
1296 goto fail;
1297 }
1298 }
1299 }
1300 goto done;
1301
1302fail:
1303 Py_CLEAR(result);
1304
1305done:
1306 HEAD_UNLOCK(runtime);
1307 return result;
1308}
1309
1310/* Python "auto thread state" API. */
1311
1312/* Keep this as a static, as it is not reliable! It can only
1313 ever be compared to the state for the *current* thread.
1314 * If not equal, then it doesn't matter that the actual
1315 value may change immediately after comparison, as it can't
1316 possibly change to the current thread's state.
1317 * If equal, then the current thread holds the lock, so the value can't
1318 change until we yield the lock.
1319*/
1320static int
1321PyThreadState_IsCurrent(PyThreadState *tstate)
1322{
1323 /* Must be the tstate for this thread */
1324 struct _gilstate_runtime_state *gilstate = &_PyRuntime.gilstate;
1325 assert(_PyGILState_GetThisThreadState(gilstate) == tstate);
1326 return tstate == _PyRuntimeGILState_GetThreadState(gilstate);
1327}
1328
1329/* Internal initialization/finalization functions called by
1330 Py_Initialize/Py_FinalizeEx
1331*/
1332PyStatus
1333_PyGILState_Init(_PyRuntimeState *runtime)
1334{
1335 struct _gilstate_runtime_state *gilstate = &runtime->gilstate;
1336 if (PyThread_tss_create(&gilstate->autoTSSkey) != 0) {
1337 return _PyStatus_NO_MEMORY();
1338 }
1339 // PyThreadState_New() calls _PyGILState_NoteThreadState() which does
1340 // nothing before autoInterpreterState is set.
1341 assert(gilstate->autoInterpreterState == NULL);
1342 return _PyStatus_OK();
1343}
1344
1345
1346PyStatus
1347_PyGILState_SetTstate(PyThreadState *tstate)
1348{
1349 if (!_Py_IsMainInterpreter(tstate->interp)) {
1350 /* Currently, PyGILState is shared by all interpreters. The main
1351 * interpreter is responsible to initialize it. */
1352 return _PyStatus_OK();
1353 }
1354
1355 /* must init with valid states */
1356 assert(tstate != NULL);
1357 assert(tstate->interp != NULL);
1358
1359 struct _gilstate_runtime_state *gilstate = &tstate->interp->runtime->gilstate;
1360
1361 gilstate->autoInterpreterState = tstate->interp;
1362 assert(PyThread_tss_get(&gilstate->autoTSSkey) == NULL);
1363 assert(tstate->gilstate_counter == 0);
1364
1365 _PyGILState_NoteThreadState(gilstate, tstate);
1366 return _PyStatus_OK();
1367}
1368
1369PyInterpreterState *
1370_PyGILState_GetInterpreterStateUnsafe(void)
1371{
1372 return _PyRuntime.gilstate.autoInterpreterState;
1373}
1374
1375void
1376_PyGILState_Fini(PyInterpreterState *interp)
1377{
1378 struct _gilstate_runtime_state *gilstate = &interp->runtime->gilstate;
1379 PyThread_tss_delete(&gilstate->autoTSSkey);
1380 gilstate->autoInterpreterState = NULL;
1381}
1382
1383#ifdef HAVE_FORK
1384/* Reset the TSS key - called by PyOS_AfterFork_Child().
1385 * This should not be necessary, but some - buggy - pthread implementations
1386 * don't reset TSS upon fork(), see issue #10517.
1387 */
1388PyStatus
1389_PyGILState_Reinit(_PyRuntimeState *runtime)
1390{
1391 struct _gilstate_runtime_state *gilstate = &runtime->gilstate;
1392 PyThreadState *tstate = _PyGILState_GetThisThreadState(gilstate);
1393
1394 PyThread_tss_delete(&gilstate->autoTSSkey);
1395 if (PyThread_tss_create(&gilstate->autoTSSkey) != 0) {
1396 return _PyStatus_NO_MEMORY();
1397 }
1398
1399 /* If the thread had an associated auto thread state, reassociate it with
1400 * the new key. */
1401 if (tstate &&
1402 PyThread_tss_set(&gilstate->autoTSSkey, (void *)tstate) != 0)
1403 {
1404 return _PyStatus_ERR("failed to set autoTSSkey");
1405 }
1406 return _PyStatus_OK();
1407}
1408#endif
1409
1410/* When a thread state is created for a thread by some mechanism other than
1411 PyGILState_Ensure, it's important that the GILState machinery knows about
1412 it so it doesn't try to create another thread state for the thread (this is
1413 a better fix for SF bug #1010677 than the first one attempted).
1414*/
1415static void
1416_PyGILState_NoteThreadState(struct _gilstate_runtime_state *gilstate, PyThreadState* tstate)
1417{
1418 /* If autoTSSkey isn't initialized, this must be the very first
1419 threadstate created in Py_Initialize(). Don't do anything for now
1420 (we'll be back here when _PyGILState_Init is called). */
1421 if (!gilstate->autoInterpreterState) {
1422 return;
1423 }
1424
1425 /* Stick the thread state for this thread in thread specific storage.
1426
1427 The only situation where you can legitimately have more than one
1428 thread state for an OS level thread is when there are multiple
1429 interpreters.
1430
1431 You shouldn't really be using the PyGILState_ APIs anyway (see issues
1432 #10915 and #15751).
1433
1434 The first thread state created for that given OS level thread will
1435 "win", which seems reasonable behaviour.
1436 */
1437 if (PyThread_tss_get(&gilstate->autoTSSkey) == NULL) {
1438 if ((PyThread_tss_set(&gilstate->autoTSSkey, (void *)tstate)) != 0) {
1439 Py_FatalError("Couldn't create autoTSSkey mapping");
1440 }
1441 }
1442
1443 /* PyGILState_Release must not try to delete this thread state. */
1444 tstate->gilstate_counter = 1;
1445}
1446
1447/* The public functions */
1448static PyThreadState *
1449_PyGILState_GetThisThreadState(struct _gilstate_runtime_state *gilstate)
1450{
1451 if (gilstate->autoInterpreterState == NULL)
1452 return NULL;
1453 return (PyThreadState *)PyThread_tss_get(&gilstate->autoTSSkey);
1454}
1455
1456PyThreadState *
1457PyGILState_GetThisThreadState(void)
1458{
1459 return _PyGILState_GetThisThreadState(&_PyRuntime.gilstate);
1460}
1461
1462int
1463PyGILState_Check(void)
1464{
1465 struct _gilstate_runtime_state *gilstate = &_PyRuntime.gilstate;
1466 if (!gilstate->check_enabled) {
1467 return 1;
1468 }
1469
1470 if (!PyThread_tss_is_created(&gilstate->autoTSSkey)) {
1471 return 1;
1472 }
1473
1474 PyThreadState *tstate = _PyRuntimeGILState_GetThreadState(gilstate);
1475 if (tstate == NULL) {
1476 return 0;
1477 }
1478
1479 return (tstate == _PyGILState_GetThisThreadState(gilstate));
1480}
1481
1482PyGILState_STATE
1483PyGILState_Ensure(void)
1484{
1485 _PyRuntimeState *runtime = &_PyRuntime;
1486 struct _gilstate_runtime_state *gilstate = &runtime->gilstate;
1487
1488 /* Note that we do not auto-init Python here - apart from
1489 potential races with 2 threads auto-initializing, pep-311
1490 spells out other issues. Embedders are expected to have
1491 called Py_Initialize(). */
1492
1493 /* Ensure that _PyEval_InitThreads() and _PyGILState_Init() have been
1494 called by Py_Initialize() */
1495#ifndef EXPERIMENTAL_ISOLATED_SUBINTERPRETERS
1496 assert(_PyEval_ThreadsInitialized(runtime));
1497#endif
1498 assert(gilstate->autoInterpreterState);
1499
1500 PyThreadState *tcur = (PyThreadState *)PyThread_tss_get(&gilstate->autoTSSkey);
1501 int current;
1502 if (tcur == NULL) {
1503 /* Create a new Python thread state for this thread */
1504 tcur = PyThreadState_New(gilstate->autoInterpreterState);
1505 if (tcur == NULL) {
1506 Py_FatalError("Couldn't create thread-state for new thread");
1507 }
1508
1509 /* This is our thread state! We'll need to delete it in the
1510 matching call to PyGILState_Release(). */
1511 tcur->gilstate_counter = 0;
1512 current = 0; /* new thread state is never current */
1513 }
1514 else {
1515 current = PyThreadState_IsCurrent(tcur);
1516 }
1517
1518 if (current == 0) {
1519 PyEval_RestoreThread(tcur);
1520 }
1521
1522 /* Update our counter in the thread-state - no need for locks:
1523 - tcur will remain valid as we hold the GIL.
1524 - the counter is safe as we are the only thread "allowed"
1525 to modify this value
1526 */
1527 ++tcur->gilstate_counter;
1528
1529 return current ? PyGILState_LOCKED : PyGILState_UNLOCKED;
1530}
1531
1532void
1533PyGILState_Release(PyGILState_STATE oldstate)
1534{
1535 _PyRuntimeState *runtime = &_PyRuntime;
1536 PyThreadState *tstate = PyThread_tss_get(&runtime->gilstate.autoTSSkey);
1537 if (tstate == NULL) {
1538 Py_FatalError("auto-releasing thread-state, "
1539 "but no thread-state for this thread");
1540 }
1541
1542 /* We must hold the GIL and have our thread state current */
1543 /* XXX - remove the check - the assert should be fine,
1544 but while this is very new (April 2003), the extra check
1545 by release-only users can't hurt.
1546 */
1547 if (!PyThreadState_IsCurrent(tstate)) {
1548 _Py_FatalErrorFormat(__func__,
1549 "thread state %p must be current when releasing",
1550 tstate);
1551 }
1552 assert(PyThreadState_IsCurrent(tstate));
1553 --tstate->gilstate_counter;
1554 assert(tstate->gilstate_counter >= 0); /* illegal counter value */
1555
1556 /* If we're going to destroy this thread-state, we must
1557 * clear it while the GIL is held, as destructors may run.
1558 */
1559 if (tstate->gilstate_counter == 0) {
1560 /* can't have been locked when we created it */
1561 assert(oldstate == PyGILState_UNLOCKED);
1562 PyThreadState_Clear(tstate);
1563 /* Delete the thread-state. Note this releases the GIL too!
1564 * It's vital that the GIL be held here, to avoid shutdown
1565 * races; see bugs 225673 and 1061968 (that nasty bug has a
1566 * habit of coming back).
1567 */
1568 assert(_PyRuntimeGILState_GetThreadState(&runtime->gilstate) == tstate);
1569 _PyThreadState_DeleteCurrent(tstate);
1570 }
1571 /* Release the lock if necessary */
1572 else if (oldstate == PyGILState_UNLOCKED)
1573 PyEval_SaveThread();
1574}
1575
1576
1577/**************************/
1578/* cross-interpreter data */
1579/**************************/
1580
1581/* cross-interpreter data */
1582
1583crossinterpdatafunc _PyCrossInterpreterData_Lookup(PyObject *);
1584
1585/* This is a separate func from _PyCrossInterpreterData_Lookup in order
1586 to keep the registry code separate. */
1587static crossinterpdatafunc
1588_lookup_getdata(PyObject *obj)
1589{
1590 crossinterpdatafunc getdata = _PyCrossInterpreterData_Lookup(obj);
1591 if (getdata == NULL && PyErr_Occurred() == 0)
1592 PyErr_Format(PyExc_ValueError,
1593 "%S does not support cross-interpreter data", obj);
1594 return getdata;
1595}
1596
1597int
1598_PyObject_CheckCrossInterpreterData(PyObject *obj)
1599{
1600 crossinterpdatafunc getdata = _lookup_getdata(obj);
1601 if (getdata == NULL) {
1602 return -1;
1603 }
1604 return 0;
1605}
1606
1607static int
1608_check_xidata(PyThreadState *tstate, _PyCrossInterpreterData *data)
1609{
1610 // data->data can be anything, including NULL, so we don't check it.
1611
1612 // data->obj may be NULL, so we don't check it.
1613
1614 if (data->interp < 0) {
1615 _PyErr_SetString(tstate, PyExc_SystemError, "missing interp");
1616 return -1;
1617 }
1618
1619 if (data->new_object == NULL) {
1620 _PyErr_SetString(tstate, PyExc_SystemError, "missing new_object func");
1621 return -1;
1622 }
1623
1624 // data->free may be NULL, so we don't check it.
1625
1626 return 0;
1627}
1628
1629int
1630_PyObject_GetCrossInterpreterData(PyObject *obj, _PyCrossInterpreterData *data)
1631{
1632 // PyThreadState_Get() aborts if tstate is NULL.
1633 PyThreadState *tstate = PyThreadState_Get();
1634 PyInterpreterState *interp = tstate->interp;
1635
1636 // Reset data before re-populating.
1637 *data = (_PyCrossInterpreterData){0};
1638 data->free = PyMem_RawFree; // Set a default that may be overridden.
1639
1640 // Call the "getdata" func for the object.
1641 Py_INCREF(obj);
1642 crossinterpdatafunc getdata = _lookup_getdata(obj);
1643 if (getdata == NULL) {
1644 Py_DECREF(obj);
1645 return -1;
1646 }
1647 int res = getdata(obj, data);
1648 Py_DECREF(obj);
1649 if (res != 0) {
1650 return -1;
1651 }
1652
1653 // Fill in the blanks and validate the result.
1654 data->interp = interp->id;
1655 if (_check_xidata(tstate, data) != 0) {
1656 _PyCrossInterpreterData_Release(data);
1657 return -1;
1658 }
1659
1660 return 0;
1661}
1662
1663static void
1664_release_xidata(void *arg)
1665{
1666 _PyCrossInterpreterData *data = (_PyCrossInterpreterData *)arg;
1667 if (data->free != NULL) {
1668 data->free(data->data);
1669 }
1670 Py_XDECREF(data->obj);
1671}
1672
1673static void
1674_call_in_interpreter(struct _gilstate_runtime_state *gilstate,
1675 PyInterpreterState *interp,
1676 void (*func)(void *), void *arg)
1677{
1678 /* We would use Py_AddPendingCall() if it weren't specific to the
1679 * main interpreter (see bpo-33608). In the meantime we take a
1680 * naive approach.
1681 */
1682 PyThreadState *save_tstate = NULL;
1683 if (interp != _PyRuntimeGILState_GetThreadState(gilstate)->interp) {
1684 // XXX Using the "head" thread isn't strictly correct.
1685 PyThreadState *tstate = PyInterpreterState_ThreadHead(interp);
1686 // XXX Possible GILState issues?
1687 save_tstate = _PyThreadState_Swap(gilstate, tstate);
1688 }
1689
1690 func(arg);
1691
1692 // Switch back.
1693 if (save_tstate != NULL) {
1694 _PyThreadState_Swap(gilstate, save_tstate);
1695 }
1696}
1697
1698void
1699_PyCrossInterpreterData_Release(_PyCrossInterpreterData *data)
1700{
1701 if (data->data == NULL && data->obj == NULL) {
1702 // Nothing to release!
1703 return;
1704 }
1705
1706 // Switch to the original interpreter.
1707 PyInterpreterState *interp = _PyInterpreterState_LookUpID(data->interp);
1708 if (interp == NULL) {
1709 // The interpreter was already destroyed.
1710 if (data->free != NULL) {
1711 // XXX Someone leaked some memory...
1712 }
1713 return;
1714 }
1715
1716 // "Release" the data and/or the object.
1717 struct _gilstate_runtime_state *gilstate = &_PyRuntime.gilstate;
1718 _call_in_interpreter(gilstate, interp, _release_xidata, data);
1719}
1720
1721PyObject *
1722_PyCrossInterpreterData_NewObject(_PyCrossInterpreterData *data)
1723{
1724 return data->new_object(data);
1725}
1726
1727/* registry of {type -> crossinterpdatafunc} */
1728
1729/* For now we use a global registry of shareable classes. An
1730 alternative would be to add a tp_* slot for a class's
1731 crossinterpdatafunc. It would be simpler and more efficient. */
1732
1733static int
1734_register_xidata(struct _xidregistry *xidregistry, PyTypeObject *cls,
1735 crossinterpdatafunc getdata)
1736{
1737 // Note that we effectively replace already registered classes
1738 // rather than failing.
1739 struct _xidregitem *newhead = PyMem_RawMalloc(sizeof(struct _xidregitem));
1740 if (newhead == NULL)
1741 return -1;
1742 newhead->cls = cls;
1743 newhead->getdata = getdata;
1744 newhead->next = xidregistry->head;
1745 xidregistry->head = newhead;
1746 return 0;
1747}
1748
1749static void _register_builtins_for_crossinterpreter_data(struct _xidregistry *xidregistry);
1750
1751int
1752_PyCrossInterpreterData_RegisterClass(PyTypeObject *cls,
1753 crossinterpdatafunc getdata)
1754{
1755 if (!PyType_Check(cls)) {
1756 PyErr_Format(PyExc_ValueError, "only classes may be registered");
1757 return -1;
1758 }
1759 if (getdata == NULL) {
1760 PyErr_Format(PyExc_ValueError, "missing 'getdata' func");
1761 return -1;
1762 }
1763
1764 // Make sure the class isn't ever deallocated.
1765 Py_INCREF((PyObject *)cls);
1766
1767 struct _xidregistry *xidregistry = &_PyRuntime.xidregistry ;
1768 PyThread_acquire_lock(xidregistry->mutex, WAIT_LOCK);
1769 if (xidregistry->head == NULL) {
1770 _register_builtins_for_crossinterpreter_data(xidregistry);
1771 }
1772 int res = _register_xidata(xidregistry, cls, getdata);
1773 PyThread_release_lock(xidregistry->mutex);
1774 return res;
1775}
1776
1777/* Cross-interpreter objects are looked up by exact match on the class.
1778 We can reassess this policy when we move from a global registry to a
1779 tp_* slot. */
1780
1781crossinterpdatafunc
1782_PyCrossInterpreterData_Lookup(PyObject *obj)
1783{
1784 struct _xidregistry *xidregistry = &_PyRuntime.xidregistry ;
1785 PyObject *cls = PyObject_Type(obj);
1786 crossinterpdatafunc getdata = NULL;
1787 PyThread_acquire_lock(xidregistry->mutex, WAIT_LOCK);
1788 struct _xidregitem *cur = xidregistry->head;
1789 if (cur == NULL) {
1790 _register_builtins_for_crossinterpreter_data(xidregistry);
1791 cur = xidregistry->head;
1792 }
1793 for(; cur != NULL; cur = cur->next) {
1794 if (cur->cls == (PyTypeObject *)cls) {
1795 getdata = cur->getdata;
1796 break;
1797 }
1798 }
1799 Py_DECREF(cls);
1800 PyThread_release_lock(xidregistry->mutex);
1801 return getdata;
1802}
1803
1804/* cross-interpreter data for builtin types */
1805
1806struct _shared_bytes_data {
1807 char *bytes;
1808 Py_ssize_t len;
1809};
1810
1811static PyObject *
1812_new_bytes_object(_PyCrossInterpreterData *data)
1813{
1814 struct _shared_bytes_data *shared = (struct _shared_bytes_data *)(data->data);
1815 return PyBytes_FromStringAndSize(shared->bytes, shared->len);
1816}
1817
1818static int
1819_bytes_shared(PyObject *obj, _PyCrossInterpreterData *data)
1820{
1821 struct _shared_bytes_data *shared = PyMem_NEW(struct _shared_bytes_data, 1);
1822 if (PyBytes_AsStringAndSize(obj, &shared->bytes, &shared->len) < 0) {
1823 return -1;
1824 }
1825 data->data = (void *)shared;
1826 Py_INCREF(obj);
1827 data->obj = obj; // Will be "released" (decref'ed) when data released.
1828 data->new_object = _new_bytes_object;
1829 data->free = PyMem_Free;
1830 return 0;
1831}
1832
1833struct _shared_str_data {
1834 int kind;
1835 const void *buffer;
1836 Py_ssize_t len;
1837};
1838
1839static PyObject *
1840_new_str_object(_PyCrossInterpreterData *data)
1841{
1842 struct _shared_str_data *shared = (struct _shared_str_data *)(data->data);
1843 return PyUnicode_FromKindAndData(shared->kind, shared->buffer, shared->len);
1844}
1845
1846static int
1847_str_shared(PyObject *obj, _PyCrossInterpreterData *data)
1848{
1849 struct _shared_str_data *shared = PyMem_NEW(struct _shared_str_data, 1);
1850 shared->kind = PyUnicode_KIND(obj);
1851 shared->buffer = PyUnicode_DATA(obj);
1852 shared->len = PyUnicode_GET_LENGTH(obj);
1853 data->data = (void *)shared;
1854 Py_INCREF(obj);
1855 data->obj = obj; // Will be "released" (decref'ed) when data released.
1856 data->new_object = _new_str_object;
1857 data->free = PyMem_Free;
1858 return 0;
1859}
1860
1861static PyObject *
1862_new_long_object(_PyCrossInterpreterData *data)
1863{
1864 return PyLong_FromSsize_t((Py_ssize_t)(data->data));
1865}
1866
1867static int
1868_long_shared(PyObject *obj, _PyCrossInterpreterData *data)
1869{
1870 /* Note that this means the size of shareable ints is bounded by
1871 * sys.maxsize. Hence on 32-bit architectures that is half the
1872 * size of maximum shareable ints on 64-bit.
1873 */
1874 Py_ssize_t value = PyLong_AsSsize_t(obj);
1875 if (value == -1 && PyErr_Occurred()) {
1876 if (PyErr_ExceptionMatches(PyExc_OverflowError)) {
1877 PyErr_SetString(PyExc_OverflowError, "try sending as bytes");
1878 }
1879 return -1;
1880 }
1881 data->data = (void *)value;
1882 data->obj = NULL;
1883 data->new_object = _new_long_object;
1884 data->free = NULL;
1885 return 0;
1886}
1887
1888static PyObject *
1889_new_none_object(_PyCrossInterpreterData *data)
1890{
1891 // XXX Singleton refcounts are problematic across interpreters...
1892 Py_INCREF(Py_None);
1893 return Py_None;
1894}
1895
1896static int
1897_none_shared(PyObject *obj, _PyCrossInterpreterData *data)
1898{
1899 data->data = NULL;
1900 // data->obj remains NULL
1901 data->new_object = _new_none_object;
1902 data->free = NULL; // There is nothing to free.
1903 return 0;
1904}
1905
1906static void
1907_register_builtins_for_crossinterpreter_data(struct _xidregistry *xidregistry)
1908{
1909 // None
1910 if (_register_xidata(xidregistry, (PyTypeObject *)PyObject_Type(Py_None), _none_shared) != 0) {
1911 Py_FatalError("could not register None for cross-interpreter sharing");
1912 }
1913
1914 // int
1915 if (_register_xidata(xidregistry, &PyLong_Type, _long_shared) != 0) {
1916 Py_FatalError("could not register int for cross-interpreter sharing");
1917 }
1918
1919 // bytes
1920 if (_register_xidata(xidregistry, &PyBytes_Type, _bytes_shared) != 0) {
1921 Py_FatalError("could not register bytes for cross-interpreter sharing");
1922 }
1923
1924 // str
1925 if (_register_xidata(xidregistry, &PyUnicode_Type, _str_shared) != 0) {
1926 Py_FatalError("could not register str for cross-interpreter sharing");
1927 }
1928}
1929
1930
1931_PyFrameEvalFunction
1932_PyInterpreterState_GetEvalFrameFunc(PyInterpreterState *interp)
1933{
1934 return interp->eval_frame;
1935}
1936
1937
1938void
1939_PyInterpreterState_SetEvalFrameFunc(PyInterpreterState *interp,
1940 _PyFrameEvalFunction eval_frame)
1941{
1942 interp->eval_frame = eval_frame;
1943}
1944
1945
1946const PyConfig*
1947_PyInterpreterState_GetConfig(PyInterpreterState *interp)
1948{
1949 return &interp->config;
1950}
1951
1952
1953int
1954_PyInterpreterState_GetConfigCopy(PyConfig *config)
1955{
1956 PyInterpreterState *interp = PyInterpreterState_Get();
1957
1958 PyStatus status = _PyConfig_Copy(config, &interp->config);
1959 if (PyStatus_Exception(status)) {
1960 _PyErr_SetFromPyStatus(status);
1961 return -1;
1962 }
1963 return 0;
1964}
1965
1966
1967const PyConfig*
1968_Py_GetConfig(void)
1969{
1970 assert(PyGILState_Check());
1971 PyThreadState *tstate = _PyThreadState_GET();
1972 return _PyInterpreterState_GetConfig(tstate->interp);
1973}
1974
1975#ifdef __cplusplus
1976}
1977#endif
1978