#include #include #include "spinlock/spinlock.h" #include "cjob/cjob.h" #include "dlmalloc/malloc.h" #include "util/util.h" CJobs CJOBS; static uint64_t cjobids = 0; void cjob_init(void) { spinlock_init(&CJOBS.spinlock); CJOBS.cjobs = NULL; } int32_t cjob_register(CJobFn fn, void *arg) { CJob *cjob = dlmalloc(sizeof(*cjob)); cjob->fn = fn; cjob->arg = arg; int32_t id = cjob->id = cjobids++; spinlock_acquire(&CJOBS.spinlock); LL_APPEND(CJOBS.cjobs, cjob); spinlock_release(&CJOBS.spinlock); return id; } void cjob_runjobs(void) { CJob *cjob, *cjobtmp; spinlock_acquire(&CJOBS.spinlock); LL_FOREACH_SAFE(CJOBS.cjobs, cjob, cjobtmp) { cjob->fn(cjob->arg); } spinlock_release(&CJOBS.spinlock); }