2002-03-24 Eric Blake <ebb9@email.byu.edu>
[official-gcc.git] / libjava / posix.cc
blob6b0ea8cad3c7ed9c729ab87e54cacb7a9fff97b5
1 // posix.cc -- Helper functions for POSIX-flavored OSs.
3 /* Copyright (C) 2000, 2001, 2002 Free Software Foundation
5 This file is part of libgcj.
7 This software is copyrighted work licensed under the terms of the
8 Libgcj License. Please consult the file "LIBGCJ_LICENSE" for
9 details. */
11 #include <config.h>
13 #include "posix.h"
15 #include <errno.h>
16 #include <signal.h>
18 #include <jvm.h>
19 #include <java/lang/Thread.h>
20 #include <java/io/InterruptedIOException.h>
22 #if defined (ECOS)
23 extern "C" unsigned long long _clock (void);
24 #endif
26 // gettimeofday implementation.
27 jlong
28 _Jv_platform_gettimeofday ()
30 #if defined (HAVE_GETTIMEOFDAY)
31 timeval tv;
32 gettimeofday (&tv, NULL);
33 return (tv.tv_sec * 1000LL) + (tv.tv_usec / 1000LL);
34 #elif defined (HAVE_TIME)
35 return time (NULL) * 1000LL;
36 #elif defined (HAVE_FTIME)
37 struct timeb t;
38 ftime (&t);
39 return (t.time * 1000LL) + t.millitm;
40 #elif defined (ECOS)
41 // FIXME.
42 return _clock();
43 #else
44 // In the absence of any function, time remains forever fixed.
45 return 23000;
46 #endif
49 // Platform-specific VM initialization.
50 void
51 _Jv_platform_initialize (void)
53 #if defined (HAVE_SIGACTION)
54 // We only want this on POSIX systems.
55 struct sigaction act;
56 act.sa_handler = SIG_IGN;
57 sigemptyset (&act.sa_mask);
58 act.sa_flags = 0;
59 sigaction (SIGPIPE, &act, NULL);
60 #else
61 signal (SIGPIPE, SIG_IGN);
62 #endif
65 static inline void
66 internal_gettimeofday (struct timeval *result)
68 #if defined (HAVE_GETTIMEOFDAY)
69 gettimeofday (result, NULL);
70 #else
71 jlong val = _Jv_platform_gettimeofday ();
72 result->tv_sec = val / 1000;
73 result->tv_usec = (val % 1000) * 1000;
74 #endif /* HAVE_GETTIMEOFDAY */
77 // A wrapper for select() which ignores EINTR.
78 int
79 _Jv_select (int n, fd_set *readfds, fd_set *writefds,
80 fd_set *exceptfds, struct timeval *timeout)
82 #ifdef HAVE_SELECT
83 // If we have a timeout, compute the absolute ending time.
84 struct timeval end, delay;
85 if (timeout)
87 internal_gettimeofday (&end);
88 end.tv_usec += timeout->tv_usec;
89 if (end.tv_usec >= 1000000)
91 ++end.tv_sec;
92 end.tv_usec -= 1000000;
94 end.tv_sec += timeout->tv_sec;
95 delay = *timeout;
97 else
99 // Placate compiler.
100 delay.tv_sec = delay.tv_usec = 0;
103 while (1)
105 int r = select (n, readfds, writefds, exceptfds,
106 timeout ? &delay : NULL);
107 if (r != -1 || errno != EINTR)
108 return r;
110 // Here we know we got EINTR.
111 if (java::lang::Thread::interrupted ())
112 throw new java::io::InterruptedIOException (JvNewStringLatin1 ("select interrupted"));
114 struct timeval after;
115 if (timeout)
117 internal_gettimeofday (&after);
118 // Now compute new timeout argument.
119 delay.tv_usec = end.tv_usec - after.tv_usec;
120 delay.tv_sec = end.tv_sec - after.tv_sec;
121 if (delay.tv_usec < 0)
123 --delay.tv_sec;
124 delay.tv_usec += 1000000;
126 if (delay.tv_sec < 0)
128 // We assume that the user wants a valid select() call
129 // more than precise timing. So if we get a series of
130 // EINTR we just keep trying with delay 0 until we get a
131 // valid result.
132 delay.tv_sec = 0;
136 #else /* HAVE_SELECT */
137 return 0;
138 #endif