* df.c (df_insn_refs_record): Use XEXP (x, 0) for USE.
[official-gcc.git] / libjava / posix.cc
blob66443d21e11599744e8cb1864f5da548b44e088e
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 <errno.h>
16 #include <signal.h>
18 #include <jvm.h>
19 #include <java/lang/Thread.h>
20 #include <java/io/InterruptedIOException.h>
22 #if defined (ECOS)
23 extern "C" unsigned long long _clock (void);
24 #endif
26 // gettimeofday implementation.
27 void
28 _Jv_platform_gettimeofday (struct timeval *tv)
30 #if defined (HAVE_GETTIMEOFDAY)
31 gettimeofday (tv, NULL);
32 #elif defined (HAVE_TIME)
33 tv->tv_sec = time (NULL);
34 tv->tv_usec = 0;
35 #elif defined (HAVE_FTIME)
36 struct timeb t;
37 ftime (&t);
38 tv->tv_sec = t.time;
39 tv->tv_usec = t.millitm * 1000;
40 #elif defined (ECOS)
41 // FIXME.
42 tv->tv_sec = _clock () / 1000;
43 tv->tv_usec = 0;
44 #else
45 // In the absence of any function, time remains forever fixed.
46 tv->tv_sec = 23;
47 tv->tv_usec = 0;
48 #endif
51 // Platform-specific VM initialization.
52 void
53 _Jv_platform_initialize (void)
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
67 // A wrapper for select() which ignores EINTR.
68 int
69 _Jv_select (int n, fd_set *readfds, fd_set *writefds,
70 fd_set *exceptfds, struct timeval *timeout)
72 #ifdef HAVE_SELECT
73 // If we have a timeout, compute the absolute ending time.
74 struct timeval end, delay;
75 if (timeout)
77 _Jv_platform_gettimeofday (&end);
78 end.tv_usec += timeout->tv_usec;
79 if (end.tv_usec >= 1000000)
81 ++end.tv_sec;
82 end.tv_usec -= 1000000;
84 end.tv_sec += timeout->tv_sec;
85 delay = *timeout;
87 else
89 // Placate compiler.
90 delay.tv_sec = delay.tv_usec = 0;
93 while (1)
95 int r = select (n, readfds, writefds, exceptfds,
96 timeout ? &delay : NULL);
97 if (r != -1 || errno != EINTR)
98 return r;
100 // Here we know we got EINTR.
101 if (java::lang::Thread::interrupted ())
102 throw new java::io::InterruptedIOException (JvNewStringLatin1 ("select interrupted"));
104 struct timeval after;
105 if (timeout)
107 _Jv_platform_gettimeofday (&after);
108 // Now compute new timeout argument.
109 delay.tv_usec = end.tv_usec - after.tv_usec;
110 delay.tv_sec = end.tv_sec - after.tv_sec;
111 if (delay.tv_usec < 0)
113 --delay.tv_sec;
114 delay.tv_usec += 1000000;
116 if (delay.tv_sec < 0)
118 // We assume that the user wants a valid select() call
119 // more than precise timing. So if we get a series of
120 // EINTR we just keep trying with delay 0 until we get a
121 // valid result.
122 delay.tv_sec = 0;
126 #else /* HAVE_SELECT */
127 return 0;
128 #endif