OSDN Git Service

PR libgcj/27171:
[pf3gnuchains/gcc-fork.git] / libjava / posix.cc
1 // posix.cc -- Helper functions for POSIX-flavored OSs.
2
3 /* Copyright (C) 2000, 2001, 2002, 2006  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 <stdlib.h>
16 #include <errno.h>
17 #include <signal.h>
18 #include <stdio.h>
19
20 #include <jvm.h>
21 #include <java/lang/Thread.h>
22 #include <java/io/InterruptedIOException.h>
23 #include <java/util/Properties.h>
24
25 #if defined (ECOS)
26 extern "C" unsigned long long _clock (void);
27 #endif
28
29 #if defined(HAVE_PROC_SELF_EXE)
30 static char exec_name[20];
31   // initialized in _Jv_platform_initialize()
32 #endif
33
34 const char *_Jv_ThisExecutable (void)
35 {
36 #if defined(DISABLE_MAIN_ARGS)
37   return "[Embedded App]";
38 #elif defined(HAVE_PROC_SELF_EXE)
39   return exec_name;
40     // initialized in _Jv_platform_initialize()
41 #else
42   return _Jv_GetSafeArg (0);
43 #endif
44 }
45
46 // gettimeofday implementation.
47 jlong
48 _Jv_platform_gettimeofday ()
49 {
50 #if defined (HAVE_GETTIMEOFDAY)
51   timeval tv;
52   gettimeofday (&tv, NULL);
53   return (tv.tv_sec * 1000LL) + (tv.tv_usec / 1000LL);
54 #elif defined (HAVE_TIME)
55   return time (NULL) * 1000LL;
56 #elif defined (HAVE_FTIME)
57   struct timeb t;
58   ftime (&t);
59   return (t.time * 1000LL) + t.millitm;
60 #elif defined (ECOS)
61   // FIXME.
62   return _clock();
63 #else
64   // In the absence of any function, time remains forever fixed.
65   return 23000;
66 #endif
67 }
68
69 jlong
70 _Jv_platform_nanotime ()
71 {
72 #ifdef HAVE_CLOCK_GETTIME
73   struct timespec now;
74   clockid_t id;
75 #ifdef CLOCK_MONOTONIC
76   id = CLOCK_MONOTONIC;
77 #elif defined (CLOCK_HIGHRES)
78   id = CLOCK_HIGHRES;
79 #else
80   id = CLOCK_REALTIME;
81 #endif
82   if (clock_gettime (id, &now) == 0)
83     {
84       jlong result = (jlong) now.tv_sec;
85       result = result * 1000 * 1000 + now.tv_nsec;
86       return result;
87     }
88   // clock_gettime failed, but we can fall through.
89 #endif // HAVE_CLOCK_GETTIME
90   return _Jv_platform_gettimeofday () * 1000LL;
91 }
92
93 // Platform-specific VM initialization.
94 void
95 _Jv_platform_initialize (void)
96 {
97 #if defined (HAVE_SIGACTION)
98   // We only want this on POSIX systems.
99   struct sigaction act;
100   act.sa_handler = SIG_IGN;
101   sigemptyset (&act.sa_mask);
102   act.sa_flags = 0;
103   sigaction (SIGPIPE, &act, NULL);
104 #else
105   signal (SIGPIPE, SIG_IGN);
106 #endif
107
108 #if defined (HAVE_PROC_SELF_EXE)
109   // Compute our executable name
110   sprintf (exec_name, "/proc/%d/exe", getpid ());
111 #endif
112 }
113
114 // Set platform-specific System properties.
115 void
116 _Jv_platform_initProperties (java::util::Properties* newprops)
117 {
118   // A convenience define.
119 #define SET(Prop,Val) \
120   newprops->put(JvNewStringLatin1 (Prop), JvNewStringLatin1 (Val))
121
122   SET ("file.separator", "/");
123   SET ("path.separator", ":");
124   SET ("line.separator", "\n");
125   const char *tmpdir = ::getenv("TMPDIR");
126   if (! tmpdir)
127     tmpdir = "/tmp";
128   SET ("java.io.tmpdir", tmpdir);
129 }
130
131 static inline void
132 internal_gettimeofday (struct timeval *result)
133 {
134 #if defined (HAVE_GETTIMEOFDAY)
135   gettimeofday (result, NULL);
136 #else
137   jlong val = _Jv_platform_gettimeofday ();
138   result->tv_sec = val / 1000;
139   result->tv_usec = (val % 1000) * 1000;
140 #endif /* HAVE_GETTIMEOFDAY */
141 }
142
143 // A wrapper for select() which ignores EINTR.
144 int
145 _Jv_select (int n, fd_set *readfds, fd_set  *writefds,
146             fd_set *exceptfds, struct timeval *timeout)
147 {
148 #ifdef HAVE_SELECT
149   // If we have a timeout, compute the absolute ending time.
150   struct timeval end, delay;
151   if (timeout)
152     {
153       internal_gettimeofday (&end);
154       end.tv_usec += timeout->tv_usec;
155       if (end.tv_usec >= 1000000)
156         {
157           ++end.tv_sec;
158           end.tv_usec -= 1000000;
159         }
160       end.tv_sec += timeout->tv_sec;
161       delay = *timeout;
162     }
163   else
164     {
165       // Placate compiler.
166       delay.tv_sec = delay.tv_usec = 0;
167     }
168
169   while (1)
170     {
171       int r = select (n, readfds, writefds, exceptfds,
172                       timeout ? &delay : NULL);
173       if (r != -1 || errno != EINTR)
174         return r;
175
176       // Here we know we got EINTR.
177       if (java::lang::Thread::interrupted ())
178         throw new java::io::InterruptedIOException (JvNewStringLatin1 ("select interrupted"));
179
180       struct timeval after;
181       if (timeout)
182         {
183           internal_gettimeofday (&after);
184           // Now compute new timeout argument.
185           delay.tv_usec = end.tv_usec - after.tv_usec;
186           delay.tv_sec = end.tv_sec - after.tv_sec;
187           if (delay.tv_usec < 0)
188             {
189               --delay.tv_sec;
190               delay.tv_usec += 1000000;
191             }
192           if (delay.tv_sec < 0)
193             {
194               // We assume that the user wants a valid select() call
195               // more than precise timing.  So if we get a series of
196               // EINTR we just keep trying with delay 0 until we get a
197               // valid result.
198               delay.tv_sec = 0;
199             }
200         }
201     }
202 #else /* HAVE_SELECT */
203   return 0;
204 #endif
205 }