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()
309306PyInterpreterState *
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*
508505PyState_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 )
554551PyState_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)
601598void
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)
691688void
692689PyThreadState_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)
705702void
706703PyThreadState_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)
760757PyThreadState *
761758_PyThreadState_UncheckedGet (void )
762759{
763- return GET_TSTATE ();
760+ return PyThreadState_GET ();
764761}
765762
766763
767764PyThreadState *
768765PyThreadState_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)
778775PyThreadState *
779776PyThreadState_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)
809806PyObject *
810807PyThreadState_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)
834831int
835832PyThreadState_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