/* ////////////////////////////////////////////////////////////////////////////////////// * 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; }
/* ////////////////////////////////////////////////////////////////////////////////////// * implementation */ tb_co_scheduler_ref_t tb_co_scheduler_init() { // done tb_bool_t ok = tb_false; tb_co_scheduler_t* scheduler = tb_null; do { // make scheduler scheduler = tb_malloc0_type(tb_co_scheduler_t); tb_assert_and_check_break(scheduler); // init dead coroutines tb_list_entry_init(&scheduler->coroutines_dead, tb_coroutine_t, entry, tb_null); // init ready coroutines tb_list_entry_init(&scheduler->coroutines_ready, tb_coroutine_t, entry, tb_null); // init suspend coroutines tb_list_entry_init(&scheduler->coroutines_suspend, tb_coroutine_t, entry, tb_null); // init original coroutine scheduler->original.scheduler = (tb_co_scheduler_ref_t)scheduler; // init running scheduler->running = &scheduler->original; // ok ok = tb_true; } while (0); // failed? if (!ok) { // exit it if (scheduler) tb_co_scheduler_exit((tb_co_scheduler_ref_t)scheduler); scheduler = tb_null; } // ok? return (tb_co_scheduler_ref_t)scheduler; }