diff mbox series

[2/2] time: Implement extra timebase defines for timespec_get and timespec_getres

Message ID 20230621183206.260-3-luoyonggang@gmail.com
State New
Headers show
Series Implement newly add timebase defines for c2x timespec_get and timespec_getres | expand

Commit Message

Yonggang Luo June 21, 2023, 6:32 p.m. UTC
these are:

  TIME_MONOTONIC_RAW
  TIME_UTC_COARSE
  TIME_MONOTONIC_COARSE
  TIME_BOOTTIME
  TIME_UTC_ALARM
  TIME_BOOTTIME_ALARM
  TIME_SGI_CYCLE
  TIME_TAI

according to https://gustedt.gitlabpages.inria.fr/c23-library/#time_monotonic-time_active-time_thread_active

Signed-off-by: Yonggang Luo <luoyonggang@gmail.com>
---
 NEWS                       | 10 ++++++++++
 include/time.h             | 34 +++++++++++++++++++++++++++++++
 time/time.h                | 10 ++++++++++
 time/tst-timespec_get.c    | 41 ++++++++++++++++++++++++++++++++++++++
 time/tst-timespec_getres.c | 35 ++++++++++++++++++++++++++++++++
 5 files changed, 130 insertions(+)
diff mbox series

Patch

diff --git a/NEWS b/NEWS
index 375e15c5e1..2889e4b6c4 100644
--- a/NEWS
+++ b/NEWS
@@ -9,6 +9,16 @@  Version 2.38
 
 Major new features:
 
+* Extra timebase defines are supported, these are:
+  TIME_MONOTONIC_RAW
+  TIME_UTC_COARSE
+  TIME_MONOTONIC_COARSE
+  TIME_BOOTTIME
+  TIME_UTC_ALARM
+  TIME_BOOTTIME_ALARM
+  TIME_SGI_CYCLE
+  TIME_TAI
+
 * C2x timebase macro defines are supported, these are:
   TIME_MONOTONIC
   TIME_ACTIVE
diff --git a/include/time.h b/include/time.h
index 77ca52ecbf..19db10ffc6 100644
--- a/include/time.h
+++ b/include/time.h
@@ -381,6 +381,40 @@  clock_from_timebase (int timebase)
     case TIME_THREAD_ACTIVE:
       clockid = CLOCK_THREAD_CPUTIME_ID;
       break;
+    case TIME_MONOTONIC_RAW:
+      clockid = CLOCK_MONOTONIC_RAW;
+      break;
+    case TIME_UTC_COARSE:
+      clockid = CLOCK_REALTIME_COARSE;
+      break;
+    case TIME_MONOTONIC_COARSE:
+      clockid = CLOCK_MONOTONIC_COARSE;
+      break;
+#    ifdef CLOCK_BOOTTIME
+    case TIME_BOOTTIME:
+      clockid = CLOCK_BOOTTIME;
+      break;
+#    endif
+#    ifdef CLOCK_REALTIME_ALARM
+    case TIME_UTC_ALARM:
+      clockid = CLOCK_REALTIME_ALARM;
+      break;
+#    endif
+#    ifdef CLOCK_BOOTTIME_ALARM
+    case TIME_BOOTTIME_ALARM:
+      clockid = CLOCK_BOOTTIME_ALARM;
+      break;
+#    endif
+#    ifdef CLOCK_SGI_CYCLE
+    case TIME_SGI_CYCLE:
+      clockid = CLOCK_SGI_CYCLE;
+      break;
+#    endif
+#    ifdef CLOCK_TAI
+    case TIME_TAI:
+      clockid = CLOCK_TAI;
+      break;
+#    endif
     default:
       break;
     }
diff --git a/time/time.h b/time/time.h
index d01c90e229..10dfabbd3f 100644
--- a/time/time.h
+++ b/time/time.h
@@ -69,6 +69,16 @@  typedef __pid_t pid_t;
 # define TIME_ACTIVE             3
 # define TIME_THREAD_ACTIVE      4
 #endif
+#ifdef __USE_GNU
+# define TIME_MONOTONIC_RAW      5
+# define TIME_UTC_COARSE         6
+# define TIME_MONOTONIC_COARSE   7
+# define TIME_BOOTTIME           8
+# define TIME_UTC_ALARM          9
+# define TIME_BOOTTIME_ALARM     10
+# define TIME_SGI_CYCLE          11
+# define TIME_TAI                12
+#endif
 
 __BEGIN_DECLS
 
