xiaoxiang781216 commented on code in PR #17675:
URL: https://github.com/apache/nuttx/pull/17675#discussion_r2659547292


##########
include/nuttx/hrtimer/hrtimer_queue.h:
##########
@@ -0,0 +1,703 @@
+/****************************************************************************
+ * include/nuttx/hrtimer/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` and `hrtimer_wait_policy` must be
+ * implemented:
+ *
+ * void hrtimer_reprogram(FAR hrtimer_queue_internal_t *queue,
+ *                        uint64_t next_expired);
+ *   - This function abstracts how to reprogram the timer hardware.
+ *
+ * void hrtimer_wait_policy(void);
+ *   - This function abstracts how to wait for the callback finished.
+ *
+ * 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);
+
+static inline_function
+void hrtimer_wait_policy(void);
+
+/* Queue dependent functions should be provided by `hrtimer_type_xxx.h`. */
+
+static inline_function
+FAR hrtimer_internal_t *hrtimer_queue_peek(
+                        FAR hrtimer_queue_internal_t *queue);
+
+static inline_function
+bool hrtimer_queue_add(FAR hrtimer_queue_t *queue, FAR hrtimer_t *timer);
+
+static inline_function
+bool hrtimer_queue_del(FAR hrtimer_queue_t *queue, FAR hrtimer_t *timer);
+
+static inline_function
+void hrtimer_queue_clear(FAR hrtimer_queue_t *queue);
+
+/* Reusable library code for user-defined high-resolution timer queue. */
+
+/****************************************************************************
+ * Name: hrtimer_queue_lock/unlock
+ *
+ * Description:
+ *   Lock/Unlock the hrtimer queue.
+ *
+ * Input Parameters:
+ *   queue - The timer queue.
+ *
+ * Returned Value:
+ *   The previous interrupt state.
+ *
+ ****************************************************************************/
+
+static inline_function
+void hrtimer_queue_lock_init(FAR hrtimer_queue_internal_t *queue)
+{
+  seqlock_init(&queue->lock);
+}
+
+static inline_function
+irqstate_t hrtimer_queue_lock(FAR hrtimer_queue_internal_t *queue)
+{
+  return write_seqlock_irqsave(&queue->lock);
+}
+
+static inline_function
+void hrtimer_queue_unlock(FAR hrtimer_queue_internal_t *queue,
+                          irqstate_t flags)
+{
+  write_sequnlock_irqrestore(&queue->lock, flags);
+}
+
+/****************************************************************************
+ * Name: hrtimer_queue_locked_read_64/32
+ *
+ * Description:
+ *   Read the value in the queue atomically.
+ *
+ * Input Parameters:
+ *   queue - The timer queue.
+ *
+ * Returned Value:
+ *   The value in the queue.
+ *
+ ****************************************************************************/
+
+static inline_function
+uint64_t hrtimer_queue_locked_read_64(FAR hrtimer_queue_internal_t *queue,
+                                      FAR const uint64_t *ptr)
+{
+  uint64_t val;
+  uint32_t seq;
+
+  do
+    {
+      seq = read_seqbegin(&queue->lock);
+      val = *ptr;
+    }
+  while (read_seqretry(&queue->lock, seq));
+
+  return val;
+}
+
+static inline_function
+uint32_t hrtimer_queue_locked_read_32(FAR hrtimer_queue_internal_t *queue,
+                                      FAR const uint32_t *ptr)
+{
+  uint32_t val;
+  uint32_t seq;
+
+  do
+    {
+      seq = read_seqbegin(&queue->lock);
+      val = *ptr;
+    }
+  while (read_seqretry(&queue->lock, seq));
+
+  return val;
+}
+
+/****************************************************************************
+ * 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.
+ *
+ ****************************************************************************/
+
+#ifdef CONFIG_ARCH_64BIT
+/* On 64-bit architectures, read/write uint64_t is atomic. */
+#  define hrtimer_queue_read_64(queue, ptr) (*(FAR volatile uint64_t *)(ptr))
+#else
+#  define hrtimer_queue_read_64(queue, ptr) \
+  hrtimer_queue_locked_read_64(queue, ptr)
+#endif
+
+#if UINT_MAX >= UINT32_MAX
+/* On 32/64-bit architectures, read/write uint32_t is atomic. */
+#  define hrtimer_queue_read_32(queue, ptr) (*(FAR volatile uint32_t *)(ptr))
+#else
+#  define hrtimer_queue_read_32(queue, ptr) \
+  hrtimer_queue_locked_read_32(queue, ptr)
+#endif
+
+/* 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_mark_running
+ *
+ * Description:
+ *   Mark the timer as running.
+ *
+ * Input Parameters:
+ *   queue - The timer queue.
+ *   timer - The timer to be marked.
+ *   cpu   - The CPU core Id.
+ *
+ * Returned Value:
+ *   None.
+ *
+ ****************************************************************************/
+
+#ifdef CONFIG_SMP
+static inline_function
+void hrtimer_queue_mark_running(FAR hrtimer_queue_internal_t *queue,
+                                FAR hrtimer_internal_t *timer, int cpu)
+{
+  queue->running[cpu] = (uintptr_t)timer;
+}
+#else
+#  define hrtimer_queue_mark_running(queue, timer, cpu)
+#endif
+
+/****************************************************************************
+ * Name: hrtimer_queue_has_ownership
+ *
+ * Description:
+ *   Check if the CPU core has ownership of the timer.
+ *
+ * Input Parameters:
+ *   queue - The timer queue.
+ *   timer - The timer to be marked.
+ *   cpu   - The CPU core Id.
+ *
+ * Returned Value:
+ *   true if the CPU core has ownership of the timer, false otherwise.
+ *
+ * Assumption:
+ *   The caller must hold the queue lock.
+ *
+ ****************************************************************************/
+
+#ifdef CONFIG_SMP
+#  define hrtimer_queue_has_ownership(queue, timer, cpu) \
+  ((queue)->running[cpu] == (uintptr_t)(timer))
+#else
+#  define hrtimer_queue_has_ownership(queue, timer, cpu) (true)
+#endif
+
+/* Helper function for debugging usage.
+ * If the ownership count > 0, it indicates that users might forget to call
+ * async_cancel to seize the timer ownership before restart the timer.
+ */
+
+#ifdef CONFIG_SMP
+unused_code static inline_function

