Skip to content

Commit 9204fb8

Browse files
authored
bpo-35081: Cleanup pystate.c and pystate.h (GH-10240)
* Remove _PyThreadState_Current * Replace GET_TSTATE() with PyThreadState_GET() * Replace GET_INTERP_STATE() with _PyInterpreterState_GET_UNSAFE() * Replace direct access to _PyThreadState_Current with PyThreadState_GET() * Replace _PyThreadState_Current with _PyRuntime.gilstate.tstate_current * Rename SET_TSTATE() to _PyThreadState_SET(), name more consistent with _PyThreadState_GET() * Update outdated comments
1 parent 3c09dca commit 9204fb8

File tree

4 files changed

+26
-31
lines changed

4 files changed

+26
-31
lines changed

‎Include/pystate.h‎

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -208,8 +208,8 @@ typedef struct _ts {
208208
* if the thread holds the last reference to the lock, decref'ing the
209209
* lock will delete the lock, and that may trigger arbitrary Python code
210210
* if there's a weakref, with a callback, to the lock. But by this time
211-
* _PyThreadState_Current is already NULL, so only the simplest of C code
212-
* can be allowed to run (in particular it must not be possible to
211+
* _PyRuntime.gilstate.tstate_current is already NULL, so only the simplest
212+
* of C code can be allowed to run (in particular it must not be possible to
213213
* release the GIL).
214214
* So instead of holding the lock directly, the tstate holds a weakref to
215215
* the lock: that's the value of on_delete_data below. Decref'ing a
@@ -307,9 +307,8 @@ PyAPI_FUNC(int) PyThreadState_SetAsyncExc(unsigned long, PyObject *);
307307
/* Assuming the current thread holds the GIL, this is the
308308
PyThreadState for the current thread. */
309309
#ifdef Py_BUILD_CORE
310-
# define _PyThreadState_Current _PyRuntime.gilstate.tstate_current
311310
# define PyThreadState_GET() \
312-
((PyThreadState*)_Py_atomic_load_relaxed(&_PyThreadState_Current))
311+
((PyThreadState*)_Py_atomic_load_relaxed(&_PyRuntime.gilstate.tstate_current))
313312
#else
314313
# define PyThreadState_GET() PyThreadState_Get()
315314
#endif

‎Objects/dictobject.c‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1314,8 +1314,8 @@ PyDict_GetItem(PyObject *op, PyObject *key)
13141314
/* We can arrive here with a NULL tstate during initialization: try
13151315
running "python -Wi" for an example related to string interning.
13161316
Let's just hope that no exception occurs then... This must be
1317-
_PyThreadState_Current and not PyThreadState_GET() because in debug
1318-
mode, the latter complains if tstate is NULL. */
1317+
PyThreadState_GET() and not PyThreadState_Get() because the latter
1318+
abort Python if tstate is NULL. */
13191319
tstate = PyThreadState_GET();
13201320
if (tstate != NULL && tstate->curexc_type != NULL) {
13211321
/* preserve the existing exception */

‎Python/ceval.c‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -188,8 +188,7 @@ PyEval_ReleaseLock(void)
188188
We therefore avoid PyThreadState_GET() which dumps a fatal error
189189
in debug mode.
190190
*/
191-
drop_gil((PyThreadState*)_Py_atomic_load_relaxed(
192-
&_PyThreadState_Current));
191+
drop_gil(PyThreadState_GET());
193192
}
194193

195194
void

‎Python/pystate.c‎

Lines changed: 20 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,9 @@
44
#include "Python.h"
55
#include "internal/pystate.h"
66

7-
#define GET_TSTATE() \
8-
((PyThreadState*)_Py_atomic_load_relaxed(&_PyThreadState_Current))
9-
#define SET_TSTATE(value) \
10-
_Py_atomic_store_relaxed(&_PyThreadState_Current, (uintptr_t)(value))
11-
#define GET_INTERP_STATE() \
12-
(GET_TSTATE()->interp)
7+
#define _PyThreadState_SET(value) \
8+
_Py_atomic_store_relaxed(&_PyRuntime.gilstate.tstate_current, \
9+
(uintptr_t)(value))
1310

1411

1512
/* --------------------------------------------------------------------------
@@ -309,7 +306,7 @@ _PyInterpreterState_DeleteExceptMain()
309306
PyInterpreterState *
310307
_PyInterpreterState_Get(void)
311308
{
312-
PyThreadState *tstate = GET_TSTATE();
309+
PyThreadState *tstate = PyThreadState_GET();
313310
if (tstate == NULL) {
314311
Py_FatalError("_PyInterpreterState_Get(): no current thread state");
315312
}
@@ -508,7 +505,7 @@ PyObject*
508505
PyState_FindModule(struct PyModuleDef* module)
509506
{
510507
Py_ssize_t index = module->m_base.m_index;
511-
PyInterpreterState *state = GET_INTERP_STATE();
508+
PyInterpreterState *state = _PyInterpreterState_GET_UNSAFE();
512509
PyObject *res;
513510
if (module->m_slots) {
514511
return NULL;
@@ -536,7 +533,7 @@ _PyState_AddModule(PyObject* module, struct PyModuleDef* def)
536533
"PyState_AddModule called on module with slots");
537534
return -1;
538535
}
539-
state = GET_INTERP_STATE();
536+
state = _PyInterpreterState_GET_UNSAFE();
540537
if (!state->modules_by_index) {
541538
state->modules_by_index = PyList_New(0);
542539
if (!state->modules_by_index)
@@ -554,7 +551,7 @@ int
554551
PyState_AddModule(PyObject* module, struct PyModuleDef* def)
555552
{
556553
Py_ssize_t index;
557-
PyInterpreterState *state = GET_INTERP_STATE();
554+
PyInterpreterState *state = _PyInterpreterState_GET_UNSAFE();
558555
if (!def) {
559556
Py_FatalError("PyState_AddModule: Module Definition is NULL");
560557
return -1;
@@ -581,7 +578,7 @@ PyState_RemoveModule(struct PyModuleDef* def)
581578
"PyState_RemoveModule called on module with slots");
582579
return -1;
583580
}
584-
state = GET_INTERP_STATE();
581+
state = _PyInterpreterState_GET_UNSAFE();
585582
if (index == 0) {
586583
Py_FatalError("PyState_RemoveModule: Module index invalid.");
587584
return -1;
@@ -601,7 +598,7 @@ PyState_RemoveModule(struct PyModuleDef* def)
601598
void
602599
_PyState_ClearModules(void)
603600
{
604-
PyInterpreterState *state = GET_INTERP_STATE();
601+
PyInterpreterState *state = _PyInterpreterState_GET_UNSAFE();
605602
if (state->modules_by_index) {
606603
Py_ssize_t i;
607604
for (i = 0; i < PyList_GET_SIZE(state->modules_by_index); i++) {
@@ -691,7 +688,7 @@ tstate_delete_common(PyThreadState *tstate)
691688
void
692689
PyThreadState_Delete(PyThreadState *tstate)
693690
{
694-
if (tstate == GET_TSTATE())
691+
if (tstate == PyThreadState_GET())
695692
Py_FatalError("PyThreadState_Delete: tstate is still current");
696693
if (_PyRuntime.gilstate.autoInterpreterState &&
697694
PyThread_tss_get(&_PyRuntime.gilstate.autoTSSkey) == tstate)
@@ -705,7 +702,7 @@ PyThreadState_Delete(PyThreadState *tstate)
705702
void
706703
PyThreadState_DeleteCurrent()
707704
{
708-
PyThreadState *tstate = GET_TSTATE();
705+
PyThreadState *tstate = PyThreadState_GET();
709706
if (tstate == NULL)
710707
Py_FatalError(
711708
"PyThreadState_DeleteCurrent: no current tstate");
@@ -715,7 +712,7 @@ PyThreadState_DeleteCurrent()
715712
{
716713
PyThread_tss_set(&_PyRuntime.gilstate.autoTSSkey, NULL);
717714
}
718-
SET_TSTATE(NULL);
715+
_PyThreadState_SET(NULL);
719716
PyEval_ReleaseLock();
720717
}
721718

@@ -760,14 +757,14 @@ _PyThreadState_DeleteExcept(PyThreadState *tstate)
760757
PyThreadState *
761758
_PyThreadState_UncheckedGet(void)
762759
{
763-
return GET_TSTATE();
760+
return PyThreadState_GET();
764761
}
765762

766763

767764
PyThreadState *
768765
PyThreadState_Get(void)
769766
{
770-
PyThreadState *tstate = GET_TSTATE();
767+
PyThreadState *tstate = PyThreadState_GET();
771768
if (tstate == NULL)
772769
Py_FatalError("PyThreadState_Get: no current thread");
773770

@@ -778,9 +775,9 @@ PyThreadState_Get(void)
778775
PyThreadState *
779776
PyThreadState_Swap(PyThreadState *newts)
780777
{
781-
PyThreadState *oldts = GET_TSTATE();
778+
PyThreadState *oldts = PyThreadState_GET();
782779

783-
SET_TSTATE(newts);
780+
_PyThreadState_SET(newts);
784781
/* It should not be possible for more than one thread state
785782
to be used for a thread. Check this the best we can in debug
786783
builds.
@@ -809,7 +806,7 @@ PyThreadState_Swap(PyThreadState *newts)
809806
PyObject *
810807
PyThreadState_GetDict(void)
811808
{
812-
PyThreadState *tstate = GET_TSTATE();
809+
PyThreadState *tstate = PyThreadState_GET();
813810
if (tstate == NULL)
814811
return NULL;
815812

@@ -834,7 +831,7 @@ PyThreadState_GetDict(void)
834831
int
835832
PyThreadState_SetAsyncExc(unsigned long id, PyObject *exc)
836833
{
837-
PyInterpreterState *interp = GET_INTERP_STATE();
834+
PyInterpreterState *interp = _PyInterpreterState_GET_UNSAFE();
838835
PyThreadState *p;
839836

840837
/* Although the GIL is held, a few C API functions can be called
@@ -960,7 +957,7 @@ PyThreadState_IsCurrent(PyThreadState *tstate)
960957
{
961958
/* Must be the tstate for this thread */
962959
assert(PyGILState_GetThisThreadState()==tstate);
963-
return tstate == GET_TSTATE();
960+
return tstate == PyThreadState_GET();
964961
}
965962

966963
/* Internal initialization/finalization functions called by
@@ -1087,7 +1084,7 @@ PyGILState_Check(void)
10871084
return 1;
10881085
}
10891086

1090-
tstate = GET_TSTATE();
1087+
tstate = PyThreadState_GET();
10911088
if (tstate == NULL)
10921089
return 0;
10931090

0 commit comments

Comments
 (0)