gustavonihei commented on a change in pull request #4779:
URL: https://github.com/apache/incubator-nuttx/pull/4779#discussion_r741945947



##########
File path: libs/libc/wqueue/work_usrthread.c
##########
@@ -193,15 +193,18 @@ static void work_process(FAR struct usr_wqueue_s *wqueue)
     {
       struct timespec rqtp;
       time_t sec;
+      uint64_t nsec;
 
       /* Wait awhile to check the work list.  We will wait here until
        * either the time elapses or until we are awakened by a semaphore.
        * Interrupts will be re-enabled while we wait.
        */
 
-      sec          = next / 1000000;
-      rqtp.tv_sec  = sec;
-      rqtp.tv_nsec = (next - (sec * 1000000)) * 1000;
+      clock_gettime(CLOCK_REALTIME, &rqtp);
+      nsec = 1000 * TICK2USEC(next);
+      sec = nsec / (1000 * 1000 * 1000);
+      rqtp.tv_sec  += sec;
+      rqtp.tv_nsec += (uint32_t)(nsec - sec * 1000 * 1000 * 1000);

Review comment:
       ```suggestion
         clock_gettime(CLOCK_REALTIME, &rqtp);
         nsec = TICK2NSEC(next);
         sec = nsec / NSEC_PER_SEC;
         rqtp.tv_sec  += sec;
         rqtp.tv_nsec += nsec - sec * NSEC_PER_SEC;
   
         if (rqtp.tv_nsec >= NSEC_PER_SEC)
           {
             rqtp.tv_sec++;
             rqtp.tv_nsec -= NSEC_PER_SEC;
           }
   ```
   Some considerations here:
   1) Suggest using the nanoseconds macros for achieving cleaner code.
   2) The cast to `uint32_t` might end up narrowing the value on 64 bits 
architectures, since `tv_nsec` type is `long`. I believe it can be removed.
   3) After incrementing `tv_nsec`, we still need to check if the final value 
is within the valid range of \[0, 1000000000\] 
(https://en.cppreference.com/w/c/chrono/timespec)




-- 
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: commits-unsubscr...@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


Reply via email to