协程学习笔记二
协程(二)
ucontext
setjmp和ucontext哪个更好
https://stackoverflow.com/questions/5536913/c-setjmp-h-and-ucontext-h-which-is-better
https://stackoverflow.com/questions/15014647/why-was-ucontext-added-to-and-then-removed-from-posix
代码学习
风云写的协程代码(不搬运了)
https://github.com/cloudwu/coroutine/
站在巨人肩膀
- 将风云的手动resume,改为loop
- 发现风云的_save_stack()在编译参数-On下是有问题的,我改为整段stack保存
- 我在树莓派的Makefile中运行的时候,要么coredump,要么栈错位,百思不得其解,还以为ucontext在aarch64兼容不好。其实就是上一个问题,我加了-O3,真是运气。然后发现了个问题,stack的占用不小,远远大于_save_stack()计算的差值,虽然在swapcontext前,只拷贝那一小段,但是stack之后的数据会自己填充。(至于为什么,我也不清楚,寻找答案中…)
- 关于makecontext后面的可变参数类型,虽然说是int,但64位的平台也支持指针
https://man7.org/linux/man-pages/man3/swapcontext.3.html
On architectures where int and pointer types are the same size
(e.g., x86-32, where both types are 32 bits), you may be able to
get away with passing pointers as arguments to makecontext()
following argc. However, doing this is not guaranteed to be
portable, is undefined according to the standards, and won't work
on architectures where pointers are larger than ints.
Nevertheless, starting with version 2.8, glibc makes some changes
to makecontext(), to permit this on some 64-bit architectures
(e.g., x86-64).
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ucontext.h>
#include <stdint.h>
#include <assert.h>
#include "queue.h"
#define COROUTINE_DEAD 0
#define COROUTINE_INIT 1
#define COROUTINE_READY 2
struct _schedule;
struct _coroutine;
typedef void (*coroutine_func)(struct _schedule*,void*);
typedef struct _schedule{
ucontext_t main;
uint64_t nco;
queue_t ready;
size_t size;
unsigned char stack[0];
}schedule;
typedef struct{
coroutine_func func;
void *arg;
int status;
ucontext_t ctx;
queue_t node;
unsigned char stack[0];
}coroutine;
schedule * coroutine_open(size_t size) {
schedule *S = malloc(sizeof(schedule) + size);// size不可过小
S->nco = 0;
S->size = size;
queue_init(&S->ready);
return S;
}
int coroutine_new(schedule *S, coroutine_func func, void *arg){
coroutine *co = malloc(sizeof(coroutine) + S->size);
co->func = func;
co->arg = arg;
co->status = COROUTINE_INIT;
queue_init(&co->node);
queue_insert_tail(&S->ready,&co->node);
S->nco++;
return 0;
}
// void
// _save_stack(struct coroutine *C, char *top) {
// char dummy = 0;
// assert(top - &dummy <= STACK_SIZE);
// printf("store %lu\n",top - &dummy);
// if(top - &dummy > C->stackcap){
// free(C->stack);
// C->stackcap = top - &dummy;
// C->stack = malloc(C->stackcap);
// }
// C->stacksize = top - &dummy;
// memcpy(C->stack, &dummy, C->stacksize);
// }
void coroutine_yield( schedule * S){
queue_t *node = queue_head(&S->ready);
coroutine *co = queue_data(node,coroutine,node);
queue_remove(&co->node);
// _save_stack(co,S->stack+STACK_SIZE); // 实际数据不只这么少,可以打印S的stack看看,但是swapcontext后却会填充...
memcpy(co->stack,S->stack,S->size);
co->status = COROUTINE_READY;
queue_insert_tail(&S->ready,&co->node);
swapcontext(&co->ctx,&S->main);
}
void resume_handle(schedule *S, coroutine *co){
co->func(S,co->arg);
queue_remove(&co->node);
co->status = COROUTINE_DEAD;
queue_insert_tail(&S->ready,&co->node);
}
int coroutine_loop(schedule *S){
while(S->nco){
queue_t *node = queue_head(&S->ready);
coroutine *co = queue_data(node,coroutine,node);
switch (co->status)
{
case COROUTINE_INIT:
getcontext(&co->ctx);
co->ctx.uc_stack.ss_sp = S->stack;
co->ctx.uc_stack.ss_size = S->size;
co->ctx.uc_link = &S->main;
makecontext(&co->ctx,(void (*)(void))resume_handle,2,S,co);
swapcontext(&S->main,&co->ctx);
break;
case COROUTINE_READY:
memcpy(S->stack,co->stack,S->size);
swapcontext(&S->main,&co->ctx);
break;
default:
queue_remove(&co->node);
free(co);
S->nco--;
break;
}
}
return 0;
}
void coroutine_close(schedule *S) {
free(S);
}
void func1(schedule* S,void* arg){
int i;
for(i=0;i<3;i++){
printf("%s %d\n",__FUNCTION__,i);
coroutine_yield(S);
}
}
void func2(schedule* S,void* arg){
int i;
for(i=0;i<5;i++){
printf("%s %d\n",__FUNCTION__,i);
coroutine_yield(S);
}
}
int main(){
schedule * sched = coroutine_open(1024*1024);
coroutine_new(sched,func1,NULL);
coroutine_new(sched,func2,NULL);
coroutine_loop(sched);
coroutine_close(sched);
return 0;
}
未完待续
看看怎么整合epoll…