bug-gnulib
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[PATCH] timespec: new function current_timespec


From: Paul Eggert
Subject: [PATCH] timespec: new function current_timespec
Date: Sun, 16 Sep 2018 21:30:03 -0700

* lib/gettime.c (gettime): Prefer clock_gettime to nanotime,
and don’t worry about it failing on a CLOCK_REALTIME arg.
POSIX requires it to succeed and I don’t know of any
counterexamples where the fallbacks would work.
(current_timespec): New function, taken from Emacs.  It is more
convenient than gettime, and can help register allocation.
* lib/timespec.h: Include arg-nonnull.h.
(current_timespec): New declaration.
(gettime, settime): Declare args to be nonnull.
* modules/timespec (Depends-on): Add snippet/arg-nonnull.
---
 ChangeLog        | 14 ++++++++++++++
 lib/gettime.c    | 29 ++++++++++++++++-------------
 lib/timespec.h   | 10 ++++++----
 modules/timespec |  1 +
 4 files changed, 37 insertions(+), 17 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index 51608264a..c796ecdda 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,17 @@
+2018-09-16  Paul Eggert  <address@hidden>
+
+       timespec: new function current_timespec
+       * lib/gettime.c (gettime): Prefer clock_gettime to nanotime,
+       and don’t worry about it failing on a CLOCK_REALTIME arg.
+       POSIX requires it to succeed and I don’t know of any
+       counterexamples where the fallbacks would work.
+       (current_timespec): New function, taken from Emacs.  It is more
+       convenient than gettime, and can help register allocation.
+       * lib/timespec.h: Include arg-nonnull.h.
+       (current_timespec): New declaration.
+       (gettime, settime): Declare args to be nonnull.
+       * modules/timespec (Depends-on): Add snippet/arg-nonnull.
+
 2018-09-16  Bruno Haible  <address@hidden>
 
        setlocale: Improve locale handling on macOS 10.12 or newer.
diff --git a/lib/gettime.c b/lib/gettime.c
index 9a4e342f1..171f22476 100644
--- a/lib/gettime.c
+++ b/lib/gettime.c
@@ -28,21 +28,24 @@
 void
 gettime (struct timespec *ts)
 {
-#if HAVE_NANOTIME
+#if defined CLOCK_REALTIME && HAVE_CLOCK_GETTIME
+  clock_gettime (CLOCK_REALTIME, ts);
+#elif HAVE_NANOTIME
   nanotime (ts);
 #else
+  struct timeval tv;
+  gettimeofday (&tv, NULL);
+  ts->tv_sec = tv.tv_sec;
+  ts->tv_nsec = tv.tv_usec * 1000;
+#endif
+}
 
-# if defined CLOCK_REALTIME && HAVE_CLOCK_GETTIME
-  if (clock_gettime (CLOCK_REALTIME, ts) == 0)
-    return;
-# endif
-
-  {
-    struct timeval tv;
-    gettimeofday (&tv, NULL);
-    ts->tv_sec = tv.tv_sec;
-    ts->tv_nsec = tv.tv_usec * 1000;
-  }
+/* Return the current system time as a struct timespec.  */
 
-#endif
+struct timespec
+current_timespec (void)
+{
+  struct timespec ts;
+  gettime (&ts);
+  return ts;
 }
diff --git a/lib/timespec.h b/lib/timespec.h
index c414cfe45..cc49668f4 100644
--- a/lib/timespec.h
+++ b/lib/timespec.h
@@ -17,9 +17,9 @@
    along with this program.  If not, see <https://www.gnu.org/licenses/>.  */
 
 #if ! defined TIMESPEC_H
-# define TIMESPEC_H
+#define TIMESPEC_H
 
-# include <time.h>
+#include <time.h>
 
 #ifndef _GL_INLINE_HEADER_BEGIN
  #error "Please include config.h first."
@@ -33,6 +33,7 @@ _GL_INLINE_HEADER_BEGIN
 extern "C" {
 #endif
 
+#include "arg-nonnull.h"
 #include "verify.h"
 
 /* Inverse resolution of timespec timestamps (in units per second),
@@ -122,8 +123,9 @@ timespectod (struct timespec a)
   return a.tv_sec + a.tv_nsec / 1e9;
 }
 
-void gettime (struct timespec *);
-int settime (struct timespec const *);
+struct timespec current_timespec (void);
+void gettime (struct timespec *) _GL_ARG_NONNULL ((1));
+int settime (struct timespec const *) _GL_ARG_NONNULL ((1));
 
 #ifdef __cplusplus
 }
diff --git a/modules/timespec b/modules/timespec
index e6e151482..956cfea22 100644
--- a/modules/timespec
+++ b/modules/timespec
@@ -8,6 +8,7 @@ m4/timespec.m4
 
 Depends-on:
 extern-inline
+snippet/arg-nonnull
 time
 verify
 
-- 
2.17.1




reply via email to

[Prev in Thread] Current Thread [Next in Thread]