2006-03-22 Thomas Koenig <Thomas.Koenig@onlien.de>
[official-gcc.git] / libjava / posix.cc
blob15fc9c5c3f85fdc58c4c0a5797ac8d5a6532d2f2
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 if (clock_gettime (CLOCK_REALTIME, &now) == 0)
76 jlong result = (jlong) now.tv_sec;
77 result = result * 1000 * 1000 + now.tv_nsec;
78 return result;
80 // clock_gettime failed, but we can fall through.
81 #endif // HAVE_CLOCK_GETTIME
82 return _Jv_platform_gettimeofday () * 1000LL;
85 // Platform-specific VM initialization.
86 void
87 _Jv_platform_initialize (void)
89 #if defined (HAVE_SIGACTION)
90 // We only want this on POSIX systems.
91 struct sigaction act;
92 act.sa_handler = SIG_IGN;
93 sigemptyset (&act.sa_mask);
94 act.sa_flags = 0;
95 sigaction (SIGPIPE, &act, NULL);
96 #else
97 signal (SIGPIPE, SIG_IGN);
98 #endif
100 #if defined (HAVE_PROC_SELF_EXE)
101 // Compute our executable name
102 sprintf (exec_name, "/proc/%d/exe", getpid ());
103 #endif
106 // Set platform-specific System properties.
107 void
108 _Jv_platform_initProperties (java::util::Properties* newprops)
110 // A convenience define.
111 #define SET(Prop,Val) \
112 newprops->put(JvNewStringLatin1 (Prop), JvNewStringLatin1 (Val))
114 SET ("file.separator", "/");
115 SET ("path.separator", ":");
116 SET ("line.separator", "\n");
117 const char *tmpdir = ::getenv("TMPDIR");
118 if (! tmpdir)
119 tmpdir = "/tmp";
120 SET ("java.io.tmpdir", tmpdir);
123 static inline void
124 internal_gettimeofday (struct timeval *result)
126 #if defined (HAVE_GETTIMEOFDAY)
127 gettimeofday (result, NULL);
128 #else
129 jlong val = _Jv_platform_gettimeofday ();
130 result->tv_sec = val / 1000;
131 result->tv_usec = (val % 1000) * 1000;
132 #endif /* HAVE_GETTIMEOFDAY */
135 // A wrapper for select() which ignores EINTR.
137 _Jv_select (int n, fd_set *readfds, fd_set *writefds,
138 fd_set *exceptfds, struct timeval *timeout)
140 #ifdef HAVE_SELECT
141 // If we have a timeout, compute the absolute ending time.
142 struct timeval end, delay;
143 if (timeout)
145 internal_gettimeofday (&end);
146 end.tv_usec += timeout->tv_usec;
147 if (end.tv_usec >= 1000000)
149 ++end.tv_sec;
150 end.tv_usec -= 1000000;
152 end.tv_sec += timeout->tv_sec;
153 delay = *timeout;
155 else
157 // Placate compiler.
158 delay.tv_sec = delay.tv_usec = 0;
161 while (1)
163 int r = select (n, readfds, writefds, exceptfds,
164 timeout ? &delay : NULL);
165 if (r != -1 || errno != EINTR)
166 return r;
168 // Here we know we got EINTR.
169 if (java::lang::Thread::interrupted ())
170 throw new java::io::InterruptedIOException (JvNewStringLatin1 ("select interrupted"));
172 struct timeval after;
173 if (timeout)
175 internal_gettimeofday (&after);
176 // Now compute new timeout argument.
177 delay.tv_usec = end.tv_usec - after.tv_usec;
178 delay.tv_sec = end.tv_sec - after.tv_sec;
179 if (delay.tv_usec < 0)
181 --delay.tv_sec;
182 delay.tv_usec += 1000000;
184 if (delay.tv_sec < 0)
186 // We assume that the user wants a valid select() call
187 // more than precise timing. So if we get a series of
188 // EINTR we just keep trying with delay 0 until we get a
189 // valid result.
190 delay.tv_sec = 0;
194 #else /* HAVE_SELECT */
195 return 0;
196 #endif