PR c++/18925
[official-gcc.git] / libjava / posix.cc
blob73c0f5f4329c938956df4285e3f3f35ac3a50371
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>
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 // Platform-specific VM initialization.
70 void
71 _Jv_platform_initialize (void)
73 #if defined (HAVE_SIGACTION)
74 // We only want this on POSIX systems.
75 struct sigaction act;
76 act.sa_handler = SIG_IGN;
77 sigemptyset (&act.sa_mask);
78 act.sa_flags = 0;
79 sigaction (SIGPIPE, &act, NULL);
80 #else
81 signal (SIGPIPE, SIG_IGN);
82 #endif
84 #if defined (HAVE_PROC_SELF_EXE)
85 // Compute our executable name
86 sprintf (exec_name, "/proc/%d/exe", getpid ());
87 #endif
90 // Set platform-specific System properties.
91 void
92 _Jv_platform_initProperties (java::util::Properties* newprops)
94 // A convenience define.
95 #define SET(Prop,Val) \
96 newprops->put(JvNewStringLatin1 (Prop), JvNewStringLatin1 (Val))
98 SET ("file.separator", "/");
99 SET ("path.separator", ":");
100 SET ("line.separator", "\n");
101 char *tmpdir = ::getenv("TMPDIR");
102 if (! tmpdir)
103 tmpdir = "/tmp";
104 SET ("java.io.tmpdir", tmpdir);
107 static inline void
108 internal_gettimeofday (struct timeval *result)
110 #if defined (HAVE_GETTIMEOFDAY)
111 gettimeofday (result, NULL);
112 #else
113 jlong val = _Jv_platform_gettimeofday ();
114 result->tv_sec = val / 1000;
115 result->tv_usec = (val % 1000) * 1000;
116 #endif /* HAVE_GETTIMEOFDAY */
119 // A wrapper for select() which ignores EINTR.
121 _Jv_select (int n, fd_set *readfds, fd_set *writefds,
122 fd_set *exceptfds, struct timeval *timeout)
124 #ifdef HAVE_SELECT
125 // If we have a timeout, compute the absolute ending time.
126 struct timeval end, delay;
127 if (timeout)
129 internal_gettimeofday (&end);
130 end.tv_usec += timeout->tv_usec;
131 if (end.tv_usec >= 1000000)
133 ++end.tv_sec;
134 end.tv_usec -= 1000000;
136 end.tv_sec += timeout->tv_sec;
137 delay = *timeout;
139 else
141 // Placate compiler.
142 delay.tv_sec = delay.tv_usec = 0;
145 while (1)
147 int r = select (n, readfds, writefds, exceptfds,
148 timeout ? &delay : NULL);
149 if (r != -1 || errno != EINTR)
150 return r;
152 // Here we know we got EINTR.
153 if (java::lang::Thread::interrupted ())
154 throw new java::io::InterruptedIOException (JvNewStringLatin1 ("select interrupted"));
156 struct timeval after;
157 if (timeout)
159 internal_gettimeofday (&after);
160 // Now compute new timeout argument.
161 delay.tv_usec = end.tv_usec - after.tv_usec;
162 delay.tv_sec = end.tv_sec - after.tv_sec;
163 if (delay.tv_usec < 0)
165 --delay.tv_sec;
166 delay.tv_usec += 1000000;
168 if (delay.tv_sec < 0)
170 // We assume that the user wants a valid select() call
171 // more than precise timing. So if we get a series of
172 // EINTR we just keep trying with delay 0 until we get a
173 // valid result.
174 delay.tv_sec = 0;
178 #else /* HAVE_SELECT */
179 return 0;
180 #endif