Skip to content

Instantly share code, notes, and snippets.

@liuliu
Created August 16, 2018 06:52
Show Gist options
  • Save liuliu/3eae63e0f2e18ea7876993098ee3e0bc to your computer and use it in GitHub Desktop.
Save liuliu/3eae63e0f2e18ea7876993098ee3e0bc to your computer and use it in GitHub Desktop.
static void _task_entry_point(uint32_t part0, uint32_t part1)
{
union ptr_splitter p;
p.part[0] = part0;
p.part[1] = part1;
task_t *task = (task_t*)p.ptr;
task->fn(task);
task->done = 1;
swapcontext(&task->schd->callee, &task->schd->caller);
}
static task_t* taskcreate(schd_t* const schd, task_fn_t fn)
{
task_t *task = (task_t*)calloc(1, sizeof(task_t));
task->schd = schd;
task->stack = (char*)calloc(1, default_stack_size);
task->fn = fn;
getcontext(&task->context);
task->context.uc_stack.ss_sp = task->stack;
task->context.uc_stack.ss_size = default_stack_size;
task->context.uc_link = 0;
union ptr_splitter p;
p.ptr = task;
makecontext(&task->context, (void (*)(void))_task_entry_point, 2, p.part[0], p.part[1]);
return task;
}
static void addtask(schd_t* const schd, task_t* const t)
{
if (schd->tail)
{
schd->tail->next = t;
t->prev = schd->tail;
} else {
schd->head = t;
t->prev = 0;
}
schd->tail = t;
t->next = 0;
}
static void taskfree(task_t* const task)
{
task_t* waitfor = task->waitfor;
while (waitfor)
{
task_t* const next = waitfor->next;
addtask(task->schd, waitfor);
waitfor = next;
}
free(task->stack);
free(task);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment