1 | // posix.cc -- Helper functions for POSIX-flavored OSs.
|
---|
2 |
|
---|
3 | /* Copyright (C) 2000, 2001, 2002 Free Software Foundation
|
---|
4 |
|
---|
5 | This file is part of libgcj.
|
---|
6 |
|
---|
7 | This software is copyrighted work licensed under the terms of the
|
---|
8 | Libgcj License. Please consult the file "LIBGCJ_LICENSE" for
|
---|
9 | details. */
|
---|
10 |
|
---|
11 | #include <config.h>
|
---|
12 |
|
---|
13 | #include "posix.h"
|
---|
14 |
|
---|
15 | #include <stdlib.h>
|
---|
16 | #include <errno.h>
|
---|
17 | #include <signal.h>
|
---|
18 |
|
---|
19 | #include <jvm.h>
|
---|
20 | #include <java/lang/Thread.h>
|
---|
21 | #include <java/io/InterruptedIOException.h>
|
---|
22 | #include <java/util/Properties.h>
|
---|
23 |
|
---|
24 | #if defined (ECOS)
|
---|
25 | extern "C" unsigned long long _clock (void);
|
---|
26 | #endif
|
---|
27 |
|
---|
28 | // gettimeofday implementation.
|
---|
29 | jlong
|
---|
30 | _Jv_platform_gettimeofday ()
|
---|
31 | {
|
---|
32 | #if defined (HAVE_GETTIMEOFDAY)
|
---|
33 | timeval tv;
|
---|
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)
|
---|
39 | struct timeb t;
|
---|
40 | ftime (&t);
|
---|
41 | return (t.time * 1000LL) + t.millitm;
|
---|
42 | #elif defined (ECOS)
|
---|
43 | // FIXME.
|
---|
44 | return _clock();
|
---|
45 | #else
|
---|
46 | // In the absence of any function, time remains forever fixed.
|
---|
47 | return 23000;
|
---|
48 | #endif
|
---|
49 | }
|
---|
50 |
|
---|
51 | // Platform-specific VM initialization.
|
---|
52 | void
|
---|
53 | _Jv_platform_initialize (void)
|
---|
54 | {
|
---|
55 | #if defined (HAVE_SIGACTION)
|
---|
56 | // We only want this on POSIX systems.
|
---|
57 | struct sigaction act;
|
---|
58 | act.sa_handler = SIG_IGN;
|
---|
59 | sigemptyset (&act.sa_mask);
|
---|
60 | act.sa_flags = 0;
|
---|
61 | sigaction (SIGPIPE, &act, NULL);
|
---|
62 | #else
|
---|
63 | signal (SIGPIPE, SIG_IGN);
|
---|
64 | #endif
|
---|
65 | }
|
---|
66 |
|
---|
67 | // Set platform-specific System properties.
|
---|
68 | void
|
---|
69 | _Jv_platform_initProperties (java::util::Properties* newprops)
|
---|
70 | {
|
---|
71 | // A convenience define.
|
---|
72 | #define SET(Prop,Val) \
|
---|
73 | newprops->put(JvNewStringLatin1 (Prop), JvNewStringLatin1 (Val))
|
---|
74 |
|
---|
75 | SET ("file.separator", "/");
|
---|
76 | SET ("path.separator", ":");
|
---|
77 | SET ("line.separator", "\n");
|
---|
78 | char *tmpdir = ::getenv("TMPDIR");
|
---|
79 | if (! tmpdir)
|
---|
80 | tmpdir = "/tmp";
|
---|
81 | SET ("java.io.tmpdir", tmpdir);
|
---|
82 | }
|
---|
83 |
|
---|
84 | static inline void
|
---|
85 | internal_gettimeofday (struct timeval *result)
|
---|
86 | {
|
---|
87 | #if defined (HAVE_GETTIMEOFDAY)
|
---|
88 | gettimeofday (result, NULL);
|
---|
89 | #else
|
---|
90 | jlong val = _Jv_platform_gettimeofday ();
|
---|
91 | result->tv_sec = val / 1000;
|
---|
92 | result->tv_usec = (val % 1000) * 1000;
|
---|
93 | #endif /* HAVE_GETTIMEOFDAY */
|
---|
94 | }
|
---|
95 |
|
---|
96 | // A wrapper for select() which ignores EINTR.
|
---|
97 | int
|
---|
98 | _Jv_select (int n, fd_set *readfds, fd_set *writefds,
|
---|
99 | fd_set *exceptfds, struct timeval *timeout)
|
---|
100 | {
|
---|
101 | #ifdef HAVE_SELECT
|
---|
102 | // If we have a timeout, compute the absolute ending time.
|
---|
103 | struct timeval end, delay;
|
---|
104 | if (timeout)
|
---|
105 | {
|
---|
106 | internal_gettimeofday (&end);
|
---|
107 | end.tv_usec += timeout->tv_usec;
|
---|
108 | if (end.tv_usec >= 1000000)
|
---|
109 | {
|
---|
110 | ++end.tv_sec;
|
---|
111 | end.tv_usec -= 1000000;
|
---|
112 | }
|
---|
113 | end.tv_sec += timeout->tv_sec;
|
---|
114 | delay = *timeout;
|
---|
115 | }
|
---|
116 | else
|
---|
117 | {
|
---|
118 | // Placate compiler.
|
---|
119 | delay.tv_sec = delay.tv_usec = 0;
|
---|
120 | }
|
---|
121 |
|
---|
122 | while (1)
|
---|
123 | {
|
---|
124 | int r = select (n, readfds, writefds, exceptfds,
|
---|
125 | timeout ? &delay : NULL);
|
---|
126 | if (r != -1 || errno != EINTR)
|
---|
127 | return r;
|
---|
128 |
|
---|
129 | // Here we know we got EINTR.
|
---|
130 | if (java::lang::Thread::interrupted ())
|
---|
131 | throw new java::io::InterruptedIOException (JvNewStringLatin1 ("select interrupted"));
|
---|
132 |
|
---|
133 | struct timeval after;
|
---|
134 | if (timeout)
|
---|
135 | {
|
---|
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)
|
---|
141 | {
|
---|
142 | --delay.tv_sec;
|
---|
143 | delay.tv_usec += 1000000;
|
---|
144 | }
|
---|
145 | if (delay.tv_sec < 0)
|
---|
146 | {
|
---|
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
|
---|
150 | // valid result.
|
---|
151 | delay.tv_sec = 0;
|
---|
152 | }
|
---|
153 | }
|
---|
154 | }
|
---|
155 | #else /* HAVE_SELECT */
|
---|
156 | return 0;
|
---|
157 | #endif
|
---|
158 | }
|
---|