1
0
mirror of https://github.com/CPunch/Laika.git synced 2024-11-21 20:40:05 +00:00

TaskService refactoring

This commit is contained in:
CPunch 2022-02-17 17:24:46 -06:00
parent 4e8febe916
commit 4e928464bd
5 changed files with 50 additions and 20 deletions

View File

@ -5,15 +5,25 @@
struct sLaika_taskService tService; struct sLaika_taskService tService;
/*void debugTask(struct sLaika_taskService *service, struct sLaika_task *task, clock_t currTick, void *uData) { /*void debugTask1(struct sLaika_taskService *service, struct sLaika_task *task, clock_t currTick, void *uData) {
printf("hello !\n"); printf("hello 1 !\n");
}
void debugTask2(struct sLaika_taskService *service, struct sLaika_task *task, clock_t currTick, void *uData) {
printf("hello 2 !\n");
}
void debugTask3(struct sLaika_taskService *service, struct sLaika_task *task, clock_t currTick, void *uData) {
printf("hello 3 !\n");
}*/ }*/
int main(int argv, char **argc) { int main(int argv, char **argc) {
struct sLaika_cnc *cnc = laikaC_newCNC(13337); struct sLaika_cnc *cnc = laikaC_newCNC(13337);
laikaT_initTaskService(&tService); laikaT_initTaskService(&tService);
/*laikaT_newTask(&tService, 2000, debugTask, NULL);*/ /*laikaT_newTask(&tService, 1000, debugTask1, NULL);
laikaT_newTask(&tService, 500, debugTask2, NULL);
laikaT_newTask(&tService, 2000, debugTask3, NULL);*/
while (true) { while (true) {
laikaC_pollPeers(cnc, laikaT_timeTillTask(&tService)); laikaC_pollPeers(cnc, laikaT_timeTillTask(&tService));

View File

@ -1,6 +1,8 @@
#ifndef LAIKA_PACKET_H #ifndef LAIKA_PACKET_H
#define LAIKA_PACKET_H #define LAIKA_PACKET_H
#include <inttypes.h>
#define LAIKA_MAGIC "LAI\x12" #define LAIKA_MAGIC "LAI\x12"
#define LAIKA_MAGICLEN 4 #define LAIKA_MAGICLEN 4

View File

@ -12,6 +12,7 @@ struct sLaika_task {
taskCallback callback; taskCallback callback;
void *uData; void *uData;
long scheduled; long scheduled;
int delta;
}; };
struct sLaika_taskService { struct sLaika_taskService {

View File

@ -24,14 +24,12 @@ void laikaT_cleanTaskService(struct sLaika_taskService *service) {
} }
} }
struct sLaika_task *laikaT_newTask(struct sLaika_taskService *service, int delta, taskCallback callback, void *uData) { void scheduleTask(struct sLaika_taskService *service, struct sLaika_task *task) {
struct sLaika_task *curr = service->headTask, *last = NULL, *task = laikaM_malloc(sizeof(struct sLaika_task)); struct sLaika_task *curr = service->headTask, *last = NULL;
task->callback = callback; task->scheduled = getTime() + task->delta;
task->uData = uData;
task->scheduled = getTime() + delta;
/* search list for event for which we're scheduled before */ /* search list for task for which we're scheduled before */
while (curr != NULL && curr->scheduled < task->scheduled) { while (curr != NULL && curr->scheduled < task->scheduled) {
last = curr; last = curr;
curr = curr->next; curr = curr->next;
@ -49,10 +47,10 @@ struct sLaika_task *laikaT_newTask(struct sLaika_taskService *service, int delta
} }
} }
void laikaT_delTask(struct sLaika_taskService *service, struct sLaika_task *task) { void unscheduleTask(struct sLaika_taskService *service, struct sLaika_task *task) {
struct sLaika_task *curr = service->headTask, *last = NULL; struct sLaika_task *curr = service->headTask, *last = NULL;
if (task == service->headTask) { if (task == service->headTask) { /* if task is root, set root to next */
service->headTask = task->next; service->headTask = task->next;
} else { } else {
/* find in list */ /* find in list */
@ -63,8 +61,23 @@ void laikaT_delTask(struct sLaika_taskService *service, struct sLaika_task *task
/* unlink */ /* unlink */
last->next = task->next; last->next = task->next;
task->next = NULL;
} }
}
struct sLaika_task *laikaT_newTask(struct sLaika_taskService *service, int delta, taskCallback callback, void *uData) {
struct sLaika_task *task = laikaM_malloc(sizeof(struct sLaika_task));
task->callback = callback;
task->uData = uData;
task->delta = delta;
task->next = NULL;
scheduleTask(service, task);
}
void laikaT_delTask(struct sLaika_taskService *service, struct sLaika_task *task) {
unscheduleTask(service, task);
laikaM_free(task); laikaM_free(task);
} }
@ -73,21 +86,25 @@ void laikaT_pollTasks(struct sLaika_taskService *service) {
clock_t currTick = getTime(); clock_t currTick = getTime();
/* run each task, list is already sorted from closest scheduled task to furthest */ /* run each task, list is already sorted from closest scheduled task to furthest */
while (curr != NULL && curr->scheduled < currTick) { /* if scheduled time is greater than currTime, all events that follow are also not scheduled yet */ while (curr != NULL && curr->scheduled <= currTick) { /* if scheduled time is greater than currTime, all events that follow are also not scheduled yet */
/* dispatch task callback */ /* walk to next task */
curr->callback(service, curr, currTick, curr->uData);
/* walk to next task and free */
last = curr; last = curr;
curr = curr->next; curr = curr->next;
laikaT_delTask(service, last);
/* reset task timer */
unscheduleTask(service, last);
scheduleTask(service, last);
/* dispatch task callback */
last->callback(service, last, currTick, last->uData);
} }
} }
/* will return the delta time in ms till the next event. -1 for no tasks scheduled */ /* will return the delta time in ms till the next event. -1 for no tasks scheduled */
int laikaT_timeTillTask(struct sLaika_taskService *service) { int laikaT_timeTillTask(struct sLaika_taskService *service) {
if (service->headTask) if (service->headTask) {
return service->headTask->scheduled - getTime(); int pause = service->headTask->scheduled - getTime();
else return (pause > 0) ? pause : 0;
} else
return -1; /* no tasks scheduled */ return -1; /* no tasks scheduled */
} }