Created
April 6, 2015 15:12
-
-
Save argorain/946bcd7b4e2f535e9a82 to your computer and use it in GitHub Desktop.
Simple round robin scheduler with table of function pointers.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "stdio.h" | |
#define MAX_SIZE 10 //maximum amount of functions in table | |
//#define SHOW_ITERATION //uncoment for show iteration numbers | |
//************ TABLES AND ROUTINES ***************************** | |
typedef enum //function callers | |
{ | |
FUN_1=0, | |
FUN_2, | |
FUN_3, | |
} fun_e; | |
typedef struct //functon table definition | |
{ | |
fun_e fun; ///< identifier | |
int (*p_fun)(int param); ///< pointer to routine | |
int period; | |
int timer; | |
} fun_table_t; | |
fun_table_t fun_table[MAX_SIZE]; //function table itself | |
unsigned int fun_amount=0; //actual amount of functions in table | |
void init_fun(fun_e fun, int (*p_fun)(int param), int period) //initialization of table. See main ;) | |
{ | |
fun_table[fun].fun=fun; | |
fun_table[fun].p_fun = *p_fun; | |
fun_table[fun].period = period; | |
fun_table[fun].timer=0; | |
fun_amount++; | |
} | |
int call_fun(fun_e fun, int param) //function caller | |
{ | |
int i=0; | |
for(i=0;i<fun_amount;i++) | |
{ | |
if(fun==fun_table[i].fun) | |
{ | |
return fun_table[i].p_fun(param); | |
} | |
} | |
return -1; //err | |
} | |
void init_timers(){ //timer initialization for scheduler | |
for(int i=0; i<fun_amount; i++){ | |
fun_table[i].timer=fun_table[i].period; | |
} | |
} | |
int check_timer(){ //SW timer checker | |
for(int i=0; i<fun_amount; i++){ | |
if(fun_table[i].timer>0) | |
fun_table[i].timer--; | |
else { | |
fun_table[i].timer=fun_table[i].period; | |
fun_table[i].p_fun(i); | |
} | |
} | |
} | |
//*************** FUNCTIONS ***************************************** | |
int fun1(int param){ | |
printf("Hello from 1st function\n"); | |
return param; | |
} | |
int fun2(int param){ | |
printf("Hello from 2nd function\n"); | |
return param*2; | |
} | |
int fun3(int param){ | |
printf("Hello from 3rd function\n"); | |
return param+2; | |
} | |
//*************** MAIN ********************************* | |
int main(int argc, char **argv){ | |
printf("Function pointers example\n\n"); | |
init_fun(FUN_1, fun1, 50); | |
init_fun(FUN_2, fun2, 100); | |
init_fun(FUN_3, fun3, 200); | |
printf("Function init OK\n\n"); | |
printf("Lets try call all functions.\n"); | |
printf("1. return: %d\n",call_fun(FUN_1, 5)); | |
printf("2. return: %d\n",call_fun(FUN_2, 5)); | |
printf("3. return: %d\n",call_fun(FUN_3, 5)); | |
printf("All OK\n\n"); | |
printf("Let's try calling by number in loop.\n"); | |
for(int i=0; i<10; i++){ | |
printf("%d. return: %d\n",i,call_fun(i, 5)); | |
} | |
printf("\nLet's try simple round-robin scheduler with fixed lifetime.\n"); | |
init_timers(); | |
for(int i=0; i<1000; i++){ | |
#ifdef SHOW_ITERATION | |
printf("iteration %d\n",i); | |
#endif | |
check_timer(); | |
} | |
printf("\n\nAnd thats all.\n"); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment