00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "global.h"
00023 #include "processor.h"
00024 #include "scheduler.h"
00025
00026
00027 struct task TaskList[TASKLIST_SIZE];
00028
00029
00030 void schedulerInit(void)
00031 {
00032 int i;
00033
00034
00035 for(i=0; i<TASKLIST_SIZE; i++)
00036 {
00037
00038 TaskList[i].runtime = 0;
00039 TaskList[i].funcptr = 0;
00040 }
00041 }
00042
00043 void schedulerRun(int systime)
00044 {
00045 int i;
00046
00047
00048 for(i=0; i<TASKLIST_SIZE; i++)
00049 {
00050 if(TaskList[i].funcptr)
00051 {
00052
00053 if(systime >= TaskList[i].runtime)
00054 {
00055
00056 TaskList[i].funcptr(i,systime);
00057 if(TaskList[i].nrepeat)
00058 {
00059 TaskList[i].runtime += TaskList[i].interval;
00060 TaskList[i].nrepeat--;
00061
00062 if(!TaskList[i].nrepeat)
00063 schedulerRemoveTask(i);
00064 }
00065 else
00066 {
00067
00068 TaskList[i].runtime += TaskList[i].interval;
00069 }
00070
00071 }
00072 }
00073 }
00074
00075
00076
00077 }
00078
00079 int schedulerAddTask(int runtime, int nrepeat, int interval, taskfuncptr taskfunc)
00080 {
00081 int i;
00082
00083 for(i=0; i<TASKLIST_SIZE; i++)
00084 {
00085 CRITICAL_SECTION_BEGIN;
00086 if(!TaskList[i].funcptr)
00087 {
00088
00089 TaskList[i].funcptr = taskfunc;
00090 TaskList[i].runtime = runtime;
00091 TaskList[i].nrepeat = nrepeat;
00092 TaskList[i].interval = interval;
00093
00094 CRITICAL_SECTION_END;
00095 return i;
00096 }
00097 CRITICAL_SECTION_END;
00098 }
00099
00100 return -1;
00101 }
00102
00103 int schedulerRemoveTask(int taskhandle)
00104 {
00105
00106 TaskList[taskhandle].runtime = 0;
00107 TaskList[taskhandle].interval = 0;
00108 TaskList[taskhandle].nrepeat = 0;
00109 TaskList[taskhandle].funcptr = 0;
00110 return taskhandle;
00111 }
00112
00113 int schedulerRemoveTaskFunc(taskfuncptr funcptr)
00114 {
00115 int i;
00116 int taskhandle = -1;
00117
00118 for(i=0; i<TASKLIST_SIZE; i++)
00119 {
00120
00121 if(TaskList[i].funcptr == funcptr)
00122 taskhandle = schedulerRemoveTask(i);
00123 }
00124 return taskhandle;
00125 }