协程库的实现方式
目前有如下几种方式来实现协程库:
第一种:利用glibc的ucontext函数族。比如云风的协程库;
第二种:利用汇编语言来切换运行时上下文。比如微信的libco;
第三种:利用C语言语法switch-case来实现切换上下文。比如Protothreads;
第四种:利用C语言的setjmp和longjmp。
ucontext函数族说明
#include <ucontext.h> int getcontext(ucontext_t *ucp); int setcontext(const ucontext_t *ucp); void makecontext(ucontext_t *ucp, void (*func)(), int argc, ...); int swapcontext(ucontext_t *oucp, ucontext_t *ucp); typedef struct ucontext { struct ucontext *uc_link; sigset_t uc_sigmask; stack_t uc_stack; mcontext_t uc_mcontext; ... } ucontext_t;协程的状态切换:
参考
1.
https://blog.csdn.net/qq910894904/article/details/41911175
2.