Browse Source

Track how long the time since last tick *actually* was and provide to

tasks

The plan is that we won't provide a hard guarantee on the next run time
so if we get several tasks that are all supposed to fire _around_ the
same time, we can instead just wake up once and run them all.
master
Adam Pippin 4 years ago
parent
commit
7de1698099
  1. 19
      Kernel.cpp
  2. 3
      Kernel.h

19
Kernel.cpp

@ -9,7 +9,8 @@ struct Task *Kernel_get_task(int pid);
struct Task tasks[MAX_TASKS];
int next_pid = 1;
unsigned long last_tick;
unsigned long last_tick_start;
unsigned long last_tick_duration;
bool inputs[2] = {false, false};
void Kernel_panic(char* message)
@ -25,7 +26,8 @@ void Kernel_panic(char* message)
void Kernel_setup()
{
last_tick = millis();
last_tick_start = millis();
last_tick_duration = 0;
//Serial.begin(115200);
}
@ -158,6 +160,11 @@ bool Kernel_read_input(int input)
return value;
}
unsigned long Kernel_get_last_tick_duration()
{
return last_tick_duration;
}
struct Task *Kernel_get_task(int pid)
{
for (int i=0; i<MAX_TASKS; i++)
@ -174,10 +181,10 @@ struct Task *Kernel_get_task(int pid)
void Kernel_tick()
{
unsigned long duration = millis_since(last_tick);
last_tick_duration = millis_since(last_tick_start);
// Store the last runtime so we can calculate duration next tick. We do this at the beginning of the method
// so it counts the time spent running tasks as time elapsed.
last_tick = millis();
last_tick_start = millis();
unsigned long next_tick_due = ULONG_MAX;
int running_tasks = 0;
@ -195,7 +202,7 @@ void Kernel_tick()
// run_interval 0 is a special case of "run whenever we run", so set TICK any time we get here
if (tasks[i].run_interval > 0)
{
if (tasks[i].run_accumulator + duration > tasks[i].run_interval)
if (tasks[i].run_accumulator + last_tick_duration > tasks[i].run_interval)
{
// Set signal
tasks[i].signal |= SIGNAL_TICK;
@ -205,7 +212,7 @@ void Kernel_tick()
else
{
// Otherwise, accumulate time
tasks[i].run_accumulator += duration;
tasks[i].run_accumulator += last_tick_duration;
}
}
else

3
Kernel.h

@ -35,6 +35,7 @@ void Kernel_signal_mask(int pid, unsigned int signal_mask);
// Get the run interval of a process
unsigned long Kernel_get_run_interval(int pid);
void Kernel_set_run_interval(int pid, unsigned long interval);
// Count how many tasks are running
int Kernel_count_running_tasks();
@ -46,6 +47,8 @@ int Kernel_get_exit_code(int pid);
// Check if a button was pressed
bool Kernel_read_input(int input);
// Get how long *actually* elapsed since the last tick since we don't provide hard guarantees
unsigned long Kernel_get_last_tick_duration();
#define SIGNAL_NONE 0x0000
#define SIGNAL_TICK 0x0001

Loading…
Cancel
Save