OSDN Git Service

2002-03-07 Adam Megacz <adam@xwt.org>
[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 * 1000 + tv.tv_usec / 1000;
34 #elif defined (HAVE_TIME)
35   return time (NULL) * 1000;
36 #elif defined (HAVE_FTIME)
37   struct timeb t;
38   ftime (&t);
39   return t.time * 1000 + 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 // A wrapper for select() which ignores EINTR.
66 int
67 _Jv_select (int n, fd_set *readfds, fd_set  *writefds,
68             fd_set *exceptfds, struct timeval *timeout)
69 {
70 #ifdef HAVE_SELECT
71   // If we have a timeout, compute the absolute ending time.
72   struct timeval end, delay;
73   if (timeout)
74     {
75       _Jv_platform_gettimeofday (&end);
76       end.tv_usec += timeout->tv_usec;
77       if (end.tv_usec >= 1000000)
78         {
79           ++end.tv_sec;
80           end.tv_usec -= 1000000;
81         }
82       end.tv_sec += timeout->tv_sec;
83       delay = *timeout;
84     }
85   else
86     {
87       // Placate compiler.
88       delay.tv_sec = delay.tv_usec = 0;
89     }
90
91   while (1)
92     {
93       int r = select (n, readfds, writefds, exceptfds,
94                       timeout ? &delay : NULL);
95       if (r != -1 || errno != EINTR)
96         return r;
97
98       // Here we know we got EINTR.
99       if (java::lang::Thread::interrupted ())
100         throw new java::io::InterruptedIOException (JvNewStringLatin1 ("select interrupted"));
101
102       struct timeval after;
103       if (timeout)
104         {
105           _Jv_platform_gettimeofday (&after);
106           // Now compute new timeout argument.
107           delay.tv_usec = end.tv_usec - after.tv_usec;
108           delay.tv_sec = end.tv_sec - after.tv_sec;
109           if (delay.tv_usec < 0)
110             {
111               --delay.tv_sec;
112               delay.tv_usec += 1000000;
113             }
114           if (delay.tv_sec < 0)
115             {
116               // We assume that the user wants a valid select() call
117               // more than precise timing.  So if we get a series of
118               // EINTR we just keep trying with delay 0 until we get a
119               // valid result.
120               delay.tv_sec = 0;
121             }
122         }
123     }
124 #else /* HAVE_SELECT */
125   return 0;
126 #endif
127 }