* doc/rtl.texi: Fix a typo.
[official-gcc.git] / libjava / posix.cc
blob608fd5dad901e0793b464bac7a23d7cf9fa2da2d
1 // posix.cc -- Helper functions for POSIX-flavored OSs.
3 /* Copyright (C) 2000, 2001, 2002, 2006 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 <stdlib.h>
16 #include <errno.h>
17 #include <signal.h>
18 #include <stdio.h>
20 #include <jvm.h>
21 #include <java/lang/Thread.h>
22 #include <java/io/InterruptedIOException.h>
23 #include <java/util/Properties.h>
25 #if defined (ECOS)
26 extern "C" unsigned long long _clock (void);
27 #endif
29 #if defined(HAVE_PROC_SELF_EXE)
30 static char exec_name[20];
31 // initialized in _Jv_platform_initialize()
32 #endif
34 const char *_Jv_ThisExecutable (void)
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
46 // gettimeofday implementation.
47 jlong
48 _Jv_platform_gettimeofday ()
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
69 jlong
70 _Jv_platform_nanotime ()
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)
84 jlong result = (jlong) now.tv_sec;
85 result = result * 1000 * 1000 + now.tv_nsec;
86 return result;
88 // clock_gettime failed, but we can fall through.
89 #endif // HAVE_CLOCK_GETTIME
90 return _Jv_platform_gettimeofday () * 1000LL;
93 // Platform-specific VM initialization.
94 void
95 _Jv_platform_initialize (void)
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
108 #if defined (HAVE_PROC_SELF_EXE)
109 // Compute our executable name
110 sprintf (exec_name, "/proc/%d/exe", getpid ());
111 #endif
114 // Set platform-specific System properties.
115 void
116 _Jv_platform_initProperties (java::util::Properties* newprops)
118 // A convenience define.
119 #define SET(Prop,Val) \
120 newprops->put(JvNewStringLatin1 (Prop), JvNewStringLatin1 (Val))
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);
131 static inline void
132 internal_gettimeofday (struct timeval *result)
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 */
143 // A wrapper for select() which ignores EINTR.
145 _Jv_select (int n, fd_set *readfds, fd_set *writefds,
146 fd_set *exceptfds, struct timeval *timeout)
148 #ifdef HAVE_SELECT
149 // If we have a timeout, compute the absolute ending time.
150 struct timeval end, delay;
151 if (timeout)
153 internal_gettimeofday (&end);
154 end.tv_usec += timeout->tv_usec;
155 if (end.tv_usec >= 1000000)
157 ++end.tv_sec;
158 end.tv_usec -= 1000000;
160 end.tv_sec += timeout->tv_sec;
161 delay = *timeout;
163 else
165 // Placate compiler.
166 delay.tv_sec = delay.tv_usec = 0;
169 while (1)
171 int r = select (n, readfds, writefds, exceptfds,
172 timeout ? &delay : NULL);
173 if (r != -1 || errno != EINTR)
174 return r;
176 // Here we know we got EINTR.
177 if (java::lang::Thread::interrupted ())
178 throw new java::io::InterruptedIOException (JvNewStringLatin1 ("select interrupted"));
180 struct timeval after;
181 if (timeout)
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)
189 --delay.tv_sec;
190 delay.tv_usec += 1000000;
192 if (delay.tv_sec < 0)
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;
202 #else /* HAVE_SELECT */
203 return 0;
204 #endif