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