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

Commit 413fabd

Browse files
Anselm KruisAnselm Kruis
Anselm Kruis
authored and
Anselm Kruis
committed
Stackless issue #139: refactor PyStacklessState and PyStacklessInterpreterState
Resize flags and reorder them to avoid padding. This is a preparation for adding another flag field for pickle related flags.
1 parent 0ee1366 commit 413fabd

File tree

4 files changed

+15
-12
lines changed

4 files changed

+15
-12
lines changed

Stackless/core/stackless_tstate.h

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ typedef struct {
2727
PyObject * schedule_hook; /* the schedule callback function */
2828
slp_schedule_hook_func * schedule_fasthook; /* the fast C-only schedule_hook */
2929
PyThreadState * initial_tstate; /* recording the main thread state */
30-
int enable_softswitch; /* the flag which decides whether we try to use soft switching */
30+
uint8_t enable_softswitch; /* the flag which decides whether we try to use soft switching */
3131
} PyStacklessInterpreterState;
3232

3333
#define SPL_INTERPRETERSTATE_NEW(interp) \
@@ -94,12 +94,10 @@ typedef struct _sts {
9494
PyObject *unwinding_retval; /* The return value during stack unwinding */
9595
Py_ssize_t frame_refcnt; /* The number of owned references to frames */
9696
int runcount;
97-
/* trap recursive scheduling via callbacks */
98-
int schedlock;
99-
int runflags; /* flags for stackless.run() behaviour */
100-
/* number of nested interpreters (1.0/2.0 merge) */
101-
int nesting_level;
97+
int nesting_level; /* number of nested interpreters */
10298
int switch_trap; /* if non-zero, switching is forbidden */
99+
uint8_t schedlock; /* trap recursive scheduling via callbacks */
100+
uint8_t runflags; /* flags for stackless.run() behaviour */
103101
#ifdef SLP_WITH_FRAME_REF_DEBUG
104102
struct _frame *next_frame; /* a ref counted copy of PyThreadState.frame */
105103
#endif
@@ -108,7 +106,7 @@ typedef struct _sts {
108106
#ifdef Py_BUILD_CORE
109107

110108
/* internal macro to temporarily disable soft interrupts */
111-
#define PY_WATCHDOG_NO_SOFT_IRQ (1<<31)
109+
#define PY_WATCHDOG_NO_SOFT_IRQ (1U<<7)
112110

113111
/* these macros go into pystate.c */
114112
#ifdef SLP_WITH_FRAME_REF_DEBUG
@@ -137,10 +135,10 @@ typedef struct _sts {
137135
tstate->st.unwinding_retval = NULL; \
138136
tstate->st.frame_refcnt = 0; \
139137
tstate->st.runcount = 0; \
140-
tstate->st.schedlock = 0; \
141138
tstate->st.nesting_level = 0; \
142-
tstate->st.runflags = 0; \
143139
tstate->st.switch_trap = 0; \
140+
tstate->st.schedlock = 0; \
141+
tstate->st.runflags = 0; \
144142
__STACKLESS_PYSTATE_NEW_NEXT_FRAME
145143

146144

Stackless/module/channelobject.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -500,7 +500,7 @@ generic_channel_cando(PyThreadState *ts, PyObject **result, PyChannelObject *sel
500500
PyTaskletObject *source = ts->st.current;
501501
PyTaskletObject *switchto, *target, *next;
502502
int interthread;
503-
int oldflags, runflags = 0;
503+
uint8_t oldflags, runflags = 0;
504504
int switched, fail;
505505

506506
/* swap data and perform necessary scheduling */

Stackless/module/scheduling.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1032,7 +1032,7 @@ slp_schedule_task_prepared(PyThreadState *ts, PyObject **result, PyTaskletObject
10321032
int transfer_result;
10331033

10341034
/* remove the no-soft-irq flag from the runflags */
1035-
int no_soft_irq = ts->st.runflags & PY_WATCHDOG_NO_SOFT_IRQ;
1035+
uint8_t no_soft_irq = ts->st.runflags & PY_WATCHDOG_NO_SOFT_IRQ;
10361036
ts->st.runflags &= ~PY_WATCHDOG_NO_SOFT_IRQ;
10371037

10381038
slp_schedule_soft_irq(ts, prev, &next, no_soft_irq);

Stackless/module/stacklessmodule.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -372,7 +372,7 @@ enable_softswitch(PyObject *self, PyObject *flag)
372372
if (newflag == -1 && PyErr_Occurred())
373373
return NULL;
374374
ret = PyBool_FromLong(ts->interp->st.enable_softswitch);
375-
ts->interp->st.enable_softswitch = newflag;
375+
ts->interp->st.enable_softswitch = !!newflag;
376376
return ret;
377377
}
378378

@@ -484,6 +484,11 @@ PyStackless_RunWatchdogEx(long timeout, int flags)
484484
long old_watermark = 0, old_interval = 0;
485485
int interrupt;
486486

487+
if (flags < 0 || flags >= (1U << (sizeof(ts->st.runflags) * CHAR_BIT))) {
488+
PyErr_SetString(PyExc_ValueError, "flags must be in [0..255]");
489+
return NULL;
490+
}
491+
487492
if (ts->st.main == NULL)
488493
return PyStackless_RunWatchdog_M(timeout, flags);
489494

0 commit comments

Comments
 (0)