Skip to content

py/schedule: Convert micropythyon.schedule() to circular buffer. #4617

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion py/mpstate.h
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,8 @@ typedef struct _mp_state_vm_t {

#if MICROPY_ENABLE_SCHEDULER
volatile int16_t sched_state;
uint16_t sched_sp;
uint8_t sched_len;
uint8_t sched_idx;
#endif

#if MICROPY_PY_THREAD_GIL
Expand Down
3 changes: 2 additions & 1 deletion py/runtime.c
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@ void mp_init(void) {
MP_STATE_VM(mp_pending_exception) = MP_OBJ_NULL;
#if MICROPY_ENABLE_SCHEDULER
MP_STATE_VM(sched_state) = MP_SCHED_IDLE;
MP_STATE_VM(sched_sp) = 0;
MP_STATE_VM(sched_idx) = 0;
MP_STATE_VM(sched_len) = 0;
#endif

#if MICROPY_ENABLE_EMERGENCY_EXCEPTION_BUF
Expand Down
2 changes: 1 addition & 1 deletion py/runtime.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ void mp_handle_pending_tail(mp_uint_t atomic_state);
#if MICROPY_ENABLE_SCHEDULER
void mp_sched_lock(void);
void mp_sched_unlock(void);
static inline unsigned int mp_sched_num_pending(void) { return MP_STATE_VM(sched_sp); }
static inline unsigned int mp_sched_num_pending(void) { return MP_STATE_VM(sched_len); }
bool mp_sched_schedule(mp_obj_t function, mp_obj_t arg);
#endif

Expand Down
26 changes: 20 additions & 6 deletions py/scheduler.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,18 @@

#if MICROPY_ENABLE_SCHEDULER

#define IDX_MASK(i) ((i) & (MICROPY_SCHEDULER_DEPTH - 1))

static inline bool mp_sched_full(void) {
MP_STATIC_ASSERT((IDX_MASK(MICROPY_SCHEDULER_DEPTH) == 0)); // MICROPY_SCHEDULER_DEPTH must be a power of 2

return mp_sched_num_pending() == MICROPY_SCHEDULER_DEPTH;
}

static inline bool mp_sched_empty(void) {
return mp_sched_num_pending() == 0;
}

// A variant of this is inlined in the VM at the pending exception check
void mp_handle_pending(void) {
if (MP_STATE_VM(sched_state) == MP_SCHED_PENDING) {
Expand All @@ -51,8 +63,10 @@ void mp_handle_pending(void) {
// or by the VM's inlined version of that function.
void mp_handle_pending_tail(mp_uint_t atomic_state) {
MP_STATE_VM(sched_state) = MP_SCHED_LOCKED;
if (MP_STATE_VM(sched_sp) > 0) {
mp_sched_item_t item = MP_STATE_VM(sched_stack)[--MP_STATE_VM(sched_sp)];
if (!mp_sched_empty()) {
mp_sched_item_t item = MP_STATE_VM(sched_stack)[MP_STATE_VM(sched_idx)];
MP_STATE_VM(sched_idx) = IDX_MASK(MP_STATE_VM(sched_idx)+1);
MP_STATE_VM(sched_len)--;
MICROPY_END_ATOMIC_SECTION(atomic_state);
mp_call_function_1_protected(item.func, item.arg);
} else {
Expand Down Expand Up @@ -87,13 +101,13 @@ void mp_sched_unlock(void) {
bool mp_sched_schedule(mp_obj_t function, mp_obj_t arg) {
mp_uint_t atomic_state = MICROPY_BEGIN_ATOMIC_SECTION();
bool ret;
if (MP_STATE_VM(sched_sp) < MICROPY_SCHEDULER_DEPTH) {
if (!mp_sched_full()) {
if (MP_STATE_VM(sched_state) == MP_SCHED_IDLE) {
MP_STATE_VM(sched_state) = MP_SCHED_PENDING;
}
MP_STATE_VM(sched_stack)[MP_STATE_VM(sched_sp)].func = function;
MP_STATE_VM(sched_stack)[MP_STATE_VM(sched_sp)].arg = arg;
++MP_STATE_VM(sched_sp);
uint8_t iput = IDX_MASK(MP_STATE_VM(sched_idx) + MP_STATE_VM(sched_len)++);
MP_STATE_VM(sched_stack)[iput].func = function;
MP_STATE_VM(sched_stack)[iput].arg = arg;
ret = true;
} else {
// schedule stack is full
Expand Down
6 changes: 3 additions & 3 deletions tests/unix/extra_coverage.py.exp
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,10 @@ sched(2)=1
sched(3)=1
sched(4)=0
unlocked
3
2
1
0
1
2
3
0123456789 b'0123456789'
7300
7300
Expand Down
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy