From 8e7328b6e0cc88f98dc25570d244833add8c296b Mon Sep 17 00:00:00 2001
From: tromey <tromey@138bc75d-0d04-0410-961f-82ee72b054a4>
Date: Wed, 2 Aug 2000 21:54:04 +0000
Subject: [PATCH] 	* Makefile.in: Rebuilt. 	* Makefile.am
 (libgcj_la_SOURCES): Added posix.cc. 	* java/net/natPlainSocketImpl.cc:
 Include posix.h. 	(accept): Use _Jv_select. 	*
 java/net/natPlainDatagramSocketImpl.cc: Include posix.h. 	(receive): Use
 _Jv_select. 	* java/io/natFileDescriptorPosix.cc: Include posix.h. 
 (available): Use _Jv_select. 	* java/lang/natSystem.cc: Include posix.h. 
 (currentTimeMillis): Use _Jv_gettimeofday. 	* include/posix.h: New file. 
 * posix.cc: New file.

git-svn-id: svn+ssh://gcc.gnu.org/svn/gcc/trunk@35435 138bc75d-0d04-0410-961f-82ee72b054a4
---
 libjava/ChangeLog                             |  13 +++
 libjava/Makefile.am                           |   2 +-
 libjava/Makefile.in                           |   6 +-
 libjava/include/posix.h                       |  27 +++++
 libjava/java/io/natFileDescriptorPosix.cc     |  11 +-
 libjava/java/lang/natSystem.cc                |  32 +-----
 .../java/net/natPlainDatagramSocketImpl.cc    |  10 +-
 libjava/java/net/natPlainSocketImpl.cc        |  10 +-
 libjava/posix.cc                              | 104 ++++++++++++++++++
 9 files changed, 159 insertions(+), 56 deletions(-)
 create mode 100644 libjava/include/posix.h
 create mode 100644 libjava/posix.cc

diff --git a/libjava/ChangeLog b/libjava/ChangeLog
index ed5bc408f1c0..6813243db35d 100644
--- a/libjava/ChangeLog
+++ b/libjava/ChangeLog
@@ -1,5 +1,18 @@
 2000-08-02  Tom Tromey  <tromey@cygnus.com>
 
+	* Makefile.in: Rebuilt.
+	* Makefile.am (libgcj_la_SOURCES): Added posix.cc.
+	* java/net/natPlainSocketImpl.cc: Include posix.h.
+	(accept): Use _Jv_select.
+	* java/net/natPlainDatagramSocketImpl.cc: Include posix.h.
+	(receive): Use _Jv_select.
+	* java/io/natFileDescriptorPosix.cc: Include posix.h.
+	(available): Use _Jv_select.
+	* java/lang/natSystem.cc: Include posix.h.
+	(currentTimeMillis): Use _Jv_gettimeofday.
+	* include/posix.h: New file.
+	* posix.cc: New file.
+
 	* scripts/encodings.pl: New file.
 	* Makefile.in: Rebuilt.
 	* Makefile.am (convert_source_files): Added IOConverter.java.
diff --git a/libjava/Makefile.am b/libjava/Makefile.am
index e4e569bca554..6bd8fcce18c9 100644
--- a/libjava/Makefile.am
+++ b/libjava/Makefile.am
@@ -118,7 +118,7 @@ javao_files = $(java_source_files:.java=.lo) \
 ## Extract the libffi object file names.
 libffi_files = `$(AR) t ../libffi/.libs/libffi.a 2>/dev/null | sed 's/\.o/\.lo/g' | sed 's/^/..\/libffi\//g'`
 
-libgcj_la_SOURCES = prims.cc jni.cc exception.cc \
+libgcj_la_SOURCES = prims.cc posix.cc jni.cc exception.cc \
 	resolve.cc defineclass.cc interpret.cc name-finder.cc
 EXTRA_libgcj_la_SOURCES = boehm.cc nogc.cc posix-threads.cc no-threads.cc \
 	$(c_source_files) $(java_source_files) $(built_java_source_files)
diff --git a/libjava/Makefile.in b/libjava/Makefile.in
index 6d51cb0a4395..f27de200cf66 100644
--- a/libjava/Makefile.in
+++ b/libjava/Makefile.in
@@ -193,7 +193,7 @@ javao_files = $(java_source_files:.java=.lo) \
 
 libffi_files = `$(AR) t ../libffi/.libs/libffi.a 2>/dev/null | sed 's/\.o/\.lo/g' | sed 's/^/..\/libffi\//g'`
 
-libgcj_la_SOURCES = prims.cc jni.cc exception.cc \
+libgcj_la_SOURCES = prims.cc posix.cc jni.cc exception.cc \
 	resolve.cc defineclass.cc interpret.cc name-finder.cc
 
 EXTRA_libgcj_la_SOURCES = boehm.cc nogc.cc posix-threads.cc no-threads.cc \
@@ -973,7 +973,7 @@ libgcjdata_a_LIBADD =
 @NEEDS_DATA_START_TRUE@libgcjdata_a_OBJECTS =  libgcjdata.o
 LTLIBRARIES =  $(toolexeclib_LTLIBRARIES)
 
-libgcj_la_OBJECTS =  prims.lo jni.lo exception.lo resolve.lo \
+libgcj_la_OBJECTS =  prims.lo posix.lo jni.lo exception.lo resolve.lo \
 defineclass.lo interpret.lo name-finder.lo
 @NATIVE_TRUE@bin_PROGRAMS =  jv-convert$(EXEEXT) gij$(EXEEXT)
 @NATIVE_TRUE@@MAINTAINER_MODE_TRUE@noinst_PROGRAMS =  \
@@ -1422,7 +1422,7 @@ DEP_FILES =  .deps/$(srcdir)/$(CONVERT_DIR)/gen-from-JIS.P \
 .deps/java/util/zip/ZipInputStream.P \
 .deps/java/util/zip/ZipOutputStream.P .deps/jni.P .deps/libgcjdata.P \
 .deps/name-finder.P .deps/no-threads.P .deps/nogc.P \
-.deps/posix-threads.P .deps/prims.P .deps/resolve.P
+.deps/posix-threads.P .deps/posix.P .deps/prims.P .deps/resolve.P
 SOURCES = $(libgcjdata_a_SOURCES) $(libgcj_la_SOURCES) $(EXTRA_libgcj_la_SOURCES) $(jv_convert_SOURCES) $(EXTRA_jv_convert_SOURCES) $(gij_SOURCES) $(gen_from_JIS_SOURCES) $(EXTRA_gen_from_JIS_SOURCES)
 OBJECTS = $(libgcjdata_a_OBJECTS) $(libgcj_la_OBJECTS) $(jv_convert_OBJECTS) $(gij_OBJECTS) $(gen_from_JIS_OBJECTS)
 
diff --git a/libjava/include/posix.h b/libjava/include/posix.h
new file mode 100644
index 000000000000..278f7eb773b4
--- /dev/null
+++ b/libjava/include/posix.h
@@ -0,0 +1,27 @@
+// posix.h -- Helper functions for POSIX-flavored OSs.
+
+/* Copyright (C) 2000  Free Software Foundation
+
+   This file is part of libgcj.
+
+This software is copyrighted work licensed under the terms of the
+Libgcj License.  Please consult the file "LIBGCJ_LICENSE" for
+details.  */
+
+#include <time.h>
+#include <sys/types.h>
+
+#ifdef HAVE_SYS_TIME_H
+#include <sys/time.h>
+#endif
+
+#ifdef HAVE_SYS_SELECT_H
+#include <sys/select.h>
+#endif
+
+#ifdef HAVE_UNISTD_H
+#include <unistd.h>
+#endif
+
+extern int _Jv_select (int n, fd_set *, fd_set *, fd_set *, struct timeval *);
+extern void _Jv_gettimeofday (struct timeval *);
diff --git a/libjava/java/io/natFileDescriptorPosix.cc b/libjava/java/io/natFileDescriptorPosix.cc
index 6e06a034dd9c..059eeb965645 100644
--- a/libjava/java/io/natFileDescriptorPosix.cc
+++ b/libjava/java/io/natFileDescriptorPosix.cc
@@ -10,16 +10,11 @@ details.  */
 
 #include <config.h>
 
-#ifdef HAVE_UNISTD_H
-#include <unistd.h>
-#endif
+#include "posix.h"
+
 #include <errno.h>
 #include <stdio.h>
 #include <string.h>
-#ifdef HAVE_SYS_TIME_H
-#include <sys/time.h>
-#endif
-#include <sys/types.h>
 #include <sys/stat.h>
 #include <sys/param.h>
 #include <fcntl.h>
