Libtask is a simple coroutine library. It runs on Linux (ARM, MIPS, and x86),FreeBSD (x86), OS X (PowerPC x86, and x86-64), and SunOS Solaris (Sparc),and is easy to port to other systems.
Libtask gives the programmer the illusion of threads, but the operating system sees only a single kernel thread.For clarity, we refer to the coroutines as "tasks," not threads.
(一)中主要分析协程正常执行调度流程。
libtask里面有两个关键函数taskcreate和taskscheduler
1. taskceate
调用taskcreate初始化一个新的task结构体,并插入到任务队列,设置状态为ready,准备被调度。taskcreate中传递function函数指针和参数,真正被调度时该函数将被执行。
int
taskcreate(void (*fn)(void*), void *arg, uint stack)
{
int id;
Task *t;
t = taskalloc(fn, arg, stack);
taskcount++;
id = t->id;
if(nalltask%64 == 0)