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
6+ #define GET_TSTATE () \
7+ ((PyThreadState*)_Py_atomic_load_relaxed(&_PyThreadState_Current))
8+ #define SET_TSTATE (value ) \
9+ _Py_atomic_store_relaxed(&_PyThreadState_Current, (Py_uintptr_t)(value))
10+ #define GET_INTERP_STATE () \
11+ (GET_TSTATE()->interp)
12+
1113
1214/* --------------------------------------------------------------------------
1315CAUTION
@@ -54,7 +56,7 @@ static PyInterpreterState *interp_head = NULL;
5456
5557/* Assuming the current thread holds the GIL, this is the
5658 PyThreadState for the current thread. */
57- _Py_atomic_address _PyThreadState_Current = {NULL };
59+ _Py_atomic_address _PyThreadState_Current = {0 };
5860PyThreadFrameGetter _PyThreadState_GetFrame = NULL ;
5961
6062#ifdef WITH_THREAD
@@ -260,7 +262,7 @@ PyObject*
260262PyState_FindModule (struct PyModuleDef * module )
261263{
262264 Py_ssize_t index = module -> m_base .m_index ;
263- PyInterpreterState * state = PyThreadState_GET () -> interp ;
265+ PyInterpreterState * state = GET_INTERP_STATE () ;
264266 PyObject * res ;
265267 if (module -> m_slots ) {
266268 return NULL ;
@@ -284,7 +286,7 @@ _PyState_AddModule(PyObject* module, struct PyModuleDef* def)
284286 "PyState_AddModule called on module with slots" );
285287 return -1 ;
286288 }
287- state = PyThreadState_GET () -> interp ;
289+ state = GET_INTERP_STATE () ;
288290 if (!def )
289291 return -1 ;
290292 if (!state -> modules_by_index ) {
304306PyState_AddModule (PyObject * module , struct PyModuleDef * def )
305307{
306308 Py_ssize_t index ;
307- PyInterpreterState * state = PyThreadState_GET () -> interp ;
309+ PyInterpreterState * state = GET_INTERP_STATE () ;
308310 if (!def ) {
309311 Py_FatalError ("PyState_AddModule: Module Definition is NULL" );
310312 return -1 ;
@@ -331,7 +333,7 @@ PyState_RemoveModule(struct PyModuleDef* def)
331333 "PyState_RemoveModule called on module with slots" );
332334 return -1 ;
333335 }
334- state = PyThreadState_GET () -> interp ;
336+ state = GET_INTERP_STATE () ;
335337 if (index == 0 ) {
336338 Py_FatalError ("PyState_RemoveModule: Module index invalid." );
337339 return -1 ;
@@ -351,7 +353,7 @@ PyState_RemoveModule(struct PyModuleDef* def)
351353void
352354_PyState_ClearModules (void )
353355{
354- PyInterpreterState * state = PyThreadState_GET () -> interp ;
356+ PyInterpreterState * state = GET_INTERP_STATE () ;
355357 if (state -> modules_by_index ) {
356358 Py_ssize_t i ;
357359 for (i = 0 ; i < PyList_GET_SIZE (state -> modules_by_index ); i ++ ) {
@@ -429,7 +431,7 @@ tstate_delete_common(PyThreadState *tstate)
429431void
430432PyThreadState_Delete (PyThreadState * tstate )
431433{
432- if (tstate == PyThreadState_GET ())
434+ if (tstate == GET_TSTATE ())
433435 Py_FatalError ("PyThreadState_Delete: tstate is still current" );
434436#ifdef WITH_THREAD
435437 if (autoInterpreterState && PyThread_get_key_value (autoTLSkey ) == tstate )
@@ -443,11 +445,11 @@ PyThreadState_Delete(PyThreadState *tstate)
443445void
444446PyThreadState_DeleteCurrent ()
445447{
446- PyThreadState * tstate = PyThreadState_GET ();
448+ PyThreadState * tstate = GET_TSTATE ();
447449 if (tstate == NULL )
448450 Py_FatalError (
449451 "PyThreadState_DeleteCurrent: no current tstate" );
450- _Py_atomic_store_relaxed ( & _PyThreadState_Current , NULL );
452+ SET_TSTATE ( NULL );
451453 if (autoInterpreterState && PyThread_get_key_value (autoTLSkey ) == tstate )
452454 PyThread_delete_key_value (autoTLSkey );
453455 tstate_delete_common (tstate );
@@ -496,14 +498,14 @@ _PyThreadState_DeleteExcept(PyThreadState *tstate)
496498PyThreadState *
497499_PyThreadState_UncheckedGet (void )
498500{
499- return PyThreadState_GET ();
501+ return GET_TSTATE ();
500502}
501503
502504
503505PyThreadState *
504506PyThreadState_Get (void )
505507{
506- PyThreadState * tstate = PyThreadState_GET ();
508+ PyThreadState * tstate = GET_TSTATE ();
507509 if (tstate == NULL )
508510 Py_FatalError ("PyThreadState_Get: no current thread" );
509511
@@ -514,9 +516,9 @@ PyThreadState_Get(void)
514516PyThreadState *
515517PyThreadState_Swap (PyThreadState * newts )
516518{
517- PyThreadState * oldts = PyThreadState_GET ();
519+ PyThreadState * oldts = GET_TSTATE ();
518520
519- _Py_atomic_store_relaxed ( & _PyThreadState_Current , newts );
521+ SET_TSTATE ( newts );
520522 /* It should not be possible for more than one thread state
521523 to be used for a thread. Check this the best we can in debug
522524 builds.
@@ -545,7 +547,7 @@ PyThreadState_Swap(PyThreadState *newts)
545547PyObject *
546548PyThreadState_GetDict (void )
547549{
548- PyThreadState * tstate = PyThreadState_GET ();
550+ PyThreadState * tstate = GET_TSTATE ();
549551 if (tstate == NULL )
550552 return NULL ;
551553
@@ -569,8 +571,7 @@ PyThreadState_GetDict(void)
569571
570572int
571573PyThreadState_SetAsyncExc (long id , PyObject * exc ) {
572- PyThreadState * tstate = PyThreadState_GET ();
573- PyInterpreterState * interp = tstate -> interp ;
574+ PyInterpreterState * interp = GET_INTERP_STATE ();
574575 PyThreadState * p ;
575576
576577 /* Although the GIL is held, a few C API functions can be called
@@ -691,7 +692,7 @@ PyThreadState_IsCurrent(PyThreadState *tstate)
691692{
692693 /* Must be the tstate for this thread */
693694 assert (PyGILState_GetThisThreadState ()== tstate );
694- return tstate == PyThreadState_GET ();
695+ return tstate == GET_TSTATE ();
695696}
696697
697698/* Internal initialization/finalization functions called by
@@ -783,7 +784,7 @@ PyGILState_GetThisThreadState(void)
783784int
784785PyGILState_Check (void )
785786{
786- PyThreadState * tstate = PyThreadState_GET ();
787+ PyThreadState * tstate = GET_TSTATE ();
787788 return tstate && (tstate == PyGILState_GetThisThreadState ());
788789}
789790
0 commit comments