bpo-40513: Per-interpreter GIL (GH-19943) · python/cpython@7be4e35

@@ -250,6 +250,21 @@ ensure_tstate_not_null(const char *func, PyThreadState *tstate)

250250

}

251251252252253+

#ifdef EXPERIMENTAL_ISOLATED_SUBINTERPRETERS

254+

int

255+

_PyEval_ThreadsInitialized(PyInterpreterState *interp)

256+

{

257+

return gil_created(&interp->ceval.gil);

258+

}

259+260+

int

261+

PyEval_ThreadsInitialized(void)

262+

{

263+

// Fatal error if there is no current interpreter

264+

PyInterpreterState *interp = PyInterpreterState_Get();

265+

return _PyEval_ThreadsInitialized(interp);

266+

}

267+

#else

253268

int

254269

_PyEval_ThreadsInitialized(_PyRuntimeState *runtime)

255270

{

@@ -262,18 +277,25 @@ PyEval_ThreadsInitialized(void)

262277

_PyRuntimeState *runtime = &_PyRuntime;

263278

return _PyEval_ThreadsInitialized(runtime);

264279

}

280+

#endif

265281266282

PyStatus

267283

_PyEval_InitGIL(PyThreadState *tstate)

268284

{

285+

#ifndef EXPERIMENTAL_ISOLATED_SUBINTERPRETERS

269286

if (!_Py_IsMainInterpreter(tstate)) {

270287

/* Currently, the GIL is shared by all interpreters,

271288

and only the main interpreter is responsible to create

272289

and destroy it. */

273290

return _PyStatus_OK();

274291

}

292+

#endif

275293294+

#ifdef EXPERIMENTAL_ISOLATED_SUBINTERPRETERS

295+

struct _gil_runtime_state *gil = &tstate->interp->ceval.gil;

296+

#else

276297

struct _gil_runtime_state *gil = &tstate->interp->runtime->ceval.gil;

298+

#endif

277299

assert(!gil_created(gil));

278300279301

PyThread_init_thread();

@@ -288,14 +310,20 @@ _PyEval_InitGIL(PyThreadState *tstate)

288310

void

289311

_PyEval_FiniGIL(PyThreadState *tstate)

290312

{

313+

#ifndef EXPERIMENTAL_ISOLATED_SUBINTERPRETERS

291314

if (!_Py_IsMainInterpreter(tstate)) {

292315

/* Currently, the GIL is shared by all interpreters,

293316

and only the main interpreter is responsible to create

294317

and destroy it. */

295318

return;

296319

}

320+

#endif

297321322+

#ifdef EXPERIMENTAL_ISOLATED_SUBINTERPRETERS

323+

struct _gil_runtime_state *gil = &tstate->interp->ceval.gil;

324+

#else

298325

struct _gil_runtime_state *gil = &tstate->interp->runtime->ceval.gil;

326+

#endif

299327

if (!gil_created(gil)) {

300328

/* First Py_InitializeFromConfig() call: the GIL doesn't exist

301329

yet: do nothing. */

@@ -413,13 +441,18 @@ PyEval_ReleaseThread(PyThreadState *tstate)

413441

void

414442

_PyEval_ReInitThreads(_PyRuntimeState *runtime)

415443

{

444+

PyThreadState *tstate = _PyRuntimeState_GetThreadState(runtime);

445+

ensure_tstate_not_null(__func__, tstate);

446+447+

#ifdef EXPERIMENTAL_ISOLATED_SUBINTERPRETERS

448+

struct _gil_runtime_state *gil = &tstate->interp->ceval.gil;

449+

#else

416450

struct _gil_runtime_state *gil = &runtime->ceval.gil;

451+

#endif

417452

if (!gil_created(gil)) {

418453

return;

419454

}

420455

recreate_gil(gil);

421-

PyThreadState *tstate = _PyRuntimeState_GetThreadState(runtime);

422-

ensure_tstate_not_null(__func__, tstate);

423456424457

take_gil(tstate);

425458

@@ -457,7 +490,11 @@ PyEval_SaveThread(void)

457490458491

struct _ceval_runtime_state *ceval = &runtime->ceval;

459492

struct _ceval_state *ceval2 = &tstate->interp->ceval;

493+

#ifdef EXPERIMENTAL_ISOLATED_SUBINTERPRETERS

494+

assert(gil_created(&ceval2->gil));

495+

#else

460496

assert(gil_created(&ceval->gil));

497+

#endif

461498

drop_gil(ceval, ceval2, tstate);

462499

return tstate;

463500

}

716753

_PyEval_InitRuntimeState(struct _ceval_runtime_state *ceval)

717754

{

718755

_Py_CheckRecursionLimit = Py_DEFAULT_RECURSION_LIMIT;

756+

#ifndef EXPERIMENTAL_ISOLATED_SUBINTERPRETERS

719757

_gil_initialize(&ceval->gil);

758+

#endif

720759

}

721760722761

int

@@ -731,6 +770,11 @@ _PyEval_InitState(struct _ceval_state *ceval)

731770

if (pending->lock == NULL) {

732771

return -1;

733772

}

773+774+

#ifdef EXPERIMENTAL_ISOLATED_SUBINTERPRETERS

775+

_gil_initialize(&ceval->gil);

776+

#endif

777+734778

return 0;

735779

}

736780