Beispiel #1
0
Datei: aiop.c Projekt: cdrr/tbox
/* //////////////////////////////////////////////////////////////////////////////////////
 * implementation
 */
tb_aiop_ref_t tb_aiop_init(tb_size_t maxn)
{
    // check
    tb_assert_and_check_return_val(maxn, tb_null);

    // done
    tb_bool_t       ok = tb_false;
    tb_aiop_impl_t* impl = tb_null;
    do
    {
        // make impl
        impl = tb_malloc0_type(tb_aiop_impl_t);
        tb_assert_and_check_break(impl);

        // init impl
        impl->maxn = maxn;

        // init lock
        if (!tb_spinlock_init(&impl->lock)) break;

        // init pool
        impl->pool = tb_fixed_pool_init(tb_null, (maxn >> 4) + 16, sizeof(tb_aioo_impl_t), tb_null, tb_null, tb_null);
        tb_assert_and_check_break(impl->pool);

        // init spak
        if (!tb_socket_pair(TB_SOCKET_TYPE_TCP, impl->spak)) break;

        // init reactor
        impl->rtor = tb_aiop_rtor_impl_init(impl);
        tb_assert_and_check_break(impl->rtor);

        // addo spak
        if (!tb_aiop_addo((tb_aiop_ref_t)impl, impl->spak[1], TB_AIOE_CODE_RECV, tb_null)) break;  

        // register lock profiler
#ifdef TB_LOCK_PROFILER_ENABLE
        tb_lock_profiler_register(tb_lock_profiler(), (tb_pointer_t)&impl->lock, TB_TRACE_MODULE_NAME);
#endif

        // ok
        ok = tb_true;

    } while (0);

    // failed?
    if (!ok)
    {
        // exit it
        if (impl) tb_aiop_exit((tb_aiop_ref_t)impl);
        impl = tb_null;
    }

    // ok?
    return (tb_aiop_ref_t)impl;
}
Beispiel #2
0
/* //////////////////////////////////////////////////////////////////////////////////////
 * implementation
 */
tb_poller_ref_t tb_poller_init(tb_cpointer_t priv)
{
    // done
    tb_bool_t               ok = tb_false;
    tb_poller_poll_ref_t    poller = tb_null;
    do
    {
        // make poller
        poller = tb_malloc0_type(tb_poller_poll_t);
        tb_assert_and_check_break(poller);

        // init poll fds
        poller->pfds = tb_vector_init(0, tb_element_mem(sizeof(struct pollfd), tb_null, tb_null));
        tb_assert_and_check_break(poller->pfds);

        // init copied poll fds
        poller->cfds = tb_vector_init(0, tb_element_mem(sizeof(struct pollfd), tb_null, tb_null));
        tb_assert_and_check_break(poller->cfds);

        // init user private data
        poller->priv = priv;

        // init pair sockets
        if (!tb_socket_pair(TB_SOCKET_TYPE_TCP, poller->pair)) break;

        // insert pair socket first
        if (!tb_poller_insert((tb_poller_ref_t)poller, poller->pair[1], TB_POLLER_EVENT_RECV, tb_null)) break;  

        // ok
        ok = tb_true;

    } while (0);

    // failed?
    if (!ok)
    {
        // exit it
        if (poller) tb_poller_exit((tb_poller_ref_t)poller);
        poller = tb_null;
    }

    // ok?
    return (tb_poller_ref_t)poller;
}