[PATCH] time: use common implementation calling gettimeofday()

Enrico Scholz <[email protected]> Sun, 20 Feb 2011 16:16:43 +0100
Newsgroups gmane.linux.lib.dietlibc
Message-ID <1298215004-28546-7-git-send-email-enrico.scholz@informatik.tu-chemnitz.de>
Instead of providing yet another arm/__time.c file for ARM-EABI, this
patch merges existing time(2) implementations (which did all the same
thing but with different code) into one global source file.  Build of
it depends on the existence of the __NR_time symbol.

The x64_64 implementation of time.S was moved to __time.S and an empty
time.S file was added to build from the correct VPATH.

Signed-off-by: Enrico Scholz <[email protected]>
---
 alpha/__time.c   |   11 -----------
 ia64/__time.c    |   14 --------------
 lib/__time.c     |   15 +++++++++++++++
 s390x/__time.c   |   10 ----------
 sparc64/__time.c |    1 -
 x86_64/__time.S  |   21 +++++++++++++++++++++
 x86_64/time.S    |   22 +---------------------
 7 files changed, 37 insertions(+), 57 deletions(-)
 delete mode 100644 alpha/__time.c
 delete mode 100644 ia64/__time.c
 create mode 100644 lib/__time.c
 delete mode 100644 s390x/__time.c
 delete mode 100644 sparc64/__time.c
 create mode 100644 x86_64/__time.S

diff --git a/alpha/__time.c b/alpha/__time.c
deleted file mode 100644
index 07275e0..0000000
--- a/alpha/__time.c
+++ /dev/null
@@ -1,11 +0,0 @@
-#include <time.h>
-#include <sys/time.h>
-
-time_t time(time_t *foo) {
-  struct timeval tv;
-  time_t tmp=(time_t)-1;
-  if (gettimeofday(&tv,0)==0)
-    tmp=(time_t)tv.tv_sec;
-  if (foo) *foo=tmp;
-  return tmp;
-}
diff --git a/ia64/__time.c b/ia64/__time.c
deleted file mode 100644
index 7547acb..0000000
--- a/ia64/__time.c
+++ /dev/null
@@ -1,14 +0,0 @@
-#include <time.h>
-#include <sys/time.h>
-
-time_t time(time_t*t) {
-  struct timeval tv;
-  time_t ret;
-  if (gettimeofday(&tv,0)) {
-    ret=(time_t)-1;
-  } else {
-    ret=(time_t)tv.tv_sec;
-  }
-  if (t) *t=ret;
-  return ret;
-}
diff --git a/lib/__time.c b/lib/__time.c
new file mode 100644
index 0000000..87a6d5d
--- /dev/null
+++ b/lib/__time.c
@@ -0,0 +1,15 @@
+#include <sys/time.h>
+#include <time.h>
+#include <syscalls.h>
+
+#ifndef __NR_time
+time_t time(time_t *t)
+{
+  struct timeval tv;
+  if (__unlikely(gettimeofday(&tv, NULL) < 0))
+    tv.tv_sec = -1;
+  if (t)
+    *t = tv.tv_sec;
+  return tv.tv_sec;
+}
+#endif
diff --git a/s390x/__time.c b/s390x/__time.c
deleted file mode 100644
index af935ae..0000000
--- a/s390x/__time.c
+++ /dev/null
@@ -1,10 +0,0 @@
-#include <time.h>
-#include <sys/time.h>
-
-time_t time(time_t *t) {
-  struct timeval tv;
-  if (gettimeofday(&tv, 0) == -1)
-    tv.tv_sec=-1;
-  if (t) *t=tv.tv_sec;
-  return tv.tv_sec;
-}
diff --git a/sparc64/__time.c b/sparc64/__time.c
deleted file mode 100644
index efa1e8b..0000000
--- a/sparc64/__time.c
+++ /dev/null
@@ -1 +0,0 @@
-#include "alpha/__time.c"
diff --git a/x86_64/__time.S b/x86_64/__time.S
new file mode 100644
index 0000000..9d2d4b4
--- /dev/null
+++ b/x86_64/__time.S
@@ -0,0 +1,21 @@
+/* implement time(2) via gettimeofday(2) on x86-64 because gettimeofday
+   is a vsyscall (i.e. no actual switch to kernel mode) */
+.text
+.global time
+.type time,@function
+time:
+	push    %rdi
+	xor	%rsi,%rsi
+	sub	$16,%rsp
+	mov	%rsp,%rdi
+	call	gettimeofday
+	pop	%rax
+	pop	%rdi
+	pop	%rdi
+	test	%rdi,%rdi
+	jz	1f
+	mov	%rax,(%rdi)
+1:
+	ret
+.Lhere:
+	.size	 time,.Lhere-time
diff --git a/x86_64/time.S b/x86_64/time.S
index 9d2d4b4..dbe7894 100644
--- a/x86_64/time.S
+++ b/x86_64/time.S
@@ -1,21 +1 @@
-/* implement time(2) via gettimeofday(2) on x86-64 because gettimeofday
-   is a vsyscall (i.e. no actual switch to kernel mode) */
-.text
-.global time
-.type time,@function
-time:
-	push    %rdi
-	xor	%rsi,%rsi
-	sub	$16,%rsp
-	mov	%rsp,%rdi
-	call	gettimeofday
-	pop	%rax
-	pop	%rdi
-	pop	%rdi
-	test	%rdi,%rdi
-	jz	1f
-	mov	%rax,(%rdi)
-1:
-	ret
-.Lhere:
-	.size	 time,.Lhere-time
+	.text	/* avoid empty source file */
-- 
1.7.4