之前看協程相關的東西時,曾一念而過想著怎麼自己來實現一個給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[2048]; 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, 0); cout << "in main, before coroutine starts" << endl; // æ§è¡func. swapcontext(&ctx_main, &ctx); cout << "back to main" << endl; // 继ç»æ§è¡func. swapcontext(&ctx_main, &ctx); cout << "back to main again" << endl; return 0; } 復制代碼 如上代碼所示,顯然我們只要簡單包裝一下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*)111); int f2 = sched->CreateCoroutine(func2, (void*)222); 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 0; } 復制代碼 如上所示,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_ = -1; 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 0; 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, 1, this); swapcontext(&mainContext_, &cor->cxt); } break; case CO_SUSPENDED: { running_ = id; cor->status = CO_RUNNING; swapcontext(&mainContext_, &cor->cxt); } break; default: assert(0); } uintptr_t ret = cor->yield; if (running_ == -1 && cor->status == CO_FINISHED) DestroyCoroutine(id); return ret; } uintptr_t CoroutineScheduler::SchedulerImpl::Yield(uintptr_t y) { if (running_ < 0) return 0; int cur = running_; running_ = -1; coroutine* cor = id2routine_[cur]; cor->yield = y; cor->status = CO_SUSPENDED; swapcontext(&cor->cxt, &mainContext_); return cor->yield; }