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