Kevin O'Connor ea546c789b sched: Improve timer vs task priority check
Rename sched_tasks_busy() to sched_check_set_tasks_busy() and change
it to only return true if tasks are active (running or requested) for
two consecutive calls.  This makes it less likely that timers will
yield to tasks except when tasks really are notably backlogged.

This also makes it less likely that multiple steppers controlling the
same rail will be interrupted by tasks mid-step.  This should slightly
improve the timing, and make it less likely that a halt during
homing/probing will occur with these steppers taking a different
number of total steps.

Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
2024-10-26 22:09:14 -04:00

49 lines
1.5 KiB
C

#ifndef __SCHED_H
#define __SCHED_H
#include <stdint.h> // uint32_t
#include "ctr.h" // DECL_CTR
// Declare an init function (called at firmware startup)
#define DECL_INIT(FUNC) _DECL_CALLLIST(ctr_run_initfuncs, FUNC)
// Declare a task function (called periodically during normal runtime)
#define DECL_TASK(FUNC) _DECL_CALLLIST(ctr_run_taskfuncs, FUNC)
// Declare a shutdown function (called on an emergency stop)
#define DECL_SHUTDOWN(FUNC) _DECL_CALLLIST(ctr_run_shutdownfuncs, FUNC)
// Timer structure for scheduling timed events (see sched_add_timer() )
struct timer {
struct timer *next;
uint_fast8_t (*func)(struct timer*);
uint32_t waketime;
};
enum { SF_DONE=0, SF_RESCHEDULE=1 };
// Task waking struct
struct task_wake {
uint8_t wake;
};
// sched.c
void sched_add_timer(struct timer*);
void sched_del_timer(struct timer *del);
unsigned int sched_timer_dispatch(void);
void sched_timer_reset(void);
void sched_wake_tasks(void);
uint8_t sched_check_set_tasks_busy(void);
void sched_wake_task(struct task_wake *w);
uint8_t sched_check_wake(struct task_wake *w);
uint8_t sched_is_shutdown(void);
void sched_clear_shutdown(void);
void sched_try_shutdown(uint_fast8_t reason);
void sched_shutdown(uint_fast8_t reason) __noreturn;
void sched_report_shutdown(void);
void sched_main(void);
// Compiler glue for DECL_X macros above.
#define _DECL_CALLLIST(NAME, FUNC) \
DECL_CTR("_DECL_CALLLIST " __stringify(NAME) " " __stringify(FUNC))
#endif // sched.h