wangchdo commented on code in PR #17489: URL: https://github.com/apache/nuttx/pull/17489#discussion_r2617271908
########## sched/hrtimer/hrtimer_start.c: ########## @@ -0,0 +1,135 @@ +/**************************************************************************** + * sched/hrtimer/hrtimer_start.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 <nuttx/config.h> +#include <nuttx/arch.h> +#include <nuttx/clock.h> +#include <errno.h> +#include <hrtimer/hrtimer.h> + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: hrtimer_insert + * + * Description: + * Insert the given high-resolution timer into the active timer RB-tree. + * If the timer already exists, it will be removed and re-inserted. + * If the inserted timer is the earliest in the tree, start the hardware + * timer to fire at its expiration. + * + * Input Parameters: + * hrtimer - Pointer to the hrtimer structure to be inserted. + * + * Returned Value: + * OK (0) on success, or a negated errno value on failure. + * + * Assumptions/Notes: + * - This function should be called with interrupts disabled or under + * spinlock protection to ensure RB-tree integrity. + * - Inline function for performance in critical path. + ****************************************************************************/ + +static inline_function +int hrtimer_insert(FAR hrtimer_t *hrtimer) +{ + int ret = OK; + FAR struct hrtimer_node_s *inserted = + RB_INSERT(hrtimer_tree_s, &g_activetree, &hrtimer->node); + + if (inserted == NULL) + { + if (&hrtimer->node == RB_MIN(hrtimer_tree_s, &g_activetree)) + { + /* If new timer is the earliest, start hardware timer */ + + ret = hrtimer_starttimer(hrtimer->expired); Review Comment: I uploaded a change for nxsched_timer_start, please check. ``` static clock_t nxsched_timer_start(clock_t ticks, clock_t interval) { int ret; if (interval > 0) { #ifdef CONFIG_SCHED_TICKLESS_LIMIT_MAX_SLEEP if (interval > g_oneshot_maxticks) { interval = g_oneshot_maxticks; } #endif /* 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. */ interval = interval <= (CONFIG_TIMER_ADJUST_USEC / USEC_PER_TICK) ? 0 : interval - (CONFIG_TIMER_ADJUST_USEC / USEC_PER_TICK); #ifdef CONFIG_HRTIMER ret = nxsched_hrtimer_start(interval); #else # ifdef CONFIG_SCHED_TICKLESS_ALARM /* Convert the delay to a time in the future (with respect * to the time when last stopped the timer). */ ret = up_alarm_tick_start(ticks + interval); # else /* [Re-]start the interval timer */ ret = up_timer_tick_start(interval); # endif #endif if (ret < 0) { serr("ERROR: up_timer_start/up_alarm_start failed: %d\n", ret); UNUSED(ret); } } return interval; } ``` **The thing i want to emphasize here (which I also pointed out in the PR description) is: when hrtimer is enabled, hardware timer will be used by hrtimer module to work in nanosec level, and os tick will be driven by a hrtimer instance instead of the hardware timer directly, this hrtimer instance is to facility tick level resolution for scheduler** **so hrtimer works above hardware timer and below wdog timer / scheduler, and it is not hrtimer calling `nxsched_reassess_timer` it should be `nxsched_reassess_timer` calling hrtimer. also `nxsched_reassess_timer` is in tick level resolution, hrtimer is in nanosec resolution, hrtimer calling `nxsched_reassess_timer` will break the nanosec resolution by hrtimer** -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
