wangchdo opened a new pull request, #17517:
URL: https://github.com/apache/nuttx/pull/17517

   ## Summary
   
   Per @xiaoxiang781216's suggestion, I have Split #17489 into two parts:
   
   Part 1: Add hrtimer only — this PR.
   Part 2: Add hrtimer along with scheduler support — the original PR #17489.
   
   
   **So the summary of this PR can be(borrowed from #17489 ):**
   
   - Introduce a high-resolution timer (hrtimer) module to NuttX, providing 
timers with nanosecond-level resolution.
   
   - Provide three new hrtimer APIs:
   
   1. hrtimer_init() – Initialize a high-resolution timer instance.
   2. hrtimer_start() – Start a high-resolution timer in absolute or relative 
mode.
   3. hrtimer_cancel() – Cancel a pending high-resolution timer.
   
   Per @xiaoxiang781216’s comments, I have updated the critical section 
protection for hrtimer to use a spinlock to improve performance.
   
   However, to prevent potential issues where hrtimer_cancel() might cancel a 
running timer and the timer instance could be freed prematurely, I have 
introduced a state machine for hrtimer:
   
   ```
   INACTIVE
      |
      | start
      v
    ARMED -------- cancel --------> CANCELED
      |                               ^
      | expire                        |
      v                               |
    RUNNING -------- cancel ----------+
      |
      | return
      v
   INACTIVE
   ```
   
   **Key points:**
   1. hrtimer_cancel() remains non-blocking.
   2. If the timer callback is currently executing, it is allowed to complete.
   3. After cancellation, the callback will not be invoked again.
   4. The caller must ensure that timer-related resources are not freed until 
the callback has returned.
   
   ## Impact
   
   Add a new high-resolution timer (hrtimer) module to NuttX.
   
   ## Testing
   
   **ostest passed on a2g-tc397-5v-tft with hrtimer enalbed**
   
   ```
   nsh> uname -a
   NuttX 0.0.0 169028d7be Dec 16 2025 10:39:58 tricore a2g-tc397-5v-tft
   nsh>
   nsh> ostest
   
   (...)
   
   user_main: hrtimer test
   
   End of test memory usage:
   VARIABLE  BEFORE   AFTER
   ======== ======== ========
   arena       28dfc    28dfc
   ordblks         6        6
   mxordblk    1f8a8    1f8a8
   uordblks     555c     555c
   fordblks    238a0    238a0
   
   Final memory usage:
   VARIABLE  BEFORE   AFTER
   ======== ======== ========
   arena       28dfc    28dfc
   ordblks         1        6
   mxordblk    24220    1f8a8
   uordblks     4bdc     555c
   fordblks    24220    238a0
   user_main: Exiting
   ostest_main: Exiting with status 0
   ```
   
   
   
   
   


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