kronkflow
A tiny task scheduler in C, built for the zappy project.
Overview
Kronkflow is a lightweight task scheduler for C, with no dependency. It keeps tasks in a binary min-heap, so the next task to run is always on top.
It handles one-time and periodic tasks. Each tick, ready tasks run stage by stage, whatever the insertion order. It was built for the zappy project, and KronkWorld uses it to run its systems.
Changelog
1.2.0
Windows
- Builds and runs on Windows, with MSVC and MinGW; dependencies are pinned to a set version.
Examples
#include <kronkflow/scheduler.h>
#include <kronkflow/task.h>
#include <stdio.h>
#include <unistd.h>
// Your task handler
static kfBool my_handler(void *context, void *data)
{
printf("Task executed with data: %ld\n", (long int)data);
return kfTrue;
}
int main(void)
{
// Create a scheduler with a capacity of 512 tasks
kfScheduler *sch = kfScheduler_create(512);
// Add a periodic task on stage 0 (runs in 2 ticks, then every 5 ticks)
kfScheduler_addTask(sch,
kfTask_opt(&my_handler, (void *)1, NULL, 0, (kfRWMasks){ 0, 0 }), 2, 5);
// Add a punctual task on stage 1 (runs once in 10 ticks).
kfScheduler_addTask(sch,
kfTask_opt(&my_handler, (void *)3, NULL, 1, (kfRWMasks){ 0, 0 }), 10, 0);
// Main loop
while (1) {
sleep(1); // Simulate time passing
kfScheduler_tick(sch, NULL);
}
kfScheduler_destroy(sch);
return 0;
}