之前看协程相关的东西时,曾一念而过想着怎么自己来实现一个给 C++ 用,但在保存现场恢复现场之类的细节上被自己的想法吓住,也没有深入去研究,后面一丢开就忘了。近来微博上看人在讨论怎么实现一个 user space 上的线程库,有人提到了 setcontext,swapcontext 之类的函数,说可以用来保存和切换上下文,我忽然觉得这应该也能用来实现协程,回头一搜,果然已经有人曾用这些函数做过相关的事情,略略看了几个,觉得到底不大好用,还不如自己搞一个简单点的。

说到 c++ 上的协程,boost 里其实已经有相关的实现了,不过接口上看用起来有些麻烦,单纯从语法上来说,我觉得 Lua 的协程最简洁易用了,概念上也比较直接,为什么不做一个类似的呢?所以我就打算照着 Lua 来山寨一个,只需要支持四个接口就够了:

1)create coroutine。

2)run/resume coroutine。

3)Yield running corouinte。

4)IsCoroutineAlive。

保存与恢复上下文

实现协程/线程,最麻烦莫过于保存和切换上下文了,好在 makecontext,swapcontext 这几个函数相当好用,已经完全帮忙解决了这个难题:makecontext 可以帮我们建立起协程的上下文,swapcontext 则可以切换不同的上下文,从而实现那种把当前函数暂时停住,切换出去执行别的函数然后再切换回来继续执行的效果:

#include <iostream>
#include <ucontext.h>
using namespace std; static char g_stack[];
static ucontext_t ctx,ctx_main; void func()
{
// do something.
cout << "enter func" << endl; swapcontext(&ctx, &ctx_main); cout << "func1 resume from yield" << endl;
// continue to do something.
} int main()
{
getcontext(&ctx);
ctx.uc_stack.ss_sp = g_stack;
ctx.uc_stack.ss_size = sizeof g_stack;
ctx.uc_link = &ctx_main; makecontext(&ctx, func, ); cout << "in main, before coroutine starts" << endl; swapcontext(&ctx_main, &ctx); cout << "back to main" << endl; swapcontext(&ctx_main, &ctx); cout << "back to main again" << endl;
return ;
}

如上代码所示,显然我们只要简单包装一下 swapcontext,很容易就可以实现 Yield 和 Resume,有了它们的帮助协程做起来就容易多了。

使用与实现

在使用 makecontext,swapcontext 的基础上,我花了一个多小时简单实现了一个协程库,参看这里,代码写下来总共才200多行,出乎意料的简单,用起来也很方便了:

#include "coroutine.h"

#include <iostream>

using namespace std;

CoroutineScheduler* sched = NULL;

void func1(void* arg)
{
uintptr_t ret;
cout << "function1 a now!,arg:" << arg << ", start to yield." << endl;
ret = sched->Yield((uintptr_t)"func1 yield 1");
cout << "1.fun1 return from yield:" << (const char*)ret << endl;
ret = sched->Yield((uintptr_t)"func1 yield 2");
cout << "2.fun1 return from yield:" << (const char*)ret << ", going to stop" << endl; } void func2(void* s)
{
cout << "function2 a now!, arg:" << s << ", start to yield." << endl;
const char* y = (const char*)sched->Yield((uintptr_t)"func2 yield 1");
cout << "fun2 return from yield:" << y <<", going to stop" << endl;
} int main()
{
sched = new CoroutineScheduler(); bool stop = false;
int f1 = sched->CreateCoroutine(func1, (void*));
int f2 = sched->CreateCoroutine(func2, (void*)); while (!stop)
{
stop = true;
if (sched->IsCoroutineAlive(f1))
{
stop = false;
const char* y1 = (const char*)sched->ResumeCoroutine(f1, (uintptr_t)"resume func1");
cout << "func1 yield:" << y1 << endl;
} if (sched->IsCoroutineAlive(f2))
{
stop = false;
const char* y2 = (const char*)sched->ResumeCoroutine(f2, (uintptr_t)"resume func2");
cout << "func2 yield:" << y2 << endl;
}
} delete sched;
return ;
}

