OSDN Git Service

2002-03-15 Eric Blake <ebb9@email.byu.edu>
[pf3gnuchains/gcc-fork.git] / libjava / posix.cc
1 // posix.cc -- Helper functions for POSIX-flavored OSs.
2
3 /* Copyright (C) 2000, 2001, 2002  Free Software Foundation
4
5    This file is part of libgcj.
6
7 This software is copyrighted work licensed under the terms of the
8 Libgcj License.  Please consult the file "LIBGCJ_LICENSE" for
9 details.  */
10
11 #include <config.h>
12
13 #include "posix.h"
14
15 #include <errno.h>
16 #include <signal.h>
17
18 #include <jvm.h>
19 #include <java/lang/Thread.h>
20 #include <java/io/InterruptedIOException.h>
21
22 #if defined (ECOS)
23 extern "C" unsigned long long _clock (void);
24 #endif
25
26 // gettimeofday implementation.
27 jlong
28 _Jv_platform_gettimeofday ()
29 {
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
47 }
48
49 // Platform-specific VM initialization.
50 void
51 _Jv_platform_initialize (void)
52 {
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
63 }
64
65 static inline void
66 internal_gettimeofday (struct timeval *result)
67 {
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 */
75 }
76
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)
81 {
82 #ifdef HAVE_SELECT
83   // If we have a timeout, compute the absolute ending time.
84   struct timeval end, delay;
85   if (timeout)
86     {
87       internal_gettimeofday (&end);
88       end.tv_usec += timeout->tv_usec;
89       if (end.tv_usec >= 1000000)
90         {
91           ++end.tv_sec;
92           end.tv_usec -= 1000000;
93         }
94       end.tv_sec += timeout->tv_sec;
95       delay = *timeout;
96     }
97   else
98     {
99       // Placate compiler.
100       delay.tv_sec = delay.tv_usec = 0;
101     }
102
103   while (1)
104     {
105       int r = select (n, readfds, writefds, exceptfds,
106                       timeout ? &delay : NULL);
107       if (r != -1 || errno != EINTR)
108         return r;
109
110       // Here we know we got EINTR.
111       if (java::lang::Thread::interrupted ())
112         throw new java::io::InterruptedIOException (JvNewStringLatin1 ("select interrupted"));
113
114       struct timeval after;
115       if (timeout)
116         {
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)
122             {
123               --delay.tv_sec;
124               delay.tv_usec += 1000000;
125             }
126           if (delay.tv_sec < 0)
127             {
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;
133             }
134         }
135     }
136 #else /* HAVE_SELECT */
137   return 0;
138 #endif
139 }