libtask协程库实现原理分析 (一)

本文深入探讨了libtask协程库的工作原理,它在多个平台上运行并提供类似线程的体验,但仅使用一个内核线程。文章重点分析了两个核心函数:taskcreate和taskscheduler。taskcreate负责初始化任务结构体,创建上下文,并将其准备好进行调度。taskscheduler则不断循环,通过contextswitch在任务之间切换,确保协程的流畅执行。
摘要由CSDN通过智能技术生成

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)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值