/* ////////////////////////////////////////////////////////////////////////////////////// * main */ tb_int_t tb_demo_coroutine_file_client_main(tb_int_t argc, tb_char_t** argv) { // check tb_assert_and_check_return_val(argc == 2 && argv[1], -1); // the coroutines count tb_size_t count = tb_atoi(argv[1]); // init scheduler tb_co_scheduler_ref_t scheduler = tb_co_scheduler_init(); if (scheduler) { // start file tb_size_t i = 0; for (i = 0; i < count; i++) { // start it tb_coroutine_start(scheduler, tb_demo_coroutine_pull, tb_null, 0); } // run scheduler tb_co_scheduler_loop(scheduler, tb_true); // exit scheduler tb_co_scheduler_exit(scheduler); } // end return 0; }
/* ////////////////////////////////////////////////////////////////////////////////////// * main */ tb_int_t tb_demo_coroutine_semaphore_main(tb_int_t argc, tb_char_t** argv) { // init scheduler tb_co_scheduler_ref_t scheduler = tb_co_scheduler_init(); if (scheduler) { // init semaphore tb_co_semaphore_ref_t semaphore = tb_co_semaphore_init(0); tb_assert(semaphore); // start coroutines tb_coroutine_start(scheduler, tb_demo_coroutine_semaphore_wait_func, semaphore, 0); tb_coroutine_start(scheduler, tb_demo_coroutine_semaphore_wait_func, semaphore, 0); tb_coroutine_start(scheduler, tb_demo_coroutine_semaphore_wait_func, semaphore, 0); tb_coroutine_start(scheduler, tb_demo_coroutine_semaphore_wait_func, semaphore, 0); tb_coroutine_start(scheduler, tb_demo_coroutine_semaphore_post_func, semaphore, 0); // run scheduler tb_co_scheduler_loop(scheduler, tb_true); // exit semaphore tb_co_semaphore_exit(semaphore); // exit scheduler tb_co_scheduler_exit(scheduler); } return 0; }
static tb_int_t tb_demo_coroutine_worker(tb_cpointer_t priv) { // init scheduler tb_co_scheduler_ref_t scheduler = tb_co_scheduler_init(); if (scheduler) { // start coroutines tb_coroutine_start(scheduler, tb_demo_coroutine_listen, priv, 0); // run scheduler, enable exclusive mode if be only one cpu tb_co_scheduler_loop(scheduler, TB_DEMO_CPU == 1); // exit scheduler tb_co_scheduler_exit(scheduler); } return 0; }
/* ////////////////////////////////////////////////////////////////////////////////////// * main */ tb_int_t tb_demo_coroutine_nest_main(tb_int_t argc, tb_char_t** argv) { // init scheduler tb_co_scheduler_ref_t scheduler = tb_co_scheduler_init(); if (scheduler) { // start coroutines tb_coroutine_start(scheduler, tb_demo_coroutine_nest_func, (tb_cpointer_t)10, 0); tb_coroutine_start(scheduler, tb_demo_coroutine_nest_func, (tb_cpointer_t)10, 0); // run scheduler tb_co_scheduler_loop(scheduler, tb_true); // exit scheduler tb_co_scheduler_exit(scheduler); } return 0; }