📄 ixosaltime.c
字号:
if (ixStatus != IX_SUCCESS) { ixOsalLog (IX_OSAL_LOG_LVL_ERROR, IX_OSAL_LOG_DEV_STDOUT, "Error creating timerLoop task\n", 0, 0, 0, 0, 0, 0); return IX_FAIL; } return IX_SUCCESS;}/* * This function allocates a new timer. It is used by Schedule and * ScheduleRepeating. */PRIVATE IX_STATUScreateNewTimer (IxOsalVoidFnVoidPtr func, void *param, UINT32 priority, UINT32 interval, BOOL isRepeating, UINT32 * timerId){ UINT32 i; IX_STATUS status = IX_SUCCESS; int osTicks; IxOsalTimeval timeVal; /* * Check if callback is NULL */ if (func == NULL) { ixOsalLog (IX_OSAL_LOG_LVL_ERROR, IX_OSAL_LOG_DEV_STDOUT, "client registered a NULL callback function\n", 0, 0, 0, 0, 0, 0); return IX_FAIL; } osTicks = ixOsalSysClockRateGet (); /* * Figure out how many milisecond per tick and compare against interval */ if (interval < (UINT32) (1000 / osTicks)) { ixOsalLog (IX_OSAL_LOG_LVL_ERROR, IX_OSAL_LOG_DEV_STDOUT, "client requested time interval (%d) finer than clock ticks\n", interval, 0, 0, 0, 0, 0); return IX_FAIL; } /* * Increment timerId */ *timerId = ++lastTimerId; status = ixOsalSemaphoreWait (&ixOsalCriticalSectSem, IX_OSAL_WAIT_FOREVER); if (status != IX_SUCCESS) { ixOsalLog (IX_OSAL_LOG_LVL_ERROR, IX_OSAL_LOG_DEV_STDOUT, "createNewTimer fail to get semaphore \n", 0, 0, 0, 0, 0, 0); return IX_FAIL; } for (i = 0; i < IX_OSAL_MAX_TIMERS; i++) { if (!ixOsalTimers[i].inUse) { break; } } if ((i >= IX_OSAL_TIMER_WARNING_THRESHOLD) && (ixOsalThresholdErr == FALSE)) { /* * This error serves as an early indication that the number of * available timer slots will need to be increased. This is done * by increasing IX_OSAL_MAX_TIMERS */ ixOsalLog (IX_OSAL_LOG_LVL_WARNING, IX_OSAL_LOG_DEV_STDOUT, "Timer threshold reached. Only %d timer slots now available\n", IX_OSAL_MAX_TIMERS - i, 0, 0, 0, 0, 0); ixOsalThresholdErr = TRUE; } if (i == IX_OSAL_MAX_TIMERS) { /* * If you get this error, increase MAX_TIMERS above */ ixOsalLog (IX_OSAL_LOG_LVL_ERROR, IX_OSAL_LOG_DEV_STDOUT, "Out of timer slots %d used - request ignored\n", i, 0, 0, 0, 0, 0); status = IX_FAIL; } else { ixOsalTimers[i].inUse = TRUE; IX_OSAL_MS_TO_TIMEVAL (interval, &timeVal); ixOsalTimers[i].period = timeVal; ixOsalTimers[i].isRepeating = isRepeating; ixOsalTimeGet (&(ixOsalTimers[i].expires)); IX_OSAL_TIME_ADD ((ixOsalTimers[i].expires), (ixOsalTimers[i].period)) ixOsalTimers[i].priority = priority; ixOsalTimers[i].callback = func; ixOsalTimers[i].callbackParam = param; ixOsalTimers[i].id = *timerId; if ((i) >= ixOsalHigestTimeSlotUsed) { ixOsalHigestTimeSlotUsed = i + 1; } status = ixOsalSemaphorePost (&ixOsalTimerRecalcSem); if (status != IX_SUCCESS) { ixOsalLog (IX_OSAL_LOG_LVL_ERROR, IX_OSAL_LOG_DEV_STDOUT, "createNewTimer: fail to release semaphore \n", 0, 0, 0, 0, 0, 0); return IX_FAIL; } } status = ixOsalSemaphorePost (&ixOsalCriticalSectSem); if (status != IX_SUCCESS) { ixOsalLog (IX_OSAL_LOG_LVL_ERROR, IX_OSAL_LOG_DEV_STDOUT, "createNewTimer: fail to release semaphore: ixOsalCriticalSectSem \n", 0, 0, 0, 0, 0, 0); return IX_FAIL; } return status;}PRIVATE IxOsalTimerRec *evaluateTimerPriority (IxOsalTimerRec * first, IxOsalTimerRec * second){ IX_OSAL_ASSERT (first != NULL || second != NULL); if (first == NULL) { return second; } if (second == NULL) { return first; } /* * For now we just compare the values of the priority with lower * values getting higher priority. * * If someone needs to change priorities of different purposes without * modifying the order of the enums, then more code will be required * here. */ if (first->priority <= second->priority) { return first; } return second;}/* * Find the next timer about to go off. Do this by starting with a time infinitely * far in the future and successively finding better timers (ones that go off * sooner). * * If a timer is found that has already expired, the flag bestTimerExpired is * set. If another timer is found that has also expired, their respective * priorities are compared using the function evaluateTimerPriority() */PRIVATE IxOsalTimerRec *findNextTimeout (IxOsalTimeval now){ IxOsalTimeval timeoutAt = { ULONG_MAX, LONG_MAX }; UINT32 i; IxOsalTimerRec *bestTimer = NULL; BOOL bestTimerExpired = FALSE; BOOL thisTimerExpired = FALSE; for (i = 0; i < ixOsalHigestTimeSlotUsed; i++) { if (ixOsalTimers[i].inUse) { thisTimerExpired = FALSE; /* * Check if this timer has expired, * i.e. 'now' must be greater than ixOsalTimers[i].expired */ if (!IX_OSAL_TIME_GT (ixOsalTimers[i].expires, now)) { thisTimerExpired = TRUE; } /* * If more than one timer has expired, determine * which callback to call first based on a priority scheme * i.e. the bestTimer */ if ((bestTimerExpired && thisTimerExpired) || IX_OSAL_TIME_EQ (ixOsalTimers[i].expires, timeoutAt)) { bestTimer = evaluateTimerPriority (bestTimer, &ixOsalTimers[i]); timeoutAt = bestTimer->expires; } else if (IX_OSAL_TIME_LT (ixOsalTimers[i].expires, timeoutAt)) { bestTimer = &ixOsalTimers[i]; timeoutAt = ixOsalTimers[i].expires; } /* * bestTimer can not be NULL here because any timer will * have a shorter timeout than the default. */ if (!IX_OSAL_TIME_GT (bestTimer->expires, now)) { bestTimerExpired = TRUE; } } } return bestTimer;}PRIVATE voidtimerSleep (IxOsalTimerRec * nextTimer, IxOsalTimeval now){ UINT32 ticks; IxOsalTimeval temp; if (nextTimer == NULL) { ticks = IX_OSAL_WAIT_FOREVER; } else { temp.secs = nextTimer->expires.secs; temp.nsecs = nextTimer->expires.nsecs; IX_OSAL_TIME_SUB (temp, now); ticks = IX_OSAL_TIMEVAL_TO_TICKS (temp); /* * We should sleep but the period is less than a tick * * away, rounding up. */ if (ticks == 0) { ticks = 1; } } /* * Note: Status is ignored here, wait intentionally */ ixOsalSemaphoreWait (&ixOsalTimerRecalcSem, ticks);}PRIVATE voidrescheduleTimer (IxOsalTimerRec * nextTimer){ if (nextTimer->isRepeating) { IX_OSAL_TIME_ADD (nextTimer->expires, nextTimer->period); } else { nextTimer->inUse = FALSE; }}/* * Maximum time spent in a call-back is defined as 100 millisec. (one * tenth of a second). */PRIVATE voidcallTimerCallback (IxOsalVoidFnVoidPtr callback, void *callbackParam){ IxOsalTimeval timevalCbBegin, timevalCbFinish; static BOOL errorReported = FALSE; ixOsalTimerCbCnt++; ixOsalTimeGet (&timevalCbBegin); callback (callbackParam); ixOsalTimeGet (&timevalCbFinish); IX_OSAL_TIME_SUB (timevalCbFinish, timevalCbBegin); if ((timevalCbFinish.nsecs > IX_MAX_TIMER_CALLBACK_TIME) || (timevalCbFinish.secs > 0)) { if (!errorReported) { ixOsalLog (IX_OSAL_LOG_LVL_WARNING, IX_OSAL_LOG_DEV_STDOUT, "Slow timer callback - %d Secs, %d nSecs \n", timevalCbFinish.secs, timevalCbFinish.nsecs, 0, 0, 0, 0); errorReported = TRUE; } }}inttimerLoop (void){ IxOsalTimeval now; IxOsalVoidFnVoidPtr callback = NULL; void *callbackParam = NULL; IxOsalTimerRec *nextTimer; IX_STATUS status; while (1) { /* * This loop catches all cases in a simple way. If multiple * timers expire together, then lowest will be <=0 until all * have been processed and the queue get won't get invoked. */ status = ixOsalSemaphoreWait (&ixOsalCriticalSectSem, IX_OSAL_WAIT_FOREVER); if (status != IX_SUCCESS) { ixOsalLog (IX_OSAL_LOG_LVL_ERROR, IX_OSAL_LOG_DEV_STDOUT, "timerLoop fail to get semaphore \n", 0, 0, 0, 0, 0, 0); return IX_FAIL; } ixOsalTimeGet (&now); nextTimer = findNextTimeout (now); if ((nextTimer == NULL) || IX_OSAL_TIME_GT ((nextTimer->expires), (now))) { callback = NULL; } else { rescheduleTimer (nextTimer); callback = nextTimer->callback; callbackParam = nextTimer->callbackParam; } status = ixOsalSemaphorePost (&ixOsalCriticalSectSem); if (status != IX_SUCCESS) { ixOsalLog (IX_OSAL_LOG_LVL_ERROR, IX_OSAL_LOG_DEV_STDOUT, "timerLoop fail to release semaphore \n", 0, 0, 0, 0, 0, 0); return IX_FAIL; } if (callback != NULL) { callTimerCallback (callback, callbackParam); } else { timerSleep (nextTimer, now); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -