Exemplo n.º 1
0
tb_timer_task_ref_t tb_timer_task_init_after(tb_timer_ref_t timer, tb_hize_t after, tb_size_t period, tb_bool_t repeat, tb_timer_task_func_t func, tb_cpointer_t priv)
{
    // check
    tb_timer_impl_t* impl = (tb_timer_impl_t*)timer;
    tb_assert_and_check_return_val(impl && func, tb_null);

    // add task
    return tb_timer_task_init_at(timer, tb_timer_now(impl) + after, period, repeat, func, priv);
}
Exemplo n.º 2
0
static tb_long_t tb_aiop_spak_runtask(tb_aiop_ptor_impl_t* impl, tb_aice_ref_t aice)
{
    // check
    tb_assert_and_check_return_val(impl && impl->aiop && impl->ltimer && impl->timer && aice, -1);
    tb_assert_and_check_return_val(aice->code == TB_AICE_CODE_RUNTASK, -1);
    tb_assert_and_check_return_val(aice->u.runtask.when, -1);

    // the aico
    tb_aiop_aico_t* aico = (tb_aiop_aico_t*)aice->aico;
    tb_assert_and_check_return_val(aico && !aico->task, -1);

    // now
    tb_hong_t now = tb_cache_time_mclock();

    // timeout?
    tb_long_t ok = -1;
    if (aice->u.runtask.when <= now)
    {
        // trace
        tb_trace_d("runtask: when: %llu, now: %lld: ok", aice->u.runtask.when, now);

        // ok
        aice->state = TB_STATE_OK;
        ok = 1;
    }
    else
    {
        // trace
        tb_trace_d("runtask: when: %llu, now: %lld: ..", aice->u.runtask.when, now);

        // wait it
        aico->aice = *aice;
        aico->waiting = 1;

        // add timeout task, is the higher precision timer?
        if (aico->base.handle)
        {
            // the top when
            tb_hize_t top = tb_timer_top(impl->timer);

            // add task
            aico->task = tb_timer_task_init_at(impl->timer, aice->u.runtask.when, 0, tb_false, tb_aiop_spak_runtask_timeout, aico);
            aico->bltimer = 0;

            // the top task is changed? spak aiop
            if (aico->task && aice->u.runtask.when < top)
                tb_aiop_spak(impl->aiop);
        }
        else
        {
            aico->task = tb_ltimer_task_init_at(impl->ltimer, aice->u.runtask.when, 0, tb_false, tb_aiop_spak_runtask_timeout, aico);
            aico->bltimer = 1;
        }

        // wait
        ok = 0;
    }

    // ok
    return ok;
}