void exec_tasks(void) { if (!task_scheduler_running) { DEBUG("The task_scheduler has been stopped"); return; } time_t now = time(NULL); gboolean rolled = now < global_last_schedule; GList *list_of_keys = g_hash_table_get_keys(tasks); for (GList *l=list_of_keys; l ;l=l->next) { task_t *task = g_hash_table_lookup(tasks, l->data); if (rolled || now >= task->next_schedule) { task->next_schedule = now + task->period; if (task->busy) WARN("Task [%s] is busy", task->id); else { task->busy = TRUE; exec_scheduled_tasks(task); } } if (task->flag_destroy) g_hash_table_remove(tasks, l->data); } g_list_free(list_of_keys); global_last_schedule = now; }
void exec_tasks (gint64 now) { if (now <= next_schedule) return; if (!task_scheduler_running) { DEBUG("The task_scheduler has been stopped"); return; } gboolean fired = FALSE; GList *list_of_keys = g_hash_table_get_keys(tasks); for (GList *l=list_of_keys; l ;l=l->next) { task_t *task = g_hash_table_lookup(tasks, l->data); if (now >= task->next_schedule) { /* advance the task */ if (task->busy) task_set_next_schedule(task, now + 1000); else task_set_next_schedule(task, now + (task->period * 1000)); /* then execute it */ if (task->busy) WARN("Task [%s] is busy", task->id); else { fired = task->busy = TRUE; exec_scheduled_tasks(task); } } if (task->flag_destroy) g_hash_table_remove(tasks, l->data); } g_list_free(list_of_keys); if (fired) next_schedule = task_first_schedule (); }