int main() { // register tasks addTask(task1Init, task1Processor, task1Cancel); addTask(task2Init, task2Processor, task2Cancel); // run init for (Task* task=task_head; task!=0; task = task->next) { task->init(task->id); } int Task->action void task_init() // called at Start command, puts task in queue int task_process() // called while true. If returns 0, remove task from queue int task_progress() // called by Progress command void task_cancel() // called by Cancel command for (Task* task=task_head; task!=0; task = task->next) { if (task->action) { switch (task->action) { case START: // check if running task_init(); break; case PROGRESS: // check if running int progress = task_progress(); // ... break; case CANCEL: // check if running task_cancel(); removeFromRunning(task); break; } } else { int result = task_process(); if (result == DONE) removeFromRunning(task); } } // main loop int tick = 0; while (tick < 100) { for (Task* task=task_head; task!=0; task = task->next) { if (task->events) { task->events = task->process(task->events); } // MANUALLY INSERT EVENTS if (task->id==1 && tick==10) task->events = 30; if (task->id==1 && tick==20) interrupt(); if (task->id==2 && tick==15) task->events = 152; } tick++; } return 0; }
bool task_abort(Task *task) { bool success = task_cancel(task); task_detach(task); return success; }