Skip to content
This repository was archived by the owner on Feb 13, 2025. It is now read-only.

Commit 71d8e0e

Browse files
author
Anselm Kruis
committed
Stackless issue #239: simplify context switching
Simplify context switching as sugested by Kristján.
1 parent d524887 commit 71d8e0e

File tree

2 files changed

+15
-20
lines changed

2 files changed

+15
-20
lines changed

Include/slp_structs.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,8 +134,7 @@ typedef struct _tasklet {
134134
int recursion_depth;
135135
PyObject *def_globals;
136136
PyObject *tsk_weakreflist;
137-
/* If the tasklet is current: the context, the value of ts->context when the main tasklet was created.
138-
* (The context of a current tasklet is always ints->tasklet.)
137+
/* If the tasklet is current: NULL. (The context of a current tasklet is always in ts->tasklet.)
139138
* If the tasklet is not current: the context for the tasklet */
140139
PyObject *context;
141140
} PyTaskletObject;

Stackless/module/scheduling.c

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -722,42 +722,32 @@ Py_LOCAL_INLINE(void) SLP_EXCHANGE_EXCINFO(PyThreadState *tstate, PyTaskletObjec
722722
PyThreadState *ts_ = (tstate);
723723
PyTaskletObject *t_ = (task);
724724
_PyErr_StackItem *exc_info;
725-
PyObject *c;
726725
assert(ts_);
727726
assert(t_);
728727
exc_info = ts_->exc_info;
729728
assert(exc_info);
730729
assert(t_->exc_info);
731730
#if 0
732-
c = PyStackless_GetCurrent();
731+
PyObject *c = PyStackless_GetCurrent();
733732
fprintf(stderr, "SLP_EXCHANGE_EXCINFO %3d current %14p,\tset task %p = %p,\ttstate %p = %p\n", __LINE__, c, t_, exc_info, ts_, t_->exc_info);
734733
Py_XDECREF(c);
735734
#endif
736735
ts_->exc_info = t_->exc_info;
737736
t_->exc_info = exc_info;
738-
c = ts_->context;
739-
ts_->context = t_->context;
740-
t_->context = c;
741-
ts_->context_ver++;
742737
}
743738
#else
744739
#define SLP_EXCHANGE_EXCINFO(tstate_, task_) \
745740
do { \
746741
PyThreadState *ts_ = (tstate_); \
747742
PyTaskletObject *t_ = (task_); \
748743
_PyErr_StackItem *exc_info; \
749-
PyObject *c; \
750744
assert(ts_); \
751745
assert(t_); \
752746
exc_info = ts_->exc_info; \
753747
assert(exc_info); \
754748
assert(t_->exc_info); \
755749
ts_->exc_info = t_->exc_info; \
756750
t_->exc_info = exc_info; \
757-
c = ts_->context; \
758-
ts_->context = t_->context; \
759-
t_->context = c; \
760-
ts_->context_ver++; \
761751
} while(0)
762752
#endif
763753

@@ -766,13 +756,23 @@ Py_LOCAL_INLINE(void) SLP_UPDATE_TSTATE_ON_SWITCH(PyThreadState *tstate, PyTaskl
766756
{
767757
SLP_EXCHANGE_EXCINFO(tstate, prev);
768758
SLP_EXCHANGE_EXCINFO(tstate, next);
759+
prev->context = tstate->context;
760+
tstate->context = next->context;
761+
tstate->context_ver++;
762+
next->context = NULL;
769763
}
770764
#else
771765
#define SLP_UPDATE_TSTATE_ON_SWITCH(tstate__, prev_, next_) \
772766
do { \
773767
PyThreadState *ts__ = (tstate__); \
774-
SLP_EXCHANGE_EXCINFO(ts__, (prev_)); \
775-
SLP_EXCHANGE_EXCINFO(ts__, (next_)); \
768+
PyTaskletObject *prev__ = (prev_); \
769+
PyTaskletObject *next__ = (next_); \
770+
SLP_EXCHANGE_EXCINFO(ts__, prev__); \
771+
SLP_EXCHANGE_EXCINFO(ts__, next__); \
772+
prev__->context = ts__->context; \
773+
ts__->context = next__->context; \
774+
ts__->context_ver++; \
775+
next__->context = NULL; \
776776
} while(0)
777777
#endif
778778

@@ -1299,8 +1299,6 @@ slp_initialize_main_and_current(void)
12991299
assert(task->exc_state.previous_item == NULL);
13001300
assert(task->exc_info == &task->exc_state);
13011301
assert(task->context == NULL);
1302-
Py_XINCREF(ts->context);
1303-
task->context = ts->context;
13041302
SLP_EXCHANGE_EXCINFO(ts, task);
13051303

13061304
NOTIFY_SCHEDULE(ts, NULL, task, -1);
@@ -1394,11 +1392,9 @@ schedule_task_destruct(PyObject **retval, PyTaskletObject *prev, PyTaskletObject
13941392
/* main is exiting */
13951393
assert(ts->st.main == NULL);
13961394
assert(ts->exc_info == &prev->exc_state);
1395+
assert(prev->context == NULL);
13971396
SLP_EXCHANGE_EXCINFO(ts, prev);
13981397
TASKLET_CLAIMVAL(prev, retval);
1399-
Py_XINCREF(prev->context);
1400-
Py_XSETREF(ts->context, prev->context);
1401-
ts->context_ver++;
14021398
if (PyBomb_Check(*retval))
14031399
*retval = slp_bomb_explode(*retval);
14041400
}

0 commit comments

Comments
 (0)