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
20 #include <java/lang/Thread.h>
21 #include <java/io/InterruptedIOException.h>
22 #include <java/util/Properties.h>
25 extern "C" unsigned long long _clock (void);
28 // gettimeofday implementation.
30 _Jv_platform_gettimeofday ()
32 #if defined (HAVE_GETTIMEOFDAY)
34 gettimeofday (&tv, NULL);
35 return (tv.tv_sec * 1000LL) + (tv.tv_usec / 1000LL);
36 #elif defined (HAVE_TIME)
37 return time (NULL) * 1000LL;
38 #elif defined (HAVE_FTIME)
41 return (t.time * 1000LL) + t.millitm;
46 // In the absence of any function, time remains forever fixed.
51 // Platform-specific VM initialization.
53 _Jv_platform_initialize (void)
55 #if defined (HAVE_SIGACTION)
56 // We only want this on POSIX systems.
58 act.sa_handler = SIG_IGN;
59 sigemptyset (&act.sa_mask);
61 sigaction (SIGPIPE, &act, NULL);
63 signal (SIGPIPE, SIG_IGN);
67 // Set platform-specific System properties.
69 _Jv_platform_initProperties (java::util::Properties* newprops)
71 // A convenience define.
72 #define SET(Prop,Val) \
73 newprops->put(JvNewStringLatin1 (Prop), JvNewStringLatin1 (Val))
75 SET ("file.separator", "/");
76 SET ("path.separator", ":");
77 SET ("line.separator", "\n");
78 char *tmpdir = ::getenv("TMPDIR");
81 SET ("java.io.tmpdir", tmpdir);
85 internal_gettimeofday (struct timeval *result)
87 #if defined (HAVE_GETTIMEOFDAY)
88 gettimeofday (result, NULL);
90 jlong val = _Jv_platform_gettimeofday ();
91 result->tv_sec = val / 1000;
92 result->tv_usec = (val % 1000) * 1000;
93 #endif /* HAVE_GETTIMEOFDAY */
96 // A wrapper for select() which ignores EINTR.
98 _Jv_select (int n, fd_set *readfds, fd_set *writefds,
99 fd_set *exceptfds, struct timeval *timeout)
102 // If we have a timeout, compute the absolute ending time.
103 struct timeval end, delay;
106 internal_gettimeofday (&end);
107 end.tv_usec += timeout->tv_usec;
108 if (end.tv_usec >= 1000000)
111 end.tv_usec -= 1000000;
113 end.tv_sec += timeout->tv_sec;
119 delay.tv_sec = delay.tv_usec = 0;
124 int r = select (n, readfds, writefds, exceptfds,
125 timeout ? &delay : NULL);
126 if (r != -1 || errno != EINTR)
129 // Here we know we got EINTR.
130 if (java::lang::Thread::interrupted ())
131 throw new java::io::InterruptedIOException (JvNewStringLatin1 ("select interrupted"));
133 struct timeval after;
136 internal_gettimeofday (&after);
137 // Now compute new timeout argument.
138 delay.tv_usec = end.tv_usec - after.tv_usec;
139 delay.tv_sec = end.tv_sec - after.tv_sec;
140 if (delay.tv_usec < 0)
143 delay.tv_usec += 1000000;
145 if (delay.tv_sec < 0)
147 // We assume that the user wants a valid select() call
148 // more than precise timing. So if we get a series of
149 // EINTR we just keep trying with delay 0 until we get a
155 #else /* HAVE_SELECT */