@@ -44,11 +44,30 @@ static void _PyThreadState_Delete(PyThreadState *tstate, int check_current);
44
44
45
45
/* the current thread state */
46
46
47
- #define _PyRuntimeGILState_GetThreadState (gilstate ) \
48
- ((PyThreadState*)_Py_atomic_load_relaxed(&(gilstate)->tstate_current))
49
- #define _PyRuntimeGILState_SetThreadState (gilstate , value ) \
50
- _Py_atomic_store_relaxed(&(gilstate)->tstate_current, \
51
- (uintptr_t)(value))
47
+ static inline PyThreadState *
48
+ current_fast_get (_PyRuntimeState * runtime )
49
+ {
50
+ // The GIL must be held by the current thread.
51
+ return (PyThreadState * )_Py_atomic_load_relaxed (
52
+ & runtime -> gilstate .tstate_current );
53
+ }
54
+
55
+ static inline void
56
+ current_fast_set (_PyRuntimeState * runtime , PyThreadState * tstate )
57
+ {
58
+ // The GIL must be held by the current thread.
59
+ assert (tstate != NULL );
60
+ _Py_atomic_store_relaxed (& runtime -> gilstate .tstate_current ,
61
+ (uintptr_t )tstate );
62
+ }
63
+
64
+ static inline void
65
+ current_fast_clear (_PyRuntimeState * runtime )
66
+ {
67
+ // The GIL must be held by the current thread.
68
+ _Py_atomic_store_relaxed (& runtime -> gilstate .tstate_current ,
69
+ (uintptr_t )NULL );
70
+ }
52
71
53
72
54
73
static int
@@ -1145,8 +1164,7 @@ PyThreadState_Clear(PyThreadState *tstate)
1145
1164
1146
1165
/* Common code for PyThreadState_Delete() and PyThreadState_DeleteCurrent() */
1147
1166
static void
1148
- tstate_delete_common (PyThreadState * tstate ,
1149
- struct _gilstate_runtime_state * gilstate )
1167
+ tstate_delete_common (PyThreadState * tstate )
1150
1168
{
1151
1169
_Py_EnsureTstateNotNULL (tstate );
1152
1170
PyInterpreterState * interp = tstate -> interp ;
@@ -1187,13 +1205,12 @@ tstate_delete_common(PyThreadState *tstate,
1187
1205
static void
1188
1206
_PyThreadState_Delete (PyThreadState * tstate , int check_current )
1189
1207
{
1190
- struct _gilstate_runtime_state * gilstate = & tstate -> interp -> runtime -> gilstate ;
1191
1208
if (check_current ) {
1192
- if (tstate == _PyRuntimeGILState_GetThreadState ( gilstate )) {
1209
+ if (tstate == current_fast_get ( tstate -> interp -> runtime )) {
1193
1210
_Py_FatalErrorFormat (__func__ , "tstate %p is still current" , tstate );
1194
1211
}
1195
1212
}
1196
- tstate_delete_common (tstate , gilstate );
1213
+ tstate_delete_common (tstate );
1197
1214
free_threadstate (tstate );
1198
1215
}
1199
1216
@@ -1209,18 +1226,16 @@ void
1209
1226
_PyThreadState_DeleteCurrent (PyThreadState * tstate )
1210
1227
{
1211
1228
_Py_EnsureTstateNotNULL (tstate );
1212
- struct _gilstate_runtime_state * gilstate = & tstate -> interp -> runtime -> gilstate ;
1213
- tstate_delete_common (tstate , gilstate );
1214
- _PyRuntimeGILState_SetThreadState (gilstate , NULL );
1229
+ tstate_delete_common (tstate );
1230
+ current_fast_clear (tstate -> interp -> runtime );
1215
1231
_PyEval_ReleaseLock (tstate );
1216
1232
free_threadstate (tstate );
1217
1233
}
1218
1234
1219
1235
void
1220
1236
PyThreadState_DeleteCurrent (void )
1221
1237
{
1222
- struct _gilstate_runtime_state * gilstate = & _PyRuntime .gilstate ;
1223
- PyThreadState * tstate = _PyRuntimeGILState_GetThreadState (gilstate );
1238
+ PyThreadState * tstate = current_fast_get (& _PyRuntime );
1224
1239
_PyThreadState_DeleteCurrent (tstate );
1225
1240
}
1226
1241
@@ -1286,9 +1301,14 @@ PyThreadState_Get(void)
1286
1301
PyThreadState *
1287
1302
_PyThreadState_Swap (_PyRuntimeState * runtime , PyThreadState * newts )
1288
1303
{
1289
- PyThreadState * oldts = _PyRuntimeGILState_GetThreadState ( & runtime -> gilstate );
1304
+ PyThreadState * oldts = current_fast_get ( runtime );
1290
1305
1291
- _PyRuntimeGILState_SetThreadState (& runtime -> gilstate , newts );
1306
+ if (newts == NULL ) {
1307
+ current_fast_clear (runtime );
1308
+ }
1309
+ else {
1310
+ current_fast_set (runtime , newts );
1311
+ }
1292
1312
/* It should not be possible for more than one thread state
1293
1313
to be used for a thread. Check this the best we can in debug
1294
1314
builds.
@@ -1590,7 +1610,7 @@ PyThreadState_IsCurrent(PyThreadState *tstate)
1590
1610
_PyRuntimeState * runtime = tstate -> interp -> runtime ;
1591
1611
/* Must be the tstate for this thread */
1592
1612
assert (tstate == current_tss_get (runtime ));
1593
- return tstate == _PyRuntimeGILState_GetThreadState ( & runtime -> gilstate );
1613
+ return tstate == current_fast_get ( runtime );
1594
1614
}
1595
1615
1596
1616
/* Internal initialization/finalization functions called by
@@ -1738,7 +1758,7 @@ PyGILState_Check(void)
1738
1758
return 1 ;
1739
1759
}
1740
1760
1741
- PyThreadState * tstate = _PyRuntimeGILState_GetThreadState ( & runtime -> gilstate );
1761
+ PyThreadState * tstate = current_fast_get ( runtime );
1742
1762
if (tstate == NULL ) {
1743
1763
return 0 ;
1744
1764
}
@@ -1830,7 +1850,7 @@ PyGILState_Release(PyGILState_STATE oldstate)
1830
1850
* races; see bugs 225673 and 1061968 (that nasty bug has a
1831
1851
* habit of coming back).
1832
1852
*/
1833
- assert (_PyRuntimeGILState_GetThreadState ( & runtime -> gilstate ) == tstate );
1853
+ assert (current_fast_get ( runtime ) == tstate );
1834
1854
_PyThreadState_DeleteCurrent (tstate );
1835
1855
}
1836
1856
/* Release the lock if necessary */
@@ -2017,7 +2037,7 @@ _call_in_interpreter(PyInterpreterState *interp, releasefunc func, void *arg)
2017
2037
*/
2018
2038
_PyRuntimeState * runtime = interp -> runtime ;
2019
2039
PyThreadState * save_tstate = NULL ;
2020
- if (interp != _PyRuntimeGILState_GetThreadState ( & runtime -> gilstate )-> interp ) {
2040
+ if (interp != current_fast_get ( runtime )-> interp ) {
2021
2041
// XXX Using the "head" thread isn't strictly correct.
2022
2042
PyThreadState * tstate = PyInterpreterState_ThreadHead (interp );
2023
2043
// XXX Possible GILState issues?
0 commit comments