Simple asynchronous scheduler. More...
Go to the source code of this file.
Functions | |
int | asched_get_timestamp (void) |
Gets the current timestamp of the board. | |
void | asched_init (const VOID_VOID_F *sched, int tick_divider) |
Initializes the scheduler. | |
void | asched_run (void) |
Executes the next row of scheduled tasks. | |
void | asched_set_timestamp (int timestamp) |
This function sets the current timestamp to timestamp . | |
void | asched_tick (void) |
Signals the scheduler to start executing the next row of tasks in the schedule. | |
Variables | |
static ASCHED_COMPLETION | asched_completion |
static int | asched_div_counter |
static int | asched_divider |
static volatile int | asched_next_task_index |
static int | asched_saved_slot_start_index |
static const VOID_VOID_F * | asched_schedule |
This array is a 2D table of tasks. | |
static volatile int | asched_slot_start_index |
static volatile long int | asched_timestamp |
Simple asynchronous scheduler.
Definition in file async_scheduler.c.
int asched_get_timestamp | ( | void | ) |
Gets the current timestamp of the board.
The timestamp is usually updated by the main brain.
Definition at line 131 of file async_scheduler.c.
void asched_init | ( | const VOID_VOID_F * | sched, | |
int | tick_divider | |||
) |
Initializes the scheduler.
Needs to be called in software setup.
sched | A 2D array representing the schedule. | |
tick_divider | Calls to asched_tick will count to tick_divider - 1 before calling the next row. |
Definition at line 39 of file async_scheduler.c.
References asched_schedule.
00039 { 00040 asched_schedule = sched; 00041 asched_divider = tick_divider; 00042 asched_div_counter = 0; 00043 asched_slot_start_index = -1; 00044 asched_saved_slot_start_index = -1; 00045 asched_next_task_index = -1; 00046 00047 asched_completion = ASCHED_DONE; 00048 }
void asched_run | ( | void | ) |
Executes the next row of scheduled tasks.
Call this function continuously from your main
while loop.
Definition at line 102 of file async_scheduler.c.
References asched_schedule.
00102 { 00103 //execute this timestep 00104 VOID_VOID_F next_task; 00105 00106 //we do not want things to run before asched_tick has been called for the first time. 00107 if(asched_slot_start_index >= 0) { 00108 while(1){ 00109 if((asched_slot_start_index != asched_saved_slot_start_index)){ //check if rescheduling has occured, 00110 asched_saved_slot_start_index = asched_slot_start_index; 00111 asched_next_task_index = asched_slot_start_index; 00112 } 00113 next_task = asched_schedule[asched_next_task_index]; 00114 if(next_task != (VOID_VOID_F)NULL) { 00115 next_task();//execute next task 00116 asched_next_task_index++; 00117 } else {//bah, race conditions!!! ??? 00118 asched_completion = ASCHED_DONE; 00119 break; 00120 } 00121 } 00122 } 00123 }
void asched_set_timestamp | ( | int | timestamp | ) |
This function sets the current timestamp to timestamp
.
timestamp | The current timestamp |
Definition at line 94 of file async_scheduler.c.
void asched_tick | ( | void | ) |
Signals the scheduler to start executing the next row of tasks in the schedule.
If the previous row did not finish before asched_tick was called again, this will cause an ERROR_ASCHED_DNF
error, and will wait for the previous row to finish executing.
Definition at line 55 of file async_scheduler.c.
References asched_schedule, and error_occurred().
00055 { 00056 int search_idx; 00057 00058 if(asched_div_counter >= (asched_divider - 1)){ 00059 //1) check if all tasks have successfully completed from the previous iteration 00060 if(asched_completion != ASCHED_DONE){ 00061 error_occurred(ERROR_ASCHED_DNF); 00062 } 00063 00064 //2) schedule next tasks 00065 if(asched_next_task_index >= 0){ 00066 search_idx = asched_next_task_index; 00067 while(asched_schedule[search_idx] != (VOID_VOID_F)NULL){//find end of current time slot in case of overrun 00068 search_idx++; 00069 } 00070 search_idx++;//advance to first task of next slot 00071 //if we have reached the end of the schedule (double NULL), wrap to the beginning 00072 if(asched_schedule[search_idx] == (VOID_VOID_F)NULL){ 00073 search_idx = 0; 00074 } 00075 } else { 00076 search_idx = 0; //first run of the scheduler 00077 } 00078 asched_slot_start_index = search_idx;//schedule the beginning of the next time slot 00079 00080 asched_completion = ASCHED_NOT_DONE; 00081 00082 //3) reset divider 00083 asched_div_counter = 0; 00084 } else { 00085 asched_div_counter++; 00086 } 00087 00088 }
const VOID_VOID_F* asched_schedule [static] |
This array is a 2D table of tasks.
One row of the array will be run for every call to asched_tick. Each row is NULL
terminated, and the array is doubly NULL
terminated. A VOID_VOID_F
is a pointer to a function that takes void
parameters and returns void
.
Definition at line 21 of file async_scheduler.c.
Referenced by asched_init(), asched_run(), and asched_tick().