Fix-Point commented on code in PR #17675:
URL: https://github.com/apache/nuttx/pull/17675#discussion_r2648136059


##########
include/nuttx/hrtimer_queue.h:
##########
@@ -0,0 +1,555 @@
+/****************************************************************************
+ * 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:
+ *   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;
+
+      /* 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);

Review Comment:
   - FMEA analysis requires us to design a safety mechanism to ensure fault 
tolerance.
   - This guard timer help us avoid checking if the queue is empty during 
expiration processing, which would also **save a branch** on the critical path.



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