]> git.ipfire.org Git - thirdparty/glibc.git/commitdiff
Use clock_gettime to implement time.
authorAdhemerval Zanella <adhemerval.zanella@linaro.org>
Thu, 24 Oct 2019 17:52:30 +0000 (17:52 +0000)
committerAdhemerval Zanella <adhemerval.zanella@linaro.org>
Wed, 30 Oct 2019 20:05:14 +0000 (17:05 -0300)
Change the default implementation of time to call clock_gettime,
to align with new Linux ports that are expected to only implement
__NR_clock_gettime.  Arch-specific implementation that either call
the time vDSO or route to gettimeofday vDSO are not removed.

Also for Linux, CLOCK_REALTIME_COARSE is used instead of generic
CLOCK_REALTIME clockid.  This takes less CPU time and its behavior
better matches what the current glibc does.

Checked on x86_64-linux-gnu, i686-linux-gnu, powerpc64le-linux-gnu,
powerpc64-linux-gnu, powerpc-linux-gnu, and aarch64-linux-gnu.

Co-authored-by: Zack Weinberg <zackw@panix.com>
Reviewed-by: Lukasz Majewski <lukma@denx.de>
sysdeps/generic/time-clockid.h [moved from sysdeps/unix/sysv/linux/time.c with 64% similarity]
sysdeps/unix/sysv/linux/powerpc/time.c
sysdeps/unix/sysv/linux/time-clockid.h [moved from sysdeps/posix/time.c with 57% similarity]
time/time.c

similarity index 64%
rename from sysdeps/unix/sysv/linux/time.c
rename to sysdeps/generic/time-clockid.h
index f461733678da83d94197e48d16e2a086597b4889..13db6e6b5bb60455eaeaa8d437f57c749ff17e11 100644 (file)
@@ -1,4 +1,5 @@
-/* Copyright (C) 2005-2019 Free Software Foundation, Inc.
+/* System specific time definitions.  Generic Version.
+   Copyright 2019 Free Software Foundation, Inc.
    This file is part of the GNU C Library.
 
    The GNU C Library is free software; you can redistribute it and/or
    License along with the GNU C Library; if not, see
    <https://www.gnu.org/licenses/>.  */
 
-#include <stddef.h>
-#include <time.h>
-
-#include <sysdep.h>
-
-#ifdef __NR_time
-
-time_t
-time (time_t *t)
-{
-  INTERNAL_SYSCALL_DECL (err);
-  time_t res = INTERNAL_SYSCALL (time, err, 1, NULL);
-  /* There cannot be any error.  */
-  if (t != NULL)
-    *t = res;
-  return res;
-}
-libc_hidden_def (time)
-
-#else
-
-# include <sysdeps/posix/time.c>
-
-#endif
+/* Timer used on clock_gettime for time implementation.  */
+#define TIME_CLOCK_GETTIME_CLOCKID CLOCK_REALTIME
index c35b80fad1cfe2ffff313e401b9344942c191177..e957b81751511ccc1ded0d328ef35422875166b5 100644 (file)
@@ -78,6 +78,6 @@ libc_hidden_def (time)
 
 #else
 
-#include <sysdeps/posix/time.c>
+#include <time/time.c>
 
 #endif /* !SHARED */
similarity index 57%
rename from sysdeps/posix/time.c
rename to sysdeps/unix/sysv/linux/time-clockid.h
index 0ab31bd7b6b1dbf54808be484a1df433d92bd97b..1bdaa1809416fb47a6e8a5e4d406b9ee3169dacf 100644 (file)
@@ -1,4 +1,5 @@
-/* Copyright (C) 1991-2019 Free Software Foundation, Inc.
+/* System specific time definitions.  Generic Version.
+   Copyright 2019 Free Software Foundation, Inc.
    This file is part of the GNU C Library.
 
    The GNU C Library is free software; you can redistribute it and/or
    License along with the GNU C Library; if not, see
    <https://www.gnu.org/licenses/>.  */
 
-#include <stddef.h>            /* For NULL.  */
-#include <time.h>
-#include <sys/time.h>
-
-
-/* Return the current time as a `time_t' and also put it in *T if T is
-   not NULL.  Time is represented as seconds from Jan 1 00:00:00 1970.  */
-time_t
-time (time_t *t)
-{
-  struct timeval tv;
-  time_t result;
-
-  if (__gettimeofday (&tv, (struct timezone *) NULL))
-    result = (time_t) -1;
-  else
-    result = (time_t) tv.tv_sec;
-
-  if (t != NULL)
-    *t = result;
-  return result;
-}
-libc_hidden_def (time)
+/* Timer used on clock_gettime for time implementation.  For Linux
+   it uses the coarse version which returns the time at the last tick
+   and mimic what time as syscall should return.  */
+#define TIME_CLOCK_GETTIME_CLOCKID CLOCK_REALTIME_COARSE
index b53a06e29c72605a26e3c03a712e103df8a7011c..e6e5eeff7e9d5707259a77dd6c414e385357ff85 100644 (file)
    License along with the GNU C Library; if not, see
    <https://www.gnu.org/licenses/>.  */
 
-#include <errno.h>
 #include <time.h>
+#include <time-clockid.h>
 
 /* Return the time now, and store it in *TIMER if not NULL.  */
 time_t
 time (time_t *timer)
 {
-  __set_errno (ENOSYS);
+  struct timespec ts;
+  __clock_gettime (TIME_CLOCK_GETTIME_CLOCKID, &ts);
 
-  if (timer != NULL)
-    *timer = (time_t) -1;
-  return (time_t) -1;
+  if (timer)
+    *timer = ts.tv_sec;
+  return ts.tv_sec;
 }
 libc_hidden_def (time)
-
-stub_warning (time)