-
-
Notifications
You must be signed in to change notification settings - Fork 30.7k
/
pystate.c
3085 lines (2618 loc) · 88.7 KB
/
pystate.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* Thread and interpreter state structures and their interfaces */
#include "Python.h"
#include "pycore_abstract.h" // _PyIndex_Check()
#include "pycore_ceval.h"
#include "pycore_code.h" // stats
#include "pycore_critical_section.h" // _PyCriticalSection_Resume()
#include "pycore_dtoa.h" // _dtoa_state_INIT()
#include "pycore_emscripten_trampoline.h" // _Py_EmscriptenTrampoline_Init()
#include "pycore_frame.h"
#include "pycore_initconfig.h" // _PyStatus_OK()
#include "pycore_object.h" // _PyType_InitCache()
#include "pycore_object_stack.h" // _PyObjectStackChunk_ClearFreeList()
#include "pycore_parking_lot.h" // _PyParkingLot_AfterFork()
#include "pycore_pyerrors.h" // _PyErr_Clear()
#include "pycore_pylifecycle.h" // _PyAST_Fini()
#include "pycore_pymem.h" // _PyMem_SetDefaultAllocator()
#include "pycore_pystate.h"
#include "pycore_runtime_init.h" // _PyRuntimeState_INIT
#include "pycore_sysmodule.h" // _PySys_Audit()
#include "pycore_obmalloc.h" // _PyMem_obmalloc_state_on_heap()
/* --------------------------------------------------------------------------
CAUTION
Always use PyMem_RawMalloc() and PyMem_RawFree() directly in this file. A
number of these functions are advertised as safe to call when the GIL isn't
held, and in a debug build Python redirects (e.g.) PyMem_NEW (etc) to Python's
debugging obmalloc functions. Those aren't thread-safe (they rely on the GIL
to avoid the expense of doing their own locking).
-------------------------------------------------------------------------- */
#ifdef HAVE_DLOPEN
# ifdef HAVE_DLFCN_H
# include <dlfcn.h>
# endif
# if !HAVE_DECL_RTLD_LAZY
# define RTLD_LAZY 1
# endif
#endif
/****************************************/
/* helpers for the current thread state */
/****************************************/
// API for the current thread state is further down.
/* "current" means one of:
- bound to the current OS thread
- holds the GIL
*/
//-------------------------------------------------
// a highly efficient lookup for the current thread
//-------------------------------------------------
/*
The stored thread state is set by PyThreadState_Swap().
For each of these functions, the GIL must be held by the current thread.
*/
#ifdef HAVE_THREAD_LOCAL
_Py_thread_local PyThreadState *_Py_tss_tstate = NULL;
#endif
static inline PyThreadState *
current_fast_get(void)
{
#ifdef HAVE_THREAD_LOCAL
return _Py_tss_tstate;
#else
// XXX Fall back to the PyThread_tss_*() API.
# error "no supported thread-local variable storage classifier"
#endif
}
static inline void
current_fast_set(_PyRuntimeState *Py_UNUSED(runtime), PyThreadState *tstate)
{
assert(tstate != NULL);
#ifdef HAVE_THREAD_LOCAL
_Py_tss_tstate = tstate;
#else
// XXX Fall back to the PyThread_tss_*() API.
# error "no supported thread-local variable storage classifier"
#endif
}
static inline void
current_fast_clear(_PyRuntimeState *Py_UNUSED(runtime))
{
#ifdef HAVE_THREAD_LOCAL
_Py_tss_tstate = NULL;
#else
// XXX Fall back to the PyThread_tss_*() API.
# error "no supported thread-local variable storage classifier"
#endif
}
#define tstate_verify_not_active(tstate) \
if (tstate == current_fast_get()) { \
_Py_FatalErrorFormat(__func__, "tstate %p is still current", tstate); \
}
PyThreadState *
_PyThreadState_GetCurrent(void)
{
return current_fast_get();
}
//------------------------------------------------
// the thread state bound to the current OS thread
//------------------------------------------------
static inline int
tstate_tss_initialized(Py_tss_t *key)
{
return PyThread_tss_is_created(key);
}
static inline int
tstate_tss_init(Py_tss_t *key)
{
assert(!tstate_tss_initialized(key));
return PyThread_tss_create(key);
}
static inline void
tstate_tss_fini(Py_tss_t *key)
{
assert(tstate_tss_initialized(key));
PyThread_tss_delete(key);
}
static inline PyThreadState *
tstate_tss_get(Py_tss_t *key)
{
assert(tstate_tss_initialized(key));
return (PyThreadState *)PyThread_tss_get(key);
}
static inline int
tstate_tss_set(Py_tss_t *key, PyThreadState *tstate)
{
assert(tstate != NULL);
assert(tstate_tss_initialized(key));
return PyThread_tss_set(key, (void *)tstate);
}
static inline int
tstate_tss_clear(Py_tss_t *key)
{
assert(tstate_tss_initialized(key));
return PyThread_tss_set(key, (void *)NULL);
}
#ifdef HAVE_FORK
/* Reset the TSS key - called by PyOS_AfterFork_Child().
* This should not be necessary, but some - buggy - pthread implementations
* don't reset TSS upon fork(), see issue #10517.
*/
static PyStatus
tstate_tss_reinit(Py_tss_t *key)
{
if (!tstate_tss_initialized(key)) {
return _PyStatus_OK();
}
PyThreadState *tstate = tstate_tss_get(key);
tstate_tss_fini(key);
if (tstate_tss_init(key) != 0) {
return _PyStatus_NO_MEMORY();
}
/* If the thread had an associated auto thread state, reassociate it with
* the new key. */
if (tstate && tstate_tss_set(key, tstate) != 0) {
return _PyStatus_ERR("failed to re-set autoTSSkey");
}
return _PyStatus_OK();
}
#endif
/*
The stored thread state is set by bind_tstate() (AKA PyThreadState_Bind().
The GIL does no need to be held for these.
*/
#define gilstate_tss_initialized(runtime) \
tstate_tss_initialized(&(runtime)->autoTSSkey)
#define gilstate_tss_init(runtime) \
tstate_tss_init(&(runtime)->autoTSSkey)
#define gilstate_tss_fini(runtime) \
tstate_tss_fini(&(runtime)->autoTSSkey)
#define gilstate_tss_get(runtime) \
tstate_tss_get(&(runtime)->autoTSSkey)
#define _gilstate_tss_set(runtime, tstate) \
tstate_tss_set(&(runtime)->autoTSSkey, tstate)
#define _gilstate_tss_clear(runtime) \
tstate_tss_clear(&(runtime)->autoTSSkey)
#define gilstate_tss_reinit(runtime) \
tstate_tss_reinit(&(runtime)->autoTSSkey)
static inline void
gilstate_tss_set(_PyRuntimeState *runtime, PyThreadState *tstate)
{
assert(tstate != NULL && tstate->interp->runtime == runtime);
if (_gilstate_tss_set(runtime, tstate) != 0) {
Py_FatalError("failed to set current tstate (TSS)");
}
}
static inline void
gilstate_tss_clear(_PyRuntimeState *runtime)
{
if (_gilstate_tss_clear(runtime) != 0) {
Py_FatalError("failed to clear current tstate (TSS)");
}
}
#ifndef NDEBUG
static inline int tstate_is_alive(PyThreadState *tstate);
static inline int
tstate_is_bound(PyThreadState *tstate)
{
return tstate->_status.bound && !tstate->_status.unbound;
}
#endif // !NDEBUG
static void bind_gilstate_tstate(PyThreadState *);
static void unbind_gilstate_tstate(PyThreadState *);
static void tstate_mimalloc_bind(PyThreadState *);
static void
bind_tstate(PyThreadState *tstate)
{
assert(tstate != NULL);
assert(tstate_is_alive(tstate) && !tstate->_status.bound);
assert(!tstate->_status.unbound); // just in case
assert(!tstate->_status.bound_gilstate);
assert(tstate != gilstate_tss_get(tstate->interp->runtime));
assert(!tstate->_status.active);
assert(tstate->thread_id == 0);
assert(tstate->native_thread_id == 0);
// Currently we don't necessarily store the thread state
// in thread-local storage (e.g. per-interpreter).
tstate->thread_id = PyThread_get_thread_ident();
#ifdef PY_HAVE_THREAD_NATIVE_ID
tstate->native_thread_id = PyThread_get_thread_native_id();
#endif
#ifdef Py_GIL_DISABLED
// Initialize biased reference counting inter-thread queue. Note that this
// needs to be initialized from the active thread.
_Py_brc_init_thread(tstate);
#endif
// mimalloc state needs to be initialized from the active thread.
tstate_mimalloc_bind(tstate);
tstate->_status.bound = 1;
}
static void
unbind_tstate(PyThreadState *tstate)
{
assert(tstate != NULL);
assert(tstate_is_bound(tstate));
#ifndef HAVE_PTHREAD_STUBS
assert(tstate->thread_id > 0);
#endif
#ifdef PY_HAVE_THREAD_NATIVE_ID
assert(tstate->native_thread_id > 0);
#endif
// We leave thread_id and native_thread_id alone
// since they can be useful for debugging.
// Check the `_status` field to know if these values
// are still valid.
// We leave tstate->_status.bound set to 1
// to indicate it was previously bound.
tstate->_status.unbound = 1;
}
/* Stick the thread state for this thread in thread specific storage.
When a thread state is created for a thread by some mechanism
other than PyGILState_Ensure(), it's important that the GILState
machinery knows about it so it doesn't try to create another
thread state for the thread.
(This is a better fix for SF bug #1010677 than the first one attempted.)
The only situation where you can legitimately have more than one
thread state for an OS level thread is when there are multiple
interpreters.
Before 3.12, the PyGILState_*() APIs didn't work with multiple
interpreters (see bpo-10915 and bpo-15751), so this function used
to set TSS only once. Thus, the first thread state created for that
given OS level thread would "win", which seemed reasonable behaviour.
*/
static void
bind_gilstate_tstate(PyThreadState *tstate)
{
assert(tstate != NULL);
assert(tstate_is_alive(tstate));
assert(tstate_is_bound(tstate));
// XXX assert(!tstate->_status.active);
assert(!tstate->_status.bound_gilstate);
_PyRuntimeState *runtime = tstate->interp->runtime;
PyThreadState *tcur = gilstate_tss_get(runtime);
assert(tstate != tcur);
if (tcur != NULL) {
tcur->_status.bound_gilstate = 0;
}
gilstate_tss_set(runtime, tstate);
tstate->_status.bound_gilstate = 1;
}
static void
unbind_gilstate_tstate(PyThreadState *tstate)
{
assert(tstate != NULL);
// XXX assert(tstate_is_alive(tstate));
assert(tstate_is_bound(tstate));
// XXX assert(!tstate->_status.active);
assert(tstate->_status.bound_gilstate);
assert(tstate == gilstate_tss_get(tstate->interp->runtime));
gilstate_tss_clear(tstate->interp->runtime);
tstate->_status.bound_gilstate = 0;
}
//----------------------------------------------
// the thread state that currently holds the GIL
//----------------------------------------------
/* This is not exported, as it is not reliable! It can only
ever be compared to the state for the *current* thread.
* If not equal, then it doesn't matter that the actual
value may change immediately after comparison, as it can't
possibly change to the current thread's state.
* If equal, then the current thread holds the lock, so the value can't
change until we yield the lock.
*/
static int
holds_gil(PyThreadState *tstate)
{
// XXX Fall back to tstate->interp->runtime->ceval.gil.last_holder
// (and tstate->interp->runtime->ceval.gil.locked).
assert(tstate != NULL);
/* Must be the tstate for this thread */
assert(tstate == gilstate_tss_get(tstate->interp->runtime));
return tstate == current_fast_get();
}
/****************************/
/* the global runtime state */
/****************************/
//----------
// lifecycle
//----------
/* Suppress deprecation warning for PyBytesObject.ob_shash */
_Py_COMP_DIAG_PUSH
_Py_COMP_DIAG_IGNORE_DEPR_DECLS
/* We use "initial" if the runtime gets re-used
(e.g. Py_Finalize() followed by Py_Initialize().
Note that we initialize "initial" relative to _PyRuntime,
to ensure pre-initialized pointers point to the active
runtime state (and not "initial"). */
static const _PyRuntimeState initial = _PyRuntimeState_INIT(_PyRuntime);
_Py_COMP_DIAG_POP
#define LOCKS_INIT(runtime) \
{ \
&(runtime)->interpreters.mutex, \
&(runtime)->xi.registry.mutex, \
&(runtime)->unicode_state.ids.mutex, \
&(runtime)->imports.extensions.mutex, \
&(runtime)->ceval.pending_mainthread.mutex, \
&(runtime)->ceval.sys_trace_profile_mutex, \
&(runtime)->atexit.mutex, \
&(runtime)->audit_hooks.mutex, \
&(runtime)->allocators.mutex, \
&(runtime)->_main_interpreter.types.mutex, \
&(runtime)->_main_interpreter.code_state.mutex, \
}
static void
init_runtime(_PyRuntimeState *runtime,
void *open_code_hook, void *open_code_userdata,
_Py_AuditHookEntry *audit_hook_head,
Py_ssize_t unicode_next_index)
{
assert(!runtime->preinitializing);
assert(!runtime->preinitialized);
assert(!runtime->core_initialized);
assert(!runtime->initialized);
assert(!runtime->_initialized);
runtime->open_code_hook = open_code_hook;
runtime->open_code_userdata = open_code_userdata;
runtime->audit_hooks.head = audit_hook_head;
PyPreConfig_InitPythonConfig(&runtime->preconfig);
// Set it to the ID of the main thread of the main interpreter.
runtime->main_thread = PyThread_get_thread_ident();
runtime->unicode_state.ids.next_index = unicode_next_index;
#if defined(__EMSCRIPTEN__) && defined(PY_CALL_TRAMPOLINE)
_Py_EmscriptenTrampoline_Init(runtime);
#endif
runtime->_initialized = 1;
}
PyStatus
_PyRuntimeState_Init(_PyRuntimeState *runtime)
{
/* We preserve the hook across init, because there is
currently no public API to set it between runtime
initialization and interpreter initialization. */
void *open_code_hook = runtime->open_code_hook;
void *open_code_userdata = runtime->open_code_userdata;
_Py_AuditHookEntry *audit_hook_head = runtime->audit_hooks.head;
// bpo-42882: Preserve next_index value if Py_Initialize()/Py_Finalize()
// is called multiple times.
Py_ssize_t unicode_next_index = runtime->unicode_state.ids.next_index;
if (runtime->_initialized) {
// Py_Initialize() must be running again.
// Reset to _PyRuntimeState_INIT.
memcpy(runtime, &initial, sizeof(*runtime));
assert(!runtime->_initialized);
}
if (gilstate_tss_init(runtime) != 0) {
_PyRuntimeState_Fini(runtime);
return _PyStatus_NO_MEMORY();
}
if (PyThread_tss_create(&runtime->trashTSSkey) != 0) {
_PyRuntimeState_Fini(runtime);
return _PyStatus_NO_MEMORY();
}
init_runtime(runtime, open_code_hook, open_code_userdata, audit_hook_head,
unicode_next_index);
return _PyStatus_OK();
}
void
_PyRuntimeState_Fini(_PyRuntimeState *runtime)
{
#ifdef Py_REF_DEBUG
/* The count is cleared by _Py_FinalizeRefTotal(). */
assert(runtime->object_state.interpreter_leaks == 0);
#endif
if (gilstate_tss_initialized(runtime)) {
gilstate_tss_fini(runtime);
}
if (PyThread_tss_is_created(&runtime->trashTSSkey)) {
PyThread_tss_delete(&runtime->trashTSSkey);
}
}
#ifdef HAVE_FORK
/* This function is called from PyOS_AfterFork_Child to ensure that
newly created child processes do not share locks with the parent. */
PyStatus
_PyRuntimeState_ReInitThreads(_PyRuntimeState *runtime)
{
// This was initially set in _PyRuntimeState_Init().
runtime->main_thread = PyThread_get_thread_ident();
// Clears the parking lot. Any waiting threads are dead. This must be
// called before releasing any locks that use the parking lot.
_PyParkingLot_AfterFork();
// Re-initialize global locks
PyMutex *locks[] = LOCKS_INIT(runtime);
for (size_t i = 0; i < Py_ARRAY_LENGTH(locks); i++) {
_PyMutex_at_fork_reinit(locks[i]);
}
#ifdef Py_GIL_DISABLED
for (PyInterpreterState *interp = runtime->interpreters.head;
interp != NULL; interp = interp->next)
{
for (int i = 0; i < NUM_WEAKREF_LIST_LOCKS; i++) {
_PyMutex_at_fork_reinit(&interp->weakref_locks[i]);
}
}
#endif
_PyTypes_AfterFork();
/* bpo-42540: id_mutex is freed by _PyInterpreterState_Delete, which does
* not force the default allocator. */
if (_PyThread_at_fork_reinit(&runtime->interpreters.main->id_mutex) < 0) {
return _PyStatus_ERR("Failed to reinitialize runtime locks");
}
PyStatus status = gilstate_tss_reinit(runtime);
if (_PyStatus_EXCEPTION(status)) {
return status;
}
if (PyThread_tss_is_created(&runtime->trashTSSkey)) {
PyThread_tss_delete(&runtime->trashTSSkey);
}
if (PyThread_tss_create(&runtime->trashTSSkey) != 0) {
return _PyStatus_NO_MEMORY();
}
_PyThread_AfterFork(&runtime->threads);
return _PyStatus_OK();
}
#endif
/*************************************/
/* the per-interpreter runtime state */
/*************************************/
//----------
// lifecycle
//----------
/* Calling this indicates that the runtime is ready to create interpreters. */
PyStatus
_PyInterpreterState_Enable(_PyRuntimeState *runtime)
{
struct pyinterpreters *interpreters = &runtime->interpreters;
interpreters->next_id = 0;
return _PyStatus_OK();
}
static PyInterpreterState *
alloc_interpreter(void)
{
return PyMem_RawCalloc(1, sizeof(PyInterpreterState));
}
static void
free_interpreter(PyInterpreterState *interp)
{
// The main interpreter is statically allocated so
// should not be freed.
if (interp != &_PyRuntime._main_interpreter) {
if (_PyMem_obmalloc_state_on_heap(interp)) {
// interpreter has its own obmalloc state, free it
PyMem_RawFree(interp->obmalloc);
interp->obmalloc = NULL;
}
PyMem_RawFree(interp);
}
}
static inline int check_interpreter_whence(long);
/* Get the interpreter state to a minimal consistent state.
Further init happens in pylifecycle.c before it can be used.
All fields not initialized here are expected to be zeroed out,
e.g. by PyMem_RawCalloc() or memset(), or otherwise pre-initialized.
The runtime state is not manipulated. Instead it is assumed that
the interpreter is getting added to the runtime.
Note that the main interpreter was statically initialized as part
of the runtime and most state is already set properly. That leaves
a small number of fields to initialize dynamically, as well as some
that are initialized lazily.
For subinterpreters we memcpy() the main interpreter in
PyInterpreterState_New(), leaving it in the same mostly-initialized
state. The only difference is that the interpreter has some
self-referential state that is statically initializexd to the
main interpreter. We fix those fields here, in addition
to the other dynamically initialized fields.
*/
static PyStatus
init_interpreter(PyInterpreterState *interp,
_PyRuntimeState *runtime, int64_t id,
PyInterpreterState *next,
long whence)
{
if (interp->_initialized) {
return _PyStatus_ERR("interpreter already initialized");
}
assert(interp->_whence == _PyInterpreterState_WHENCE_NOTSET);
assert(check_interpreter_whence(whence) == 0);
interp->_whence = whence;
assert(runtime != NULL);
interp->runtime = runtime;
assert(id > 0 || (id == 0 && interp == runtime->interpreters.main));
interp->id = id;
assert(runtime->interpreters.head == interp);
assert(next != NULL || (interp == runtime->interpreters.main));
interp->next = next;
PyStatus status = _PyObject_InitState(interp);
if (_PyStatus_EXCEPTION(status)) {
return status;
}
_PyEval_InitState(interp);
_PyGC_InitState(&interp->gc);
PyConfig_InitPythonConfig(&interp->config);
_PyType_InitCache(interp);
#ifdef Py_GIL_DISABLED
_Py_brc_init_state(interp);
#endif
llist_init(&interp->mem_free_queue.head);
for (int i = 0; i < _PY_MONITORING_UNGROUPED_EVENTS; i++) {
interp->monitors.tools[i] = 0;
}
for (int t = 0; t < PY_MONITORING_TOOL_IDS; t++) {
for (int e = 0; e < _PY_MONITORING_EVENTS; e++) {
interp->monitoring_callables[t][e] = NULL;
}
}
interp->sys_profile_initialized = false;
interp->sys_trace_initialized = false;
#ifdef _Py_TIER2
(void)_Py_SetOptimizer(interp, NULL);
interp->executor_list_head = NULL;
#endif
if (interp != &runtime->_main_interpreter) {
/* Fix the self-referential, statically initialized fields. */
interp->dtoa = (struct _dtoa_state)_dtoa_state_INIT(interp);
}
interp->_initialized = 1;
return _PyStatus_OK();
}
PyStatus
_PyInterpreterState_New(PyThreadState *tstate, PyInterpreterState **pinterp)
{
*pinterp = NULL;
// Don't get runtime from tstate since tstate can be NULL
_PyRuntimeState *runtime = &_PyRuntime;
// tstate is NULL when pycore_create_interpreter() calls
// _PyInterpreterState_New() to create the main interpreter.
if (tstate != NULL) {
if (_PySys_Audit(tstate, "cpython.PyInterpreterState_New", NULL) < 0) {
return _PyStatus_ERR("sys.audit failed");
}
}
/* We completely serialize creation of multiple interpreters, since
it simplifies things here and blocking concurrent calls isn't a problem.
Regardless, we must fully block subinterpreter creation until
after the main interpreter is created. */
HEAD_LOCK(runtime);
struct pyinterpreters *interpreters = &runtime->interpreters;
int64_t id = interpreters->next_id;
interpreters->next_id += 1;
// Allocate the interpreter and add it to the runtime state.
PyInterpreterState *interp;
PyStatus status;
PyInterpreterState *old_head = interpreters->head;
if (old_head == NULL) {
// We are creating the main interpreter.
assert(interpreters->main == NULL);
assert(id == 0);
interp = &runtime->_main_interpreter;
assert(interp->id == 0);
assert(interp->next == NULL);
interpreters->main = interp;
}
else {
assert(interpreters->main != NULL);
assert(id != 0);
interp = alloc_interpreter();
if (interp == NULL) {
status = _PyStatus_NO_MEMORY();
goto error;
}
// Set to _PyInterpreterState_INIT.
memcpy(interp, &initial._main_interpreter, sizeof(*interp));
if (id < 0) {
/* overflow or Py_Initialize() not called yet! */
status = _PyStatus_ERR("failed to get an interpreter ID");
goto error;
}
}
interpreters->head = interp;
long whence = _PyInterpreterState_WHENCE_UNKNOWN;
status = init_interpreter(interp, runtime,
id, old_head, whence);
if (_PyStatus_EXCEPTION(status)) {
goto error;
}
HEAD_UNLOCK(runtime);
assert(interp != NULL);
*pinterp = interp;
return _PyStatus_OK();
error:
HEAD_UNLOCK(runtime);
if (interp != NULL) {
free_interpreter(interp);
}
return status;
}
PyInterpreterState *
PyInterpreterState_New(void)
{
// tstate can be NULL
PyThreadState *tstate = current_fast_get();
PyInterpreterState *interp;
PyStatus status = _PyInterpreterState_New(tstate, &interp);
if (_PyStatus_EXCEPTION(status)) {
Py_ExitStatusException(status);
}
assert(interp != NULL);
return interp;
}
static void
interpreter_clear(PyInterpreterState *interp, PyThreadState *tstate)
{
assert(interp != NULL);
assert(tstate != NULL);
_PyRuntimeState *runtime = interp->runtime;
/* XXX Conditions we need to enforce:
* the GIL must be held by the current thread
* tstate must be the "current" thread state (current_fast_get())
* tstate->interp must be interp
* for the main interpreter, tstate must be the main thread
*/
// XXX Ideally, we would not rely on any thread state in this function
// (and we would drop the "tstate" argument).
if (_PySys_Audit(tstate, "cpython.PyInterpreterState_Clear", NULL) < 0) {
_PyErr_Clear(tstate);
}
// Clear the current/main thread state last.
HEAD_LOCK(runtime);
PyThreadState *p = interp->threads.head;
HEAD_UNLOCK(runtime);
while (p != NULL) {
// See https://github.com/python/cpython/issues/102126
// Must be called without HEAD_LOCK held as it can deadlock
// if any finalizer tries to acquire that lock.
PyThreadState_Clear(p);
HEAD_LOCK(runtime);
p = p->next;
HEAD_UNLOCK(runtime);
}
if (tstate->interp == interp) {
/* We fix tstate->_status below when we for sure aren't using it
(e.g. no longer need the GIL). */
// XXX Eliminate the need to do this.
tstate->_status.cleared = 0;
}
#ifdef _Py_TIER2
_PyOptimizerObject *old = _Py_SetOptimizer(interp, NULL);
assert(old != NULL);
Py_DECREF(old);
#endif
/* It is possible that any of the objects below have a finalizer
that runs Python code or otherwise relies on a thread state
or even the interpreter state. For now we trust that isn't
a problem.
*/
// XXX Make sure we properly deal with problematic finalizers.
Py_CLEAR(interp->audit_hooks);
// At this time, all the threads should be cleared so we don't need atomic
// operations for instrumentation_version or eval_breaker.
interp->ceval.instrumentation_version = 0;
tstate->eval_breaker = 0;
for (int i = 0; i < _PY_MONITORING_UNGROUPED_EVENTS; i++) {
interp->monitors.tools[i] = 0;
}
for (int t = 0; t < PY_MONITORING_TOOL_IDS; t++) {
for (int e = 0; e < _PY_MONITORING_EVENTS; e++) {
Py_CLEAR(interp->monitoring_callables[t][e]);
}
}
interp->sys_profile_initialized = false;
interp->sys_trace_initialized = false;
for (int t = 0; t < PY_MONITORING_TOOL_IDS; t++) {
Py_CLEAR(interp->monitoring_tool_names[t]);
}
PyConfig_Clear(&interp->config);
_PyCodec_Fini(interp);
assert(interp->imports.modules == NULL);
assert(interp->imports.modules_by_index == NULL);
assert(interp->imports.importlib == NULL);
assert(interp->imports.import_func == NULL);
Py_CLEAR(interp->sysdict_copy);
Py_CLEAR(interp->builtins_copy);
Py_CLEAR(interp->dict);
#ifdef HAVE_FORK
Py_CLEAR(interp->before_forkers);
Py_CLEAR(interp->after_forkers_parent);
Py_CLEAR(interp->after_forkers_child);
#endif
_PyAST_Fini(interp);
_PyWarnings_Fini(interp);
_PyAtExit_Fini(interp);
// All Python types must be destroyed before the last GC collection. Python
// types create a reference cycle to themselves in their in their
// PyTypeObject.tp_mro member (the tuple contains the type).
/* Last garbage collection on this interpreter */
_PyGC_CollectNoFail(tstate);
_PyGC_Fini(interp);
/* We don't clear sysdict and builtins until the end of this function.
Because clearing other attributes can execute arbitrary Python code
which requires sysdict and builtins. */
PyDict_Clear(interp->sysdict);
PyDict_Clear(interp->builtins);
Py_CLEAR(interp->sysdict);
Py_CLEAR(interp->builtins);
if (tstate->interp == interp) {
/* We are now safe to fix tstate->_status.cleared. */
// XXX Do this (much) earlier?
tstate->_status.cleared = 1;
}
for (int i=0; i < DICT_MAX_WATCHERS; i++) {
interp->dict_state.watchers[i] = NULL;
}
for (int i=0; i < TYPE_MAX_WATCHERS; i++) {
interp->type_watchers[i] = NULL;
}
for (int i=0; i < FUNC_MAX_WATCHERS; i++) {
interp->func_watchers[i] = NULL;
}
interp->active_func_watchers = 0;
for (int i=0; i < CODE_MAX_WATCHERS; i++) {
interp->code_watchers[i] = NULL;
}
interp->active_code_watchers = 0;
// XXX Once we have one allocator per interpreter (i.e.
// per-interpreter GC) we must ensure that all of the interpreter's
// objects have been cleaned up at the point.
}
void
PyInterpreterState_Clear(PyInterpreterState *interp)
{
// Use the current Python thread state to call audit hooks and to collect
// garbage. It can be different than the current Python thread state
// of 'interp'.
PyThreadState *current_tstate = current_fast_get();
_PyImport_ClearCore(interp);
interpreter_clear(interp, current_tstate);
}
void
_PyInterpreterState_Clear(PyThreadState *tstate)
{
_PyImport_ClearCore(tstate->interp);
interpreter_clear(tstate->interp, tstate);
}
static inline void tstate_deactivate(PyThreadState *tstate);
static void tstate_set_detached(PyThreadState *tstate, int detached_state);
static void zapthreads(PyInterpreterState *interp);
void
PyInterpreterState_Delete(PyInterpreterState *interp)
{
_PyRuntimeState *runtime = interp->runtime;
struct pyinterpreters *interpreters = &runtime->interpreters;
// XXX Clearing the "current" thread state should happen before
// we start finalizing the interpreter (or the current thread state).
PyThreadState *tcur = current_fast_get();
if (tcur != NULL && interp == tcur->interp) {
/* Unset current thread. After this, many C API calls become crashy. */
_PyThreadState_Detach(tcur);
}
zapthreads(interp);
// XXX These two calls should be done at the end of clear_interpreter(),
// but currently some objects get decref'ed after that.
#ifdef Py_REF_DEBUG
_PyInterpreterState_FinalizeRefTotal(interp);
#endif
_PyInterpreterState_FinalizeAllocatedBlocks(interp);
HEAD_LOCK(runtime);
PyInterpreterState **p;
for (p = &interpreters->head; ; p = &(*p)->next) {
if (*p == NULL) {
Py_FatalError("NULL interpreter");
}
if (*p == interp) {
break;
}
}
if (interp->threads.head != NULL) {
Py_FatalError("remaining threads");
}
*p = interp->next;
if (interpreters->main == interp) {
interpreters->main = NULL;
if (interpreters->head != NULL) {
Py_FatalError("remaining subinterpreters");
}
}
HEAD_UNLOCK(runtime);
if (interp->id_mutex != NULL) {
PyThread_free_lock(interp->id_mutex);
}
_Py_qsbr_fini(interp);
_PyObject_FiniState(interp);
free_interpreter(interp);
}
#ifdef HAVE_FORK
/*
* Delete all interpreter states except the main interpreter. If there
* is a current interpreter state, it *must* be the main interpreter.
*/
PyStatus
_PyInterpreterState_DeleteExceptMain(_PyRuntimeState *runtime)