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

Commit a11414b

Browse files
author
Anselm Kruis
committed
Stackless issue #239: refactor SLP_EXCHANGE_EXCINFO to become a function
Use a Py_LOCAL_INLINE function instead of a complicated macro. It is much simpler to debug. In the next commit, we will context switching to this function.
1 parent e61fde5 commit a11414b

File tree

1 file changed

+38
-23
lines changed

1 file changed

+38
-23
lines changed

Stackless/module/scheduling.c

Lines changed: 38 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -712,36 +712,54 @@ new_lock(void)
712712
* the exc_info-pointer in the thread state.
713713
*/
714714

715+
#if 1
716+
Py_LOCAL_INLINE(void) SLP_EXCHANGE_EXCINFO(PyThreadState *tstate, PyTaskletObject *task)
717+
{
718+
PyThreadState *ts_ = (tstate);
719+
PyTaskletObject *t_ = (task);
720+
_PyErr_StackItem *exc_info;
721+
assert(ts_);
722+
assert(t_);
723+
exc_info = ts_->exc_info;
724+
assert(exc_info);
725+
assert(t_->exc_info);
715726
#if 0
716-
#define SLP_EXCHANGE_EXCINFO(tstate, task) \
727+
PyObject *c;
728+
c = PyStackless_GetCurrent();
729+
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);
730+
Py_XDECREF(c);
731+
#endif
732+
ts_->exc_info = t_->exc_info;
733+
t_->exc_info = exc_info;
734+
}
735+
#else
736+
#define SLP_EXCHANGE_EXCINFO(tstate_, task_) \
717737
do { \
718-
PyThreadState *ts_ = (tstate); \
719-
PyTaskletObject *t_ = (task); \
738+
PyThreadState *ts_ = (tstate_); \
739+
PyTaskletObject *t_ = (task_); \
720740
_PyErr_StackItem *exc_info; \
721-
PyObject * c = PyStackless_GetCurrent(); \
722741
assert(ts_); \
723742
assert(t_); \
724743
exc_info = ts_->exc_info; \
725744
assert(exc_info); \
726745
assert(t_->exc_info); \
727-
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); \
728-
Py_XDECREF(c); \
729746
ts_->exc_info = t_->exc_info; \
730747
t_->exc_info = exc_info; \
731748
} while(0)
749+
#endif
750+
751+
#if 1
752+
Py_LOCAL_INLINE(void) SLP_UPDATE_TSTATE_ON_SWITCH(PyThreadState *tstate, PyTaskletObject *prev, PyTaskletObject *next)
753+
{
754+
SLP_EXCHANGE_EXCINFO(tstate, prev);
755+
SLP_EXCHANGE_EXCINFO(tstate, next);
756+
}
732757
#else
733-
#define SLP_EXCHANGE_EXCINFO(tstate, task) \
758+
#define SLP_UPDATE_TSTATE_ON_SWITCH(tstate__, prev_, next_) \
734759
do { \
735-
PyThreadState *ts_ = (tstate); \
736-
PyTaskletObject *t_ = (task); \
737-
_PyErr_StackItem *exc_info; \
738-
assert(ts_); \
739-
assert(t_); \
740-
exc_info = ts_->exc_info; \
741-
assert(exc_info); \
742-
assert(t_->exc_info); \
743-
ts_->exc_info = t_->exc_info; \
744-
t_->exc_info = exc_info; \
760+
PyThreadState *ts__ = (tstate__); \
761+
SLP_EXCHANGE_EXCINFO(ts__, (prev_)); \
762+
SLP_EXCHANGE_EXCINFO(ts__, (next_)); \
745763
} while(0)
746764
#endif
747765

@@ -1147,10 +1165,9 @@ slp_schedule_task_prepared(PyThreadState *ts, PyObject **result, PyTaskletObject
11471165
retval = slp_bomb_explode(retval);
11481166
}
11491167
/* no failure possible from here on */
1150-
SLP_EXCHANGE_EXCINFO(ts, prev);
1168+
SLP_UPDATE_TSTATE_ON_SWITCH(ts, prev, next);
11511169
ts->recursion_depth = next->recursion_depth;
11521170
ts->st.current = next;
1153-
SLP_EXCHANGE_EXCINFO(ts, next);
11541171
if (did_switch)
11551172
*did_switch = 1;
11561173
*result = STACKLESS_PACK(ts, retval);
@@ -1175,8 +1192,7 @@ slp_schedule_task_prepared(PyThreadState *ts, PyObject **result, PyTaskletObject
11751192
else
11761193
transfer = slp_transfer;
11771194

1178-
SLP_EXCHANGE_EXCINFO(ts, prev);
1179-
SLP_EXCHANGE_EXCINFO(ts, next);
1195+
SLP_UPDATE_TSTATE_ON_SWITCH(ts, prev, next);
11801196

11811197
transfer_result = transfer(cstprev, next->cstate, prev);
11821198
/* Note: If the transfer was successful from here on "prev" holds the
@@ -1224,8 +1240,7 @@ slp_schedule_task_prepared(PyThreadState *ts, PyObject **result, PyTaskletObject
12241240
}
12251241
else {
12261242
/* Failed transfer. */
1227-
SLP_EXCHANGE_EXCINFO(ts, next);
1228-
SLP_EXCHANGE_EXCINFO(ts, prev);
1243+
SLP_UPDATE_TSTATE_ON_SWITCH(ts, next, prev);
12291244
PyFrameObject *f = SLP_CLAIM_NEXT_FRAME(ts);
12301245
Py_XSETREF(next->f.frame, f); /* revert the Py_CLEAR(next->f.frame) */
12311246
kill_wrap_bad_guy(prev, next);

0 commit comments

Comments
 (0)