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
21 #include <java/lang/Thread.h>
22 #include <java/io/InterruptedIOException.h>
23 #include <java/util/Properties.h>
26 extern "C" unsigned long long _clock (void);
29 #if defined(HAVE_PROC_SELF_EXE)
30 static char exec_name[20];
31 // initialized in _Jv_platform_initialize()
34 const char *_Jv_ThisExecutable (void)
36 #if defined(DISABLE_MAIN_ARGS)
37 return "[Embedded App]";
38 #elif defined(HAVE_PROC_SELF_EXE)
40 // initialized in _Jv_platform_initialize()
42 return _Jv_GetSafeArg (0);
46 // gettimeofday implementation.
48 _Jv_platform_gettimeofday ()
50 #if defined (HAVE_GETTIMEOFDAY)
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)
59 return (t.time * 1000LL) + t.millitm;
64 // In the absence of any function, time remains forever fixed.
70 _Jv_platform_nanotime ()
72 #ifdef HAVE_CLOCK_GETTIME
75 #ifdef CLOCK_MONOTONIC
77 #elif defined (CLOCK_HIGHRES)
82 if (clock_gettime (id, &now) == 0)
84 jlong result = (jlong) now.tv_sec;
85 result = result * 1000 * 1000 + now.tv_nsec;
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.
95 _Jv_platform_initialize (void)
97 #if defined (HAVE_SIGACTION)
98 // We only want this on POSIX systems.
100 act.sa_handler = SIG_IGN;
101 sigemptyset (&act.sa_mask);
103 sigaction (SIGPIPE, &act, NULL);
105 signal (SIGPIPE, SIG_IGN);
108 #if defined (HAVE_PROC_SELF_EXE)
109 // Compute our executable name
110 sprintf (exec_name, "/proc/%d/exe", getpid ());
114 // Set platform-specific System properties.
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");
128 SET ("java.io.tmpdir", tmpdir);
132 internal_gettimeofday (struct timeval *result)
134 #if defined (HAVE_GETTIMEOFDAY)
135 gettimeofday (result, NULL);
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)
149 // If we have a timeout, compute the absolute ending time.
150 struct timeval end, delay;
153 internal_gettimeofday (&end);
154 end.tv_usec += timeout->tv_usec;
155 if (end.tv_usec >= 1000000)
158 end.tv_usec -= 1000000;
160 end.tv_sec += timeout->tv_sec;
166 delay.tv_sec = delay.tv_usec = 0;
171 int r = select (n, readfds, writefds, exceptfds,
172 timeout ? &delay : NULL);
173 if (r != -1 || errno != EINTR)
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;
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)
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
202 #else /* HAVE_SELECT */