예제 #1
0
//------------------------------------------------------------------------------
tOplkError timeru_modifyTimer(tTimerHdl* pTimerHdl_p, ULONG timeInMs_p, tTimerArg argument_p)
{
    tOplkError          ret = kErrorOk;
    tTimeruData*        pData;

    // check pointer to handle
    if (pTimerHdl_p == NULL)
        return kErrorTimerInvalidHandle;

    // check handle itself, i.e. was the handle initialized before
    if (*pTimerHdl_p == 0)
    {
        return timeru_setTimer(pTimerHdl_p, timeInMs_p, argument_p);
    }
    pData = (tTimeruData*)*pTimerHdl_p;
    if ((tTimeruData*)pData->timer.data != pData)
        return kErrorTimerInvalidHandle;

    mod_timer(&pData->timer, (jiffies + 1 + ((timeInMs_p * HZ) + 999) / 1000));

    // copy the TimerArg after the timer is restarted,
    // so that a timer occurred immediately before mod_timer
    // won't use the new TimerArg and
    // therefore the old timer cannot be distinguished from the new one.
    // But if the new timer is too fast, it may get lost.
    OPLK_MEMCPY(&pData->timerArgument, &argument_p, sizeof(tTimerArg));

    // check if timer is really running
    if (timer_pending(&pData->timer) == 0)
    {   // timer is not running
        // retry starting it
        add_timer(&pData->timer);
    }
    return ret;
}
//------------------------------------------------------------------------------
tOplkError timeru_modifyTimer(tTimerHdl* pTimerHdl_p, ULONG timeInMs_p, tTimerArg argument_p)
{
    tOplkError      ret;

    ret = timeru_deleteTimer(pTimerHdl_p);
    if (ret != kErrorOk)
        return ret;

    ret = timeru_setTimer(pTimerHdl_p, timeInMs_p, argument_p);
    return ret;
}
//------------------------------------------------------------------------------
tOplkError timeru_modifyTimer(tTimerHdl* pTimerHdl_p,
                              ULONG timeInMs_p,
                              const tTimerArg* pArgument_p)
{
    tTimeruData*        pData;
    struct itimerspec   relTime;

    if (pTimerHdl_p == NULL)
        return kErrorTimerInvalidHandle;

    // check handle itself, i.e. was the handle initialized before
    if (*pTimerHdl_p == 0)
        return timeru_setTimer(pTimerHdl_p, timeInMs_p, pArgument_p);

    pData = (tTimeruData*)*pTimerHdl_p;

    if (timeInMs_p >= 1000)
    {
        relTime.it_value.tv_sec = (timeInMs_p / 1000);
        relTime.it_value.tv_nsec = (timeInMs_p % 1000) * 1000000;
    }
    else
    {
        relTime.it_value.tv_sec = 0;
        relTime.it_value.tv_nsec = timeInMs_p * 1000000;
    }

    DEBUG_LVL_TIMERU_TRACE("%s() Modify timer:%08x timeInMs_p=%ld\n",
                           __func__,
                           *pTimerHdl_p,
                           timeInMs_p);

    relTime.it_interval.tv_sec = 0;
    relTime.it_interval.tv_nsec = 0;

    if (hrtimer_settime(pData->timer, 0, &relTime, NULL) != 0)
    {
        DEBUG_LVL_ERROR_TRACE("%s() Error timer_settime!\n", __func__);
        return kErrorTimerNoTimerCreated;
    }

    // copy the TimerArg after the timer is restarted,
    // so that a timer occurred immediately before hrtimer_settime
    // won't use the new timerArg and
    // therefore the old timer cannot be distinguished from the new one.
    // But if the new timer is too fast, it may get lost.
    OPLK_MEMCPY(&pData->timerArg, pArgument_p, sizeof(tTimerArg));

    return kErrorOk;
}