@@ -309,7 +304,7 @@ java::io::FileDescriptor::available (void)
       struct timeval tv;
       tv.tv_sec = 0;
       tv.tv_usec = 0;
-      r = ::select (fd + 1, &rd, NULL, NULL, &tv);
+      r = _Jv_select (fd + 1, &rd, NULL, NULL, &tv);
       if (r == -1)
 	goto posix_error;
       num = r == 0 ? 0 : 1;
diff --git a/libjava/java/lang/natSystem.cc b/libjava/java/lang/natSystem.cc
index e376ec2f84c5..2d82c896a07e 100644
--- a/libjava/java/lang/natSystem.cc
+++ b/libjava/java/lang/natSystem.cc
@@ -11,16 +11,9 @@ details.  */
 #include <config.h>
 
 #include <string.h>
-#include <time.h>
 #include <stdlib.h>
 
-#ifdef HAVE_UNISTD_H
-#include <unistd.h>
-#endif
-
-#ifdef HAVE_SYS_TIME_H
-#include <sys/time.h>
-#endif
+#include "posix.h"
 
 #ifdef HAVE_PWD_H
 #include <pwd.h>
@@ -48,10 +41,6 @@ extern java::lang::Class SystemClass;
 
 
 
-#if defined (ECOS)
-extern "C" unsigned long long _clock (void);
-#endif
-
 void
 java::lang::System::setErr (java::io::PrintStream *newErr)
 {
@@ -152,24 +141,9 @@ java::lang::System::currentTimeMillis (void)
 {
   jlong r;
 
-#if defined (HAVE_GETTIMEOFDAY)
   struct timeval tv;
-  gettimeofday (&tv, NULL);
-  r = (jlong) tv.tv_sec * 1000 + tv.tv_usec / 1000;
-#elif defined (HAVE_TIME)
-  r = time (NULL) * 1000;
-#elif defined (HAVE_FTIME)
-  struct timeb t;
-  ftime (&t);
-  r = t.time * 1000 + t.millitm;
-#elif defined (ECOS)
-  r = _clock();
-#else
-  // In the absence of any function, time remains forever fixed.
-  r = 23;
-#endif
-
-  return r;
+  _Jv_gettimeofday (&tv);
+  return (jlong) tv.tv_sec * 1000 + tv.tv_usec / 1000;
 }
 
 jint
diff --git a/libjava/java/net/natPlainDatagramSocketImpl.cc b/libjava/java/net/natPlainDatagramSocketImpl.cc
index 94383f8309dc..fe83b1053f6c 100644
--- a/libjava/java/net/natPlainDatagramSocketImpl.cc
+++ b/libjava/java/net/natPlainDatagramSocketImpl.cc
@@ -17,16 +17,10 @@ details.  */
 #define ENOPROTOOPT 109
 #endif
 #else /* USE_WINSOCK */
-#ifdef HAVE_SYS_TYPES_H
-#include <sys/types.h>
-#endif
+#include "posix.h"
 #ifdef HAVE_SYS_SOCKET_H
 #include <sys/socket.h>
 #endif
-#include <sys/time.h>
-#ifdef HAVE_SYS_SELECT_H
-#include <sys/select.h>
-#endif
 #ifdef HAVE_NETINET_IN_H
 #include <netinet/in.h>
 #endif
@@ -314,7 +308,7 @@ java::net::PlainDatagramSocketImpl::receive (java::net::DatagramPacket *p)
       tv.tv_sec = timeout / 1000;
       tv.tv_usec = (timeout % 1000) * 1000;
       int retval;
-      if ((retval = select (fnum + 1, &rset, NULL, NULL, &tv)) < 0)
+      if ((retval = _Jv_select (fnum + 1, &rset, NULL, NULL, &tv)) < 0)
 	goto error;
       else if (retval == 0)
 	JvThrow (new java::io::InterruptedIOException ());
diff --git a/libjava/java/net/natPlainSocketImpl.cc b/libjava/java/net/natPlainSocketImpl.cc
index 52187461f49a..18fd74ba447e 100644
--- a/libjava/java/net/natPlainSocketImpl.cc
+++ b/libjava/java/net/natPlainSocketImpl.cc
@@ -1,4 +1,4 @@
-/* Copyright (C) 1998, 1999  Free Software Foundation
+/* Copyright (C) 1998, 1999, 2000  Free Software Foundation
 
    This file is part of libgcj.
 
@@ -19,12 +19,8 @@ details.  */
 #define ENOPROTOOPT 109
 #endif
 #else /* USE_WINSOCK */
-#include <sys/types.h>
+#include "posix.h"
 #include <sys/socket.h>
-#include <sys/time.h>
-#ifdef HAVE_SYS_SELECT_H
-#include <sys/select.h>
-#endif
 #include <netinet/in.h>
 #include <netinet/tcp.h>
 #include <errno.h>
@@ -249,7 +245,7 @@ java::net::PlainSocketImpl::accept (java::net::PlainSocketImpl *s)
       tv.tv_sec = timeout / 1000;
       tv.tv_usec = (timeout % 1000) * 1000;
       int retval;
-      if ((retval = select (fnum + 1, &rset, NULL, NULL, &tv)) < 0)
+      if ((retval = _Jv_select (fnum + 1, &rset, NULL, NULL, &tv)) < 0)
 	goto error;
       else if (retval == 0)
 	JvThrow (new java::io::InterruptedIOException (
diff --git a/libjava/posix.cc b/libjava/posix.cc
new file mode 100644
index 000000000000..d470a644da3c
--- /dev/null
+++ b/libjava/posix.cc
@@ -0,0 +1,104 @@
+// posix.cc -- Helper functions for POSIX-flavored OSs.
+
+/* Copyright (C) 2000  Free Software Foundation
+
+   This file is part of libgcj.
+
+This software is copyrighted work licensed under the terms of the
+Libgcj License.  Please consult the file "LIBGCJ_LICENSE" for
+details.  */
+
+#include <config.h>
+
+#include "posix.h"
+
+#include <errno.h>
+
+#if defined (ECOS)
+extern "C" unsigned long long _clock (void);
+#endif
+
+// gettimeofday implementation.
+void
+_Jv_gettimeofday (struct timeval *tv)
+{
+#if defined (HAVE_GETTIMEOFDAY)
+  gettimeofday (tv, NULL);
+#elif defined (HAVE_TIME)
+  tv->tv_sec = time (NULL);
+  tv->tv_usec = 0;
+#elif defined (HAVE_FTIME)
+  struct timeb t;
+  ftime (&t);
+  tv->tv_sec = t.time;
+  tv->tv_usec = t.millitm * 1000;
+#elif defined (ECOS)
+  // FIXME.
+  tv->tv_sec = _clock () / 1000;
+  tv->tv_usec = 0;
+#else
+  // In the absence of any function, time remains forever fixed.
+  tv->tv_sec = 23;
+  tv->tv_usec = 0;
+#endif
+}
+
+// A wrapper for select() which ignores EINTR.
+int
+_Jv_select (int n, fd_set *readfds, fd_set  *writefds,
+	    fd_set *exceptfds, struct timeval *timeout)
+{
+#ifdef HAVE_SELECT
+  // If we have a timeout, compute the absolute ending time.
+  struct timeval end, delay;
+  if (timeout)
+    {
+      _Jv_gettimeofday (&end);
+      end.tv_usec += timeout->tv_usec;
+      if (end.tv_usec >= 1000000)
+	{
+	  ++end.tv_sec;
+	  end.tv_usec -= 1000000;
+	}
+      end.tv_sec += timeout->tv_sec;
+      delay = *timeout;
+    }
+  else
+    {
+      // Placate compiler.
+      delay.tv_sec = delay.tv_usec = 0;
+    }
+
+  while (1)
+    {
+      int r = select (n, readfds, writefds, exceptfds,
+		      timeout ? &delay : NULL);
+      if (r != -1 || errno != EINTR)
+	return r;
+
+      struct timeval after;
+      if (timeout)
+	{
+	  _Jv_gettimeofday (&after);
+	  // Now compute new timeout argument.
+	  delay.tv_usec = end.tv_usec - after.tv_usec;
+	  delay.tv_sec = end.tv_sec - after.tv_sec;
+	  if (delay.tv_usec < 0)
+	    {
+	      --delay.tv_sec;
+	      delay.tv_usec += 1000000;
+	    }
+	  if (delay.tv_sec < 0)
+	    {
+	      // We assume that the user wants a valid select() call
+	      // more than precise timing.  So if we get a series of
+	      // EINTR we just keep trying with delay 0 until we get a
+	      // valid result.
+	      delay.tv_sec = 0;
+	    }
+	}
+    }
+#else /* HAVE_SELECT */
+  return 0;
+#endif
+}
-- 
GitLab