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

   ## Summary
   
   @Fix-Point tested the latest merged hrtimer implementation and found an 
issue where a running hrtimer is not allowed to be restarted.
   
   This patch fixes that issue.
   
   ## Impact
   
   This patch fixes an issue in the standalone hrtimer module implementation, 
without affecting any other NuttX functions.
   
   ## Testing
   
   **test code:**
   
   ```
   #include <nuttx/config.h>
   #include <stdio.h>
   
   #include <nuttx/hrtimer.h>
   
   #define HRTIMER_TEST_THREAD_NR (CONFIG_SMP_NCPUS * 8)
   
   /****************************************************************************
    * Public Functions
    
****************************************************************************/
   
   static uint32_t test_callback(FAR struct hrtimer_s *hrtimer)
   {
     printf("test_callback called!\n");
     return 0;
   }
   
   static void* test_thread(void *arg)
   {
     irqstate_t  flags;
     hrtimer_t   timer;
     spinlock_t  lock = SP_UNLOCKED;
     hrtimer_init(&timer, test_callback, NULL);
     while (1)
       {
         uint64_t delay = rand() % NSEC_PER_MSEC;
         int ret;
   
         /* Simulate the usage of driver->wait_dog. */
   
         flags = spin_lock_irqsave(&lock);
   
         /* The driver lock acquired */
   
         /* First try, failed. Because hrtimer_start can not ensure the timer 
being started. */
   
         ret = hrtimer_cancel(&timer);
         // ret = hrtimer_start(&timer, 10 * NSEC_PER_USEC, HRTIMER_MODE_REL); 
/* May fail */
   
         /* This try-loop start should be OK. But it failed again.
          * Besides, we can not sleep or spin in the critical sections.
          */
   
         while (hrtimer_start(&timer, 10 * NSEC_PER_USEC, HRTIMER_MODE_REL) != 
OK);
         ret = OK;
   
         /* Second try, Success, but we can not sleep or spin in the critical 
section. */
   
         // ret = hrtimer_cancel_sync(&timer); /* Sleep in critical sections */
         // ret = hrtimer_start(&timer, delay, HRTIMER_MODE_REL);
   
   
         spin_unlock_irqrestore(&lock, flags);
   
         if (ret != OK)
           {
             printf("hrtimer_start failed\n");
           }
         up_ndelay(delay);
       }
     return NULL;
   }
   
   /****************************************************************************
    * hello_main
    
****************************************************************************/
   
   int main(int argc, FAR char *argv[])
   {
     unsigned int   thread_id;
     pthread_attr_t attr;
     pthread_t      pthreads[HRTIMER_TEST_THREAD_NR];
   
     printf("hrtimer_test start...\n");
   
     ASSERT(pthread_attr_init(&attr) == 0);
   
     /* Create wdog test thread */
   
     for (thread_id = 0; thread_id < HRTIMER_TEST_THREAD_NR; thread_id++)
       {
         ASSERT(pthread_create(&pthreads[thread_id], &attr,
                               test_thread, NULL) == 0);
       }
   
     for (thread_id = 0; thread_id < HRTIMER_TEST_THREAD_NR; thread_id++)
       {
         pthread_join(pthreads[thread_id], NULL);
       }
   
     ASSERT(pthread_attr_destroy(&attr) == 0);
   
     printf("hrtimer_test end...\n");
     return 0;
   }
   ```
   
   **test log on rv-virt:smp64,** 
   
   NuttShell (NSH)
   nsh> 
   nsh> uname -a
   NuttX 0.0.0 15e0a835ab-dirty Dec 19 2025 16:57:08 risc-v rv-virt
   nsh> 
   nsh> hello
   
   test callback ...
   test callback ...
   test callback ...
   test callback ...
   test callback ...
   test callback ...
   test callback ...
   test callback ...
   (....)


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