#include <stdbool.h>
#include <stdint.h>
#include <stddef.h>
#include <stdio.h>
#include <xev.h>
typedef struct {
xev_threadpool_task pool_task;
bool done;
} job_t;
#define container_of(ptr, type, member) \
((type *) ((char *) (ptr) - offsetof(type, member)))
void task_callback(xev_threadpool_task* t) {
job_t *job = container_of(t, job_t, pool_task);
job->done = true;
}
int main(void) {
xev_threadpool pool;
if (xev_threadpool_init(&pool, NULL) != 0) {
printf("xev_threadpool_init failure\n");
return 1;
}
xev_threadpool_batch batch;
xev_threadpool_batch_init(&batch);
const int TASK_COUNT = 128;
job_t jobs[TASK_COUNT];
for (int i = 0; i < TASK_COUNT; i++) {
jobs[i].done = false;
xev_threadpool_task_init(&jobs[i].pool_task, &task_callback);
xev_threadpool_batch_push_task(&batch, &jobs[i].pool_task);
}
xev_threadpool_schedule(&pool, &batch);
while (true) {
bool done = true;
for (int i = 0; i < TASK_COUNT; i++) {
if (!jobs[i].done) {
done = false;
break;
}
}
if (done) break;
}
xev_threadpool_shutdown(&pool);
xev_threadpool_deinit(&pool);
printf("%d tasks completed!\n", TASK_COUNT);
return 0;
}