@@ -1276,10 +1276,6 @@ PyThreadState_Clear(PyThreadState *tstate)
1276
1276
* for the main interpreter, current_fast_get() must be the main thread
1277
1277
*/
1278
1278
1279
- // The GIL must be held by the current thread,
1280
- // which must not be the target.
1281
- // XXX Enforce that (check current_fast_get()).
1282
-
1283
1279
int verbose = _PyInterpreterState_GetConfig (tstate -> interp )-> verbose ;
1284
1280
1285
1281
if (verbose && tstate -> cframe -> current_frame != NULL ) {
@@ -1536,6 +1532,26 @@ PyThreadState_GetID(PyThreadState *tstate)
1536
1532
}
1537
1533
1538
1534
1535
+ static inline void
1536
+ tstate_activate (PyThreadState * tstate )
1537
+ {
1538
+ assert (tstate != NULL );
1539
+ assert (tstate_is_alive (tstate ) && tstate_is_bound (tstate ));
1540
+ assert (!tstate -> _status .active );
1541
+ tstate -> _status .active = 1 ;
1542
+ }
1543
+
1544
+ static inline void
1545
+ tstate_deactivate (PyThreadState * tstate )
1546
+ {
1547
+ assert (tstate != NULL );
1548
+ assert (tstate_is_bound (tstate ));
1549
+ // XXX assert(tstate_is_alive(tstate) && tstate_is_bound(tstate));
1550
+ assert (tstate -> _status .active );
1551
+ tstate -> _status .active = 0 ;
1552
+ }
1553
+
1554
+
1539
1555
//----------
1540
1556
// other API
1541
1557
//----------
@@ -1612,15 +1628,19 @@ PyThreadState *
1612
1628
_PyThreadState_Swap (_PyRuntimeState * runtime , PyThreadState * newts )
1613
1629
{
1614
1630
PyThreadState * oldts = current_fast_get (runtime );
1615
- // XXX assert(oldts == NULL || tstate_is_alive(oldts));
1616
1631
// XXX tstate_is_bound(oldts)
1632
+ if (oldts != NULL ) {
1633
+ // XXX assert(oldts == NULL || tstate_is_alive(oldts));
1634
+ tstate_deactivate (oldts );
1635
+ }
1617
1636
1618
1637
if (newts == NULL ) {
1619
1638
current_fast_clear (runtime );
1620
1639
}
1621
1640
else {
1622
1641
assert (tstate_is_alive (newts ) && tstate_is_bound (newts ));
1623
1642
current_fast_set (runtime , newts );
1643
+ tstate_activate (newts );
1624
1644
}
1625
1645
1626
1646
/* It should not be possible for more than one thread state
0 commit comments