/* coroutine in coroutine recursive */ void coro(ConcurrentContext *aContext) { ConcurrentContext *context; unsigned char *stack; int deep; deep = (int)ctx_get_userptr(aContext); if (deep == 0) { return; } printf("%*scoro(deep=%d) start\n", deep, " ", deep); context = malloc(ctx_sizeof()); stack = malloc(sizeof(*stack) * STACK_SIZE); ctx_construct(context, stack, STACK_SIZE, coro, (void *)(deep - 1)); while (!ctx_is_done(context)) { printf("%*scoro(deep=%d) first\n", deep, " ", deep); ctx_resume(context); yield(aContext); } ctx_reset(context); while (!ctx_is_done(context)) { printf("%*scoro(deep=%d) second\n", deep, " ", deep); ctx_resume(context); yield(aContext); } ctx_destruct(context); free(stack); free(context); printf("%*scoro(deep=%d) finish\n", deep, " ", deep); }
int main(void) { uint8_t *stack; const int stack_size = 1024 * 4; int data[] = { 12, 13, 14, 15, 10, 9, 7, 1, 2, 11, 3, 4, 8, 6, 5, 0 }; struct sort_ctx sort_arg; sort_arg.data = data; sort_arg.num_data = sizeof(data) / sizeof(data[0]); concurrent_init(); g_context = malloc(ctx_sizeof()); stack = malloc(sizeof(*stack) * stack_size); ctx_construct(g_context, stack, stack_size, coro, (void *)&sort_arg); while (!ctx_is_done(g_context)) { size_t stack_size_used = ctx_get_stack_used(g_context); printf("stack used:%3zdbytes(%2.0f%%); data={ ", stack_size_used, ((float)stack_size_used / stack_size) * 100.0f); print_datas(&sort_arg); printf("}\n"); resume(g_context); } ctx_destruct(g_context); free(stack); free(g_context); concurrent_fin(); return EXIT_SUCCESS; }
int main(void) { ConcurrentContext *context; unsigned char *stack; concurrent_init(); context = malloc(ctx_sizeof()); stack = malloc(sizeof(*stack) * STACK_SIZE); ctx_construct(context, stack, STACK_SIZE, coro, /* deep = */(void *)3); while (!ctx_is_done(context)) { printf("main()\n"); ctx_resume(context); } ctx_destruct(context); free(stack); free(context); concurrent_fin(); printf("main() finish\n"); return 0; }