diff --git a/time/tst-timespec_get.c b/time/tst-timespec_get.c
index f97327666c..99a08279fc 100644
--- a/time/tst-timespec_get.c
+++ b/time/tst-timespec_get.c
@@ -28,6 +28,13 @@  test_timespec_get (int timebase)
     TEST_VERIFY (ts.tv_nsec < 1000000000);
 }
 
+static void
+test_timespec_get_not_support(int timebase)
+{
+    struct timespec ts;
+    TEST_COMPARE (timespec_get (&ts, timebase), 0);
+}
+
 static int
 do_test (void)
 {
@@ -43,6 +50,40 @@  do_test (void)
     test_timespec_get (TIME_THREAD_ACTIVE);
   }
 
+  {
+    test_timespec_get (TIME_MONOTONIC_RAW);
+    test_timespec_get (TIME_UTC_COARSE);
+    test_timespec_get (TIME_MONOTONIC_COARSE);
+  }
+
+  {
+#ifdef CLOCK_BOOTTIME
+    test_timespec_get (TIME_BOOTTIME);
+#else
+    test_timespec_get_not_support (TIME_BOOTTIME);
+#endif
+#ifdef CLOCK_REALTIME_ALARM
+    test_timespec_get (TIME_UTC_ALARM);
+#else
+    test_timespec_get_not_support (TIME_UTC_ALARM);
+#endif
+#ifdef CLOCK_BOOTTIME_ALARM
+    test_timespec_get (TIME_BOOTTIME_ALARM);
+#else
+    test_timespec_get_not_support (TIME_BOOTTIME_ALARM);
+#endif
+#ifdef CLOCK_SGI_CYCLE
+    test_timespec_get (TIME_SGI_CYCLE);
+#else
+    test_timespec_get_not_support (TIME_SGI_CYCLE);
+#endif
+#ifdef CLOCK_TAI
+    test_timespec_get (TIME_TAI);
+#else
+    test_timespec_get_not_support (TIME_TAI);
+#endif
+  }
+
   return 0;
 }
 
diff --git a/time/tst-timespec_getres.c b/time/tst-timespec_getres.c
index 960f7c3298..623a1d841c 100644
--- a/time/tst-timespec_getres.c
+++ b/time/tst-timespec_getres.c
@@ -54,6 +54,41 @@  do_test (void)
     test_timespec_getres (CLOCK_THREAD_CPUTIME_ID, TIME_THREAD_ACTIVE);
   }
 
+  {
+    test_timespec_getres (CLOCK_MONOTONIC_RAW, TIME_MONOTONIC_RAW);
+    test_timespec_getres (CLOCK_REALTIME_COARSE, TIME_UTC_COARSE);
+    test_timespec_getres (CLOCK_MONOTONIC_COARSE, TIME_MONOTONIC_COARSE);
+  }
+
+  {
+    struct timespec ts;
+#ifdef CLOCK_BOOTTIME
+    test_timespec_getres (CLOCK_BOOTTIME, TIME_BOOTTIME);
+#else
+    TEST_COMPARE (timespec_getres (&ts, TIME_BOOTTIME), 0);
+#endif
+#ifdef CLOCK_REALTIME_ALARM
+    test_timespec_getres (CLOCK_REALTIME_ALARM, TIME_UTC_ALARM);
+#else
+    TEST_COMPARE (timespec_getres (&ts, TIME_UTC_ALARM), 0);
+#endif
+#ifdef CLOCK_BOOTTIME_ALARM
+    test_timespec_getres (CLOCK_BOOTTIME_ALARM, TIME_BOOTTIME_ALARM);
+#else
+    TEST_COMPARE (timespec_getres (&ts, TIME_BOOTTIME_ALARM), 0);
+#endif
+#ifdef CLOCK_SGI_CYCLE
+    test_timespec_getres (CLOCK_SGI_CYCLE, TIME_SGI_CYCLE);
+#else
+    TEST_COMPARE (timespec_getres (&ts, TIME_SGI_CYCLE), 0);
+#endif
+#ifdef CLOCK_TAI
+    test_timespec_getres (CLOCK_TAI, TIME_TAI);
+#else
+    TEST_COMPARE (timespec_getres (&ts, TIME_TAI), 0);
+#endif
+  }
+
   return 0;
 }