bpo-36710: Add runtime parameter to _PyThreadState_Init() (GH-12935) · python/cpython@8bb3230 (original) (raw)
`@@ -133,7 +133,9 @@ _PyRuntimeState_ReInitThreads(void)
`
133
133
` WAIT_LOCK)
`
134
134
`#define HEAD_UNLOCK() PyThread_release_lock(_PyRuntime.interpreters.mutex)
`
135
135
``
136
``
`-
static void _PyGILState_NoteThreadState(PyThreadState* tstate);
`
``
136
`+
/* Forward declaration */
`
``
137
`+
static void _PyGILState_NoteThreadState(
`
``
138
`+
struct _gilstate_runtime_state gilstate, PyThreadState tstate);
`
137
139
``
138
140
`_PyInitError
`
139
141
`_PyInterpreterState_Enable(_PyRuntimeState *runtime)
`
`@@ -487,71 +489,74 @@ static PyThreadState *
`
487
489
`new_threadstate(PyInterpreterState *interp, int init)
`
488
490
`{
`
489
491
`PyThreadState *tstate = (PyThreadState *)PyMem_RawMalloc(sizeof(PyThreadState));
`
``
492
`+
if (tstate == NULL) {
`
``
493
`+
return NULL;
`
``
494
`+
}
`
490
495
``
491
``
`-
if (_PyThreadState_GetFrame == NULL)
`
``
496
`+
if (_PyThreadState_GetFrame == NULL) {
`
492
497
`_PyThreadState_GetFrame = threadstate_getframe;
`
``
498
`+
}
`
493
499
``
494
``
`-
if (tstate != NULL) {
`
495
``
`-
tstate->interp = interp;
`
496
``
-
497
``
`-
tstate->frame = NULL;
`
498
``
`-
tstate->recursion_depth = 0;
`
499
``
`-
tstate->overflowed = 0;
`
500
``
`-
tstate->recursion_critical = 0;
`
501
``
`-
tstate->stackcheck_counter = 0;
`
502
``
`-
tstate->tracing = 0;
`
503
``
`-
tstate->use_tracing = 0;
`
504
``
`-
tstate->gilstate_counter = 0;
`
505
``
`-
tstate->async_exc = NULL;
`
506
``
`-
tstate->thread_id = PyThread_get_thread_ident();
`
``
500
`+
tstate->interp = interp;
`
507
501
``
508
``
`-
tstate->dict = NULL;
`
``
502
`+
tstate->frame = NULL;
`
``
503
`+
tstate->recursion_depth = 0;
`
``
504
`+
tstate->overflowed = 0;
`
``
505
`+
tstate->recursion_critical = 0;
`
``
506
`+
tstate->stackcheck_counter = 0;
`
``
507
`+
tstate->tracing = 0;
`
``
508
`+
tstate->use_tracing = 0;
`
``
509
`+
tstate->gilstate_counter = 0;
`
``
510
`+
tstate->async_exc = NULL;
`
``
511
`+
tstate->thread_id = PyThread_get_thread_ident();
`
509
512
``
510
``
`-
tstate->curexc_type = NULL;
`
511
``
`-
tstate->curexc_value = NULL;
`
512
``
`-
tstate->curexc_traceback = NULL;
`
``
513
`+
tstate->dict = NULL;
`
513
514
``
514
``
`-
tstate->exc_state.exc_type = NULL;
`
515
``
`-
tstate->exc_state.exc_value = NULL;
`
516
``
`-
tstate->exc_state.exc_traceback = NULL;
`
517
``
`-
tstate->exc_state.previous_item = NULL;
`
518
``
`-
tstate->exc_info = &tstate->exc_state;
`
``
515
`+
tstate->curexc_type = NULL;
`
``
516
`+
tstate->curexc_value = NULL;
`
``
517
`+
tstate->curexc_traceback = NULL;
`
519
518
``
520
``
`-
tstate->c_profilefunc = NULL;
`
521
``
`-
tstate->c_tracefunc = NULL;
`
522
``
`-
tstate->c_profileobj = NULL;
`
523
``
`-
tstate->c_traceobj = NULL;
`
``
519
`+
tstate->exc_state.exc_type = NULL;
`
``
520
`+
tstate->exc_state.exc_value = NULL;
`
``
521
`+
tstate->exc_state.exc_traceback = NULL;
`
``
522
`+
tstate->exc_state.previous_item = NULL;
`
``
523
`+
tstate->exc_info = &tstate->exc_state;
`
524
524
``
525
``
`-
tstate->trash_delete_nesting = 0;
`
526
``
`-
tstate->trash_delete_later = NULL;
`
527
``
`-
tstate->on_delete = NULL;
`
528
``
`-
tstate->on_delete_data = NULL;
`
``
525
`+
tstate->c_profilefunc = NULL;
`
``
526
`+
tstate->c_tracefunc = NULL;
`
``
527
`+
tstate->c_profileobj = NULL;
`
``
528
`+
tstate->c_traceobj = NULL;
`
529
529
``
530
``
`-
tstate->coroutine_origin_tracking_depth = 0;
`
``
530
`+
tstate->trash_delete_nesting = 0;
`
``
531
`+
tstate->trash_delete_later = NULL;
`
``
532
`+
tstate->on_delete = NULL;
`
``
533
`+
tstate->on_delete_data = NULL;
`
531
534
``
532
``
`-
tstate->coroutine_wrapper = NULL;
`
533
``
`-
tstate->in_coroutine_wrapper = 0;
`
``
535
`+
tstate->coroutine_origin_tracking_depth = 0;
`
534
536
``
535
``
`-
tstate->async_gen_firstiter = NULL;
`
536
``
`-
tstate->async_gen_finalizer = NULL;
`
``
537
`+
tstate->coroutine_wrapper = NULL;
`
``
538
`+
tstate->in_coroutine_wrapper = 0;
`
537
539
``
538
``
`-
tstate->context = NULL;
`
539
``
`-
tstate->context_ver = 1;
`
``
540
`+
tstate->async_gen_firstiter = NULL;
`
``
541
`+
tstate->async_gen_finalizer = NULL;
`
540
542
``
541
``
`-
tstate->id = ++interp->tstate_next_unique_id;
`
``
543
`+
tstate->context = NULL;
`
``
544
`+
tstate->context_ver = 1;
`
542
545
``
543
``
`-
if (init)
`
544
``
`-
_PyThreadState_Init(tstate);
`
``
546
`+
tstate->id = ++interp->tstate_next_unique_id;
`
545
547
``
546
``
`-
HEAD_LOCK();
`
547
``
`-
tstate->prev = NULL;
`
548
``
`-
tstate->next = interp->tstate_head;
`
549
``
`-
if (tstate->next)
`
550
``
`-
tstate->next->prev = tstate;
`
551
``
`-
interp->tstate_head = tstate;
`
552
``
`-
HEAD_UNLOCK();
`
``
548
`+
if (init) {
`
``
549
`+
_PyThreadState_Init(&_PyRuntime, tstate);
`
553
550
` }
`
554
551
``
``
552
`+
HEAD_LOCK();
`
``
553
`+
tstate->prev = NULL;
`
``
554
`+
tstate->next = interp->tstate_head;
`
``
555
`+
if (tstate->next)
`
``
556
`+
tstate->next->prev = tstate;
`
``
557
`+
interp->tstate_head = tstate;
`
``
558
`+
HEAD_UNLOCK();
`
``
559
+
555
560
`return tstate;
`
556
561
`}
`
557
562
``
`@@ -568,9 +573,9 @@ _PyThreadState_Prealloc(PyInterpreterState *interp)
`
568
573
`}
`
569
574
``
570
575
`void
`
571
``
`-
_PyThreadState_Init(PyThreadState *tstate)
`
``
576
`+
_PyThreadState_Init(_PyRuntimeState *runtime, PyThreadState *tstate)
`
572
577
`{
`
573
``
`-
_PyGILState_NoteThreadState(tstate);
`
``
578
`+
_PyGILState_NoteThreadState(&runtime->gilstate, tstate);
`
574
579
`}
`
575
580
``
576
581
`PyObject*
`
`@@ -1037,17 +1042,23 @@ PyThreadState_IsCurrent(PyThreadState *tstate)
`
1037
1042
` Py_Initialize/Py_FinalizeEx
`
1038
1043
`*/
`
1039
1044
`void
`
1040
``
`-
_PyGILState_Init(PyInterpreterState *i, PyThreadState *t)
`
``
1045
`+
_PyGILState_Init(PyInterpreterState *interp, PyThreadState *tstate)
`
1041
1046
`{
`
1042
``
`-
assert(i && t); /* must init with valid states */
`
1043
``
`-
if (PyThread_tss_create(&_PyRuntime.gilstate.autoTSSkey) != 0) {
`
``
1047
`+
/* must init with valid states */
`
``
1048
`+
assert(interp != NULL);
`
``
1049
`+
assert(tstate != NULL);
`
``
1050
+
``
1051
`+
_PyRuntimeState *runtime = &_PyRuntime;
`
``
1052
`+
struct _gilstate_runtime_state *gilstate = &runtime->gilstate;
`
``
1053
+
``
1054
`+
if (PyThread_tss_create(&gilstate->autoTSSkey) != 0) {
`
1044
1055
`Py_FatalError("Could not allocate TSS entry");
`
1045
1056
` }
`
1046
``
`-
_PyRuntime.gilstate.autoInterpreterState = i;
`
1047
``
`-
assert(PyThread_tss_get(&_PyRuntime.gilstate.autoTSSkey) == NULL);
`
1048
``
`-
assert(t->gilstate_counter == 0);
`
``
1057
`+
gilstate->autoInterpreterState = interp;
`
``
1058
`+
assert(PyThread_tss_get(&gilstate->autoTSSkey) == NULL);
`
``
1059
`+
assert(tstate->gilstate_counter == 0);
`
1049
1060
``
1050
``
`-
_PyGILState_NoteThreadState(t);
`
``
1061
`+
_PyGILState_NoteThreadState(gilstate, tstate);
`
1051
1062
`}
`
1052
1063
``
1053
1064
`PyInterpreterState *
`
`@@ -1104,13 +1115,14 @@ _PyGILState_Reinit(void)
`
1104
1115
` a better fix for SF bug #1010677 than the first one attempted).
`
1105
1116
`*/
`
1106
1117
`static void
`
1107
``
`-
_PyGILState_NoteThreadState(PyThreadState* tstate)
`
``
1118
`+
_PyGILState_NoteThreadState(struct _gilstate_runtime_state gilstate, PyThreadState tstate)
`
1108
1119
`{
`
1109
1120
`/* If autoTSSkey isn't initialized, this must be the very first
`
1110
1121
` threadstate created in Py_Initialize(). Don't do anything for now
`
1111
1122
` (we'll be back here when _PyGILState_Init is called). */
`
1112
``
`-
if (!_PyRuntime.gilstate.autoInterpreterState)
`
``
1123
`+
if (!gilstate->autoInterpreterState) {
`
1113
1124
`return;
`
``
1125
`+
}
`
1114
1126
``
1115
1127
`/* Stick the thread state for this thread in thread specific storage.
`
1116
1128
``
`@@ -1124,10 +1136,8 @@ _PyGILState_NoteThreadState(PyThreadState* tstate)
`
1124
1136
` The first thread state created for that given OS level thread will
`
1125
1137
` "win", which seems reasonable behaviour.
`
1126
1138
` */
`
1127
``
`-
if (PyThread_tss_get(&_PyRuntime.gilstate.autoTSSkey) == NULL) {
`
1128
``
`-
if ((PyThread_tss_set(&_PyRuntime.gilstate.autoTSSkey, (void *)tstate)
`
1129
``
`-
) != 0)
`
1130
``
`-
{
`
``
1139
`+
if (PyThread_tss_get(&gilstate->autoTSSkey) == NULL) {
`
``
1140
`+
if ((PyThread_tss_set(&gilstate->autoTSSkey, (void *)tstate)) != 0) {
`
1131
1141
`Py_FatalError("Couldn't create autoTSSkey mapping");
`
1132
1142
` }
`
1133
1143
` }
`