Пример #1
0
/* 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);
}
Пример #2
0
int main(void)
{
    ConcurrentContext *context;
    unsigned char *stack;
    const int stack_size = 1024 * 4;

    concurrent_init();
    context = malloc(ctx_sizeof());
    stack = malloc(sizeof(*stack) * stack_size);
    ctx_construct(context, stack, stack_size, generator, NULL);
    printf("generate: %s\n", (char *)gen_voidptr(context));
    printf("generate: %s\n", (char *)gen_voidptr(context));
    printf("generate: %s\n", (char *)gen_voidptr(context));
    printf("generate: %s\n", (char *)gen_voidptr(context));
    ctx_reset(context);
    printf("generate: %s\n", (char *)gen_voidptr(context));
    printf("generate: %s\n", (char *)gen_voidptr(context));
    printf("generate: %s\n", (char *)gen_voidptr(context));
    printf("generate: %s\n", (char *)gen_voidptr(context));
    ctx_destruct(context);

    free(stack);
    free(context);
    concurrent_fin();
    return 0;
}
Пример #3
0
int main(void)
{
    int i;

    srand(time(NULL));
    concurrent_init();
    for (i = 0; i < 1000; i++) {
        int stack_size = 1024 + 16 + (rand() % 16);
        int torture_offset = rand() % 16;
        ConcurrentContext *context;
        unsigned char *stack;
        ConcurrentStatus status;

        context = malloc(ctx_sizeof());
        stack = malloc(sizeof(*stack) * stack_size);
        status = ctx_construct(context,
                               stack + torture_offset,
                               stack_size - torture_offset,
                               test_generator, NULL);

        assert(status == ConcurrentStatus_SUCCESS);
        assert(strcmp(gen_str(context), "pong") == 0);

        ctx_destruct(context);
        free(stack);
        free(context);
    }
    concurrent_fin();
    return 0;
}
Пример #4
0
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;
}
Пример #5
0
int main(void)
{
    struct concurrent_ctx *ctx0;
    struct concurrent_ctx *ctx1;
    uint8_t *stack0;
    uint8_t *stack1;
    const size_t stack_size = 1024 * 2;

    concurrent_init();

    ctx0 = malloc(ctx_sizeof());
    stack0 = malloc(sizeof(*stack0) * stack_size);
    ctx_construct(ctx0, stack0, stack_size, task0, NULL);

    ctx1 = malloc(ctx_sizeof());
    stack1 = malloc(sizeof(*stack1) * stack_size);
    ctx_construct(ctx1, stack1, stack_size, task1, NULL);

    {
        int i;
        for (i = 0; i < 10; i++) {
            printf("main: resume task0\n");
            resume(ctx0);
            printf("main: resume task1\n");
            resume(ctx1);
        }
    }

    printf("finish\n");

    ctx_destruct(ctx1);
    free(stack1);
    free(ctx1);
    ctx_destruct(ctx0);
    free(stack0);
    free(ctx0);

    concurrent_fin();
    return EXIT_SUCCESS;
}
Пример #6
0
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;
}
Пример #7
0
int main(void)
{
    struct concurrent_ctx *ctx;
    uint8_t *stack;
    const size_t stack_size = 1024*4;
    long double f;

    concurrent_init();
    ctx = malloc(ctx_sizeof());
    stack = malloc(sizeof(*stack) * stack_size);
    ctx_construct(ctx, stack, stack_size, coro, NULL);
    for (f = 0.0f; f < 2.0f; f += 0.2f) {
        printf("main: %f\n", (double)f);
        resume(ctx);
    }
    ctx_destruct(ctx);
    free(stack);
    free(ctx);
    concurrent_fin();
    return EXIT_SUCCESS;
}