anchao commented on code in PR #17675: URL: https://github.com/apache/nuttx/pull/17675#discussion_r2646901476
########## include/nuttx/hrtimer_queue.h: ########## @@ -0,0 +1,533 @@ +/**************************************************************************** + * include/nuttx/hrtimer_queue.h + * + * 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. + * + ****************************************************************************/ + +#ifndef __INCLUDE_HRTIMER_QUEUE_H +#define __INCLUDE_HRTIMER_QUEUE_H + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include <nuttx/config.h> + +#include <nuttx/compiler.h> +#include <nuttx/clock.h> +#include <nuttx/seqlock.h> + +#include <nuttx/hrtimer_queue_type.h> + +#include <stdint.h> + +/* This header file should be only included for internal use, + * DO NOT EXPOSE IT TO USERS. + * + * Before including this file, Please provide the following inputs: + * + * Include the hrtimer_type_xxx.h header file. This header file should + * provide the implementation of the queue operations and definition for + * internal hrtimer. + * + * Function implementation: static inline_function + * void hrtimer_reprogram(FAR USER_HRTIMER_QUEUE_TYPE *queue, + * uint64_t next_expired); + * - Reprogram the timer hardware to the next expired time. + */ + +#ifdef __cplusplus +#define EXTERN extern "C" +extern "C" +{ +#else +#define EXTERN extern +#endif + +/**************************************************************************** + * Inline function + ****************************************************************************/ + +/* The relied function hrtimer_reprogram must be implemented. + * Please note the forward declaration and reverse dependency here. + * We instead of the function pointers because most functional-safety + * compilers (E.g. GHC, Tasking and CompCert C) do not support inlining + * function pointers, which would introduce additional memory and + * performance overhead. + */ + +static inline_function +void hrtimer_reprogram(FAR hrtimer_queue_internal_t *queue, + uint64_t next_expired); + +/* Reusable library code for user-defined high-resolution timer queue. */ + +/**************************************************************************** + * Name: hrtimer_queue_init + * + * Description: + * Initialize the hrtimer queue. + * + * Input Parameters: + * queue - The timer queue. + * guard_timer - The guard timer. + * + * Returned Value: + * None. + * + ****************************************************************************/ + +static inline_function +void hrtimer_queue_init(FAR hrtimer_queue_internal_t *queue) +{ + FAR hrtimer_internal_t *guard_timer = &queue->guard_timer; Review Comment: add null pointer check or remove debug assert on line 102 ########## include/nuttx/hrtimer_queue.h: ########## @@ -0,0 +1,533 @@ +/**************************************************************************** + * include/nuttx/hrtimer_queue.h + * + * 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. + * + ****************************************************************************/ + +#ifndef __INCLUDE_HRTIMER_QUEUE_H +#define __INCLUDE_HRTIMER_QUEUE_H + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include <nuttx/config.h> + +#include <nuttx/compiler.h> +#include <nuttx/clock.h> +#include <nuttx/seqlock.h> + +#include <nuttx/hrtimer_queue_type.h> + +#include <stdint.h> + +/* This header file should be only included for internal use, + * DO NOT EXPOSE IT TO USERS. + * + * Before including this file, Please provide the following inputs: + * + * Include the hrtimer_type_xxx.h header file. This header file should + * provide the implementation of the queue operations and definition for + * internal hrtimer. + * + * Function implementation: static inline_function + * void hrtimer_reprogram(FAR USER_HRTIMER_QUEUE_TYPE *queue, + * uint64_t next_expired); + * - Reprogram the timer hardware to the next expired time. + */ + +#ifdef __cplusplus +#define EXTERN extern "C" +extern "C" +{ +#else +#define EXTERN extern +#endif + +/**************************************************************************** + * Inline function + ****************************************************************************/ + +/* The relied function hrtimer_reprogram must be implemented. + * Please note the forward declaration and reverse dependency here. + * We instead of the function pointers because most functional-safety + * compilers (E.g. GHC, Tasking and CompCert C) do not support inlining + * function pointers, which would introduce additional memory and + * performance overhead. + */ + +static inline_function +void hrtimer_reprogram(FAR hrtimer_queue_internal_t *queue, + uint64_t next_expired); + +/* Reusable library code for user-defined high-resolution timer queue. */ + +/**************************************************************************** + * Name: hrtimer_queue_init + * + * Description: + * Initialize the hrtimer queue. + * + * Input Parameters: + * queue - The timer queue. + * guard_timer - The guard timer. + * + * Returned Value: + * None. + * + ****************************************************************************/ + +static inline_function +void hrtimer_queue_init(FAR hrtimer_queue_internal_t *queue) +{ + FAR hrtimer_internal_t *guard_timer = &queue->guard_timer; + int cpu; + + DEBUGASSERT(queue); + + /* The guard timer is designed to ensure the system will enter the safe + * state if the timing system is not working properly. + * It can be customized by the user after the hrtimer_queue_init and + * before the first user hrtimer is added. + * If the guard timer fired, it means 292-years have passed, which is + * impossible. In this case we should trigger the kernel panic and reboot + * the system. Here we assume the system will reboot after jumping to NULL. + */ + + hrtimer_fill(guard_timer, NULL, NULL, INT64_MAX); + + seqlock_init(&queue->lock); + + hrtimer_queue_clear(queue); + + queue->next_expired = UINT64_MAX; + hrtimer_queue_add(queue, guard_timer); + + memset(queue->running, 0, sizeof(queue->running)); +} + +/**************************************************************************** + * Name: hrtimer_queue_read_64/32 + * + * Description: + * Internal function to read the value in the queue atomically. + * Do not use this function if you are not sure about the thread-safe + * of the value you are reading. + * + * Input Parameters: + * queue - The timer queue. + * ptr - The pointer to be read. + * + * Returned Value: + * The value in the queue. + * + ****************************************************************************/ + +static inline_function +uint64_t hrtimer_queue_read_64(FAR hrtimer_queue_internal_t *queue, + FAR const uint64_t *ptr) +{ + uint64_t val; + + DEBUGASSERT(queue && ptr); + +#ifdef CONFIG_ARCH_64BIT + /* On 64-bit architectures, read/write uint64_t is atomic. */ + + val = *((FAR volatile uint64_t *)ptr); +#else + uint32_t seq; + + do + { + seq = read_seqbegin(&queue->lock); + val = *ptr; + } + while (read_seqretry(&queue->lock, seq)); +#endif + + return val; +} + +static inline_function +uint32_t hrtimer_queue_read_32(FAR hrtimer_queue_internal_t *queue, + FAR const uint32_t *ptr) +{ + uint32_t val; + + DEBUGASSERT(queue && ptr); + +#if (UINT_MAX >= UINT32_MAX) + /* On 32/64-bit architectures, read/write uint32_t is atomic. */ + + val = *((FAR volatile uint32_t *)ptr); +#else + uint32_t seq; + + do + { + seq = read_seqbegin(&queue->lock); + val = *ptr; + } + while (read_seqretry(&queue->lock, seq)); +#endif + + return val; +} + +/* Generic function to read the value in the queue atomically. */ + +#define hrtimer_queue_read(queue, ptr) \ + (sizeof(*(ptr)) == 8u ? \ + hrtimer_queue_read_64(queue, (FAR const uint64_t *)(ptr)) : \ + (sizeof(*(ptr)) == 4u ? \ + hrtimer_queue_read_32(queue, (FAR const uint32_t *)(ptr)) : 0u)) + +/**************************************************************************** + * Name: hrtimer_queue_expiry + * + * Description: + * Process the time expiration of the hrtimer queue. + * + * Input Parameters: + * queue - The hrtimer queue. + * current - The current time. + * reprogram - The timer reprogram function. + * + * Returned Value: + * The next expiration time of the hrtimer queue. + * + ****************************************************************************/ + +static inline_function +uint64_t hrtimer_queue_expiry(FAR hrtimer_queue_internal_t *queue, + uint64_t current) +{ + FAR hrtimer_internal_t *timer; + FAR void *arg; + hrtimer_callback_t func; + uint64_t next_expired; + uint64_t expired; + int cpu = this_cpu(); Review Comment: cpu may changed without lock ########## include/nuttx/hrtimer.h: ########## @@ -28,190 +28,182 @@ ****************************************************************************/ #include <nuttx/config.h> -#include <nuttx/clock.h> -#include <nuttx/compiler.h> -#include <nuttx/spinlock.h> -#include <stdint.h> -#include <sys/tree.h> +#include <nuttx/hrtimer_queue_type.h> /**************************************************************************** * Public Types ****************************************************************************/ -/* High-resolution timer modes - * - * HRTIMER_MODE_ABS - Absolute expiration time - * HRTIMER_MODE_REL - Relative timeout from the current time - */ - -enum hrtimer_mode_e -{ - HRTIMER_MODE_ABS = 0, /* Absolute expiration time */ - HRTIMER_MODE_REL /* Relative delay from now */ -}; - -/* High-resolution timer states - * - * State transitions are managed internally by the hrtimer framework. - * Callers must not modify the state directly. - */ +#ifdef CONFIG_HRTIMER_LIST +typedef struct hrtimer_list_s hrtimer_t; +typedef struct hrtimer_list_queue_s hrtimer_queue_t; +#else +typedef struct hrtimer_rb_s hrtimer_t; +typedef struct hrtimer_rb_queue_s hrtimer_queue_t; +#endif -enum hrtimer_state_e +#ifdef __cplusplus +#define EXTERN extern "C" +extern "C" { - HRTIMER_STATE_INACTIVE = 0, /* Timer is inactive and not queued */ - HRTIMER_STATE_ARMED, /* Timer is armed and waiting for expiry */ - HRTIMER_STATE_RUNNING, /* Timer callback is currently executing */ - HRTIMER_STATE_CANCELED /* Timer canceled (callback may be running) */ -}; - -/* Forward declarations */ +#else +#define EXTERN extern +#endif -struct hrtimer_s; -struct hrtimer_node_s; +/**************************************************************************** + * Public Function Prototypes + ****************************************************************************/ -typedef struct hrtimer_s hrtimer_t; -typedef struct hrtimer_node_s hrtimer_node_t; +/* Wrapped version for NuttX scheduler. */ -/* Callback type for high-resolution timer expiration +/**************************************************************************** + * Name: hrtimer_async_restart * - * The callback is invoked when the timer expires. It is executed in - * timer context and must not block. - */ - -typedef uint32_t (*hrtimer_cb)(FAR struct hrtimer_s *hrtimer); - -/* Red-black tree node used to order hrtimers by expiration time */ - -struct hrtimer_node_s -{ - RB_ENTRY(hrtimer_node_s) entry; /* RB-tree linkage */ -}; - -/* High-resolution timer object + * Description: + * Restart the hrtimer with relative or absolute time in nanoseconds. + * These functions allow restarting the hrtimer that has been set to + * cancelled state via `hrtimer_async_cancel`. + * Please be aware of concurrency issues. Concurrency errors are prone to + * occur in this use case. + * + * Input Parameters: + * timer - The hrtimer to be started. + * + * Returned Value: + * Zero on success. + * -EINVAL on if one of the parameter is invalid. + * + * Assumption: + * The timer and func should be not NULL. + * The timer should be cancelled or completed. * - * The timer is ordered by absolute expiration time and managed by the - * hrtimer core. The content of this structure should not be accessed - * directly by users except through the provided APIs. - */ - -struct hrtimer_s -{ - hrtimer_node_t node; /* RB-tree node for sorted insertion */ - enum hrtimer_state_e state; /* Current timer state */ - hrtimer_cb func; /* Expiration callback function */ - FAR void *arg; /* Argument passed to callback */ - uint64_t expired; /* Absolute expiration time (ns) */ -}; - -/**************************************************************************** - * Public Function Prototypes ****************************************************************************/ -#ifdef __cplusplus -#define EXTERN extern "C" -extern "C" -{ -#else -#define EXTERN extern -#endif +int hrtimer_async_restart(FAR hrtimer_t *timer); /**************************************************************************** - * Name: hrtimer_init + * Name: hrtimer_start * * Description: - * Initialize a high-resolution timer instance. This function sets the - * expiration callback and its argument. The timer is initialized in the - * inactive state and is not armed until hrtimer_start() is called. + * Start the hrtimer with relative or absolute time in nanoseconds. + * These functions can only be called when the caller has the ownership of + * the timer. * * Input Parameters: - * hrtimer - Pointer to the hrtimer instance to be initialized - * func - Expiration callback function - * arg - Argument passed to the callback + * timer - The hrtimer to be started. + * func - The callback function to be called when the hrtimer expires. + * arg - The argument to be passed to the callback function. + * time - The relative or absolute expiration time in nanoseconds. + * mode - The timer mode, relative or absolute. * * Returned Value: - * None + * Zero on success. + * -EINVAL on if one of the parameter is invalid or the timer is in pending + * state. + * ****************************************************************************/ static inline_function -void hrtimer_init(FAR hrtimer_t *hrtimer, - hrtimer_cb func, - FAR void *arg) +int hrtimer_restart(hrtimer_t *timer, hrtimer_callback_t func, + FAR void *arg, uint64_t time, uint32_t mode) { - hrtimer->state = HRTIMER_STATE_INACTIVE; - hrtimer->func = func; - hrtimer->arg = arg; + uint64_t expired = time; + + if (mode == HRTIMER_MODE_REL) + { + expired = time <= HRTIMER_MAX_DELAY ? time : HRTIMER_MAX_DELAY; + expired += clock_systime_nsec(); + } + + DEBUGASSERT(mode <= 1u); + + hrtimer_fill(timer, func, arg, expired); + return hrtimer_async_restart(timer); +} + +static inline_function +int hrtimer_start(FAR hrtimer_t *timer, hrtimer_callback_t func, Review Comment: why was the parameter prototype changed? If func/arg are determined in hrtimer_init or hrtimer_setup, it will yield better performance for the start process. -- 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]
