bpo-36710: PyOS_AfterFork_Child() pass runtime parameter (GH-12936) · python/cpython@b930a2d (original) (raw)

`@@ -108,23 +108,31 @@ _PyRuntimeState_Fini(_PyRuntimeState *runtime)

`

108

108

` */

`

109

109

``

110

110

`void

`

111

``

`-

_PyRuntimeState_ReInitThreads(void)

`

``

111

`+

_PyRuntimeState_ReInitThreads(_PyRuntimeState *runtime)

`

112

112

`{

`

113

113

`// This was initially set in _PyRuntimeState_Init().

`

114

``

`-

_PyRuntime.main_thread = PyThread_get_thread_ident();

`

``

114

`+

runtime->main_thread = PyThread_get_thread_ident();

`

``

115

+

``

116

`+

/* Force default allocator, since _PyRuntimeState_Fini() must

`

``

117

`+

use the same allocator than this function. */

`

``

118

`+

PyMemAllocatorEx old_alloc;

`

``

119

`+

_PyMem_SetDefaultAllocator(PYMEM_DOMAIN_RAW, &old_alloc);

`

``

120

+

``

121

`+

runtime->interpreters.mutex = PyThread_allocate_lock();

`

``

122

`+

runtime->interpreters.main->id_mutex = PyThread_allocate_lock();

`

``

123

`+

runtime->xidregistry.mutex = PyThread_allocate_lock();

`

``

124

+

``

125

`+

PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &old_alloc);

`

115

126

``

116

``

`-

_PyRuntime.interpreters.mutex = PyThread_allocate_lock();

`

117

``

`-

if (_PyRuntime.interpreters.mutex == NULL) {

`

``

127

`+

if (runtime->interpreters.mutex == NULL) {

`

118

128

`Py_FatalError("Can't initialize lock for runtime interpreters");

`

119

129

` }

`

120

130

``

121

``

`-

_PyRuntime.interpreters.main->id_mutex = PyThread_allocate_lock();

`

122

``

`-

if (_PyRuntime.interpreters.main->id_mutex == NULL) {

`

``

131

`+

if (runtime->interpreters.main->id_mutex == NULL) {

`

123

132

`Py_FatalError("Can't initialize ID lock for main interpreter");

`

124

133

` }

`

125

134

``

126

``

`-

_PyRuntime.xidregistry.mutex = PyThread_allocate_lock();

`

127

``

`-

if (_PyRuntime.xidregistry.mutex == NULL) {

`

``

135

`+

if (runtime->xidregistry.mutex == NULL) {

`

128

136

`Py_FatalError("Can't initialize lock for cross-interpreter data registry");

`

129

137

` }

`

130

138

`}

`

`@@ -290,20 +298,22 @@ PyInterpreterState_Delete(PyInterpreterState *interp)

`

290

298

` * is a current interpreter state, it must be the main interpreter.

`

291

299

` */

`

292

300

`void

`

293

``

`-

_PyInterpreterState_DeleteExceptMain()

`

``

301

`+

_PyInterpreterState_DeleteExceptMain(_PyRuntimeState *runtime)

`

294

302

`{

`

``

303

`+

struct pyinterpreters *interpreters = &runtime->interpreters;

`

``

304

+

295

305

`PyThreadState *tstate = PyThreadState_Swap(NULL);

`

296

``

`-

if (tstate != NULL && tstate->interp != _PyRuntime.interpreters.main) {

`

``

306

`+

if (tstate != NULL && tstate->interp != interpreters->main) {

`

297

307

`Py_FatalError("PyInterpreterState_DeleteExceptMain: not main interpreter");

`

298

308

` }

`

299

309

``

300

310

`HEAD_LOCK();

`

301

``

`-

PyInterpreterState *interp = _PyRuntime.interpreters.head;

`

302

``

`-

_PyRuntime.interpreters.head = NULL;

`

``

311

`+

PyInterpreterState *interp = interpreters->head;

`

``

312

`+

interpreters->head = NULL;

`

303

313

`while (interp != NULL) {

`

304

``

`-

if (interp == _PyRuntime.interpreters.main) {

`

305

``

`-

_PyRuntime.interpreters.main->next = NULL;

`

306

``

`-

_PyRuntime.interpreters.head = interp;

`

``

314

`+

if (interp == interpreters->main) {

`

``

315

`+

interpreters->main->next = NULL;

`

``

316

`+

interpreters->head = interp;

`

307

317

`interp = interp->next;

`

308

318

`continue;

`

309

319

` }

`

`@@ -319,7 +329,7 @@ _PyInterpreterState_DeleteExceptMain()

`

319

329

` }

`

320

330

`HEAD_UNLOCK();

`

321

331

``

322

``

`-

if (_PyRuntime.interpreters.head == NULL) {

`

``

332

`+

if (interpreters->head == NULL) {

`

323

333

`Py_FatalError("PyInterpreterState_DeleteExceptMain: missing main");

`

324

334

` }

`

325

335

`PyThreadState_Swap(tstate);

`

`@@ -1079,31 +1089,20 @@ _PyGILState_Fini(void)

`

1079

1089

` * don't reset TSS upon fork(), see issue #10517.

`

1080

1090

` */

`

1081

1091

`void

`

1082

``

`-

_PyGILState_Reinit(void)

`

``

1092

`+

_PyGILState_Reinit(_PyRuntimeState *runtime)

`

1083

1093

`{

`

1084

``

`-

/* Force default allocator, since _PyRuntimeState_Fini() must

`

1085

``

`-

use the same allocator than this function. */

`

1086

``

`-

PyMemAllocatorEx old_alloc;

`

1087

``

`-

_PyMem_SetDefaultAllocator(PYMEM_DOMAIN_RAW, &old_alloc);

`

1088

``

-

1089

``

`-

_PyRuntime.interpreters.mutex = PyThread_allocate_lock();

`

1090

``

-

1091

``

`-

PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &old_alloc);

`

1092

``

-

1093

``

`-

if (_PyRuntime.interpreters.mutex == NULL) {

`

1094

``

`-

Py_FatalError("Can't initialize threads for interpreter");

`

1095

``

`-

}

`

1096

``

-

``

1094

`+

struct _gilstate_runtime_state *gilstate = &runtime->gilstate;

`

1097

1095

`PyThreadState *tstate = PyGILState_GetThisThreadState();

`

1098

``

`-

PyThread_tss_delete(&_PyRuntime.gilstate.autoTSSkey);

`

1099

``

`-

if (PyThread_tss_create(&_PyRuntime.gilstate.autoTSSkey) != 0) {

`

``

1096

+

``

1097

`+

PyThread_tss_delete(&gilstate->autoTSSkey);

`

``

1098

`+

if (PyThread_tss_create(&gilstate->autoTSSkey) != 0) {

`

1100

1099

`Py_FatalError("Could not allocate TSS entry");

`

1101

1100

` }

`

1102

1101

``

1103

1102

`/* If the thread had an associated auto thread state, reassociate it with

`

1104

1103

` * the new key. */

`

1105

1104

`if (tstate &&

`

1106

``

`-

PyThread_tss_set(&_PyRuntime.gilstate.autoTSSkey, (void *)tstate) != 0)

`

``

1105

`+

PyThread_tss_set(&gilstate->autoTSSkey, (void *)tstate) != 0)

`

1107

1106

` {

`

1108

1107

`Py_FatalError("Couldn't create autoTSSkey mapping");

`

1109

1108

` }

`