Review Comment:
   move after static



##########
include/nuttx/hrtimer_queue_type.h:
##########
@@ -0,0 +1,137 @@
+/****************************************************************************
+ * include/nuttx/hrtimer_queue_type.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_TYPE_H
+#define __INCLUDE_HRTIMER_QUEUE_TYPE_H
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+
+#include <nuttx/compiler.h>
+#include <nuttx/clock.h>
+#include <nuttx/seqlock.h>
+
+#include <nuttx/list.h>
+#include <sys/tree.h>
+
+#include <stdint.h>
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+/* The pending state indicates the timer belongs to the shared hrtimer queue
+ * and is waiting for the next hrtimer expiry.
+ */
+
+#define HRTIMER_ISPENDING(timer)       ((timer)->func != NULL)
+
+/* The maximum delay tick should be INT64_MAX. However, if there are expired
+ * hrtimer in the queue, HRTIMER_TIME_BEFORE/AFTER might be incorrect, so we
+ * limited the delay to INT64_MAX >> 1, assuming all expired hrtimer can be
+ * processed within HRTIMER_MAX_DELAY.
+ */
+
+#define HRTIMER_MAX_DELAY              (INT64_MAX >> 1)
+
+#define HRTIMER_TIME_BEFORE(t1, t2)    ((int64_t)((t2) - (t1)) > 0)
+#define HRTIMER_TIME_BEFORE_EQ(t1, t2) ((int64_t)((t2) - (t1)) >= 0)
+#define HRTIMER_TIME_AFTER(t1, t2)     ((int64_t)((t2) - (t1)) < 0)
+#define HRTIMER_TIME_AFTER_EQ(t1, t2)  ((int64_t)((t2) - (t1)) <= 0)
+
+/****************************************************************************
+ * Public Types
+ ****************************************************************************/
+
+/* This is the form of the callback function that is called when the
+ * hrtimer function expires. The return value is next delay time.
+ * If the return value is not equal 0 indicates it is periodic timer.
+ */
+
+typedef CODE uint64_t (*hrtimer_callback_t)(FAR void *arg, uint64_t expired);
+
+/* Type template for customed hrtimer. */
+
+#define HRTIMER_TYPE_DECLARE(name, node_def, queue_def, lock_def) \
+typedef struct name##_s \
+{ \
+  node_def \
+  uint64_t           expired; /* Expired time */ \
+  hrtimer_callback_t func;    /* Callback function */ \
+  FAR void          *arg;     /* Callback argument */ \
+} name##_t; \
+typedef struct name##_queue_s \
+{ \
+  lock_def \
+  queue_def \
+  uint64_t  next_expired;              /* Next expired time */ \
+  name##_t  guard_timer;               /* Guard timer for functional-safety. 
*/ \
+  uintptr_t running[CONFIG_SMP_NCPUS]; /* Hazard pointers for memory 
reclamation. */ \
+} name##_queue_t;
+
+/* Definition of the hrtimer_list_t. */
+
+HRTIMER_TYPE_DECLARE(hrtimer_list,
+                     struct list_node node;  /* Supports a doubly linked list, 
16-bytes for 64-bit architectures */,
+                     struct list_node queue; /* HRTimer doubly linked-list 
queue */,
+                     seqcount_t lock;        /* The RW-lock */)
+
+/* Definition of the hrtimer_rb_t. */
+
+RB_HEAD(hrtimer_tree_s, hrtimer_rb_s);
+HRTIMER_TYPE_DECLARE(hrtimer_rb,
+                     RB_ENTRY(hrtimer_rb_s)   node;  /* Supports a RB-tree 
node, 32-bytes for 64-bit architectures */,
+                     FAR struct hrtimer_rb_s *first; /* Cached left-most timer 
in the queue */
+                     struct hrtimer_tree_s    root;  /* HRTimer 
red-black-tree-based queue */,
+                     seqcount_t               lock;  /* The RW-lock */)
+
+/****************************************************************************
+ * Name: hrtimer_fill
+ *
+ * Description:
+ *   Internal function to fill the timer.
+ *
+ * Input Parameters:
+ *   timer    - The timer to be set.
+ *   function - The callback function of the timer.
+ *   argument - The argument of the callback function.
+ *   time     - The expired time.
+ *
+ * Returned Value:
+ *   None.
+ *
+ ****************************************************************************/
+
+#define hrtimer_fill(timer, function, argument, time) \
+  do { \

Review Comment:
   ```
   do
     {
     }
   while (0)
   ```



##########
include/nuttx/hrtimer/hrtimer_queue.h:
##########
@@ -0,0 +1,703 @@
+/****************************************************************************
+ * include/nuttx/hrtimer/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` and `hrtimer_wait_policy` must be
+ * implemented:
+ *
+ * void hrtimer_reprogram(FAR hrtimer_queue_internal_t *queue,
+ *                        uint64_t next_expired);
+ *   - This function abstracts how to reprogram the timer hardware.
+ *
+ * void hrtimer_wait_policy(void);
+ *   - This function abstracts how to wait for the callback finished.
+ *
+ * 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);
+
+static inline_function
+void hrtimer_wait_policy(void);
+
+/* Queue dependent functions should be provided by `hrtimer_type_xxx.h`. */
+
+static inline_function
+FAR hrtimer_internal_t *hrtimer_queue_peek(
+                        FAR hrtimer_queue_internal_t *queue);
+
+static inline_function
+bool hrtimer_queue_add(FAR hrtimer_queue_t *queue, FAR hrtimer_t *timer);
+
+static inline_function
+bool hrtimer_queue_del(FAR hrtimer_queue_t *queue, FAR hrtimer_t *timer);
+
+static inline_function
+void hrtimer_queue_clear(FAR hrtimer_queue_t *queue);
+
+/* Reusable library code for user-defined high-resolution timer queue. */
+
+/****************************************************************************
+ * Name: hrtimer_queue_lock/unlock
+ *
+ * Description:
+ *   Lock/Unlock the hrtimer queue.
+ *
+ * Input Parameters:
+ *   queue - The timer queue.
+ *
+ * Returned Value:
+ *   The previous interrupt state.
+ *
+ ****************************************************************************/
+
+static inline_function
+void hrtimer_queue_lock_init(FAR hrtimer_queue_internal_t *queue)
+{
+  seqlock_init(&queue->lock);
+}
+
+static inline_function
+irqstate_t hrtimer_queue_lock(FAR hrtimer_queue_internal_t *queue)
+{
+  return write_seqlock_irqsave(&queue->lock);
+}
+
+static inline_function
+void hrtimer_queue_unlock(FAR hrtimer_queue_internal_t *queue,
+                          irqstate_t flags)
+{
+  write_sequnlock_irqrestore(&queue->lock, flags);
+}
+
+/****************************************************************************
+ * Name: hrtimer_queue_locked_read_64/32
+ *
+ * Description:
+ *   Read the value in the queue atomically.
+ *
+ * Input Parameters:
+ *   queue - The timer queue.
+ *
+ * Returned Value:
+ *   The value in the queue.
+ *
+ ****************************************************************************/
+
+static inline_function
+uint64_t hrtimer_queue_locked_read_64(FAR hrtimer_queue_internal_t *queue,
+                                      FAR const uint64_t *ptr)
+{
+  uint64_t val;
+  uint32_t seq;
+
+  do
+    {
+      seq = read_seqbegin(&queue->lock);
+      val = *ptr;
+    }
+  while (read_seqretry(&queue->lock, seq));
+
+  return val;
+}
+
+static inline_function
+uint32_t hrtimer_queue_locked_read_32(FAR hrtimer_queue_internal_t *queue,
+                                      FAR const uint32_t *ptr)
+{
+  uint32_t val;
+  uint32_t seq;
+
+  do
+    {
+      seq = read_seqbegin(&queue->lock);
+      val = *ptr;
+    }
+  while (read_seqretry(&queue->lock, seq));
+
+  return val;
+}
+
+/****************************************************************************
+ * 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.
+ *
+ ****************************************************************************/
+
+#ifdef CONFIG_ARCH_64BIT
+/* On 64-bit architectures, read/write uint64_t is atomic. */
+#  define hrtimer_queue_read_64(queue, ptr) (*(FAR volatile uint64_t *)(ptr))
+#else
+#  define hrtimer_queue_read_64(queue, ptr) \
+  hrtimer_queue_locked_read_64(queue, ptr)
+#endif
+
+#if UINT_MAX >= UINT32_MAX
+/* On 32/64-bit architectures, read/write uint32_t is atomic. */
+#  define hrtimer_queue_read_32(queue, ptr) (*(FAR volatile uint32_t *)(ptr))
+#else
+#  define hrtimer_queue_read_32(queue, ptr) \
+  hrtimer_queue_locked_read_32(queue, ptr)
+#endif
+
+/* 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_mark_running
+ *
+ * Description:
+ *   Mark the timer as running.
+ *
+ * Input Parameters:
+ *   queue - The timer queue.
+ *   timer - The timer to be marked.
+ *   cpu   - The CPU core Id.
+ *
+ * Returned Value:
+ *   None.
+ *
+ ****************************************************************************/
+
+#ifdef CONFIG_SMP
+static inline_function
+void hrtimer_queue_mark_running(FAR hrtimer_queue_internal_t *queue,
+                                FAR hrtimer_internal_t *timer, int cpu)
+{
+  queue->running[cpu] = (uintptr_t)timer;
+}
+#else
+#  define hrtimer_queue_mark_running(queue, timer, cpu)
+#endif
+
+/****************************************************************************
+ * Name: hrtimer_queue_has_ownership
+ *
+ * Description:
+ *   Check if the CPU core has ownership of the timer.
+ *
+ * Input Parameters:
+ *   queue - The timer queue.
+ *   timer - The timer to be marked.
+ *   cpu   - The CPU core Id.
+ *
+ * Returned Value:
+ *   true if the CPU core has ownership of the timer, false otherwise.
+ *
+ * Assumption:
+ *   The caller must hold the queue lock.
+ *
+ ****************************************************************************/
+
+#ifdef CONFIG_SMP
+#  define hrtimer_queue_has_ownership(queue, timer, cpu) \
+  ((queue)->running[cpu] == (uintptr_t)(timer))
+#else
+#  define hrtimer_queue_has_ownership(queue, timer, cpu) (true)
+#endif
+
+/* Helper function for debugging usage.
+ * If the ownership count > 0, it indicates that users might forget to call
+ * async_cancel to seize the timer ownership before restart the timer.
+ */
+
+#ifdef CONFIG_SMP
+unused_code static inline_function
+unsigned hrtimer_queue_count_ownership(FAR hrtimer_queue_internal_t *queue,
+                                       FAR hrtimer_internal_t *timer)
+{
+  int      cpu;
+  unsigned owner = 0u;
+
+  for (cpu = 0; cpu < CONFIG_SMP_NCPUS; cpu++)
+    {
+      if (hrtimer_queue_has_ownership(queue, timer, cpu))
+        {
+          owner++;
+        }
+    }
+
+  return owner;
+}
+#else
+#  define hrtimer_queue_count_ownership(queue, timer) 0u
+#endif
+
+/****************************************************************************
+ * Name: hrtimer_queue_is_running
+ *
+ * Description:
+ *   Check if the CPU core is running the timer. This can be called
+ *   without the queue lock.
+ *
+ * Input Parameters:
+ *   queue - The timer queue.
+ *   timer - The timer to be marked.
+ *   cpu   - The CPU core Id.
+ *
+ * Returned Value:
+ *   true if the CPU core has ownership of the timer, false otherwise.
+ *
+ ****************************************************************************/
+
+#define hrtimer_queue_is_running(queue, timer, cpu) \
+  (hrtimer_queue_read(queue, &(queue)->running[cpu]) == (uintptr_t)(timer))
+
+/****************************************************************************
+ * Name: hrtimer_queue_init
+ *
+ * Description:
+ *   Initialize the hrtimer queue.
+ *
+ * Input Parameters:
+ *   queue - The timer queue.
+ *
+ * Returned Value:
+ *   0 on OK, -EINVAL on error.
+ *
+ ****************************************************************************/
+
+static inline_function
+int hrtimer_queue_init(FAR hrtimer_queue_internal_t *queue)
+{
+  int ret = -EINVAL;
+  int cpu;
+
+  if (queue)
+    {
+      FAR hrtimer_internal_t *guard_timer = &queue->guard_timer;
+
+      hrtimer_queue_lock_init(&queue->lock);
+      hrtimer_queue_clear(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);
+
+      queue->next_expired = UINT64_MAX;
+      hrtimer_queue_add(queue, guard_timer);
+
+      memset(queue->running, 0, sizeof(queue->running));
+      ret = OK;
+    }
+
+  return ret;
+}
+
+/****************************************************************************
+ * 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.
+ *
+ * Assumption:
+ *   The caller should be in the interrupt context, where the cpu core id
+ *   should not change.
+ *
+ ****************************************************************************/
+
+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();
+  irqstate_t              flags = hrtimer_queue_lock(&queue->lock);
+
+  /* Check if we need processing the expiration. */
+
+  while (HRTIMER_TIME_BEFORE_EQ(queue->next_expired, current))
+    {
+      timer   = hrtimer_queue_peek(queue);
+
+      /* Remove the hrtimer from the head of the queue
+       * and update the next_expired.
+       */
+
+      func    = timer->func;
+      arg     = timer->arg;
+      expired = timer->expired;
+
+      /* Clear the pending state */
+
+      timer->func = NULL;
+      hrtimer_queue_del(queue, timer);
+
+      /* Mark the hrtimer in the running state. In this case,
+       * User can not reclaim (deallocate or modify) the hrtimer.
+       */
+
+      hrtimer_queue_mark_running(queue, timer, cpu);
+      hrtimer_queue_unlock(&queue->lock, flags);
+
+      /* Execute the hrtimer function. Here we can not modify the hrtimer
+       * anymore because the hrtimer might be modified by other users.
+       * The expired time is passing as the callback version to help users
+       * to avoid race conditions.
+       */
+
+      next_expired = expired + func(arg, expired);
+      DEBUGASSERT(HRTIMER_TIME_AFTER_EQ(next_expired, expired));
+
+      flags = hrtimer_queue_lock(&queue->lock);
+
+      /* In the interrupt context, the cpu core id should not change.
+       * NuttX currently does not support kernel preemption, thus interrupts
+       * cannot be nested within interrupt context.
+       * Even if kernel preemption and interrupt nesting are supported in the
+       * future, we can avoid nested calls to `hrtimer_expiry` by an
+       * interrupt nesting counter.
+       * Besides, in SMP mode, during interrupt handling, we use per-core
+       * `ARCH_INTERRUPTSTACK`, ensuring that the core ID remains unchanged
+       * while in interrupt context.
+       */
+
+      DEBUGASSERT(cpu == this_cpu());
+
+      /* If the running timer has not changed, we can re-enqueue the
+       * timer or release the ownership. Otherwise, the hrtimer should
+       * have been cancelled. In this case, we lose the ownership of the
+       * hrtimer and can not modify this hrtimer anymore.
+       */
+
+      if (next_expired != expired &&
+          hrtimer_queue_has_ownership(queue, timer, cpu))
+        {
+          /* Re-enqueue the hrtimer if it is periodic. */
+
+          timer->func    = func;
+          timer->expired = next_expired;
+          hrtimer_queue_add(queue, timer);
+        }
+
+      /* Clear the running state and release the hrtimer reference. */
+
+      hrtimer_queue_mark_running(&queue->lock, NULL, cpu);
+    }
+
+  next_expired = queue->next_expired;
+  hrtimer_reprogram(queue, next_expired);
+
+  hrtimer_queue_unlock(&queue->lock, flags);
+
+  return next_expired;
+}
+
+/****************************************************************************
+ * Name: hrtimer_queue_start
+ *
+ * Description:
+ *   Start the hrtimer asynchronously.
+ *
+ * Input Parameters:
+ *   queue   - The hrtimer queue.
+ *   timer   - The hrtimer to be set.
+ *   reprogram - The timer reprogram function.
+ *
+ * Returned Value:
+ *   None.
+ *
+ * Assumption:
+ *   The timer is not in the queue.
+ *
+ ****************************************************************************/
+
+static inline_function
+void hrtimer_queue_start(FAR hrtimer_queue_internal_t *queue,
+                         FAR hrtimer_internal_t *timer)
+{
+  irqstate_t flags;
+
+  DEBUGASSERT(queue && timer && timer->func);
+
+  flags = hrtimer_queue_lock(&queue->lock);
+
+  /* The queue should not have the timer ownership before. */
+
+  DEBUGASSERT(hrtimer_queue_count_ownership(queue, timer) == 0u);
+
+  /* Reprogram the timer when the queue head has changed. */
+
+  if (hrtimer_queue_add(queue, timer))
+    {
+      hrtimer_reprogram(queue, queue->next_expired);
+    }
+
+  hrtimer_queue_unlock(&queue->lock, flags);
+}
+
+/****************************************************************************
+ * Name: hrtimer_queue_async_cancel
+ *
+ * Description:
+ *   Dequeue and set the timer to the cancelled state asynchronously.
+ *   The write ownership of the timer is removed from the queue expiration.
+ *   However, the callback function may still be executing on another CPU.
+ *
+ * Input Parameters:
+ *   queue - The hrtimer queue.
+ *   timer - The hrtimer to be canceled.
+ *   reprogram - The timer reprogram function.
+ *
+ * Returned Value:
+ *   The count of the timer references. Zero means the timer is not
+ *   referenced by any core. Generally, only one reference to a timer
+ *   can exist at the same time. However, when a timer may be restarted
+ *   at the pending state (i.e., by calling hrtimer_restart), more references
+ *   to the timer may exist.
+ *   -EINVAL on if the timer is not in the pending state.
+ *
+ ****************************************************************************/
+
+static inline_function
+int hrtimer_queue_async_cancel(FAR hrtimer_queue_internal_t *queue,
+                               FAR hrtimer_internal_t *timer)
+{
+  uint64_t        next_expired;
+  irqstate_t             flags;

Review Comment:
   remove the extra spaces



##########
include/nuttx/hrtimer_queue_type.h:
##########
@@ -0,0 +1,137 @@
+/****************************************************************************
+ * include/nuttx/hrtimer_queue_type.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_TYPE_H
+#define __INCLUDE_HRTIMER_QUEUE_TYPE_H
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+
+#include <nuttx/compiler.h>
+#include <nuttx/clock.h>
+#include <nuttx/seqlock.h>
+
+#include <nuttx/list.h>
+#include <sys/tree.h>

Review Comment:
   why include here, but not hrtimer_type_xxx.h



##########
include/nuttx/hrtimer/hrtimer_queue.h:
##########
@@ -0,0 +1,703 @@
+/****************************************************************************
+ * include/nuttx/hrtimer/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` and `hrtimer_wait_policy` must be
+ * implemented:
+ *
+ * void hrtimer_reprogram(FAR hrtimer_queue_internal_t *queue,
+ *                        uint64_t next_expired);
+ *   - This function abstracts how to reprogram the timer hardware.
+ *
+ * void hrtimer_wait_policy(void);
+ *   - This function abstracts how to wait for the callback finished.
+ *
+ * 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);
+
+static inline_function
+void hrtimer_wait_policy(void);
+
+/* Queue dependent functions should be provided by `hrtimer_type_xxx.h`. */
+
+static inline_function
+FAR hrtimer_internal_t *hrtimer_queue_peek(
+                        FAR hrtimer_queue_internal_t *queue);
+
+static inline_function
+bool hrtimer_queue_add(FAR hrtimer_queue_t *queue, FAR hrtimer_t *timer);
+
+static inline_function
+bool hrtimer_queue_del(FAR hrtimer_queue_t *queue, FAR hrtimer_t *timer);
+
+static inline_function
+void hrtimer_queue_clear(FAR hrtimer_queue_t *queue);
+
+/* Reusable library code for user-defined high-resolution timer queue. */
+
+/****************************************************************************
+ * Name: hrtimer_queue_lock/unlock
+ *
+ * Description:
+ *   Lock/Unlock the hrtimer queue.
+ *
+ * Input Parameters:
+ *   queue - The timer queue.
+ *
+ * Returned Value:
+ *   The previous interrupt state.
+ *
+ ****************************************************************************/
+
+static inline_function
+void hrtimer_queue_lock_init(FAR hrtimer_queue_internal_t *queue)
+{
+  seqlock_init(&queue->lock);
+}
+
+static inline_function
+irqstate_t hrtimer_queue_lock(FAR hrtimer_queue_internal_t *queue)
+{
+  return write_seqlock_irqsave(&queue->lock);
+}
+
+static inline_function
+void hrtimer_queue_unlock(FAR hrtimer_queue_internal_t *queue,
+                          irqstate_t flags)
+{
+  write_sequnlock_irqrestore(&queue->lock, flags);
+}
+
+/****************************************************************************
+ * Name: hrtimer_queue_locked_read_64/32
+ *
+ * Description:
+ *   Read the value in the queue atomically.
+ *
+ * Input Parameters:
+ *   queue - The timer queue.
+ *
+ * Returned Value:
+ *   The value in the queue.
+ *
+ ****************************************************************************/
+
+static inline_function
+uint64_t hrtimer_queue_locked_read_64(FAR hrtimer_queue_internal_t *queue,
+                                      FAR const uint64_t *ptr)
+{
+  uint64_t val;
+  uint32_t seq;
+
+  do
+    {
+      seq = read_seqbegin(&queue->lock);
+      val = *ptr;
+    }
+  while (read_seqretry(&queue->lock, seq));
+
+  return val;
+}
+
+static inline_function
+uint32_t hrtimer_queue_locked_read_32(FAR hrtimer_queue_internal_t *queue,
+                                      FAR const uint32_t *ptr)
+{
+  uint32_t val;
+  uint32_t seq;
+
+  do
+    {
+      seq = read_seqbegin(&queue->lock);
+      val = *ptr;
+    }
+  while (read_seqretry(&queue->lock, seq));
+
+  return val;
+}
+
+/****************************************************************************
+ * 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.
+ *
+ ****************************************************************************/
+
+#ifdef CONFIG_ARCH_64BIT
+/* On 64-bit architectures, read/write uint64_t is atomic. */
+#  define hrtimer_queue_read_64(queue, ptr) (*(FAR volatile uint64_t *)(ptr))
+#else
+#  define hrtimer_queue_read_64(queue, ptr) \
+  hrtimer_queue_locked_read_64(queue, ptr)
+#endif
+
+#if UINT_MAX >= UINT32_MAX
+/* On 32/64-bit architectures, read/write uint32_t is atomic. */
+#  define hrtimer_queue_read_32(queue, ptr) (*(FAR volatile uint32_t *)(ptr))
+#else
+#  define hrtimer_queue_read_32(queue, ptr) \
+  hrtimer_queue_locked_read_32(queue, ptr)
+#endif
+
+/* 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_mark_running
+ *
+ * Description:
+ *   Mark the timer as running.
+ *
+ * Input Parameters:
+ *   queue - The timer queue.
+ *   timer - The timer to be marked.
+ *   cpu   - The CPU core Id.
+ *
+ * Returned Value:
+ *   None.
+ *
+ ****************************************************************************/
+
+#ifdef CONFIG_SMP
+static inline_function
+void hrtimer_queue_mark_running(FAR hrtimer_queue_internal_t *queue,
+                                FAR hrtimer_internal_t *timer, int cpu)
+{
+  queue->running[cpu] = (uintptr_t)timer;
+}
+#else
+#  define hrtimer_queue_mark_running(queue, timer, cpu)
+#endif
+
+/****************************************************************************
+ * Name: hrtimer_queue_has_ownership
+ *
+ * Description:
+ *   Check if the CPU core has ownership of the timer.
+ *
+ * Input Parameters:
+ *   queue - The timer queue.
+ *   timer - The timer to be marked.
+ *   cpu   - The CPU core Id.
+ *
+ * Returned Value:
+ *   true if the CPU core has ownership of the timer, false otherwise.
+ *
+ * Assumption:
+ *   The caller must hold the queue lock.
+ *
+ ****************************************************************************/
+
+#ifdef CONFIG_SMP
+#  define hrtimer_queue_has_ownership(queue, timer, cpu) \
+  ((queue)->running[cpu] == (uintptr_t)(timer))
+#else
+#  define hrtimer_queue_has_ownership(queue, timer, cpu) (true)
+#endif
+
+/* Helper function for debugging usage.
+ * If the ownership count > 0, it indicates that users might forget to call
+ * async_cancel to seize the timer ownership before restart the timer.
+ */
+
+#ifdef CONFIG_SMP
+unused_code static inline_function
+unsigned hrtimer_queue_count_ownership(FAR hrtimer_queue_internal_t *queue,

Review Comment:
   unsigned int



##########
include/nuttx/hrtimer/hrtimer_queue.h:
##########
@@ -0,0 +1,703 @@
+/****************************************************************************
+ * include/nuttx/hrtimer/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` and `hrtimer_wait_policy` must be
+ * implemented:
+ *
+ * void hrtimer_reprogram(FAR hrtimer_queue_internal_t *queue,
+ *                        uint64_t next_expired);
+ *   - This function abstracts how to reprogram the timer hardware.
+ *
+ * void hrtimer_wait_policy(void);
+ *   - This function abstracts how to wait for the callback finished.
+ *
+ * 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);
+
+static inline_function
+void hrtimer_wait_policy(void);
+
+/* Queue dependent functions should be provided by `hrtimer_type_xxx.h`. */
+
+static inline_function
+FAR hrtimer_internal_t *hrtimer_queue_peek(
+                        FAR hrtimer_queue_internal_t *queue);
+
+static inline_function
+bool hrtimer_queue_add(FAR hrtimer_queue_t *queue, FAR hrtimer_t *timer);
+
+static inline_function
+bool hrtimer_queue_del(FAR hrtimer_queue_t *queue, FAR hrtimer_t *timer);
+
+static inline_function
+void hrtimer_queue_clear(FAR hrtimer_queue_t *queue);
+
+/* Reusable library code for user-defined high-resolution timer queue. */
+
+/****************************************************************************
+ * Name: hrtimer_queue_lock/unlock
+ *
+ * Description:
+ *   Lock/Unlock the hrtimer queue.
+ *
+ * Input Parameters:
+ *   queue - The timer queue.
+ *
+ * Returned Value:
+ *   The previous interrupt state.
+ *
+ ****************************************************************************/
+
+static inline_function
+void hrtimer_queue_lock_init(FAR hrtimer_queue_internal_t *queue)
+{
+  seqlock_init(&queue->lock);
+}
+
+static inline_function
+irqstate_t hrtimer_queue_lock(FAR hrtimer_queue_internal_t *queue)
+{
+  return write_seqlock_irqsave(&queue->lock);
+}
+
+static inline_function
+void hrtimer_queue_unlock(FAR hrtimer_queue_internal_t *queue,
+                          irqstate_t flags)
+{
+  write_sequnlock_irqrestore(&queue->lock, flags);
+}
+
+/****************************************************************************
+ * Name: hrtimer_queue_locked_read_64/32
+ *
+ * Description:
+ *   Read the value in the queue atomically.
+ *
+ * Input Parameters:
+ *   queue - The timer queue.
+ *
+ * Returned Value:
+ *   The value in the queue.
+ *
+ ****************************************************************************/
+
+static inline_function
+uint64_t hrtimer_queue_locked_read_64(FAR hrtimer_queue_internal_t *queue,
+                                      FAR const uint64_t *ptr)
+{
+  uint64_t val;
+  uint32_t seq;
+
+  do
+    {
+      seq = read_seqbegin(&queue->lock);
+      val = *ptr;
+    }
+  while (read_seqretry(&queue->lock, seq));
+
+  return val;
+}
+
+static inline_function
+uint32_t hrtimer_queue_locked_read_32(FAR hrtimer_queue_internal_t *queue,
+                                      FAR const uint32_t *ptr)
+{
+  uint32_t val;
+  uint32_t seq;
+
+  do
+    {
+      seq = read_seqbegin(&queue->lock);
+      val = *ptr;
+    }
+  while (read_seqretry(&queue->lock, seq));
+
+  return val;
+}
+
+/****************************************************************************
+ * 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.
+ *
+ ****************************************************************************/
+
+#ifdef CONFIG_ARCH_64BIT
+/* On 64-bit architectures, read/write uint64_t is atomic. */
+#  define hrtimer_queue_read_64(queue, ptr) (*(FAR volatile uint64_t *)(ptr))
+#else
+#  define hrtimer_queue_read_64(queue, ptr) \
+  hrtimer_queue_locked_read_64(queue, ptr)
+#endif
+
+#if UINT_MAX >= UINT32_MAX
+/* On 32/64-bit architectures, read/write uint32_t is atomic. */
+#  define hrtimer_queue_read_32(queue, ptr) (*(FAR volatile uint32_t *)(ptr))
+#else
+#  define hrtimer_queue_read_32(queue, ptr) \
+  hrtimer_queue_locked_read_32(queue, ptr)
+#endif
+
+/* 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_mark_running
+ *
+ * Description:
+ *   Mark the timer as running.
+ *
+ * Input Parameters:
+ *   queue - The timer queue.
+ *   timer - The timer to be marked.
+ *   cpu   - The CPU core Id.
+ *
+ * Returned Value:
+ *   None.
+ *
+ ****************************************************************************/
+
+#ifdef CONFIG_SMP
+static inline_function
+void hrtimer_queue_mark_running(FAR hrtimer_queue_internal_t *queue,
+                                FAR hrtimer_internal_t *timer, int cpu)
+{
+  queue->running[cpu] = (uintptr_t)timer;
+}
+#else
+#  define hrtimer_queue_mark_running(queue, timer, cpu)
+#endif
+
+/****************************************************************************
+ * Name: hrtimer_queue_has_ownership
+ *
+ * Description:
+ *   Check if the CPU core has ownership of the timer.
+ *
+ * Input Parameters:
+ *   queue - The timer queue.
+ *   timer - The timer to be marked.
+ *   cpu   - The CPU core Id.
+ *
+ * Returned Value:
+ *   true if the CPU core has ownership of the timer, false otherwise.
+ *
+ * Assumption:
+ *   The caller must hold the queue lock.
+ *
+ ****************************************************************************/
+
+#ifdef CONFIG_SMP
+#  define hrtimer_queue_has_ownership(queue, timer, cpu) \
+  ((queue)->running[cpu] == (uintptr_t)(timer))
+#else
+#  define hrtimer_queue_has_ownership(queue, timer, cpu) (true)
+#endif
+
+/* Helper function for debugging usage.
+ * If the ownership count > 0, it indicates that users might forget to call
+ * async_cancel to seize the timer ownership before restart the timer.
+ */
+
+#ifdef CONFIG_SMP
+unused_code static inline_function
+unsigned hrtimer_queue_count_ownership(FAR hrtimer_queue_internal_t *queue,
+                                       FAR hrtimer_internal_t *timer)
+{
+  int      cpu;
+  unsigned owner = 0u;
+
+  for (cpu = 0; cpu < CONFIG_SMP_NCPUS; cpu++)
+    {
+      if (hrtimer_queue_has_ownership(queue, timer, cpu))
+        {
+          owner++;
+        }
+    }
+
+  return owner;
+}
+#else
+#  define hrtimer_queue_count_ownership(queue, timer) 0u
+#endif
+
+/****************************************************************************
+ * Name: hrtimer_queue_is_running
+ *
+ * Description:
+ *   Check if the CPU core is running the timer. This can be called
+ *   without the queue lock.
+ *
+ * Input Parameters:
+ *   queue - The timer queue.
+ *   timer - The timer to be marked.
+ *   cpu   - The CPU core Id.
+ *
+ * Returned Value:
+ *   true if the CPU core has ownership of the timer, false otherwise.
+ *
+ ****************************************************************************/
+
+#define hrtimer_queue_is_running(queue, timer, cpu) \

Review Comment:
   what's differenct from hrtimer_queue_has_ownership



##########
include/nuttx/hrtimer/hrtimer_queue.h:
##########
@@ -0,0 +1,703 @@
+/****************************************************************************
+ * include/nuttx/hrtimer/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` and `hrtimer_wait_policy` must be
+ * implemented:
+ *
+ * void hrtimer_reprogram(FAR hrtimer_queue_internal_t *queue,
+ *                        uint64_t next_expired);
+ *   - This function abstracts how to reprogram the timer hardware.
+ *
+ * void hrtimer_wait_policy(void);
+ *   - This function abstracts how to wait for the callback finished.
+ *
+ * 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);
+
+static inline_function
+void hrtimer_wait_policy(void);
+
+/* Queue dependent functions should be provided by `hrtimer_type_xxx.h`. */
+
+static inline_function
+FAR hrtimer_internal_t *hrtimer_queue_peek(
+                        FAR hrtimer_queue_internal_t *queue);
+
+static inline_function
+bool hrtimer_queue_add(FAR hrtimer_queue_t *queue, FAR hrtimer_t *timer);
+
+static inline_function
+bool hrtimer_queue_del(FAR hrtimer_queue_t *queue, FAR hrtimer_t *timer);
+
+static inline_function
+void hrtimer_queue_clear(FAR hrtimer_queue_t *queue);
+
+/* Reusable library code for user-defined high-resolution timer queue. */
+
+/****************************************************************************
+ * Name: hrtimer_queue_lock/unlock
+ *
+ * Description:
+ *   Lock/Unlock the hrtimer queue.
+ *
+ * Input Parameters:
+ *   queue - The timer queue.
+ *
+ * Returned Value:
+ *   The previous interrupt state.
+ *
+ ****************************************************************************/
+
+static inline_function
+void hrtimer_queue_lock_init(FAR hrtimer_queue_internal_t *queue)
+{
+  seqlock_init(&queue->lock);
+}
+
+static inline_function
+irqstate_t hrtimer_queue_lock(FAR hrtimer_queue_internal_t *queue)
+{
+  return write_seqlock_irqsave(&queue->lock);
+}
+
+static inline_function
+void hrtimer_queue_unlock(FAR hrtimer_queue_internal_t *queue,
+                          irqstate_t flags)
+{
+  write_sequnlock_irqrestore(&queue->lock, flags);
+}
+
+/****************************************************************************
+ * Name: hrtimer_queue_locked_read_64/32
+ *
+ * Description:
+ *   Read the value in the queue atomically.
+ *
+ * Input Parameters:
+ *   queue - The timer queue.
+ *
+ * Returned Value:
+ *   The value in the queue.
+ *
+ ****************************************************************************/
+
+static inline_function
+uint64_t hrtimer_queue_locked_read_64(FAR hrtimer_queue_internal_t *queue,
+                                      FAR const uint64_t *ptr)
+{
+  uint64_t val;
+  uint32_t seq;
+
+  do
+    {
+      seq = read_seqbegin(&queue->lock);
+      val = *ptr;
+    }
+  while (read_seqretry(&queue->lock, seq));
+
+  return val;
+}
+
+static inline_function
+uint32_t hrtimer_queue_locked_read_32(FAR hrtimer_queue_internal_t *queue,
+                                      FAR const uint32_t *ptr)
+{
+  uint32_t val;
+  uint32_t seq;
+
+  do
+    {
+      seq = read_seqbegin(&queue->lock);
+      val = *ptr;
+    }
+  while (read_seqretry(&queue->lock, seq));
+
+  return val;
+}
+
+/****************************************************************************
+ * 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.
+ *
+ ****************************************************************************/
+
+#ifdef CONFIG_ARCH_64BIT
+/* On 64-bit architectures, read/write uint64_t is atomic. */
+#  define hrtimer_queue_read_64(queue, ptr) (*(FAR volatile uint64_t *)(ptr))
+#else
+#  define hrtimer_queue_read_64(queue, ptr) \
+  hrtimer_queue_locked_read_64(queue, ptr)
+#endif
+
+#if UINT_MAX >= UINT32_MAX
+/* On 32/64-bit architectures, read/write uint32_t is atomic. */
+#  define hrtimer_queue_read_32(queue, ptr) (*(FAR volatile uint32_t *)(ptr))
+#else
+#  define hrtimer_queue_read_32(queue, ptr) \
+  hrtimer_queue_locked_read_32(queue, ptr)
+#endif
+
+/* 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_mark_running
+ *
+ * Description:
+ *   Mark the timer as running.
+ *
+ * Input Parameters:
+ *   queue - The timer queue.
+ *   timer - The timer to be marked.
+ *   cpu   - The CPU core Id.
+ *
+ * Returned Value:
+ *   None.
+ *
+ ****************************************************************************/
+
+#ifdef CONFIG_SMP
+static inline_function
+void hrtimer_queue_mark_running(FAR hrtimer_queue_internal_t *queue,
+                                FAR hrtimer_internal_t *timer, int cpu)
+{
+  queue->running[cpu] = (uintptr_t)timer;
+}
+#else
+#  define hrtimer_queue_mark_running(queue, timer, cpu)
+#endif
+
+/****************************************************************************
+ * Name: hrtimer_queue_has_ownership
+ *
+ * Description:
+ *   Check if the CPU core has ownership of the timer.
+ *
+ * Input Parameters:
+ *   queue - The timer queue.
+ *   timer - The timer to be marked.
+ *   cpu   - The CPU core Id.
+ *
+ * Returned Value:
+ *   true if the CPU core has ownership of the timer, false otherwise.
+ *
+ * Assumption:
+ *   The caller must hold the queue lock.
+ *
+ ****************************************************************************/
+
+#ifdef CONFIG_SMP
+#  define hrtimer_queue_has_ownership(queue, timer, cpu) \
+  ((queue)->running[cpu] == (uintptr_t)(timer))
+#else
+#  define hrtimer_queue_has_ownership(queue, timer, cpu) (true)
+#endif
+
+/* Helper function for debugging usage.
+ * If the ownership count > 0, it indicates that users might forget to call
+ * async_cancel to seize the timer ownership before restart the timer.
+ */
+
+#ifdef CONFIG_SMP
+unused_code static inline_function
+unsigned hrtimer_queue_count_ownership(FAR hrtimer_queue_internal_t *queue,
+                                       FAR hrtimer_internal_t *timer)
+{
+  int      cpu;
+  unsigned owner = 0u;
+
+  for (cpu = 0; cpu < CONFIG_SMP_NCPUS; cpu++)
+    {
+      if (hrtimer_queue_has_ownership(queue, timer, cpu))
+        {
+          owner++;
+        }
+    }
+
+  return owner;
+}
+#else
+#  define hrtimer_queue_count_ownership(queue, timer) 0u
+#endif
+
+/****************************************************************************
+ * Name: hrtimer_queue_is_running
+ *
+ * Description:
+ *   Check if the CPU core is running the timer. This can be called
+ *   without the queue lock.
+ *
+ * Input Parameters:
+ *   queue - The timer queue.
+ *   timer - The timer to be marked.
+ *   cpu   - The CPU core Id.
+ *
+ * Returned Value:
+ *   true if the CPU core has ownership of the timer, false otherwise.
+ *
+ ****************************************************************************/
+
+#define hrtimer_queue_is_running(queue, timer, cpu) \
+  (hrtimer_queue_read(queue, &(queue)->running[cpu]) == (uintptr_t)(timer))

Review Comment:
   how about non-SMP case



-- 
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]

Reply via email to