33
44#include "Python.h"
55
6+ #ifndef Py_BUILD_CORE
7+ /* ensure that PyThreadState_GET() is a macro, not an alias to
8+ * PyThreadState_Get() */
9+ # error "pystate.c must be compiled with Py_BUILD_CORE defined"
10+ #endif
11+
612/* --------------------------------------------------------------------------
713CAUTION
814
@@ -423,7 +429,7 @@ tstate_delete_common(PyThreadState *tstate)
423429void
424430PyThreadState_Delete (PyThreadState * tstate )
425431{
426- if (tstate == ( PyThreadState * ) _Py_atomic_load_relaxed ( & _PyThreadState_Current ))
432+ if (tstate == PyThreadState_GET ( ))
427433 Py_FatalError ("PyThreadState_Delete: tstate is still current" );
428434#ifdef WITH_THREAD
429435 if (autoInterpreterState && PyThread_get_key_value (autoTLSkey ) == tstate )
@@ -437,8 +443,7 @@ PyThreadState_Delete(PyThreadState *tstate)
437443void
438444PyThreadState_DeleteCurrent ()
439445{
440- PyThreadState * tstate = (PyThreadState * )_Py_atomic_load_relaxed (
441- & _PyThreadState_Current );
446+ PyThreadState * tstate = PyThreadState_GET ();
442447 if (tstate == NULL )
443448 Py_FatalError (
444449 "PyThreadState_DeleteCurrent: no current tstate" );
@@ -488,11 +493,17 @@ _PyThreadState_DeleteExcept(PyThreadState *tstate)
488493}
489494
490495
496+ PyThreadState *
497+ _PyThreadState_UncheckedGet (void )
498+ {
499+ return PyThreadState_GET ();
500+ }
501+
502+
491503PyThreadState *
492504PyThreadState_Get (void )
493505{
494- PyThreadState * tstate = (PyThreadState * )_Py_atomic_load_relaxed (
495- & _PyThreadState_Current );
506+ PyThreadState * tstate = PyThreadState_GET ();
496507 if (tstate == NULL )
497508 Py_FatalError ("PyThreadState_Get: no current thread" );
498509
@@ -503,8 +514,7 @@ PyThreadState_Get(void)
503514PyThreadState *
504515PyThreadState_Swap (PyThreadState * newts )
505516{
506- PyThreadState * oldts = (PyThreadState * )_Py_atomic_load_relaxed (
507- & _PyThreadState_Current );
517+ PyThreadState * oldts = PyThreadState_GET ();
508518
509519 _Py_atomic_store_relaxed (& _PyThreadState_Current , newts );
510520 /* It should not be possible for more than one thread state
@@ -535,8 +545,7 @@ PyThreadState_Swap(PyThreadState *newts)
535545PyObject *
536546PyThreadState_GetDict (void )
537547{
538- PyThreadState * tstate = (PyThreadState * )_Py_atomic_load_relaxed (
539- & _PyThreadState_Current );
548+ PyThreadState * tstate = PyThreadState_GET ();
540549 if (tstate == NULL )
541550 return NULL ;
542551
@@ -682,7 +691,7 @@ PyThreadState_IsCurrent(PyThreadState *tstate)
682691{
683692 /* Must be the tstate for this thread */
684693 assert (PyGILState_GetThisThreadState ()== tstate );
685- return tstate == ( PyThreadState * ) _Py_atomic_load_relaxed ( & _PyThreadState_Current );
694+ return tstate == PyThreadState_GET ( );
686695}
687696
688697/* Internal initialization/finalization functions called by
@@ -774,9 +783,7 @@ PyGILState_GetThisThreadState(void)
774783int
775784PyGILState_Check (void )
776785{
777- /* can't use PyThreadState_Get() since it will assert that it has the GIL */
778- PyThreadState * tstate = (PyThreadState * )_Py_atomic_load_relaxed (
779- & _PyThreadState_Current );
786+ PyThreadState * tstate = PyThreadState_GET ();
780787 return tstate && (tstate == PyGILState_GetThisThreadState ());
781788}
782789
0 commit comments