一種協程的 C/C++ 實現
介紹
在前幾天接觸到了協程的概念,覺得很有趣。因為我可以使用一個線程來實現一個類似多線程的程序,如果使用協程來替代線程,就可以省去很多原子操作和內存柵欄的麻煩,大大減少與線程同步相關的系統調用。因為我只有一個線程,而且協程之間的切換是可以由函數自己決定的。
我有見過幾種協程的實現,因為沒有 C/C++ 的原生支持,所以多數的庫使用了匯編代碼,還有些庫利用了 C 語言的 setjmp
和 longjmp
但是要求函數里面使用 static local 的變量來保存協程內部的數據。我討厭寫匯編和使用 static local 變量,所以想出了一種稍微優雅一點又有點奇技淫巧的實現方法。 這篇文章將向你展示這種方法基本原理和實現。
基本原理
用 C/C++ 實現的最大困難就是創建,保存和恢復程序的上下文。因為這涉及到了程序棧的管理,以及 CPU 寄存器的訪問,但是這兩項內容在 C/C++ 標准里面都沒有嚴格的定義,所以我們是不可能有一個完全跨平台的 C/C++ 實現的。但是利用操作系統提供的 API,我們仍然可以避免使用匯編代碼,接下來會向你展示使用 POSIX 的 pthread 實現的一種簡單的協程框架。什么!??Pthread?那你的程序豈不是多線程了?那還叫協程嗎!沒錯,確實是多線程的,不過僅僅是在協程被創建之前的短暫瞬間。
要創建子程序的上下文,我們可以調用 pthread_create
函數來創建一個真正的線程,這樣操作系統就會幫我們創建上下文(這里包括初始化 CPU 寄存器和程序棧)。然后在線程啟動時,使用 C 語言的 setjmp
把這些寄存器備份到外部的 buffer 里面。創建完后,這個線程便失去了它的存在價值,所以可以果斷干掉它了。不過還需要注意一點,就是在創建線程之前,需要調用 pthread_attr_setstack
函數來顯式地聲明使用的程序棧,這樣線程退出的時候,系統就不會自動銷毀這個程序棧。至於上下文的恢復,顯然就是使用 longjmp
函數了。
創建上下文
下面是 RoutineInfo 的定義。為了簡單起見,所有錯誤處理代碼都被省略了,原版本的代碼在 coroutine.cpp
文件中,省略版的代碼在 coroutine_demonstration.cpp
文件中。
typedef void * (*RoutineHandler)(void*);
struct RoutineInfo{
void * param;
RoutineHandler handler;
void * ret;
bool stopped;
jmp_buf buf;
void *stackbase;
size_t stacksize;
pthread_attr_t attr;
// size: the stack size
RoutineInfo(size_t size){
param = NULL;
handler = NULL;
ret = NULL;
stopped = false;
stackbase = malloc(size);
stacksize = size;
pthread_attr_init(&attr);
if(stacksize)
pthread_attr_setstack(&attr,stackbase,stacksize);
}
~RoutineInfo(){
pthread_attr_destroy(&attr);
free(stackbase);
}
};
然后,我們需要一下全局的列表來保存這些 RoutineInfo 對象。
std::list<RoutineInfo*> InitRoutines(){
std::list<RoutineInfo*> list;
RoutineInfo *main = new RoutineInfo(0);
list.push_back(main);
return list;
}
std::list<RoutineInfo*> routines = InitRoutines();
接下來是協程的創建,注意當協程的時候,程序棧有可能已經被損壞了,所以需要一個 stackBack
作為程序棧的備份,用來做后面的恢復。
void *stackBackup = NULL;
void *CoroutineStart(void *pRoutineInfo);
int CreateCoroutine(RoutineHandler handler,void* param ){
RoutineInfo* info = new RoutineInfo(PTHREAD_STACK_MIN+ 0x4000);
info->param = param;
info->handler = handler;
pthread_t thread;
int ret = pthread_create( &thread, &(info->attr), CoroutineStart, info);
void* status;
pthread_join(thread,&status);
memcpy(info->stackbase,stackBackup,info->stacksize); // restore the stack
routines.push_back(info); // add the routine to the end of the list
return 0;
}
然后是 CoroutinneStart 函數。當線程進入這個函數的時候,使用 setjmp
保存上下文,然后備份它自己的程序棧,然后直接退出線程。
void Switch();
void *CoroutineStart(void *pRoutineInfo){
RoutineInfo& info = *(RoutineInfo*)pRoutineInfo;
if( !setjmp(info.buf)){
// back up the stack, and then exit
stackBackup = realloc(stackBackup,info.stacksize);
memcpy(stackBackup,info.stackbase, info.stacksize);
pthread_exit(NULL);
return (void*)0;
}
info.ret = info.handler(info.param);
info.stopped = true;
Switch(); // never return
return (void*)0; // suppress compiler warning
}
上下文切換
一個協程主動調用 Switch()
函數,才切換到另一個協程。
std::list<RoutineInfo*> stoppedRoutines = std::list<RoutineInfo*>();
void Switch(){
RoutineInfo* current = routines.front();
routines.pop_front();
if(current->stopped){
// The stack is stored in the RoutineInfo object,
// delete the object later, now know
stoppedRoutines.push_back(current);
longjmp( (*routines.begin())->buf ,1);
}
routines.push_back(current); // adjust the routines to the end of list
if( !setjmp(current->buf) ){
longjmp( (*routines.begin())->buf ,1);
}
if(stoppedRoutines.size()){
delete stoppedRoutines.front();
stoppedRoutines.pop_front();
}
}
演示
用戶的代碼很簡單,就像使用一個線程庫一樣,一個協程主動調用 Switch()
函數主動讓出 CPU 時間給另一個協程。
#include <iostream>
using namespace std;
#include <sys/wait.h>
void* foo(void*){
for(int i=0; i<2; ++i){
cout<<"foo: "<<i<<endl;
sleep(1);
Switch();
}
}
int main(){
CreateCoroutine(foo,NULL);
for(int i=0; i<6; ++i){
cout<<"main: "<<i<<endl;
sleep(1);
Switch();
}
}
記得在鏈接的時候加上 -lpthread
鏈接選項。程序的執行結果如下所示:
[roxma@VM_6_207_centos coroutine]$ g++ coroutime_demonstration.cpp -lpthread -o a.out
[roxma@VM_6_207_centos coroutine]$ ls
a.out coroutime.cpp coroutime_demonstration.cpp README.md
[roxma@VM_6_207_centos coroutine]$ ./a.out
main: 0
foo: 0
main: 1
foo: 1
main: 2
main: 3
main: 4
main: 5
原文及代碼下載
https://github.com/roxma/cpp_learn/tree/master/cpp/linux_programming/coroutine