int main(int argc, char **argv) { long counter = atol(argv[1]); int stacksize = atoi(argv[2]); printf("total:%ld, stackszie:%dKB\n", counter, stacksize); coro_stack_alloc(&mainstack, stacksize*1024); coro_create(&mainctx, NULL, NULL, mainstack.sptr, mainstack.ssze); int i; for(i=0; i<INX; i++) { index[i] = i; coro_stack_alloc(&stack[i], stacksize*1024); coro_create(&ctx[i], coro_body, &index[i], stack[i].sptr, stack[i].ssze); } while(1) { coro_transfer(&mainctx, &ctx[0]); count++; if(count > counter) { break; } } printf("switch count:%ld\n", count); return 0; }
int main(int argc, char **argv) { coro_create(&mainctx, NULL, NULL, NULL, 0); coro_stack_alloc(&stack, 0); coro_create(&ctx, coro_body, NULL, stack.sptr, stack.ssze); printf("Created a coro\n"); coro_transfer(&mainctx, &ctx); printf("Back in main\n"); coro_transfer(&mainctx, &ctx); printf("Back in main again\n"); return 0; }
void uvc_create(char *name, unsigned int size, coro_func func, void *arg){ uvc_ctx *ctx = (uvc_ctx *)malloc(sizeof(uvc_ctx)); memset(ctx, 0, sizeof(uvc_ctx)); //ctx->data=arg; coro_stack_alloc(&ctx->stack, size); coro_create(&ctx->cur, func, arg, ctx->stack.sptr, ctx->stack.ssze); if (name == NULL || strlen(name) == 0){ sprintf(ctx->name, "coro"); } else{ sprintf(ctx->name, name); } uvc_ready(ctx); //uvc_resume(ctx); return; }
static uvc_thread_env *uvc_get_env(){ uvc_ctx *ctx = NULL; uv_once(&once,uvc_init); uvc_thread_env *env=(uvc_thread_env *)uv_key_get(&uvc_key); if(env==NULL){ env=(uvc_thread_env *)malloc(sizeof(uvc_thread_env)); memset(env,0,sizeof(uvc_thread_env)); env->loop = uv_loop_new(); queue_init(&env->pending_queue); queue_init(&env->ready_queue); ctx = (uvc_ctx *)malloc(sizeof(uvc_ctx)); memset(ctx, 0, sizeof(uvc_ctx)); coro_stack_alloc(&ctx->stack, 0); coro_create(&ctx->cur, NULL, NULL, ctx->stack.sptr, ctx->stack.ssze); sprintf(ctx->name, "ROOT"); env->schedule_task = ctx; env->runing_task = ctx; ctx->status = UVC_STATUS_RUNING; uv_key_set(&uvc_key,env); } return env; }