aboutsummaryrefslogtreecommitdiffstats
path: root/src/sched.c
diff options
context:
space:
mode:
authorKevin O'Connor <kevin@koconnor.net>2021-10-08 11:59:56 -0400
committerKevin O'Connor <kevin@koconnor.net>2021-10-27 12:18:28 -0400
commitc015c0708fae671ee2f5889da99281386afcd375 (patch)
tree3a6325b9cffdc0a02534515d2b174e3308750ce4 /src/sched.c
parent86fee2d517861546c7ba78cc1b59b83c2738ace4 (diff)
downloadkutter-c015c0708fae671ee2f5889da99281386afcd375.tar.gz
kutter-c015c0708fae671ee2f5889da99281386afcd375.tar.xz
kutter-c015c0708fae671ee2f5889da99281386afcd375.zip
sched: Place common global variables into a struct
Some architectures do better if the global variables accessed from the timer dispatch code are in a struct. Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
Diffstat (limited to 'src/sched.c')
-rw-r--r--src/sched.c76
1 files changed, 39 insertions, 37 deletions
diff --git a/src/sched.c b/src/sched.c
index cede4755..0167303b 100644
--- a/src/sched.c
+++ b/src/sched.c
@@ -1,6 +1,6 @@
// Basic scheduling functions and startup/shutdown code.
//
-// Copyright (C) 2016,2017 Kevin O'Connor <kevin@koconnor.net>
+// Copyright (C) 2016-2021 Kevin O'Connor <kevin@koconnor.net>
//
// This file may be distributed under the terms of the GNU GPLv3 license.
@@ -15,14 +15,19 @@
#include "sched.h" // sched_check_periodic
#include "stepper.h" // stepper_event
+static struct timer periodic_timer, sentinel_timer, deleted_timer;
+
+static struct {
+ struct timer *timer_list;
+ int8_t tasks_status;
+ uint8_t shutdown_status, shutdown_reason;
+} SchedStatus = { .timer_list = &periodic_timer };
+
/****************************************************************
* Timers
****************************************************************/
-static struct timer periodic_timer, *timer_list = &periodic_timer;
-static struct timer sentinel_timer, deleted_timer;
-
// The periodic_timer simplifies the timer code by ensuring there is
// always a timer on the timer list and that there is always a timer
// not far in the future.
@@ -62,7 +67,7 @@ static struct timer sentinel_timer = {
static void __always_inline
insert_timer(struct timer *t, uint32_t waketime)
{
- struct timer *prev, *pos = timer_list;
+ struct timer *prev, *pos = SchedStatus.timer_list;
for (;;) {
prev = pos;
if (CONFIG_MACH_AVR)
@@ -82,9 +87,9 @@ sched_add_timer(struct timer *add)
{
uint32_t waketime = add->waketime;
irqstatus_t flag = irq_save();
- if (unlikely(timer_is_before(waketime, timer_list->waketime))) {
+ if (unlikely(timer_is_before(waketime, SchedStatus.timer_list->waketime))) {
// This timer is before all other scheduled timers
- struct timer *tl = timer_list;
+ struct timer *tl = SchedStatus.timer_list;
if (timer_is_before(waketime, timer_read_time()))
try_shutdown("Timer too close");
if (tl == &deleted_timer)
@@ -93,7 +98,7 @@ sched_add_timer(struct timer *add)
add->next = tl;
deleted_timer.waketime = waketime;
deleted_timer.next = add;
- timer_list = &deleted_timer;
+ SchedStatus.timer_list = &deleted_timer;
timer_kick();
} else {
insert_timer(add, waketime);
@@ -117,15 +122,15 @@ void
sched_del_timer(struct timer *del)
{
irqstatus_t flag = irq_save();
- if (timer_list == del) {
+ if (SchedStatus.timer_list == del) {
// Deleting the next active timer - replace with deleted_timer
deleted_timer.waketime = del->waketime;
deleted_timer.next = del->next;
- timer_list = &deleted_timer;
+ SchedStatus.timer_list = &deleted_timer;
} else {
// Find and remove from timer list (if present)
struct timer *pos;
- for (pos = timer_list; pos->next; pos = pos->next) {
+ for (pos = SchedStatus.timer_list; pos->next; pos = pos->next) {
if (pos->next == del) {
pos->next = del->next;
break;
@@ -140,7 +145,7 @@ unsigned int
sched_timer_dispatch(void)
{
// Invoke timer callback
- struct timer *t = timer_list;
+ struct timer *t = SchedStatus.timer_list;
uint_fast8_t res;
uint32_t updated_waketime;
if (CONFIG_INLINE_STEPPER_HACK && likely(!t->func)) {
@@ -155,10 +160,10 @@ sched_timer_dispatch(void)
unsigned int next_waketime = updated_waketime;
if (unlikely(res == SF_DONE)) {
next_waketime = t->next->waketime;
- timer_list = t->next;
+ SchedStatus.timer_list = t->next;
} else if (!timer_is_before(updated_waketime, t->next->waketime)) {
next_waketime = t->next->waketime;
- timer_list = t->next;
+ SchedStatus.timer_list = t->next;
insert_timer(t, updated_waketime);
}
@@ -169,7 +174,7 @@ sched_timer_dispatch(void)
void
sched_timer_reset(void)
{
- timer_list = &deleted_timer;
+ SchedStatus.timer_list = &deleted_timer;
deleted_timer.waketime = periodic_timer.waketime;
deleted_timer.next = &periodic_timer;
periodic_timer.next = &sentinel_timer;
@@ -181,8 +186,6 @@ sched_timer_reset(void)
* Tasks
****************************************************************/
-static int_fast8_t tasks_status;
-
#define TS_IDLE -1
#define TS_REQUESTED 0
#define TS_RUNNING 1
@@ -191,14 +194,14 @@ static int_fast8_t tasks_status;
void
sched_wake_tasks(void)
{
- tasks_status = TS_REQUESTED;
+ SchedStatus.tasks_status = TS_REQUESTED;
}
// Check if tasks need to be run
uint8_t
sched_tasks_busy(void)
{
- return tasks_status >= TS_REQUESTED;
+ return SchedStatus.tasks_status >= TS_REQUESTED;
}
// Note that a task is ready to run
@@ -227,20 +230,20 @@ run_tasks(void)
for (;;) {
// Check if can sleep
irq_poll();
- if (tasks_status != TS_REQUESTED) {
+ if (SchedStatus.tasks_status != TS_REQUESTED) {
start -= timer_read_time();
irq_disable();
- if (tasks_status != TS_REQUESTED) {
+ if (SchedStatus.tasks_status != TS_REQUESTED) {
// Sleep processor (only run timers) until tasks woken
- tasks_status = TS_IDLE;
+ SchedStatus.tasks_status = TS_IDLE;
do {
irq_wait();
- } while (tasks_status != TS_REQUESTED);
+ } while (SchedStatus.tasks_status != TS_REQUESTED);
}
irq_enable();
start += timer_read_time();
}
- tasks_status = TS_RUNNING;
+ SchedStatus.tasks_status = TS_RUNNING;
// Run all tasks
extern void ctr_run_taskfuncs(void);
@@ -258,25 +261,23 @@ run_tasks(void)
* Shutdown processing
****************************************************************/
-static uint_fast8_t shutdown_status, shutdown_reason;
-
// Return true if the machine is in an emergency stop state
uint8_t
sched_is_shutdown(void)
{
- return !!shutdown_status;
+ return !!SchedStatus.shutdown_status;
}
// Transition out of shutdown state
void
sched_clear_shutdown(void)
{
- if (!shutdown_status)
+ if (!SchedStatus.shutdown_status)
shutdown("Shutdown cleared when not shutdown");
- if (shutdown_status == 2)
+ if (SchedStatus.shutdown_status == 2)
// Ignore attempt to clear shutdown if still processing shutdown
return;
- shutdown_status = 0;
+ SchedStatus.shutdown_status = 0;
}
// Invoke all shutdown functions (as declared by DECL_SHUTDOWN)
@@ -285,30 +286,31 @@ run_shutdown(int reason)
{
irq_disable();
uint32_t cur = timer_read_time();
- if (!shutdown_status)
- shutdown_reason = reason;
- shutdown_status = 2;
+ if (!SchedStatus.shutdown_status)
+ SchedStatus.shutdown_reason = reason;
+ SchedStatus.shutdown_status = 2;
sched_timer_reset();
extern void ctr_run_shutdownfuncs(void);
ctr_run_shutdownfuncs();
- shutdown_status = 1;
+ SchedStatus.shutdown_status = 1;
irq_enable();
- sendf("shutdown clock=%u static_string_id=%hu", cur, shutdown_reason);
+ sendf("shutdown clock=%u static_string_id=%hu", cur
+ , SchedStatus.shutdown_reason);
}
// Report the last shutdown reason code
void
sched_report_shutdown(void)
{
- sendf("is_shutdown static_string_id=%hu", shutdown_reason);
+ sendf("is_shutdown static_string_id=%hu", SchedStatus.shutdown_reason);
}
// Shutdown the machine if not already in the process of shutting down
void __always_inline
sched_try_shutdown(uint_fast8_t reason)
{
- if (!shutdown_status)
+ if (!SchedStatus.shutdown_status)
sched_shutdown(reason);
}