mirror of
https://github.com/apache/nuttx.git
synced 2026-09-26 01:46:57 +08:00
sched/wdog: Refactor wdog module
This commit refactors the wdog module to use absolute time representation internally. The main improvements include: 1. Fixed recursive watchdog handling caused by calling wd_start within watchdog timeout callback function. 2. Simplified timer processing to improve performance and enhance code readability. 3. Improved accuracy of timers. 4. Reduced critical section and interrupt disable time, improving real-time performance. Signed-off-by: ouyangxiangzhen <ouyangxiangzhen@xiaomi.com> Signed-off-by: ligd <liguiding1@xiaomi.com>
This commit is contained in:
committed by
Xiang Xiao
parent
7dabc6ff2f
commit
3b111c8b99
@@ -390,6 +390,47 @@ void clock_timespec_subtract(FAR const struct timespec *ts1,
|
||||
FAR const struct timespec *ts2,
|
||||
FAR struct timespec *ts3);
|
||||
|
||||
/****************************************************************************
|
||||
* Name: clock_compare
|
||||
*
|
||||
* Description:
|
||||
* This function is used for check whether the expired time is reached.
|
||||
* It take the ticks wrap-around into consideration.
|
||||
*
|
||||
* Input Parameters:
|
||||
* tick1 - Expected time in clock ticks
|
||||
* tick2 - Current time in clock ticks
|
||||
*
|
||||
* Returned Value:
|
||||
* true - Expected ticks is timeout.
|
||||
* false - Otherwise.
|
||||
*
|
||||
* Assumptions:
|
||||
* The type of delay value should be sclock_t.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
/* clock_compare considers tick wraparound, discussed as follows:
|
||||
* Assuming clock_t is a 64-bit data type.
|
||||
*
|
||||
* Case 1: If tick2 - tick1 > 2^63, it is considered expired
|
||||
* or expired after tick2 wraparound.
|
||||
*
|
||||
* Case 2: If tick2 - tick1 <= 2^63,
|
||||
* it is considered not expired.
|
||||
*
|
||||
* For bit-63 as the sign bit, we can simplify this to:
|
||||
* (sclock_t)(tick2 - tick1) >= 0.
|
||||
*
|
||||
* However, this function requires an assumption to work correctly:
|
||||
* Assumes the timer delay time does not exceed SCLOCK_MAX (2^63 - 1).
|
||||
*
|
||||
* The range of the delay data type sclock_t being
|
||||
* [- (SCLOCK_MAX + 1), SCLOCK_MAX] ensures this assumption holds.
|
||||
*/
|
||||
|
||||
#define clock_compare(tick1, tick2) ((sclock_t)((tick2) - (tick1)) >= 0)
|
||||
|
||||
/****************************************************************************
|
||||
* Name: clock_isleapyear
|
||||
*
|
||||
|
||||
+40
-1
@@ -83,7 +83,7 @@ struct wdog_s
|
||||
#ifdef CONFIG_PIC
|
||||
FAR void *picbase; /* PIC base address */
|
||||
#endif
|
||||
sclock_t lag; /* Timer associated with the delay */
|
||||
clock_t expired; /* Timer associated with the absoulute time */
|
||||
};
|
||||
|
||||
/****************************************************************************
|
||||
@@ -137,6 +137,45 @@ extern "C"
|
||||
int wd_start(FAR struct wdog_s *wdog, sclock_t delay,
|
||||
wdentry_t wdentry, wdparm_t arg);
|
||||
|
||||
/****************************************************************************
|
||||
* Name: wd_start_absolute
|
||||
*
|
||||
* Description:
|
||||
* This function adds a watchdog timer to the active timer queue. The
|
||||
* specified watchdog function at 'wdentry' will be called from the
|
||||
* interrupt level after the specified number of ticks has reached.
|
||||
* Watchdog timers may be started from the interrupt level.
|
||||
*
|
||||
* Watchdog timers execute in the address environment that was in effect
|
||||
* when wd_start() is called.
|
||||
*
|
||||
* Watchdog timers execute only once.
|
||||
*
|
||||
* To replace either the timeout delay or the function to be executed,
|
||||
* call wd_start again with the same wdog; only the most recent wdStart()
|
||||
* on a given watchdog ID has any effect.
|
||||
*
|
||||
* Input Parameters:
|
||||
* wdog - Watchdog ID
|
||||
* ticks - Absoulute time in clock ticks
|
||||
* wdentry - Function to call on timeout
|
||||
* arg - Parameter to pass to wdentry.
|
||||
*
|
||||
* NOTE: The parameter must be of type wdparm_t.
|
||||
*
|
||||
* Returned Value:
|
||||
* Zero (OK) is returned on success; a negated errno value is return to
|
||||
* indicate the nature of any failure.
|
||||
*
|
||||
* Assumptions:
|
||||
* The watchdog routine runs in the context of the timer interrupt handler
|
||||
* and is subject to all ISR restrictions.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
int wd_start_absolute(FAR struct wdog_s *wdog, clock_t ticks,
|
||||
wdentry_t wdentry, wdparm_t arg);
|
||||
|
||||
/****************************************************************************
|
||||
* Name: wd_cancel
|
||||
*
|
||||
|
||||
Reference in New Issue
Block a user