void coroutine_close(struct schedule *S) { int i; for (i=0;i<S->cap;i++) { struct coroutine * co = S->co[i]; if (co) { _co_delete(co); } } free(S->co); S->co = NULL; free(S); }
static void WINAPI fiberProc(void *fiber_nbr) { struct schedule *S = (struct schedule *)fiber_nbr; int id = S->running; struct coroutine *C = S->co[id]; C->func(S,C->ud); _co_delete(C); S->co[id] = NULL; --S->nco; S->running = -1; SwitchToFiber(S->main); }
static void mainfunc(uint32_t low32, uint32_t hi32) { uintptr_t ptr = (uintptr_t)low32 | ((uintptr_t)hi32 << 32); struct schedule *S = (struct schedule *)ptr; int id = S->running; struct coroutine *C = S->co[id]; // 得到对应的coroutine C->func(S, C->ud); // 运行对应的函数, ud是用户传入的参数 _co_delete(C); // 如果运行完成了,就删除这个coroutine S->co[id] = NULL; --S->nco; S->running = -1; }
static void mainfunc(uint32_t low32, uint32_t hi32) { uintptr_t ptr = (uintptr_t)low32 | ((uintptr_t)hi32 << 32); struct schedule *S = (struct schedule *)ptr; int id = S->running; struct coroutine *C = S->co[id]; C->func(S,C->ud); _co_delete(C); S->co[id] = NULL; --S->nco; S->running = -1; }
// coroutine_close的时候是强制关闭的,可能部分coroutine还未执行完 void coroutine_close(struct schedule *sched) { int i; for (i=0;i<sched->cap;i++) { struct coroutine * co = sched->co[i]; if (co) { _co_delete(co); } } free(sched->co); sched->co = NULL; free(sched); }
int coroutine_delete( struct schedule * sched, int id ) { assert(id>=0 && id < sched->cap); struct coroutine *C = sched->co[id]; if (NULL == C) { return -1; } C->callbacks.ondelete_(sched, C->callbacks.ud_); _co_delete(C); sched->co[id] = NULL; --sched->nco; return 0; }
//关闭schedule,释放所有的coroutine void coroutine_close(struct schedule *S) { printf("%s:%s\n", __FILE__, __FUNCTION__); int i; for (i=0;i<S->cap;i++) { struct coroutine * co = S->co[i]; if (co) { _co_delete(co); } } free(S->co); S->co = NULL; free(S); }
//从这里,调用coroutine的callback,当callback()返回后,此coroutine即结束 static void mainfunc(uint32_t low32, uint32_t hi32) { printf("%s:%s\n", __FILE__, __FUNCTION__); //为啥整了个这样的东东, ptr -- schedule* uintptr_t ptr = (uintptr_t)low32 | ((uintptr_t)hi32 << 32); struct schedule *S = (struct schedule *)ptr; int id = S->running;//可能-1吗, 目前是调用此函数前就设置了此次croutine id struct coroutine *C = S->co[id]; C->func(S,C->ud); //调用coroutine的callback(?) //当func()调用结束后,此co即可宣告死亡 _co_delete(C); S->co[id] = NULL; --S->nco; S->running = -1; //-1代表当前没有coroutine在执行, 在这个状态下,执行流(将)交给主线程(main) }