2003-04-07 Aldy Hernandez <aldyh@redhat.com>
[official-gcc.git] / libjava / posix.cc
blob2f808334d7c8c237d98103d2e493ac2b854641b8
1 // posix.cc -- Helper functions for POSIX-flavored OSs.
3 /* Copyright (C) 2000, 2001, 2002 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>
19 #include <jvm.h>
20 #include <java/lang/Thread.h>
21 #include <java/io/InterruptedIOException.h>
22 #include <java/util/Properties.h>
24 #if defined (ECOS)
25 extern "C" unsigned long long _clock (void);
26 #endif
28 // platform-specific executable name
29 extern const char **_Jv_argv;
31 #if defined(HAVE_PROC_SELF_EXE)
32 static char exec_name[20];
33 // initialized in _Jv_platform_initialize()
34 #endif
36 const char *_Jv_ThisExecutable (void)
38 #if defined(DISABLE_MAIN_ARGS)
39 return "[Embedded App]";
40 #elif defined(HAVE_PROC_SELF_EXE)
41 return exec_name;
42 // initialized in _Jv_platform_initialize()
43 #else
44 return _Jv_argv[0];
45 #endif
48 // gettimeofday implementation.
49 jlong
50 _Jv_platform_gettimeofday ()
52 #if defined (HAVE_GETTIMEOFDAY)
53 timeval tv;
54 gettimeofday (&tv, NULL);
55 return (tv.tv_sec * 1000LL) + (tv.tv_usec / 1000LL);
56 #elif defined (HAVE_TIME)
57 return time (NULL) * 1000LL;
58 #elif defined (HAVE_FTIME)
59 struct timeb t;
60 ftime (&t);
61 return (t.time * 1000LL) + t.millitm;
62 #elif defined (ECOS)
63 // FIXME.
64 return _clock();
65 #else
66 // In the absence of any function, time remains forever fixed.
67 return 23000;
68 #endif
71 // Platform-specific VM initialization.
72 void
73 _Jv_platform_initialize (void)
75 #if defined (HAVE_SIGACTION)
76 // We only want this on POSIX systems.
77 struct sigaction act;
78 act.sa_handler = SIG_IGN;
79 sigemptyset (&act.sa_mask);
80 act.sa_flags = 0;
81 sigaction (SIGPIPE, &act, NULL);
82 #else
83 signal (SIGPIPE, SIG_IGN);
84 #endif
86 #if defined (HAVE_PROC_SELF_EXE)
87 // Compute our executable name
88 sprintf (exec_name, "/proc/%d/exe", getpid ());
89 #endif
92 // Set platform-specific System properties.
93 void
94 _Jv_platform_initProperties (java::util::Properties* newprops)
96 // A convenience define.
97 #define SET(Prop,Val) \
98 newprops->put(JvNewStringLatin1 (Prop), JvNewStringLatin1 (Val))
100 SET ("file.separator", "/");
101 SET ("path.separator", ":");
102 SET ("line.separator", "\n");
103 char *tmpdir = ::getenv("TMPDIR");
104 if (! tmpdir)
105 tmpdir = "/tmp";
106 SET ("java.io.tmpdir", tmpdir);
109 static inline void
110 internal_gettimeofday (struct timeval *result)
112 #if defined (HAVE_GETTIMEOFDAY)
113 gettimeofday (result, NULL);
114 #else
115 jlong val = _Jv_platform_gettimeofday ();
116 result->tv_sec = val / 1000;
117 result->tv_usec = (val % 1000) * 1000;
118 #endif /* HAVE_GETTIMEOFDAY */
121 // A wrapper for select() which ignores EINTR.
123 _Jv_select (int n, fd_set *readfds, fd_set *writefds,
124 fd_set *exceptfds, struct timeval *timeout)
126 #ifdef HAVE_SELECT
127 // If we have a timeout, compute the absolute ending time.
128 struct timeval end, delay;
129 if (timeout)
131 internal_gettimeofday (&end);
132 end.tv_usec += timeout->tv_usec;
133 if (end.tv_usec >= 1000000)
135 ++end.tv_sec;
136 end.tv_usec -= 1000000;
138 end.tv_sec += timeout->tv_sec;
139 delay = *timeout;
141 else
143 // Placate compiler.
144 delay.tv_sec = delay.tv_usec = 0;
147 while (1)
149 int r = select (n, readfds, writefds, exceptfds,
150 timeout ? &delay : NULL);
151 if (r != -1 || errno != EINTR)
152 return r;
154 // Here we know we got EINTR.
155 if (java::lang::Thread::interrupted ())
156 throw new java::io::InterruptedIOException (JvNewStringLatin1 ("select interrupted"));
158 struct timeval after;
159 if (timeout)
161 internal_gettimeofday (&after);
162 // Now compute new timeout argument.
163 delay.tv_usec = end.tv_usec - after.tv_usec;
164 delay.tv_sec = end.tv_sec - after.tv_sec;
165 if (delay.tv_usec < 0)
167 --delay.tv_sec;
168 delay.tv_usec += 1000000;
170 if (delay.tv_sec < 0)
172 // We assume that the user wants a valid select() call
173 // more than precise timing. So if we get a series of
174 // EINTR we just keep trying with delay 0 until we get a
175 // valid result.
176 delay.tv_sec = 0;
180 #else /* HAVE_SELECT */
181 return 0;
182 #endif