如上所示,Yield 里传的参数会在调用 Resume 时被返回,同理 Resume 里的第二个参数,会在 Yield 里被返回,这种机制也是模仿 Lua 来的,有些时候可以用来在协程间传递一些参数,很方便,看起来也挺酷的,但在实现上却相当地简洁,核心代码如下:

// static function
void CoroutineScheduler::SchedulerImpl::Schedule(void* arg)
{
assert(arg);
SchedulerImpl* sched = (SchedulerImpl*) arg; int running = sched->running_; coroutine* cor = sched->id2routine_[running];
assert(cor); cor->func(cor->arg); sched->running_ = -;
cor->status = CO_FINISHED;
} // resume coroutine.
uintptr_t CoroutineScheduler::SchedulerImpl::ResumeCoroutine(int id, uintptr_t y)
{
coroutine* cor = id2routine_[id];
if (cor == NULL || cor->status == CO_RUNNING) return ; cor->yield = y;
switch (cor->status)
{
case CO_READY:
{
getcontext(&cor->cxt); cor->status = CO_RUNNING;
cor->cxt.uc_stack.ss_sp = cor->stack;
cor->cxt.uc_stack.ss_size = stacksize_;
// sucessor context.
cor->cxt.uc_link = &mainContext_; running_ = id;
makecontext(&cor->cxt, (void (*)())Schedule, , this);
swapcontext(&mainContext_, &cor->cxt);
}
break;
case CO_SUSPENDED:
{
running_ = id;
cor->status = CO_RUNNING;
swapcontext(&mainContext_, &cor->cxt);
}
break;
default:
assert();
} uintptr_t ret = cor->yield; if (running_ == - && cor->status == CO_FINISHED) DestroyCoroutine(id); return ret;
} uintptr_t CoroutineScheduler::SchedulerImpl::Yield(uintptr_t y)
{
if (running_ < ) return ; int cur = running_;
running_ = -; coroutine* cor = id2routine_[cur]; cor->yield = y;
cor->status = CO_SUSPENDED; swapcontext(&cor->cxt, &mainContext_);
return cor->yield;
}

单就代码量和程序结构而言,以上的实现很简洁,但细节上看,每个协程都要分配一个一定大小的栈空间,空间效率上可能不大好,不够轻量;运行效率上来说,swapcontext 的执行效率如何,现在也未知,只是出于学习的目的,就先这样吧,可以再了解了解别人是怎么做的。

最新文章

  1. 多线程获取不到HttpContext
  2. Graph Valid Tree
  3. javascript--关于错误
  4. 5 Ways to Learn and Remember Absolutely Anything
  5. Ado.net利用反射执行SQL得到实体
  6. JavaScript操作DOM的那些坑
  7. 动态链接库中分配内存引起的问题-- windows已在XX.exe中触发一个断点
  8. 第9条:覆盖equals时总要覆盖hashCode
  9. money 和 smallmoney
  10. DEDECMS 猜你喜欢
  11. 14.6.3.4 Configuring InnoDB Buffer Pool Prefetching (Read-Ahead) 配置InnoDB Buffer pool 预取
  12. Activiti工作流学习-----基于5.19.0版本(3)
  13. 修改mysql的默认字符集
  14. Redhat 5.8系统安装R语言作Arima模型预测
  15. [转]HTTPS连接的前几毫秒发生了什么
  16. 布局display属性(一)--【Flex】
  17. 修改host,上github
  18. keybd_event 被 SendInput 替代
  19. PReLU——Delving Deep into Rectifiers: Surpassing Human-Level Performance on ImageNet Classification
  20. git-定制属于你的log格式

热门文章

  1. 29 _react-router说明
  2. MFC笔记4
  3. 1、python环境安装及软件介绍
  4. yii2.0如何优化路由
  5. NLTK 统计词频
  6. redis在windows系统下的安装和两个问题
  7. AltiumDesigner印制导线的走向及形状
  8. Web常见安全漏洞-SQL注入
  9. oracle数据库卸数及ddl导出
  10. POJ 3107.Godfather 树形dp