diff --git a/arch/arm/src/common/Toolchain.defs b/arch/arm/src/common/Toolchain.defs index b4acab756e19d..cbebe416936bc 100644 --- a/arch/arm/src/common/Toolchain.defs +++ b/arch/arm/src/common/Toolchain.defs @@ -737,8 +737,8 @@ ifeq ($(CONFIG_FDPIC),y) LDMODULEFLAGS = -m armelf_linux_fdpiceabi -shared -z now \ -T $(call CONVERT_PATH,$(TOPDIR)/libs/libc/elf/gnu-elf.ld) else - CELFFLAGS += $(PICFLAGS) -mpic-register=r9 - CXXELFFLAGS += $(PICFLAGS) -mpic-register=r9 + CELFFLAGS += $(PICFLAGS) + CXXELFFLAGS += $(PICFLAGS) # Generate an executable elf, need to ignore undefined symbols LDELFFLAGS += $(LDXLINKER) --unresolved-symbols=ignore-in-object-files diff --git a/boards/arm/eoss3/quickfeather/configs/nsh/defconfig b/boards/arm/eoss3/quickfeather/configs/nsh/defconfig index 9466f0efd7a34..224c38ff2272a 100644 --- a/boards/arm/eoss3/quickfeather/configs/nsh/defconfig +++ b/boards/arm/eoss3/quickfeather/configs/nsh/defconfig @@ -15,6 +15,7 @@ CONFIG_ARCH_STACKDUMP=y CONFIG_BOARD_LOOPSPERMSEC=61440 CONFIG_BUILTIN=y CONFIG_DEBUG_FEATURES=y +CONFIG_DEBUG_FULLOPT=y CONFIG_DEBUG_SYMBOLS=y CONFIG_DISABLE_MQUEUE=y CONFIG_EOSS3_UART=y @@ -22,6 +23,7 @@ CONFIG_EXAMPLES_HELLO=y CONFIG_FS_PROCFS=y CONFIG_INIT_ENTRYPOINT="nsh_main" CONFIG_INIT_STACKSIZE=4096 +CONFIG_LTO_FULL=y CONFIG_NSH_BUILTIN_APPS=y CONFIG_NSH_DISABLE_BASENAME=y CONFIG_NSH_DISABLE_CMP=y diff --git a/boards/arm/imx9/imx93-qsb/configs/rpmsg/defconfig b/boards/arm/imx9/imx93-qsb/configs/rpmsg/defconfig index 85d9ab2b531a8..1b8c7c2ff78e0 100644 --- a/boards/arm/imx9/imx93-qsb/configs/rpmsg/defconfig +++ b/boards/arm/imx9/imx93-qsb/configs/rpmsg/defconfig @@ -8,6 +8,7 @@ # CONFIG_ARCH_LEDS is not set # CONFIG_ARCH_RAMFUNCS is not set # CONFIG_DEBUG_FUNCTIONNAME is not set +# CONFIG_LIBC_TEMPBUFFER is not set # CONFIG_NSH_CMDOPT_HEXDUMP is not set # CONFIG_RPMSG_PROCFS is not set CONFIG_ARCH="arm" diff --git a/boards/arm/lpc17xx_40xx/lx_cpu/configs/nsh/defconfig b/boards/arm/lpc17xx_40xx/lx_cpu/configs/nsh/defconfig index 4a00424e79aea..8e65676f147f7 100644 --- a/boards/arm/lpc17xx_40xx/lx_cpu/configs/nsh/defconfig +++ b/boards/arm/lpc17xx_40xx/lx_cpu/configs/nsh/defconfig @@ -6,6 +6,7 @@ # modifications. # # CONFIG_EXAMPLES_ELF_FSMOUNT is not set +# CONFIG_LIBC_TEMPBUFFER is not set # CONFIG_NSH_DISABLE_SHUTDOWN is not set # CONFIG_SPI_EXCHANGE is not set CONFIG_ALLOW_BSD_COMPONENTS=y @@ -89,6 +90,7 @@ CONFIG_LPC17_40_SSP1=y CONFIG_LPC17_40_UART0=y CONFIG_LPC17_40_USBDEV=y CONFIG_LPC17_40_USBHOST=y +CONFIG_LTO_FULL=y CONFIG_M25P_SUBSECTOR_ERASE=y CONFIG_MMCSD=y CONFIG_MMCSD_SDIO=y diff --git a/boards/arm/stm32f3/nucleo-f302r8/configs/cansock/defconfig b/boards/arm/stm32f3/nucleo-f302r8/configs/cansock/defconfig index b2680c26b99b3..68e9fc69e8962 100644 --- a/boards/arm/stm32f3/nucleo-f302r8/configs/cansock/defconfig +++ b/boards/arm/stm32f3/nucleo-f302r8/configs/cansock/defconfig @@ -19,7 +19,6 @@ CONFIG_ARCH_CHIP_STM32=y CONFIG_ARCH_CHIP_STM32F302R8=y CONFIG_ARCH_CHIP_STM32F3=y CONFIG_ARCH_INTERRUPTSTACK=1024 -CONFIG_ARCH_STACKDUMP=y CONFIG_BOARD_INITTHREAD_STACKSIZE=1024 CONFIG_BOARD_LOOPSPERMSEC=8499 CONFIG_BUILTIN=y diff --git a/boards/arm/stm32g4/nucleo-g431rb/configs/cansock/defconfig b/boards/arm/stm32g4/nucleo-g431rb/configs/cansock/defconfig index 26ea0807d5cf5..344c2682c40c6 100644 --- a/boards/arm/stm32g4/nucleo-g431rb/configs/cansock/defconfig +++ b/boards/arm/stm32g4/nucleo-g431rb/configs/cansock/defconfig @@ -20,7 +20,6 @@ CONFIG_ARCH_CHIP_STM32=y CONFIG_ARCH_CHIP_STM32G431R=y CONFIG_ARCH_CHIP_STM32G4=y CONFIG_ARCH_INTERRUPTSTACK=1024 -CONFIG_ARCH_STACKDUMP=y CONFIG_BOARD_LOOPSPERMSEC=8499 CONFIG_BOARD_NUCLEO_G431RB_USE_HSE=y CONFIG_BUILTIN=y diff --git a/boards/arm/stm32l4/stm32l476vg-disco/configs/nsh/defconfig b/boards/arm/stm32l4/stm32l476vg-disco/configs/nsh/defconfig index 8f07bd33dcf5d..1a854e53c74a4 100644 --- a/boards/arm/stm32l4/stm32l476vg-disco/configs/nsh/defconfig +++ b/boards/arm/stm32l4/stm32l476vg-disco/configs/nsh/defconfig @@ -44,7 +44,7 @@ CONFIG_NSH_BUILTIN_APPS=y CONFIG_NSH_DISABLE_IFUPDOWN=y CONFIG_NSH_FILEIOSIZE=512 CONFIG_NSH_READLINE=y -CONFIG_PREALLOC_TIMERS=4 +CONFIG_PREALLOC_TIMERS=2 CONFIG_RAM_SIZE=98304 CONFIG_RAM_START=0x20000000 CONFIG_RAW_BINARY=y diff --git a/boards/arm64/fvp-v8r/fvp-armv8r/configs/citest/defconfig b/boards/arm64/fvp-v8r/fvp-armv8r/configs/citest/defconfig index 168dc7bf97572..b036f13597d1b 100644 --- a/boards/arm64/fvp-v8r/fvp-armv8r/configs/citest/defconfig +++ b/boards/arm64/fvp-v8r/fvp-armv8r/configs/citest/defconfig @@ -27,8 +27,6 @@ CONFIG_EXAMPLES_HELLO=y CONFIG_EXPERIMENTAL=y CONFIG_FS_PROCFS=y CONFIG_FS_ROMFS=y -CONFIG_HRTIMER=y -CONFIG_HRTIMER_LIST=y CONFIG_IDLETHREAD_STACKSIZE=8192 CONFIG_INIT_ENTRYPOINT="nsh_main" CONFIG_INTELHEX_BINARY=y diff --git a/boards/risc-v/c906/smartl-c906/configs/sotest/defconfig b/boards/risc-v/c906/smartl-c906/configs/sotest/defconfig index 9e5caac9e0d08..fa62f811656b5 100644 --- a/boards/risc-v/c906/smartl-c906/configs/sotest/defconfig +++ b/boards/risc-v/c906/smartl-c906/configs/sotest/defconfig @@ -27,7 +27,6 @@ CONFIG_EXAMPLES_HELLO=y CONFIG_EXAMPLES_SOTEST=y CONFIG_FS_PROCFS=y CONFIG_FS_ROMFS=y -CONFIG_HRTIMER=y CONFIG_IDLETHREAD_STACKSIZE=2048 CONFIG_INIT_ENTRYPOINT="nsh_main" CONFIG_INIT_STACKSIZE=3072 diff --git a/boards/risc-v/qemu-rv/rv-virt/configs/citest64/defconfig b/boards/risc-v/qemu-rv/rv-virt/configs/citest64/defconfig index bb45d675e0ca5..8736def1fcd4c 100644 --- a/boards/risc-v/qemu-rv/rv-virt/configs/citest64/defconfig +++ b/boards/risc-v/qemu-rv/rv-virt/configs/citest64/defconfig @@ -47,7 +47,6 @@ CONFIG_FS_PROCFS=y CONFIG_FS_ROMFS=y CONFIG_FS_TMPFS=y CONFIG_HAVE_CXX=y -CONFIG_HRTIMER=y CONFIG_IDLETHREAD_STACKSIZE=2048 CONFIG_INIT_ENTRYPOINT="init_main" CONFIG_INIT_STACKSIZE=4096 diff --git a/boards/sim/sim/sim/configs/ostest/defconfig b/boards/sim/sim/sim/configs/ostest/defconfig index 823979e2aed56..c2b1460267808 100644 --- a/boards/sim/sim/sim/configs/ostest/defconfig +++ b/boards/sim/sim/sim/configs/ostest/defconfig @@ -18,8 +18,6 @@ CONFIG_DEBUG_ASSERTIONS=y CONFIG_DEBUG_FEATURES=y CONFIG_DEBUG_SYMBOLS=y CONFIG_FS_NAMED_SEMAPHORES=y -CONFIG_HRTIMER=y -CONFIG_HRTIMER_LIST=y CONFIG_IDLETHREAD_STACKSIZE=4096 CONFIG_INIT_ENTRYPOINT="ostest_main" CONFIG_MM_KASAN=y diff --git a/boards/tricore/tc3x/a2g-tc375-lite/configs/nsh/defconfig b/boards/tricore/tc3x/a2g-tc375-lite/configs/nsh/defconfig index 5770b9dcb8cb6..ba0ce93921fba 100644 --- a/boards/tricore/tc3x/a2g-tc375-lite/configs/nsh/defconfig +++ b/boards/tricore/tc3x/a2g-tc375-lite/configs/nsh/defconfig @@ -27,7 +27,6 @@ CONFIG_EXAMPLES_HELLO=y CONFIG_EXPERIMENTAL=y CONFIG_FS_PROCFS=y CONFIG_FS_TMPFS=y -CONFIG_HRTIMER=y CONFIG_INIT_ENTRYPOINT="nsh_main" CONFIG_LIBC_MEMFD_ERROR=y CONFIG_LIBM=y diff --git a/boards/tricore/tc3x/a2g-tc397-5v-tft/configs/nsh/defconfig b/boards/tricore/tc3x/a2g-tc397-5v-tft/configs/nsh/defconfig index c95e415d2e5bf..717e8838c2c42 100644 --- a/boards/tricore/tc3x/a2g-tc397-5v-tft/configs/nsh/defconfig +++ b/boards/tricore/tc3x/a2g-tc397-5v-tft/configs/nsh/defconfig @@ -27,7 +27,6 @@ CONFIG_EXAMPLES_HELLO=y CONFIG_EXPERIMENTAL=y CONFIG_FS_PROCFS=y CONFIG_FS_TMPFS=y -CONFIG_HRTIMER=y CONFIG_INIT_ENTRYPOINT="nsh_main" CONFIG_LIBC_MEMFD_ERROR=y CONFIG_LIBM=y diff --git a/drivers/timers/arch_alarm.c b/drivers/timers/arch_alarm.c index 18ba8f56f1452..b5f2bd98e0100 100644 --- a/drivers/timers/arch_alarm.c +++ b/drivers/timers/arch_alarm.c @@ -284,17 +284,14 @@ void up_alarm_set_lowerhalf(FAR struct oneshot_lowerhalf_s *lower) void weak_function up_timer_getmask(FAR clock_t *mask) { - *mask = 0; + clock_t maxticks = 0; - if (g_oneshot_lower != NULL) - { - clock_t maxticks = 0; + DEBUGASSERT(g_oneshot_lower); - ONESHOT_TICK_MAX_DELAY(g_oneshot_lower, &maxticks); + ONESHOT_TICK_MAX_DELAY(g_oneshot_lower, &maxticks); - *mask = maxticks == 0 ? 0 : - UINT64_MAX >> (sizeof(clock_t) * 8u - flsx(maxticks)); - } + *mask = maxticks == 0 ? 0 : + UINT64_MAX >> (sizeof(clock_t) * 8u - flsx(maxticks)); } int weak_function up_timer_gettick(FAR clock_t *ticks) @@ -368,21 +365,6 @@ int weak_function up_alarm_cancel(FAR struct timespec *ts) return ret; } -int weak_function up_alarm_tick_cancel(FAR clock_t *ticks) -{ - int ret = -EAGAIN; - - if (g_oneshot_lower != NULL) - { - ret = ONESHOT_TICK_CANCEL(g_oneshot_lower, ticks); - -#ifndef CONFIG_ONESHOT_COUNT - ONESHOT_TICK_CURRENT(g_oneshot_lower, ticks); -#endif - } - - return ret; -} #endif /**************************************************************************** @@ -409,7 +391,7 @@ int weak_function up_alarm_tick_cancel(FAR clock_t *ticks) * ****************************************************************************/ -#if defined(CONFIG_SCHED_TICKLESS) || defined(CONFIG_HRTIMER) +#ifdef CONFIG_SCHED_TICKLESS int weak_function up_alarm_start(FAR const struct timespec *ts) { int ret = -EAGAIN; @@ -421,18 +403,6 @@ int weak_function up_alarm_start(FAR const struct timespec *ts) return ret; } - -int weak_function up_alarm_tick_start(clock_t ticks) -{ - int ret = -EAGAIN; - - if (g_oneshot_lower != NULL) - { - ret = ONESHOT_TICK_ABSOLUTE(g_oneshot_lower, ticks); - } - - return ret; -} #endif /**************************************************************************** diff --git a/drivers/timers/arch_timer.c b/drivers/timers/arch_timer.c index e5c82e5dfecb8..75654c9909150 100644 --- a/drivers/timers/arch_timer.c +++ b/drivers/timers/arch_timer.c @@ -354,19 +354,6 @@ int weak_function up_timer_gettime(struct timespec *ts) ****************************************************************************/ #ifdef CONFIG_SCHED_TICKLESS -int weak_function up_timer_tick_cancel(FAR clock_t *ticks) -{ - int ret = -EAGAIN; - - if (g_timer.lower != NULL) - { - *ticks = update_timeout(g_oneshot_maxticks); - ret = OK; - } - - return ret; -} - int weak_function up_timer_cancel(FAR struct timespec *ts) { int ret = -EAGAIN; @@ -414,19 +401,6 @@ int weak_function up_timer_cancel(FAR struct timespec *ts) ****************************************************************************/ #ifdef CONFIG_SCHED_TICKLESS -int weak_function up_timer_tick_start(clock_t ticks) -{ - int ret = -EAGAIN; - - if (g_timer.lower != NULL) - { - update_timeout(ticks); - ret = OK; - } - - return ret; -} - int weak_function up_timer_start(FAR const struct timespec *ts) { int ret = -EAGAIN; diff --git a/include/nuttx/arch.h b/include/nuttx/arch.h index 5f5f5abc75622..3440ff929370c 100644 --- a/include/nuttx/arch.h +++ b/include/nuttx/arch.h @@ -2089,7 +2089,6 @@ void up_timer_getmask(FAR clock_t *mask); #if defined(CONFIG_SCHED_TICKLESS) && defined(CONFIG_SCHED_TICKLESS_ALARM) int up_alarm_cancel(FAR struct timespec *ts); -int up_alarm_tick_cancel(FAR clock_t *ticks); #endif /**************************************************************************** @@ -2117,10 +2116,8 @@ int up_alarm_tick_cancel(FAR clock_t *ticks); * ****************************************************************************/ -#if (defined(CONFIG_HRTIMER) && defined(CONFIG_ALARM_ARCH)) || \ - (defined(CONFIG_SCHED_TICKLESS) && defined(CONFIG_SCHED_TICKLESS_ALARM)) +#if defined(CONFIG_SCHED_TICKLESS) && defined(CONFIG_SCHED_TICKLESS_ALARM) int up_alarm_start(FAR const struct timespec *ts); -int up_alarm_tick_start(clock_t ticks); #endif /**************************************************************************** @@ -2161,7 +2158,6 @@ int up_alarm_tick_start(clock_t ticks); #if defined(CONFIG_SCHED_TICKLESS) && !defined(CONFIG_SCHED_TICKLESS_ALARM) int up_timer_cancel(FAR struct timespec *ts); -int up_timer_tick_cancel(FAR clock_t *ticks); #endif /**************************************************************************** @@ -2189,10 +2185,8 @@ int up_timer_tick_cancel(FAR clock_t *ticks); * ****************************************************************************/ -#if (defined(CONFIG_HRTIMER) && defined(CONFIG_TIMER_ARCH)) || \ - (defined(CONFIG_SCHED_TICKLESS) && !defined(CONFIG_SCHED_TICKLESS_ALARM)) +#if defined(CONFIG_SCHED_TICKLESS) && !defined(CONFIG_SCHED_TICKLESS_ALARM) int up_timer_start(FAR const struct timespec *ts); -int up_timer_tick_start(clock_t ticks); #endif /**************************************************************************** diff --git a/include/nuttx/hrtimer.h b/include/nuttx/hrtimer.h index 2a54bfb96d532..a314152df7e15 100644 --- a/include/nuttx/hrtimer.h +++ b/include/nuttx/hrtimer.h @@ -32,6 +32,7 @@ #include #include +#include #include #include @@ -96,6 +97,32 @@ typedef struct hrtimer_s uint64_t expired; /* Absolute expiration time (ns) */ } hrtimer_t; +#ifdef CONFIG_HRTIMER_TREE +RB_HEAD(hrtimer_tree_s, hrtimer_s); /* RB-Tree */ +#endif + +/* High-resolution timer queue + * + * Contains all active high-resolution timers sorted by absolute expiration + * time, plus the cached earliest timer for O(1) first access. Each CPU + * owns a private queue instance. + * + * The queue does not embed any lock. Callers of the queue helpers + * (hrtimer_queue_insert, hrtimer_queue_remove and hrtimer_queue_first) + * are responsible for providing their own synchronization (e.g. + * enter/leave_critical_section). + */ + +typedef struct hrtimer_queue_s +{ +#ifdef CONFIG_HRTIMER_TREE + struct hrtimer_tree_s tree; /* RB-tree of active timers */ + hrtimer_t *head; /* Cached earliest expired node */ +#else + struct list_node list; /* Sorted list of active timers */ +#endif +} hrtimer_queue_t; + /**************************************************************************** * Public Function Prototypes ****************************************************************************/ @@ -242,6 +269,35 @@ int hrtimer_start(FAR hrtimer_t *hrtimer, hrtimer_entry_t func, uint64_t hrtimer_gettime(FAR const hrtimer_t *timer); +/**************************************************************************** + * Name: hrtimer_is_active + * + * Description: + * Check whether the hrtimer is armed. The test is defined by the + * remaining time reported by hrtimer_gettime(): a non-zero remainder + * means the timer is pending and has not expired yet. A timer whose + * expiration time has been reached reports inactive even if it has + * not been processed yet, and a zero-delay timer is inactive at the + * moment it is queued. + * + * Input Parameters: + * timer - Pointer to the high-resolution timer instance. + * + * Returned Value: + * true if the timer is armed and has not expired yet. + * + ****************************************************************************/ + +static inline_function +bool hrtimer_is_active(FAR const hrtimer_t *timer) +{ + /* A non-zero remainder means the timer is pending and has not expired + * yet. Evaluate the argument once. + */ + + return hrtimer_gettime(timer) != 0; +} + #ifdef __cplusplus } #endif diff --git a/include/nuttx/sched_note.h b/include/nuttx/sched_note.h index 65d2a65f7dadd..01b4505f95813 100644 --- a/include/nuttx/sched_note.h +++ b/include/nuttx/sched_note.h @@ -701,7 +701,7 @@ void sched_note_wdog_enter(FAR void *handler, FAR const void *arg); void sched_note_wdog_leave(FAR void *handler, FAR const void *arg); #else # define sched_note_wdog_start(h,a) -# define sched_note_wdog_cancel(h,a) +# define sched_note_wdog_cancel(h,a) (void)(h); (void)(a) # define sched_note_wdog_enter(h,a) # define sched_note_wdog_leave(h,a) #endif diff --git a/include/nuttx/wdog.h b/include/nuttx/wdog.h index 28a0c237cc7fb..8cc3b36b92219 100644 --- a/include/nuttx/wdog.h +++ b/include/nuttx/wdog.h @@ -32,6 +32,7 @@ #include #include #include +#include #include #include #include @@ -40,21 +41,15 @@ * Pre-processor Definitions ****************************************************************************/ -#define WDOG_ISACTIVE(w) ((w)->func != NULL) - -/* The maximum delay tick are supposed to be CLOCK_MAX >> 1. - * However, if there are expired wdog timers in the wdog queue, - * clock_compare might be incorrect when the delay is CLOCK_MAX >> 1. - * e.g. Current tick is 123, and there is an expired wdog timer with the - * expired ticks 100. If we insert a wdog timer with delay CLOCK_MAX >> 1, - * Then clock_compare(100, 123 + CLOCK_MAX >> 1) will return false, leading - * to the new wdog timer queued before the expired wdog timer. - * So we limited the delay to CLOCK_MAX >> 2, which is 2^30 - 1 or 2^62 - 1. - * Assuming all expired wdog timer can be processed within WDOG_MAX_DELAY - * ticks, this ensure the correct enqueue of the wdog timer. +#define WDOG_ISACTIVE(w) ((w)->timer.func != NULL) + +/* The wdog delay is limited to the maximum time span the hrtimer can + * represent: HRTIMER_MAX_DELAY (2^62 nanoseconds) converted to ticks. + * Any request beyond that is rejected with -EINVAL at wd_start() / + * wd_start_next(). */ -#define WDOG_MAX_DELAY (CLOCK_MAX >> 2) +#define WDOG_MAX_DELAY (HRTIMER_MAX_DELAY / NSEC_PER_TICK) /**************************************************************************** * Public Type Declarations @@ -84,13 +79,14 @@ typedef CODE void (*wdentry_t)(wdparm_t arg); struct wdog_s { - struct list_node node; /* Supports a doubly linked list */ + struct hrtimer_s timer; /* Embedded high-resolution timer; func stores + * the user callback (wdentry_t) cast to + * hrtimer_entry_t; expired stores the absolute + * expiration time in nanoseconds */ wdparm_t arg; /* Callback argument */ - wdentry_t func; /* Function to execute when delay expires */ #ifdef CONFIG_PIC FAR void *picbase; /* PIC base address */ #endif - clock_t expired; /* Timer associated with the absolute time */ }; /**************************************************************************** @@ -133,13 +129,24 @@ extern "C" * indicate the nature of any failure. * * Assumptions: + * The wdog and wdentry should not be NULL. * The watchdog routine runs in the context of the timer interrupt handler * and is subject to all ISR restrictions. * ****************************************************************************/ +int wd_start_abstick_internal(FAR struct wdog_s *wdog, uint64_t nsec, + wdentry_t wdentry, wdparm_t arg); + +static inline_function int wd_start_abstick(FAR struct wdog_s *wdog, clock_t ticks, - wdentry_t wdentry, wdparm_t arg); + wdentry_t wdentry, wdparm_t arg) +{ + return wdog && wdentry ? + wd_start_abstick_internal(wdog, TICK2NSEC((uint64_t)ticks), + wdentry, arg) : + -EINVAL; +} /**************************************************************************** * Name: wd_start @@ -181,16 +188,11 @@ static inline_function int wd_start(FAR struct wdog_s *wdog, clock_t delay, wdentry_t wdentry, wdparm_t arg) { - int ret = -EINVAL; - /* Ensure delay is within the range the wdog can handle. */ - if (delay >= 0 && delay <= WDOG_MAX_DELAY) - { - ret = wd_start_abstick(wdog, clock_delay2abstick(delay), wdentry, arg); - } - - return ret; + return (uint64_t)delay <= WDOG_MAX_DELAY ? + wd_start_abstick(wdog, clock_delay2abstick(delay), wdentry, arg) : + -EINVAL; } /**************************************************************************** @@ -328,12 +330,10 @@ int wd_start_next(FAR struct wdog_s *wdog, clock_t delay, { /* Ensure delay is within the range the wdog can handle. */ - if (delay <= 0 || delay > WDOG_MAX_DELAY) - { - return -EINVAL; - } - - return wd_start_abstick(wdog, wdog->expired + delay, wdentry, arg); + return delay <= WDOG_MAX_DELAY && wdog && wdentry ? + wd_start_abstick_internal(wdog, + wdog->timer.expired + TICK2NSEC((uint64_t)delay), + wdentry, arg) : -EINVAL; } /**************************************************************************** @@ -350,9 +350,17 @@ int wd_start_next(FAR struct wdog_s *wdog, clock_t delay, * Zero (OK) is returned on success; A negated errno value is returned to * indicate the nature of any failure. * + * Assumptions: + * The wdog is not NULL. + * ****************************************************************************/ -int wd_cancel(FAR struct wdog_s *wdog); +int wd_cancel_internal(FAR struct wdog_s *wdog); + +static inline_function int wd_cancel(FAR struct wdog_s *wdog) +{ + return wdog ? wd_cancel_internal(wdog) : -EINVAL; +} /**************************************************************************** * Name: wd_gettime @@ -371,7 +379,10 @@ int wd_cancel(FAR struct wdog_s *wdog); * ****************************************************************************/ -clock_t wd_gettime(FAR struct wdog_s *wdog); +static inline_function clock_t wd_gettime(FAR struct wdog_s *wdog) +{ + return wdog ? NSEC2TICK(hrtimer_gettime(&wdog->timer)) : 0; +} #ifdef __cplusplus } diff --git a/include/nuttx/wqueue.h b/include/nuttx/wqueue.h index ba2f025f79897..83de174124583 100644 --- a/include/nuttx/wqueue.h +++ b/include/nuttx/wqueue.h @@ -36,6 +36,7 @@ #include #include #include +#include /**************************************************************************** * Pre-processor Definitions diff --git a/libs/libc/misc/lib_tempbuffer.c b/libs/libc/misc/lib_tempbuffer.c index eaba5776aaac9..bec9af18dac75 100644 --- a/libs/libc/misc/lib_tempbuffer.c +++ b/libs/libc/misc/lib_tempbuffer.c @@ -47,7 +47,7 @@ struct tempbuffer_s { - atomic_t free_bitmap; /* Bitmap of free buffer */ + atomic_t used_bitmap; /* Bitmap of used buffer (0 = free) */ char buffer[CONFIG_LIBC_MAX_TEMPBUFFER][TEMP_MAX_SIZE]; }; @@ -55,10 +55,13 @@ struct tempbuffer_s * Private Data ****************************************************************************/ -static struct tempbuffer_s g_tempbuffer = -{ - (1u << CONFIG_LIBC_MAX_TEMPBUFFER) - 1, -}; +/* Zero-initialized so the whole structure lives in .bss instead of .data. + * A cleared used_bitmap means every slot is free, so no explicit initializer + * is needed. Keeping the 512-byte buffer out of .data saves that much + * flash. + */ + +static struct tempbuffer_s g_tempbuffer; /**************************************************************************** * Private Functions @@ -88,19 +91,24 @@ FAR void *lib_get_tempbuffer(size_t nbytes) { if (nbytes <= TEMP_MAX_SIZE) { + /* A cleared bit means the slot is free, so invert the bitmap + * (masked to the valid slots) and take the lowest free slot. + */ + for (; ; ) { int32_t update; - int32_t free_bitmap = atomic_read(&g_tempbuffer.free_bitmap); - int index = ffsl(free_bitmap) - 1; + int32_t used_bitmap = atomic_read(&g_tempbuffer.used_bitmap); + int index = ffsl(~used_bitmap & + ((1ul << CONFIG_LIBC_MAX_TEMPBUFFER) - 1)) - 1; if (index < 0 || index >= CONFIG_LIBC_MAX_TEMPBUFFER) { break; } - update = free_bitmap & ~(1u << index); - if (atomic_cmpxchg(&g_tempbuffer.free_bitmap, &free_bitmap, + update = used_bitmap | (1u << index); + if (atomic_cmpxchg(&g_tempbuffer.used_bitmap, &used_bitmap, update)) { return g_tempbuffer.buffer[index]; @@ -141,9 +149,11 @@ void lib_put_tempbuffer(FAR void *buffer) if (index >= 0 && index < CONFIG_LIBC_MAX_TEMPBUFFER) { - DEBUGASSERT((atomic_read(&g_tempbuffer.free_bitmap) & - (1u << index)) == 0); - atomic_or_acquire(&g_tempbuffer.free_bitmap, 1u << index); + /* Mark the corresponding bit as free (clear the used bit) */ + + DEBUGASSERT((atomic_read(&g_tempbuffer.used_bitmap) & + (1u << index)) != 0); + atomic_and_acquire(&g_tempbuffer.used_bitmap, ~(1u << index)); return; } diff --git a/net/ipfrag/ipfrag.c b/net/ipfrag/ipfrag.c index 54c132de2c377..4158eaee349c3 100644 --- a/net/ipfrag/ipfrag.c +++ b/net/ipfrag/ipfrag.c @@ -1074,7 +1074,7 @@ int32_t ip_fragout_slice(FAR struct iob_s *iob, uint8_t domain, uint16_t mtu, void ip_frag_startwdog(void) { - if (g_wdfragtimeout.func == NULL) + if (!WDOG_ISACTIVE(&g_wdfragtimeout)) { wd_start(&g_wdfragtimeout, REASSEMBLY_TIMEOUT_TICKS, ip_fragin_timerout_expiry, (wdparm_t)NULL); diff --git a/sched/Kconfig b/sched/Kconfig index 4fbd978845564..1c40d321aee92 100644 --- a/sched/Kconfig +++ b/sched/Kconfig @@ -2337,17 +2337,9 @@ config CUSTOM_SEMAPHORE_MAXVALUE Enable to support custom max value for semaphores. When this option is enabled, the max value of a semaphore can be set -config HRTIMER - bool "High resolution timer support" - default n - ---help--- - Enable to support high resolution timer - -if HRTIMER - choice prompt "HRTimer management data structure type" - default HRTIMER_TREE + default HRTIMER_LIST config HRTIMER_LIST bool "List-based timer management" @@ -2384,5 +2376,3 @@ config HRTIMER_PRECISION_SHIFT 10 - 1024 ns (~1 us) precision 15 - 32768 ns (~32 us) precision 20 - 1048576 ns (~1 ms) precision - -endif # HRTIMER diff --git a/sched/hrtimer/CMakeLists.txt b/sched/hrtimer/CMakeLists.txt index 74020614a53bb..4f7c47c2d67c8 100644 --- a/sched/hrtimer/CMakeLists.txt +++ b/sched/hrtimer/CMakeLists.txt @@ -22,9 +22,14 @@ # Add hrtimer-related files to the build set(CSRCS) -if(CONFIG_HRTIMER) - list(APPEND CSRCS hrtimer_cancel.c hrtimer_initialize.c hrtimer_start.c - hrtimer_gettime.c) -endif() + +list( + APPEND + CSRCS + hrtimer_cancel.c + hrtimer_queue.c + hrtimer_initialize.c + hrtimer_start.c + hrtimer_gettime.c) target_sources(sched PRIVATE ${CSRCS}) diff --git a/sched/hrtimer/Make.defs b/sched/hrtimer/Make.defs index dfd326ad61b6f..6d63344910df4 100644 --- a/sched/hrtimer/Make.defs +++ b/sched/hrtimer/Make.defs @@ -22,10 +22,8 @@ # Add hrtimer-related files to the build -ifeq ($(CONFIG_HRTIMER),y) - CSRCS += hrtimer_cancel.c hrtimer_initialize.c hrtimer_start.c - CSRCS += hrtimer_gettime.c -endif +CSRCS += hrtimer_cancel.c hrtimer_queue.c hrtimer_initialize.c hrtimer_start.c +CSRCS += hrtimer_gettime.c # Include hrtimer build support diff --git a/sched/hrtimer/hrtimer.h b/sched/hrtimer/hrtimer.h index 4e6a38df6aeb7..57f8004536d60 100644 --- a/sched/hrtimer/hrtimer.h +++ b/sched/hrtimer/hrtimer.h @@ -39,8 +39,6 @@ #include "sched/sched.h" -#ifdef CONFIG_HRTIMER - /**************************************************************************** * Pre-processor Definitions ****************************************************************************/ @@ -90,33 +88,47 @@ RB_HEAD(hrtimer_tree_s, hrtimer_s); /* Seqcount protecting access to the hrtimer queue and timer state */ -extern seqcount_t g_hrtimer_lock; +#ifdef CONFIG_SMP +DECLARE_PER_CPU_BMP(seqcount_t, g_hrtimer_lock); +#define g_hrtimer_lock this_cpu_var_bmp(g_hrtimer_lock) +#endif /* The time of the last hrtimer_process() run, used as the reference * point for hrtimer_last_expired(). */ -extern uint64_t g_hrtimer_last_expired; +DECLARE_PER_CPU_BMP(uint64_t, g_hrtimer_last_expired); +#define g_hrtimer_last_expired this_cpu_var_bmp(g_hrtimer_last_expired) -#ifdef CONFIG_HRTIMER_TREE -/* Red-Black tree containing all active high-resolution timers */ - -extern struct hrtimer_tree_s g_hrtimer_tree; -extern FAR struct hrtimer_s *g_hrtimer_head; -#else -/* List containing all active high-resolution timers */ +/* Queue holding all active high-resolution timers sorted by + * absolute expiration time. + */ -extern struct list_node g_hrtimer_list; -#endif +DECLARE_PER_CPU_BMP(struct hrtimer_queue_s, g_hrtimer_queue); +#define g_hrtimer_queue this_cpu_var_bmp(g_hrtimer_queue) -#ifdef CONFIG_SMP /* Array of pointers to currently running high-resolution timers * for each CPU in SMP configurations. Index corresponds to CPU ID. */ -extern uintptr_t g_hrtimer_running[CONFIG_SMP_NCPUS]; +#ifdef CONFIG_SMP +DECLARE_PER_CPU_SMP(uintptr_t, g_hrtimer_running); +# define hrtimer_running_slot(cpu) per_cpu_var_smp(g_hrtimer_running, cpu) +#else +DECLARE_PER_CPU_BMP(uintptr_t, g_hrtimer_running); +# define hrtimer_running_slot(cpu) this_cpu_var_bmp(g_hrtimer_running) #endif +/**************************************************************************** + * Public Function Prototypes + ****************************************************************************/ + +void hrtimer_queue_initialize(FAR struct hrtimer_queue_s *queue); +bool hrtimer_queue_insert(FAR struct hrtimer_queue_s *queue, + FAR hrtimer_t *hrtimer); +bool hrtimer_queue_remove(FAR struct hrtimer_queue_s *queue, + FAR hrtimer_t *hrtimer); + /**************************************************************************** * Inline Functions ****************************************************************************/ @@ -172,43 +184,41 @@ void hrtimer_unlock(irqstate_t flags) } /**************************************************************************** - * Name: hrtimer_get_first + * Name: hrtimer_last_expired * * Description: - * Return the earliest expiring pending timer. + * Return the last expired time of the hrtimer. + * + * Input Parameters: + * None. * * Returned Value: - * Pointer to the earliest timer, or NULL if none are pending. + * The last expired time in nanoseconds. * ****************************************************************************/ -static inline_function FAR hrtimer_t *hrtimer_get_first(void) +static inline_function uint64_t hrtimer_last_expired(void) { -#ifdef CONFIG_HRTIMER_TREE - return g_hrtimer_head; -#else - return predict_false(list_is_empty(&g_hrtimer_list)) ? - NULL : list_first_entry(&g_hrtimer_list, FAR hrtimer_t, node); -#endif + return g_hrtimer_last_expired; } /**************************************************************************** - * Name: hrtimer_last_expired + * Name: hrtimer_initialize * * Description: - * Return the last expired time of the hrtimer. + * Called from nx_start() to initialize the per-CPU hrtimer data. * * Input Parameters: * None. * * Returned Value: - * The last expired time in nanoseconds. + * None. * ****************************************************************************/ -static inline_function uint64_t hrtimer_last_expired(void) +static inline_function void hrtimer_initialize(void) { - return g_hrtimer_last_expired; + hrtimer_queue_initialize(&g_hrtimer_queue); } /**************************************************************************** @@ -225,9 +235,20 @@ static inline_function uint64_t hrtimer_last_expired(void) * ****************************************************************************/ +static inline_function +FAR hrtimer_t *hrtimer_queue_first(FAR struct hrtimer_queue_s *queue) +{ +#ifdef CONFIG_HRTIMER_TREE + return queue->head; +#else + return predict_false(list_is_empty(&queue->list)) ? + NULL : list_first_entry(&queue->list, FAR hrtimer_t, node); +#endif +} + static inline_function uint64_t hrtimer_next_expired(void) { - FAR hrtimer_t *first = hrtimer_get_first(); + FAR hrtimer_t *first = hrtimer_queue_first(&g_hrtimer_queue); return predict_true(first != NULL) ? first->expired : hrtimer_last_expired() + INT64_MAX; @@ -365,24 +386,6 @@ RB_PROTOTYPE(hrtimer_tree_s, hrtimer_s, node, hrtimer_compare); * ****************************************************************************/ -static inline_function void hrtimer_remove(FAR hrtimer_t *hrtimer) -{ -#ifdef CONFIG_HRTIMER_TREE - if (g_hrtimer_head == hrtimer) - { - g_hrtimer_head = RB_NEXT(hrtimer_tree_s, &g_hrtimer_tree, hrtimer); - } - - RB_REMOVE(hrtimer_tree_s, &g_hrtimer_tree, hrtimer); -#else - list_delete_fast(&hrtimer->node); -#endif - - /* Explicitly mark the timer as dequeued. */ - - hrtimer->func = NULL; -} - /**************************************************************************** * Name: hrtimer_insert * @@ -395,34 +398,6 @@ static inline_function void hrtimer_remove(FAR hrtimer_t *hrtimer) * ****************************************************************************/ -static inline_function void hrtimer_insert(FAR hrtimer_t *hrtimer) -{ -#ifdef CONFIG_HRTIMER_TREE - RB_INSERT(hrtimer_tree_s, &g_hrtimer_tree, hrtimer); - - if (predict_false(g_hrtimer_head == NULL) || - HRTIMER_TIME_BEFORE(hrtimer->expired, g_hrtimer_head->expired)) - { - g_hrtimer_head = hrtimer; - } -#else - FAR hrtimer_t *curr; - uint64_t expired = hrtimer->expired; - - list_for_every_entry(&g_hrtimer_list, curr, hrtimer_t, node) - { - /* Until curr->expired has not timed out relative to expired */ - - if (!clock_compare(curr->expired, expired)) - { - break; - } - } - - list_add_before(&curr->node, &hrtimer->node); -#endif -} - /**************************************************************************** * Name: hrtimer_read_64/32 * @@ -506,12 +481,8 @@ DEFINE_HRTIMER_READ(32, uint32_t) * ****************************************************************************/ -#ifdef CONFIG_SMP -# define hrtimer_mark_running(timer, cpu) \ - (g_hrtimer_running[cpu] = (uintptr_t)(timer)) -#else -# define hrtimer_mark_running(timer, cpu) UNUSED(cpu) -#endif +#define hrtimer_mark_running(timer, cpu) \ + (hrtimer_running_slot(cpu) = (uintptr_t)(timer)) /**************************************************************************** * Name: hrtimer_unmark_running @@ -557,13 +528,45 @@ void hrtimer_unmark_running(int cpu) #ifdef CONFIG_SMP # define hrtimer_is_running(timer, cpu) \ - (hrtimer_read_ptr(&g_hrtimer_running[cpu]) == (uintptr_t)(timer)) + (hrtimer_read_ptr(&hrtimer_running_slot(cpu)) == (uintptr_t)(timer)) #else # define hrtimer_is_running(timer, cpu) (true) #endif #define hrtimer_is_cancelling(timer, cpu) \ hrtimer_is_running((uintptr_t)(timer) | 0x1u, cpu) +/**************************************************************************** + * Name: hrtimer_is_referenced + * + * Description: + * Check if the CPU core holds the timer in its running slot, either as + * the running timer or in the cancel-marked form (slot = timer | 1). + * + * Input Parameters: + * timer - The timer to be checked. + * cpu - The CPU core Id. + * + * Returned Value: + * true if the timer is referenced by the CPU core, false otherwise. + * + * Assumption: + * This function can only be called from the critical section. + * + ****************************************************************************/ + +#ifdef CONFIG_SMP +#define hrtimer_is_referenced(timer, cpu) \ + ((hrtimer_read_ptr(&hrtimer_running_slot(cpu)) ^ (uintptr_t)(timer)) <= 0x1u) +#else +/* The cancel-marked form of the running slot is only produced by + * hrtimer_cancel_running(), which is compiled for CONFIG_SMP. On a + * uniprocessor the slot holds the running timer pointer or NULL. + */ + +#define hrtimer_is_referenced(timer, cpu) \ + (hrtimer_read_ptr(&hrtimer_running_slot(cpu)) == (uintptr_t)(timer)) +#endif + /**************************************************************************** * Name: hrtimer_cancel_running * @@ -601,18 +604,19 @@ int hrtimer_cancel_running(FAR hrtimer_t *timer) for (cpu = 0; cpu < CONFIG_SMP_NCPUS; cpu++) { /* This is a faster implementation equivalent to - * (g_hrtimer_running[cpu] & (~(uintptr_t)0x1u)) == timer, + * (hrtimer_running_slot(cpu) & + * (~(uintptr_t)0x1u)) == timer, * Assuming the pointer of the timer is at least 2-bytes aligned * (the last bit must be zero). */ - if ((g_hrtimer_running[cpu] ^ cancelled) <= 0x1u) + if ((hrtimer_running_slot(cpu) ^ cancelled) <= 0x1u) { /* Set the timer to the cancelled state and revoke the write * ownership of the timer from the queue. */ - g_hrtimer_running[cpu] = cancelled; + hrtimer_running_slot(cpu) = cancelled; refs++; } } @@ -716,7 +720,8 @@ static inline_function void hrtimer_process(uint64_t now) * the queue. */ - while (predict_true((hrtimer = hrtimer_get_first()) != NULL)) + while (predict_true((hrtimer = + hrtimer_queue_first(&g_hrtimer_queue)) != NULL)) { expired = hrtimer->expired; @@ -736,7 +741,7 @@ static inline_function void hrtimer_process(uint64_t now) /* Remove the expired timer from the timer queue */ - hrtimer_remove(hrtimer); + hrtimer_queue_remove(&g_hrtimer_queue, hrtimer); /* Leave critical section before invoking the callback */ @@ -764,7 +769,7 @@ static inline_function void hrtimer_process(uint64_t now) { hrtimer->expired = expired + delay; hrtimer->func = func; - hrtimer_insert(hrtimer); + hrtimer_queue_insert(&g_hrtimer_queue, hrtimer); } hrtimer_unmark_running(cpu); @@ -775,5 +780,4 @@ static inline_function void hrtimer_process(uint64_t now) hrtimer_unlock(flags); } -#endif /* CONFIG_HRTIMER */ #endif /* __SCHED_HRTIMER_HRTIMER_H */ diff --git a/sched/hrtimer/hrtimer_cancel.c b/sched/hrtimer/hrtimer_cancel.c index 0df910eea4c7e..4ee10ef455575 100644 --- a/sched/hrtimer/hrtimer_cancel.c +++ b/sched/hrtimer/hrtimer_cancel.c @@ -104,7 +104,7 @@ int hrtimer_cancel(FAR hrtimer_t *hrtimer) hrtimer->expired = hrtimer_last_expired(); - hrtimer_remove(hrtimer); + hrtimer_queue_remove(&g_hrtimer_queue, hrtimer); /* Reprogram the hardware timer only if the earliest expiration * changed. diff --git a/sched/hrtimer/hrtimer_initialize.c b/sched/hrtimer/hrtimer_initialize.c index e89db89691c3a..a87ba6251afcf 100644 --- a/sched/hrtimer/hrtimer_initialize.c +++ b/sched/hrtimer/hrtimer_initialize.c @@ -32,13 +32,17 @@ * Public Data ****************************************************************************/ -#ifdef CONFIG_SMP /* Array of pointers to currently running high-resolution timers * for each CPU in SMP configurations. Index corresponds to CPU ID. */ -uintptr_t g_hrtimer_running[CONFIG_SMP_NCPUS]; +#ifdef CONFIG_SMP +DEFINE_PER_CPU_BSS_SMP(uintptr_t, g_hrtimer_running); +#else +DEFINE_PER_CPU_BSS_BMP(uintptr_t, g_hrtimer_running); +#endif +#ifdef CONFIG_SMP /* Global spinlock protecting the high-resolution timer subsystem. * * This spinlock serializes access to the hrtimer queue and @@ -46,7 +50,9 @@ uintptr_t g_hrtimer_running[CONFIG_SMP_NCPUS]; * timer queue is modified. */ -seqcount_t g_hrtimer_lock = SEQLOCK_INITIALIZER; +#undef g_hrtimer_lock +DEFINE_PER_CPU_BMP(seqcount_t, g_hrtimer_lock) = SEQLOCK_INITIALIZER; +#define g_hrtimer_lock this_cpu_var_bmp(g_hrtimer_lock) #endif /* CONFIG_SMP */ /* HRTimer queue for all active high-resolution timers. @@ -58,14 +64,17 @@ seqcount_t g_hrtimer_lock = SEQLOCK_INITIALIZER; * both configurations. */ -uint64_t g_hrtimer_last_expired; +#undef g_hrtimer_last_expired +DEFINE_PER_CPU_BSS_BMP(uint64_t, g_hrtimer_last_expired); +#define g_hrtimer_last_expired this_cpu_var_bmp(g_hrtimer_last_expired) -#ifdef CONFIG_HRTIMER_TREE -struct hrtimer_tree_s g_hrtimer_tree; -struct FAR hrtimer_s *g_hrtimer_head; -#else -struct list_node g_hrtimer_list = LIST_INITIAL_VALUE(g_hrtimer_list); -#endif +/* Queue holding all active high-resolution timers sorted by + * absolute expiration time. + */ + +#undef g_hrtimer_queue +DEFINE_PER_CPU_BSS_BMP(struct hrtimer_queue_s, g_hrtimer_queue); +#define g_hrtimer_queue this_cpu_var_bmp(g_hrtimer_queue) /**************************************************************************** * Public Functions diff --git a/sched/hrtimer/hrtimer_queue.c b/sched/hrtimer/hrtimer_queue.c new file mode 100644 index 0000000000000..e9d14b1122f24 --- /dev/null +++ b/sched/hrtimer/hrtimer_queue.c @@ -0,0 +1,167 @@ +/**************************************************************************** + * sched/hrtimer/hrtimer_queue.c + * + * SPDX-License-Identifier: Apache-2.0 + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. The + * ASF licenses this file to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the + * License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + * + ****************************************************************************/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include +#include +#include +#include +#include +#include +#include +#include + +#include "hrtimer/hrtimer.h" + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: hrtimer_queue_initialize + * + * Description: + * Initialize the hrtimer queue. The queue is embedded in the caller's + * data (zero-initialized BSS is also acceptable for tree mode, but + * explicit initialization keeps the sentinel consistent). + * + * Input Parameters: + * queue - Pointer to the hrtimer queue. + * + * Returned Value: + * None. + * + ****************************************************************************/ + +void hrtimer_queue_initialize(FAR struct hrtimer_queue_s *queue) +{ +#ifdef CONFIG_HRTIMER_TREE + RB_INIT(&queue->tree); + queue->head = NULL; +#else + list_initialize(&queue->list); +#endif +} + +/**************************************************************************** + * Name: hrtimer_queue_insert + * + * Description: + * Insert a timer into the hrtimer queue according to its expiration + * time. + * + * Input Parameters: + * queue - Pointer to the hrtimer queue. + * hrtimer - Pointer to the timer to insert. + * + * Returned Value: + * true if the timer is added at the head of the queue, false otherwise. + * + * Assumption: + * The caller must hold the queue lock. + * The caller must ensure that the timer is not already in the queue. + * + ****************************************************************************/ + +bool hrtimer_queue_insert(FAR struct hrtimer_queue_s *queue, + FAR hrtimer_t *hrtimer) +{ +#ifdef CONFIG_HRTIMER_TREE + bool is_head = false; + + RB_INSERT(hrtimer_tree_s, &queue->tree, hrtimer); + + /* An empty queue has a NULL head. */ + + if (predict_false(queue->head == NULL) || + HRTIMER_TIME_BEFORE(hrtimer->expired, queue->head->expired)) + { + queue->head = hrtimer; + is_head = true; + } + + return is_head; +#else + FAR hrtimer_t *curr; + uint64_t expired = hrtimer->expired; + + list_for_every_entry(&queue->list, curr, hrtimer_t, node) + { + /* Until curr->expired has not timed out relative to expired */ + + if (!HRTIMER_TIME_BEFORE_EQ(curr->expired, expired)) + { + break; + } + } + + list_add_before(&curr->node, &hrtimer->node); + return list_is_head(&queue->list, &hrtimer->node); +#endif +} + +/**************************************************************************** + * Name: hrtimer_queue_remove + * + * Description: + * Remove a timer from the queue and mark it as dequeued. + * + * Input Parameters: + * queue - Pointer to the hrtimer queue. + * hrtimer - Pointer to the timer to remove. + * + * Returned Value: + * true if the timer was the head of the queue, false otherwise. + * + * Assumption: + * The caller must hold the queue lock. + * The caller must ensure that the timer is in the queue. + * + ****************************************************************************/ + +bool hrtimer_queue_remove(FAR struct hrtimer_queue_s *queue, + FAR hrtimer_t *hrtimer) +{ +#ifdef CONFIG_HRTIMER_TREE + bool is_head = queue->head == hrtimer; + + if (is_head) + { + queue->head = RB_NEXT(hrtimer_tree_s, &queue->tree, hrtimer); + } + + RB_REMOVE(hrtimer_tree_s, &queue->tree, hrtimer); +#else + bool is_head = list_is_head(&queue->list, &hrtimer->node); + + list_delete_fast(&hrtimer->node); +#endif + + /* Explicitly mark the timer as dequeued. */ + + hrtimer->func = NULL; + + return is_head; +} diff --git a/sched/hrtimer/hrtimer_start.c b/sched/hrtimer/hrtimer_start.c index 0f75e6c6b61a7..89006bef695c7 100644 --- a/sched/hrtimer/hrtimer_start.c +++ b/sched/hrtimer/hrtimer_start.c @@ -79,7 +79,7 @@ int hrtimer_start_absolute(FAR hrtimer_t *hrtimer, hrtimer_entry_t func, if (hrtimer_is_pending(hrtimer)) { - hrtimer_remove(hrtimer); + hrtimer_queue_remove(&g_hrtimer_queue, hrtimer); } hrtimer->func = func; @@ -87,7 +87,7 @@ int hrtimer_start_absolute(FAR hrtimer_t *hrtimer, hrtimer_entry_t func, /* Insert the timer into the hrtimer queue. */ - hrtimer_insert(hrtimer); + hrtimer_queue_insert(&g_hrtimer_queue, hrtimer); /* Reprogram the hardware timer only if the earliest expiration changed. */ diff --git a/sched/init/nx_start.c b/sched/init/nx_start.c index 53d4d28172b7a..867ed1f7aca90 100644 --- a/sched/init/nx_start.c +++ b/sched/init/nx_start.c @@ -555,6 +555,10 @@ void nx_start(void) wdlist_initialize(); + /* Initialize hrtimer *****************************************************/ + + hrtimer_initialize(); + /* Initialize task list table *********************************************/ tasklist_initialize(); diff --git a/sched/sched/sched_processtick.c b/sched/sched/sched_processtick.c index 5ada01f80460d..6c91d315d1e08 100644 --- a/sched/sched/sched_processtick.c +++ b/sched/sched/sched_processtick.c @@ -186,11 +186,7 @@ void nxsched_process_timer(void) /* Process watchdogs */ -#ifdef CONFIG_HRTIMER hrtimer_process(clock_systime_nsec()); -#else - wd_timer(clock_systime_ticks()); -#endif #ifdef CONFIG_SYSTEMTICK_HOOK /* Call out to a user-provided function in order to perform board-specific, diff --git a/sched/sched/sched_processtickless.c b/sched/sched/sched_processtickless.c index a6cd637e0d162..1aa23476bbb12 100644 --- a/sched/sched/sched_processtickless.c +++ b/sched/sched/sched_processtickless.c @@ -93,9 +93,14 @@ static clock_t nxsched_process_scheduler(clock_t ticks, clock_t elapsed, static DEFINE_PER_CPU_BSS_BMP(clock_t, g_timer_tick); #define g_timer_tick this_cpu_var_bmp(g_timer_tick) -/* Wdog timer for scheduler event. */ +/* HRTimer for scheduler event. */ -static struct wdog_s g_sched_event; +static hrtimer_t g_sched_event; + +/* True while the scheduler event hrtimer callback is running. */ + +#define sched_in_expiration() \ + hrtimer_is_referenced(&g_sched_event, this_cpu()) /**************************************************************************** * Private Functions @@ -114,30 +119,9 @@ int up_timer_gettick(FAR clock_t *ticks) } #endif -#if defined(CONFIG_SCHED_TICKLESS_ALARM) && !defined(CONFIG_ALARM_ARCH) -int up_alarm_tick_start(clock_t ticks) +static clock_t nxsched_process_event(clock_t ticks, bool noswitches) { - struct timespec ts; - - clock_ticks2time(&ts, ticks); - return up_alarm_start(&ts); -} -#endif - -#if !defined(CONFIG_SCHED_TICKLESS_ALARM) && !defined(CONFIG_TIMER_ARCH) -int up_timer_tick_start(clock_t ticks) -{ - struct timespec ts; - - clock_ticks2time(&ts, ticks); - return up_timer_start(&ts); -} -#endif - -static void nxsched_process_event(clock_t ticks, bool noswitches) -{ - clock_t next; - clock_t elapsed; + clock_t elapsed = 0; /* Ensure the g_timer_tick is monotonic. */ @@ -147,21 +131,25 @@ static void nxsched_process_event(clock_t ticks, bool noswitches) elapsed = ticks - g_timer_tick; g_timer_tick = ticks; + } - /* Process the timer ticks and set up the next interval (or not) */ + /* Process the timer ticks and set up the next interval (or not) */ - next = nxsched_process_scheduler(ticks, elapsed, noswitches); - nxsched_timer_start(ticks, next); - } + return nxsched_process_scheduler(ticks, elapsed, noswitches); } -static void nxsched_wdog_expiration(wdparm_t arg) +static uint64_t nxsched_hrtimer_expiration(const hrtimer_t *timer, + uint64_t expired) { - /* Since the g_wdexpired has updated in the nxsched_timer_expiration, - * here we do not need to get tick again. - */ + clock_t delay; + clock_t now = div_const(expired, NSEC_PER_TICK); + irqstate_t flags = enter_critical_section(); - nxsched_process_event(g_wdexpired, false); + delay = nxsched_process_event(now, false); + + leave_critical_section(flags); + + return delay != CLOCK_MAX ? TICK2NSEC(delay) + 1u : 0u; } /**************************************************************************** @@ -343,39 +331,18 @@ clock_t nxsched_process_scheduler(clock_t ticks, clock_t elapsed, void nxsched_process_timer(void) { -#ifdef CONFIG_HRTIMER - uint64_t nsec = clock_systime_nsec(); - clock_t ticks = NSEC2TICK(nsec); - - clock_update_sched_ticks(ticks); - - hrtimer_process(nsec); -#else - irqstate_t flags; - clock_t ticks; - - flags = enter_critical_section(); - -#ifdef CONFIG_CLOCK_TIMEKEEPING - /* Process wall time */ - - clock_update_wall_time(); -#endif - - /* Do not move the up_timer_gettick out of the critical section, - * it will violate the invariant that the g_timer_tick should be monotonic. - */ - - up_timer_gettick(&ticks); - - /* Update sched ticks */ + uint64_t nsec; + struct timespec ts = + { + 0 + }; - clock_update_sched_ticks(ticks); + up_timer_gettime(&ts); + nsec = clock_time2nsec(&ts); - wd_timer(ticks); + clock_update_sched_ticks(NSEC2TICK(nsec)); - leave_critical_section(flags); -#endif + hrtimer_process(nsec); } /**************************************************************************** @@ -387,7 +354,7 @@ void nxsched_process_timer(void) * * - If the watchdog at the head of the expiration list changes (or if its * delay changes. This can occur as a consequence of the actions of - * wd_start() or wd_cancel(). + * hrtimer_start() or hrtimer_cancel(). * - When pre-emption is re-enabled. A previous time slice may have * expired while pre-emption was enabled and now needs to be executed. * @@ -417,14 +384,16 @@ void nxsched_process_timer(void) void nxsched_reassess_timer(void) { - /* If we are in the wdog callback, there is no need to get tick again. */ + clock_t next; + clock_t now; - if (!wd_in_callback()) + up_timer_gettick(&now); + next = nxsched_process_event(now, true); + + if (!sched_in_expiration()) { - up_timer_gettick(&g_wdexpired); + nxsched_timer_start(now, next); } - - nxsched_process_event(g_wdexpired, true); } /**************************************************************************** @@ -451,13 +420,15 @@ void nxsched_timer_start(clock_t now, clock_t delay) { if (delay != CLOCK_MAX) { - DEBUGASSERT(delay <= UINT32_MAX); - wd_start_abstick(&g_sched_event, now + delay, - nxsched_wdog_expiration, 0u); + DEBUGASSERT((uint64_t)delay <= UINT32_MAX && + (uint64_t)delay <= HRTIMER_MAX_DELAY / NSEC_PER_TICK); + hrtimer_start(&g_sched_event, nxsched_hrtimer_expiration, + TICK2NSEC(now + delay), + HRTIMER_MODE_ABS); } else { - wd_cancel(&g_sched_event); + hrtimer_cancel(&g_sched_event); } } diff --git a/sched/wdog/CMakeLists.txt b/sched/wdog/CMakeLists.txt index e38a78e368ed0..c2cc5b8457a45 100644 --- a/sched/wdog/CMakeLists.txt +++ b/sched/wdog/CMakeLists.txt @@ -20,5 +20,4 @@ # # ############################################################################## -target_sources(sched PRIVATE wd_initialize.c wd_start.c wd_cancel.c - wd_gettime.c) +target_sources(sched PRIVATE wd_initialize.c wd_start.c wd_cancel.c) diff --git a/sched/wdog/Make.defs b/sched/wdog/Make.defs index e3fbbe4004a4e..118bbccf55c91 100644 --- a/sched/wdog/Make.defs +++ b/sched/wdog/Make.defs @@ -20,7 +20,7 @@ # ############################################################################ -CSRCS += wd_initialize.c wd_start.c wd_cancel.c wd_gettime.c +CSRCS += wd_initialize.c wd_start.c wd_cancel.c # Include wdog build support diff --git a/sched/wdog/wd_cancel.c b/sched/wdog/wd_cancel.c index 5d4cbd7c339b8..c8ef8a8734109 100644 --- a/sched/wdog/wd_cancel.c +++ b/sched/wdog/wd_cancel.c @@ -43,7 +43,7 @@ ****************************************************************************/ /**************************************************************************** - * Name: wd_cancel + * Name: wd_cancel_internal * * Description: * This function cancels a currently running watchdog timer. Watchdog @@ -56,60 +56,51 @@ * Zero (OK) is returned on success; A negated errno value is returned to * indicate the nature of any failure. * + * Assumptions: + * The wdog and wdentry is not NULL. + * ****************************************************************************/ -int wd_cancel(FAR struct wdog_s *wdog) +int wd_cancel_internal(FAR struct wdog_s *wdog) { - FAR struct wdog_s *first; - irqstate_t flags; - int ret = -EINVAL; + int ret = -EINVAL; + irqstate_t flags = enter_critical_section(); + hrtimer_entry_t func = wdog->timer.func; + uint64_t expired = wdog->timer.expired; + + /* Make sure that the watchdog is valid and still active. */ - if (wdog != NULL) + if (hrtimer_is_pending(&wdog->timer)) { - /* Prohibit timer interactions with the timer queue until the - * cancellation is complete + /* hrtimer_queue_remove already set timer.func to NULL, releasing + * the queue membership (hrtimer_is_pending checks timer.func). */ - flags = enter_critical_section(); - - /* Make sure that the watchdog is valid and still active. */ - - if (WDOG_ISACTIVE(wdog)) + if (hrtimer_queue_remove(&g_wdqueue, &wdog->timer) && + !wd_in_callback()) { - first = list_first_entry(&g_wdactivelist, struct wdog_s, node); - - /* Now, remove the watchdog from the timer queue */ - - list_delete_fast(&wdog->node); + /* If the watchdog was at the head of the queue, re-arm or + * cancel the driver hrtimer as appropriate. + */ - /* Mark the watchdog inactive */ + FAR hrtimer_t *first = hrtimer_queue_first(&g_wdqueue); - wdog->func = NULL; - - if (first == wdog && !wd_in_callback()) + if (first) { - /* If the watchdog is at the head of the timer queue, then - * we will need to re-adjust the interval timer that will - * generate the next interval event. - */ - - if (!list_is_empty(&g_wdactivelist)) - { - wd_timer_start(wd_next_expire(), false); - } - else - { - wd_timer_cancel(); - } + hrtimer_start_absolute(&g_wdtimer, wd_timer, first->expired); + } + else + { + hrtimer_cancel(&g_wdtimer); } - - ret = OK; } - leave_critical_section(flags); - sched_note_wdog_cancel((FAR void *)wdog->func, - (FAR void *)(uintptr_t)wdog->expired); + ret = OK; } + leave_critical_section(flags); + + sched_note_wdog_cancel((FAR void *)func, (FAR void *)(uintptr_t)expired); + return ret; } diff --git a/sched/wdog/wd_gettime.c b/sched/wdog/wd_gettime.c deleted file mode 100644 index 8fe8e49a59148..0000000000000 --- a/sched/wdog/wd_gettime.c +++ /dev/null @@ -1,77 +0,0 @@ -/**************************************************************************** - * sched/wdog/wd_gettime.c - * - * SPDX-License-Identifier: Apache-2.0 - * - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. The - * ASF licenses this file to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance with the - * License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT - * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the - * License for the specific language governing permissions and limitations - * under the License. - * - ****************************************************************************/ - -/**************************************************************************** - * Included Files - ****************************************************************************/ - -#include - -#include -#include - -#include "wdog/wdog.h" - -/**************************************************************************** - * Public Functions - ****************************************************************************/ - -/**************************************************************************** - * Name: wd_gettime - * - * Description: - * This function returns the time remaining before the specified watchdog - * timer expires. - * - * Input Parameters: - * wdog - watchdog ID - * - * Returned Value: - * The time in system ticks remaining until the watchdog time expires. - * Zero means either that wdog is not valid or that the wdog has already - * expired. - * - ****************************************************************************/ - -clock_t wd_gettime(FAR struct wdog_s *wdog) -{ - irqstate_t flags; - clock_t expired; - bool is_active; - clock_t delay = 0; - - if (wdog != NULL && WDOG_ISACTIVE(wdog)) - { - flags = enter_critical_section(); - is_active = WDOG_ISACTIVE(wdog); - expired = wdog->expired; - leave_critical_section(flags); - - if (is_active) - { - delay = expired - clock_systime_ticks(); - delay = delay >= 0 ? delay : 0; - } - } - - return delay; -} diff --git a/sched/wdog/wd_initialize.c b/sched/wdog/wd_initialize.c index 6a05753e39d76..7ff1529157ed3 100644 --- a/sched/wdog/wd_initialize.c +++ b/sched/wdog/wd_initialize.c @@ -34,35 +34,24 @@ * Public Data ****************************************************************************/ -/* The g_wdactivelist data structure is a singly linked list ordered by - * watchdog expiration time. When watchdog timers expire,the functions on - * this linked list are removed and the function is called. +/* HRTimer queue for all active watchdog timers. + * The queue is ordered by absolute expiration time and may be empty. + * All queue mutations are protected by enter_critical_section() to + * guarantee that the timeout processing executes atomically. */ -#undef g_wdactivelist -DEFINE_PER_CPU_BSS_BMP(struct list_node, g_wdactivelist); -#define g_wdactivelist this_cpu_var_bmp(g_wdactivelist) +#undef g_wdqueue +DEFINE_PER_CPU_BSS_BMP(struct hrtimer_queue_s, g_wdqueue); +#define g_wdqueue this_cpu_var_bmp(g_wdqueue) -#ifdef CONFIG_HRTIMER -# undef g_wdtimer -DEFINE_PER_CPU_BSS_BMP(struct hrtimer_s, g_wdtimer); -# define g_wdtimer this_cpu_var_bmp(g_wdtimer) -#endif - -# undef g_wdtimernested -DEFINE_PER_CPU_BSS_BMP(bool, g_wdtimernested); -# define g_wdtimernested this_cpu_var_bmp(g_wdtimernested) - -#ifdef CONFIG_HRTIMER - -#endif - -#if defined(CONFIG_SCHED_TICKLESS) || defined(CONFIG_HRTIMER) +/* HRTimer that sits in the default (system) hrtimer queue. + * When it fires, its callback wd_timer() processes all expired watchdog + * timers from g_wdqueue. + */ -#undef g_wdexpired -DEFINE_PER_CPU_BSS_BMP(clock_t, g_wdexpired); -# define g_wdexpired this_cpu_var_bmp(g_wdexpired) -#endif +#undef g_wdtimer +DEFINE_PER_CPU_BSS_BMP(struct hrtimer_s, g_wdtimer); +#define g_wdtimer this_cpu_var_bmp(g_wdtimer) /**************************************************************************** * Public Functions diff --git a/sched/wdog/wd_start.c b/sched/wdog/wd_start.c index 7cb3e8e0481c2..ed8504d8eec65 100644 --- a/sched/wdog/wd_start.c +++ b/sched/wdog/wd_start.c @@ -37,9 +37,11 @@ #include #include +#include #include #include +#include "hrtimer/hrtimer.h" #include "sched/sched.h" #include "wdog/wdog.h" @@ -89,174 +91,12 @@ * Private Functions ****************************************************************************/ -/**************************************************************************** - * Name: wd_expiration - * - * Description: - * Check if the timer for the watchdog at the head of list is ready to - * run. If so, remove the watchdog from the list and execute it. - * - * Input Parameters: - * ticks - current time in ticks - * - * Returned Value: - * None - * - ****************************************************************************/ - -static inline_function clock_t wd_expiration(clock_t ticks) -{ - FAR struct wdog_s *wdog; - irqstate_t flags; - wdentry_t func; - wdparm_t arg; - clock_t next_ticks = ticks; - - flags = enter_critical_section(); - - wd_update_expire(ticks); - - wd_set_nested(true); - - /* Process the watchdog at the head of the list as well as any - * other watchdogs that became ready to run at this time - */ - - while (!list_is_empty(&g_wdactivelist)) - { - wdog = list_first_entry(&g_wdactivelist, struct wdog_s, node); - - /* Check if watchdog has expired; - * re-evaluate after updating current ticks if needed - */ - - if (!clock_compare(wdog->expired, ticks)) - { - next_ticks = wdog->expired; - break; - } - - /* Remove the watchdog from the head of the list */ - - list_delete_fast(&wdog->node); - - /* Indicate that the watchdog is no longer active. */ - - func = wdog->func; - arg = wdog->arg; - wdog->func = NULL; - - /* Execute the watchdog function */ - - up_setpicbase(wdog->picbase); - CALL_FUNC(func, arg); - } - - wd_set_nested(false); - - if (next_ticks != ticks) - { - wd_timer_start(next_ticks, true); - } - - leave_critical_section(flags); - - return next_ticks; -} - -/**************************************************************************** - * Name: wd_insert - * - * Description: - * Insert the timer into the global list to ensure that - * the list is sorted in increasing order of expiration absolute time. - * - * Input Parameters: - * wdog - Watchdog ID - * expired - expired absolute time in clock ticks - * wdentry - Function to call on timeout - * arg - Parameter to pass to wdentry - * - * Assumptions: - * wdog and wdentry is not NULL. - * - * Returned Value: - * Whether the head of the watchdog list has changed. - * - ****************************************************************************/ - -static inline_function -bool wd_insert(FAR struct wdog_s *wdog, clock_t expired, - wdentry_t wdentry, wdparm_t arg) -{ - FAR struct wdog_s *curr; - FAR struct wdog_s *head; - - wdog->func = wdentry; - up_getpicbase(&wdog->picbase); - wdog->arg = arg; - wdog->expired = expired; - - /* Empty list: insert directly as the first active watchdog. */ - - if (list_is_empty(&g_wdactivelist)) - { - list_add_head(&g_wdactivelist, &wdog->node); - return true; - } - - /* Traverse the watchdog list */ - - head = list_first_entry(&g_wdactivelist, struct wdog_s, node); - - /* New earliest expiration: insert at the head and reassess timer. */ - - if (!clock_compare(head->expired, expired)) - { - list_add_head(&g_wdactivelist, &wdog->node); - return true; - } - - curr = list_last_entry(&g_wdactivelist, struct wdog_s, node); - - /* New latest expiration: append at the tail without scanning. */ - - if (clock_compare(curr->expired, expired)) - { - list_add_tail(&g_wdactivelist, &wdog->node); - return false; - } - - list_for_every_entry(&g_wdactivelist, curr, struct wdog_s, node) - { - /* Until curr->expired has not timed out relative to expired */ - - if (!clock_compare(curr->expired, expired)) - { - break; - } - } - - /* There are two cases: - * - Traverse to the end, where curr == &g_wdactivelist. - * - Find a curr such that curr->expected has not timed out - * relative to expired. - * In either case 1 or 2, we just insert the wdog before curr. - */ - - list_add_before(&curr->node, &wdog->node); - - /* Return whether the head of the watchdog list has changed. */ - - return head == curr; -} - /**************************************************************************** * Public Functions ****************************************************************************/ /**************************************************************************** - * Name: wd_start_abstick + * Name: wd_start_abstick_internal * * Description: * This function adds a watchdog timer to the active timer queue. The @@ -286,73 +126,59 @@ bool wd_insert(FAR struct wdog_s *wdog, clock_t expired, * indicate the nature of any failure. * * Assumptions: + * The wdog and wdentry is not NULL. * The watchdog routine runs in the context of the timer interrupt handler * and is subject to all ISR restrictions. * ****************************************************************************/ -int wd_start_abstick(FAR struct wdog_s *wdog, clock_t ticks, - wdentry_t wdentry, wdparm_t arg) +int wd_start_abstick_internal(FAR struct wdog_s *wdog, uint64_t nsec, + wdentry_t wdentry, wdparm_t arg) { - irqstate_t flags; bool reassess = false; - int ret = -EINVAL; - - /* Verify the wdog and setup parameters */ + irqstate_t flags = enter_critical_section(); - if (wdog != NULL && wdentry != NULL) - { - /* NOTE: There is a race condition here... the caller may receive - * the watchdog between the time that wd_start_abstick is called and - * the critical section is established. - */ + DEBUGASSERT(wdog && wdentry); - flags = enter_critical_section(); + /* If the wdog is already active, remove it from the wdog queue first. */ - /* If the wdog is canceling, restarting the wdog is not allowed. */ + if (hrtimer_is_pending(&wdog->timer)) + { + reassess = hrtimer_queue_remove(&g_wdqueue, &wdog->timer); + } -#if defined(CONFIG_SCHED_TICKLESS) || defined(CONFIG_HRTIMER) - /* We need to reassess timer if the watchdog - * list head has changed. - */ + /* Populate wdog fields. The user callback (wdentry_t) is stored + * directly in timer.func, reusing the hrtimer's func field. This + * is safe because the wdog queue is only processed by wd_timer(), + * which casts it back to wdentry_t before invoking. + */ - if (WDOG_ISACTIVE(wdog)) - { - reassess |= list_is_head(&g_wdactivelist, &wdog->node); - list_delete_fast(&wdog->node); - } + wdog->arg = arg; +#ifdef CONFIG_PIC + up_getpicbase(&wdog->picbase); +#endif - reassess |= wd_insert(wdog, ticks, wdentry, arg); - reassess &= !wd_in_callback(); + wdog->timer.func = (hrtimer_entry_t)wdentry; + wdog->timer.expired = nsec; - if (reassess) - { - /* Resume the interval timer that will generate the next - * interval event. If the timer at the head of the list - * changed, then this will pick that new delay. - */ + reassess |= hrtimer_queue_insert(&g_wdqueue, &wdog->timer); - wd_timer_start(wd_next_expire(), false); - } -#else - UNUSED(reassess); + /* Re-arm the driver hrtimer if the queue head changed and we are + * not currently inside wd_timer() processing (in which case wd_timer() + * will return the correct next delay itself). + */ - /* Check if the watchdog has been started. If so, delete it. */ + if (reassess && !wd_in_callback()) + { + hrtimer_start_absolute(&g_wdtimer, wd_timer, + hrtimer_queue_first(&g_wdqueue)->expired); + } - if (WDOG_ISACTIVE(wdog)) - { - list_delete_fast(&wdog->node); - } + leave_critical_section(flags); - wd_insert(wdog, ticks, wdentry, arg); -#endif - leave_critical_section(flags); - sched_note_wdog_start(wdentry, - (FAR void *)(uintptr_t)ticks); - ret = OK; - } + sched_note_wdog_start(wdentry, (FAR void *)(uintptr_t)nsec); - return ret; + return OK; } /**************************************************************************** @@ -380,23 +206,50 @@ int wd_start_abstick(FAR struct wdog_s *wdog, clock_t ticks, * ****************************************************************************/ -#ifndef CONFIG_HRTIMER -void wd_timer(clock_t ticks) +uint64_t wd_timer(FAR const struct hrtimer_s *timer, uint64_t expired) { - /* Check if there are any active watchdogs to process */ + wdentry_t func; + wdparm_t arg; + FAR struct wdog_s *wdog; + uint64_t next_expired = expired; + irqstate_t flags = enter_critical_section(); + FAR hrtimer_t *first = hrtimer_queue_first(&g_wdqueue); + + /* wd_in_callback() below distinguishes the in-progress processing + * through the hrtimer running slot, so wd_start()/wd_cancel() called + * from the callbacks skip re-arming the driver hrtimer: wd_timer() + * computes and returns the next delay itself. + */ - wd_expiration(ticks); -} -#else -uint64_t wd_timer(const hrtimer_t *timer, uint64_t expired) -{ - /* Check if there are any active watchdogs to process */ + /* Process all watchdog timers that have expired. The queue may be + * empty, in which case hrtimer_queue_first() returns NULL. + */ - clock_t tick = div_const(expired, NSEC_PER_TICK); - clock_t delay = wd_expiration(tick) - tick; - uint64_t nsec = delay <= HRTIMER_MAX_DELAY / NSEC_PER_TICK ? - TICK2NSEC(delay) : HRTIMER_MAX_DELAY; + for (; first != NULL; first = hrtimer_queue_first(&g_wdqueue)) + { + if (!HRTIMER_TIME_BEFORE_EQ(first->expired, expired)) + { + next_expired = first->expired; + break; + } - return nsec; -} + wdog = container_of(first, struct wdog_s, timer); + func = (wdentry_t)first->func; + arg = wdog->arg; + + hrtimer_queue_remove(&g_wdqueue, first); + +#ifdef CONFIG_PIC + up_setpicbase(wdog->picbase); #endif + + /* Invoke the user callback with critical-sections. */ + + CALL_FUNC(func, arg); + } + + leave_critical_section(flags); + + return next_expired - expired <= HRTIMER_MAX_DELAY ? + next_expired - expired : HRTIMER_MAX_DELAY; +} diff --git a/sched/wdog/wdog.h b/sched/wdog/wdog.h index 873005b083c6a..3ef62325bd92b 100644 --- a/sched/wdog/wdog.h +++ b/sched/wdog/wdog.h @@ -38,10 +38,9 @@ #include #include #include +#include -#ifdef CONFIG_HRTIMER -# include -#endif +#include "hrtimer/hrtimer.h" /**************************************************************************** * Pre-processor Definitions @@ -56,25 +55,22 @@ extern "C" { #endif -/* The g_wdactivelist data structure is a singly linked list ordered by - * watchdog expiration time. When watchdog timers expire,the functions on - * this linked list are removed and the function is called. +/* HRTimer queue for all active watchdog timers. + * The queue is ordered by absolute expiration time and may be empty. + * All queue mutations are protected by enter_critical_section() to + * guarantee that the timeout processing executes atomically. */ -DECLARE_PER_CPU_BMP(struct list_node, g_wdactivelist); -#define g_wdactivelist this_cpu_var_bmp(g_wdactivelist) +DECLARE_PER_CPU_BMP(struct hrtimer_queue_s, g_wdqueue); +#define g_wdqueue this_cpu_var_bmp(g_wdqueue) -#ifdef CONFIG_HRTIMER -DECLARE_PER_CPU_BMP(struct hrtimer_s, g_wdtimer); -# define g_wdtimer this_cpu_var_bmp(g_wdtimer) -#endif +/* HRTimer that sits in the default (system) hrtimer queue. + * When it fires, its callback wd_timer() processes all expired watchdog + * timers from g_wdqueue. + */ -#if defined(CONFIG_SCHED_TICKLESS) || defined(CONFIG_HRTIMER) -DECLARE_PER_CPU_BMP(bool, g_wdtimernested); -# define g_wdtimernested this_cpu_var_bmp(g_wdtimernested) -DECLARE_PER_CPU_BMP(clock_t, g_wdexpired); -# define g_wdexpired this_cpu_var_bmp(g_wdexpired) -#endif +DECLARE_PER_CPU_BMP(struct hrtimer_s, g_wdtimer); +#define g_wdtimer this_cpu_var_bmp(g_wdtimer) /**************************************************************************** * Public Function Prototypes @@ -106,105 +102,20 @@ DECLARE_PER_CPU_BMP(clock_t, g_wdexpired); * ****************************************************************************/ -#ifndef CONFIG_HRTIMER -void wd_timer(clock_t ticks); -#else -uint64_t wd_timer(const hrtimer_t *timer, uint64_t expired); -#endif +uint64_t wd_timer(FAR const struct hrtimer_s *timer, uint64_t expired); /**************************************************************************** * Inline functions ****************************************************************************/ -#if defined(CONFIG_SCHED_TICKLESS) || defined(CONFIG_HRTIMER) -# define wd_in_callback() (g_wdtimernested) -# define wd_set_nested(f) (g_wdtimernested = (f)) -# define wd_update_expire(expired) (g_wdexpired = (expired)) -#else -# define wd_in_callback() (false) -# define wd_set_nested(f) -# define wd_update_expire(expired) -#endif - -#ifdef CONFIG_HRTIMER -static inline_function void wd_timer_start(clock_t tick, bool in_expiration) -{ - if (!in_expiration) - { - clock_t interval = clock_compare(g_wdexpired, tick) ? - tick - g_wdexpired : 0; - uint64_t nsec = interval <= HRTIMER_MAX_DELAY / NSEC_PER_TICK ? - TICK2NSEC(g_wdexpired + interval) : - TICK2NSEC(g_wdexpired) + HRTIMER_MAX_DELAY; - hrtimer_start(&g_wdtimer, wd_timer, nsec, HRTIMER_MODE_ABS); - } -} - -static inline_function void wd_timer_cancel(void) -{ - hrtimer_cancel(&g_wdtimer); -} -#elif defined(CONFIG_SCHED_TICKLESS) -static inline_function clock_t wd_adjust_next_tick(clock_t tick) -{ - clock_t next_tick = tick; -#ifdef CONFIG_SCHED_TICKLESS_LIMIT_MAX_SLEEP - clock_t interval = clock_compare(g_wdexpired, tick) ? - tick - g_wdexpired : 0u; - interval = interval <= g_oneshot_maxticks ? - interval : g_oneshot_maxticks; - next_tick = g_wdexpired + interval; -#endif - -#if CONFIG_TIMER_ADJUST_NSEC > 0 - /* Normally, timer event cannot triggered on exact time due to the - * existence of interrupt latency. - * Assuming that the interrupt latency is distributed within - * [Best-Case Execution Time, Worst-Case Execution Time], - * we can set the timer adjustment value to the BCET to reduce the latency. - * After the adjustment, the timer interrupt latency will be - * [0, WCET - BCET]. - * Please use this carefully, if the timer adjustment value is not the - * best-case interrupt latency, it will immediately fired another timer - * interrupt, which may result in a much larger timer interrupt latency. - */ - - next_tick -= CONFIG_TIMER_ADJUST_NSEC / NSEC_PER_TICK; -#endif - - return next_tick; -} - -static inline_function void wd_timer_start(clock_t tick, bool in_expiration) -{ - clock_t next_tick = wd_adjust_next_tick(tick); -#ifdef CONFIG_SCHED_TICKLESS_ALARM - up_alarm_tick_start(next_tick); -#else - clock_t now = clock_systime_ticks(); - clock_t delay = clock_compare(now, next_tick) ? next_tick - now : 0; - up_timer_tick_start(delay); -#endif -} - -static inline_function void wd_timer_cancel(void) -{ - struct timespec ts; -#ifdef CONFIG_SCHED_TICKLESS_ALARM - up_alarm_cancel(&ts); -#else - up_timer_cancel(&ts); -#endif -} -#else -# define wd_timer_start(next_tick, in_expiration) -# define wd_timer_cancel() -#endif +/* While wd_timer() runs, the hrtimer core keeps this CPU's running slot + * (g_hrtimer_running) set to g_wdtimer, so wd_start()/wd_cancel() + * invoked from a watchdog callback can detect the in-progress processing + * via wd_in_callback() and skip re-arming the driver hrtimer. The + * XOR-compare also matches the cancel-marked form (slot = timer | 1). + */ -static inline_function clock_t wd_next_expire(void) -{ - return list_first_entry(&g_wdactivelist, struct wdog_s, node)->expired; -} +#define wd_in_callback() hrtimer_is_referenced(&g_wdtimer, this_cpu()) /**************************************************************************** * Public Function Prototypes @@ -212,12 +123,13 @@ static inline_function clock_t wd_next_expire(void) static inline_function clock_t wd_get_next_expire(clock_t curr) { - clock_t next = curr; - irqstate_t flags = enter_critical_section(); + clock_t next = curr; + irqstate_t flags = enter_critical_section(); + FAR hrtimer_t *first = hrtimer_queue_first(&g_wdqueue); - if (!list_is_empty(&g_wdactivelist)) + if (first != NULL) { - next = wd_next_expire(); + next = NSEC2TICK(first->expired); } leave_critical_section(flags); @@ -234,7 +146,7 @@ static inline_function clock_t wd_get_next_expire(clock_t curr) static inline_function void wdlist_initialize(void) { - list_initialize(&g_wdactivelist); + hrtimer_queue_initialize(&g_wdqueue); } #ifdef __cplusplus diff --git a/sched/wqueue/kwork_queue.c b/sched/wqueue/kwork_queue.c index 2b0e4413a64eb..9faa84e77fc4f 100644 --- a/sched/wqueue/kwork_queue.c +++ b/sched/wqueue/kwork_queue.c @@ -77,7 +77,7 @@ static int work_qqueue(FAR struct kwork_wqueue_s *wqueue, bool retimer; if (wqueue == NULL || work == NULL || worker == NULL || - delay < 0 || delay > WDOG_MAX_DELAY) + (uint64_t)delay > HRTIMER_MAX_DELAY / NSEC_PER_TICK) { return -EINVAL; } diff --git a/sched/wqueue/kwork_thread.c b/sched/wqueue/kwork_thread.c index b5c5f0d49415e..030496f962748 100644 --- a/sched/wqueue/kwork_thread.c +++ b/sched/wqueue/kwork_thread.c @@ -25,6 +25,7 @@ ****************************************************************************/ #include +#include #include #include @@ -488,31 +489,18 @@ void work_queue_dispatch(FAR struct kwork_wqueue_s *wqueue) * ****************************************************************************/ -#ifdef CONFIG_HRTIMER uint64_t work_timer_expired(FAR const hrtimer_t *timer, uint64_t expired) -{ - FAR struct kwork_wqueue_s *wq = - container_of(timer, struct kwork_wqueue_s, timer); - - UNUSED(expired); - - /* Wake up at least one worker thread to dispatch the expired work. */ - - nxsem_post(&wq->sem); - return 0; -} -#else -void work_timer_expired(wdparm_t arg) { /* The work time expired callback will wake up at least one worker thread * to dispatch the expired work. */ - FAR struct kwork_wqueue_s *wq = (FAR struct kwork_wqueue_s *)arg; + FAR struct kwork_wqueue_s *wq = container_of(timer, struct kwork_wqueue_s, + timer); nxsem_post(&wq->sem); + return 0; } -#endif /**************************************************************************** * Name: work_queue_alloc @@ -547,7 +535,7 @@ static FAR struct kwork_wqueue_s *work_queue_alloc(int nthreads) list_initialize(&wqueue->expired); list_initialize(&wqueue->pending); - wqueue->timer.func = NULL; + hrtimer_init(&wqueue->timer); nxsem_init(&wqueue->sem, 0, 0); nxsem_init(&wqueue->exsem, 0, 0); wqueue->nthreads = nthreads; diff --git a/sched/wqueue/wqueue.h b/sched/wqueue/wqueue.h index 6d53f677c3eb3..f0dfead03f539 100644 --- a/sched/wqueue/wqueue.h +++ b/sched/wqueue/wqueue.h @@ -38,10 +38,7 @@ #include #include #include - -#ifdef CONFIG_HRTIMER -# include -#endif +#include #ifdef CONFIG_SCHED_WORKQUEUE @@ -90,11 +87,7 @@ struct kwork_wqueue_s #ifdef CONFIG_SMP atomic_t nposting; /* Wakeups posted after the unlock */ #endif -#ifdef CONFIG_HRTIMER - hrtimer_t timer; /* Timer of the pending queue */ -#else - struct wdog_s timer; /* Timer of the pending queue */ -#endif + struct hrtimer_s timer; /* Timer of the pending queue */ }; /* This structure defines the state of one high-priority work queue. This @@ -296,11 +289,7 @@ bool work_remove(FAR struct kwork_wqueue_s *wqueue, * ****************************************************************************/ -#ifdef CONFIG_HRTIMER uint64_t work_timer_expired(FAR const hrtimer_t *timer, uint64_t expired); -#else -void work_timer_expired(wdparm_t arg); -#endif /**************************************************************************** * Name: work_timer_isactive/work_timer_cancel/work_timer_cancel_sync @@ -311,16 +300,10 @@ void work_timer_expired(wdparm_t arg); * ****************************************************************************/ -#ifdef CONFIG_HRTIMER -# define work_timer_isactive(wqueue) ((wqueue)->timer.func != NULL) +# define work_timer_isactive(wqueue) hrtimer_is_active(&(wqueue)->timer) # define work_timer_cancel(wqueue) hrtimer_cancel(&(wqueue)->timer) # define work_timer_cancel_sync(wqueue) \ hrtimer_cancel_sync(&(wqueue)->timer) -#else -# define work_timer_isactive(wqueue) WDOG_ISACTIVE(&(wqueue)->timer) -# define work_timer_cancel(wqueue) wd_cancel(&(wqueue)->timer) -# define work_timer_cancel_sync(wqueue) wd_cancel(&(wqueue)->timer) -#endif /**************************************************************************** * Name: work_timer_start @@ -329,10 +312,9 @@ void work_timer_expired(wdparm_t arg); * Internal public function to start the timer of the workqueue at the * absolute tick of the first pending work. * - * With CONFIG_HRTIMER the timer is an hrtimer, which has its own lock. - * So the caller can hold wqueue->lock: the order is always wqueue->lock - * first, the hrtimer lock second. wd_start_abstick() enters the - * critical section instead. + * The timer is an hrtimer, which has its own lock. So the caller can + * hold wqueue->lock: the order is always wqueue->lock first, the + * hrtimer lock second. * * Input Parameters: * wqueue - The work queue. @@ -346,13 +328,8 @@ void work_timer_expired(wdparm_t arg); static inline_function void work_timer_start(FAR struct kwork_wqueue_s *wqueue, clock_t qtime) { -#ifdef CONFIG_HRTIMER hrtimer_start(&wqueue->timer, work_timer_expired, TICK2NSEC(qtime), HRTIMER_MODE_ABS); -#else - wd_start_abstick(&wqueue->timer, qtime, - work_timer_expired, (wdparm_t)wqueue); -#endif } /****************************************************************************