You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@nuttx.apache.org by gn...@apache.org on 2020/02/15 13:18:28 UTC

[incubator-nuttx] 01/04: Make compare_timespec public so timer driver could reuse it

This is an automated email from the ASF dual-hosted git repository.

gnutt pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-nuttx.git

commit 20a9a62fdf90957b05ff95dfbf18da8e9b30331c
Author: Xiang Xiao <xi...@xiaomi.com>
AuthorDate: Fri Feb 7 13:38:52 2020 +0800

    Make compare_timespec public so timer driver could reuse it
---
 drivers/timers/arch_alarm.c       | 15 +--------------
 include/nuttx/clock.h             | 13 +++++++++++++
 sched/clock/clock_abstime2ticks.c | 16 ++++++----------
 3 files changed, 20 insertions(+), 24 deletions(-)

diff --git a/drivers/timers/arch_alarm.c b/drivers/timers/arch_alarm.c
index 9f8d953..c1e1918 100644
--- a/drivers/timers/arch_alarm.c
+++ b/drivers/timers/arch_alarm.c
@@ -84,19 +84,6 @@ static inline void timespec_from_usec(FAR struct timespec *ts,
   ts->tv_nsec   = microseconds * NSEC_PER_USEC;
 }
 
-static inline int timespec_compare(FAR const struct timespec *ts1,
-                                   FAR const struct timespec *ts2)
-{
-  if (ts1->tv_sec != ts2->tv_sec)
-    {
-      return ts1->tv_sec - ts2->tv_sec;
-    }
-  else
-    {
-      return ts1->tv_nsec - ts2->tv_nsec;
-    }
-}
-
 static void udelay_accurate(useconds_t microseconds)
 {
   struct timespec now;
@@ -107,7 +94,7 @@ static void udelay_accurate(useconds_t microseconds)
   timespec_from_usec(&delta, microseconds);
   clock_timespec_add(&now, &delta, &end);
 
-  while (timespec_compare(&now, &end) < 0)
+  while (clock_timespec_compare(&now, &end) < 0)
     {
       ONESHOT_CURRENT(g_oneshot_lower, &now);
     }
diff --git a/include/nuttx/clock.h b/include/nuttx/clock.h
index 1ae712c..72177f1 100644
--- a/include/nuttx/clock.h
+++ b/include/nuttx/clock.h
@@ -271,6 +271,19 @@ EXTERN volatile clock_t g_system_timer;
  ****************************************************************************/
 
 /****************************************************************************
+ * Name: clock_timespec_compare
+ *
+ * Description:
+ *    Return < 0 if time ts1 is before time ts2
+ *    Return > 0 if time ts2 is before time ts1
+ *    Return 0 if time ts1 is the same as time ts2
+ *
+ ****************************************************************************/
+
+int clock_timespec_compare(FAR const struct timespec *ts1,
+                           FAR const struct timespec *ts2);
+
+/****************************************************************************
  * Name:  clock_timespec_add
  *
  * Description:
diff --git a/sched/clock/clock_abstime2ticks.c b/sched/clock/clock_abstime2ticks.c
index 3f812c7..38dbb75 100644
--- a/sched/clock/clock_abstime2ticks.c
+++ b/sched/clock/clock_abstime2ticks.c
@@ -45,11 +45,11 @@
 #include "clock/clock.h"
 
 /****************************************************************************
- * Private Functions
+ * Public Functions
  ****************************************************************************/
 
 /****************************************************************************
- * Name: compare_timespec
+ * Name: clock_timespec_compare
  *
  * Description:
  *    Return < 0 if time a is before time b
@@ -58,8 +58,8 @@
  *
  ****************************************************************************/
 
-static long compare_timespec(FAR const struct timespec *a,
-                             FAR const struct timespec *b)
+int clock_timespec_compare(FAR const struct timespec *a,
+                           FAR const struct timespec *b)
 {
   if (a->tv_sec < b->tv_sec)
     {
@@ -71,14 +71,10 @@ static long compare_timespec(FAR const struct timespec *a,
       return 1;
     }
 
-  return (long)a->tv_nsec - (long)b->tv_nsec;
+  return a->tv_nsec - b->tv_nsec;
 }
 
 /****************************************************************************
- * Public Functions
- ****************************************************************************/
-
-/****************************************************************************
  * Name: clock_abstime2ticks
  *
  * Description:
@@ -115,7 +111,7 @@ int clock_abstime2ticks(clockid_t clockid, FAR const struct timespec *abstime,
       return EINVAL;
     }
 
-  if (compare_timespec(abstime, &currtime) < 0)
+  if (clock_timespec_compare(abstime, &currtime) < 0)
     {
       /* Every caller of clock_abstime2ticks check 'ticks < 0' to see if
        * absolute time is in the past. So lets just return negative tick