Created
April 26, 2023 21:51
-
-
Save ghazanhaider/fff3de48568d27f2e05295d7754e5c14 to your computer and use it in GitHub Desktop.
Simple task scheduler
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> | |
// task functions | |
void taskOne (void) { puts("Thread 1\n"); } | |
void taskTwo (void) { puts("Thread 2\n"); } | |
void taskThree (void) { puts("Thread 3\n"); } | |
// Function array of tasks | |
void *funcArray[] = { | |
&taskOne, | |
&taskTwo, | |
&taskThree | |
}; | |
// task Variables | |
int currentTask=0; | |
int taskCount = (int)(sizeof(funcArray) / sizeof(void *)); | |
// task Switcher | |
void taskSwitch(void) { | |
void (*funcptr)(void); | |
if (currentTask >= taskCount) currentTask = 0; | |
(funcptr) = funcArray[currentTask++]; | |
(*funcptr)(); | |
} | |
int main(void) { | |
puts("Starting..\n"); | |
taskSwitch(); | |
taskSwitch(); | |
taskSwitch(); | |
taskSwitch(); | |
taskSwitch(); | |
printf("sizeof *funcarray: %d\n",taskCount); | |
return 0; | |
} | |
/* | |
taskSwitch() should be triggered by a tick timer. | |
Housekeeping in this function is done before switching into | |
the task because the task and therefore this function might | |
be preempted multiple times. Each time it runs the task | |
fresh instead of continuing. | |
TODO: | |
1) taskSwitch should return the (* funcptr) to run, this way | |
each switch is only one frame deep, right now it is two | |
frames deep, taskSwitch and task(), taking two slots in | |
the stack per pre-emption. | |
2) If a task is busy, it should be continued, not started fresh. | |
3) If a task times out, skip that task from here on and log it | |
4) A mechanism to add/remove tasks? |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment