mirror of
				https://github.com/CPunch/Laika.git
				synced 2025-10-24 23:30:15 +00:00 
			
		
		
		
	TaskService refactoring
This commit is contained in:
		| @@ -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)); | ||||
|   | ||||
| @@ -1,6 +1,8 @@ | ||||
| #ifndef LAIKA_PACKET_H | ||||
| #define LAIKA_PACKET_H | ||||
|  | ||||
| #include <inttypes.h> | ||||
|  | ||||
| #define LAIKA_MAGIC "LAI\x12" | ||||
| #define LAIKA_MAGICLEN 4 | ||||
|  | ||||
|   | ||||
| @@ -12,6 +12,7 @@ struct sLaika_task { | ||||
|     taskCallback callback; | ||||
|     void *uData; | ||||
|     long scheduled; | ||||
|     int delta; | ||||
| }; | ||||
|  | ||||
| struct sLaika_taskService { | ||||
|   | ||||
| @@ -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 */ | ||||
| } | ||||
		Reference in New Issue
	
	Block a user