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
19 #include <java/lang/Thread.h>
20 #include <java/io/InterruptedIOException.h>
23 extern "C" unsigned long long _clock (void);
26 // gettimeofday implementation.
28 _Jv_platform_gettimeofday ()
30 #if defined (HAVE_GETTIMEOFDAY)
32 gettimeofday (&tv, NULL);
33 return tv.tv_sec * 1000 + tv.tv_usec / 1000;
34 #elif defined (HAVE_TIME)
35 return time (NULL) * 1000;
36 #elif defined (HAVE_FTIME)
39 return t.time * 1000 + t.millitm;
44 // In the absence of any function, time remains forever fixed.
49 // Platform-specific VM initialization.
51 _Jv_platform_initialize (void)
53 #if defined (HAVE_SIGACTION)
54 // We only want this on POSIX systems.
56 act.sa_handler = SIG_IGN;
57 sigemptyset (&act.sa_mask);
59 sigaction (SIGPIPE, &act, NULL);
61 signal (SIGPIPE, SIG_IGN);
65 // A wrapper for select() which ignores EINTR.
67 _Jv_select (int n, fd_set *readfds, fd_set *writefds,
68 fd_set *exceptfds, struct timeval *timeout)
71 // If we have a timeout, compute the absolute ending time.
72 struct timeval end, delay;
75 _Jv_platform_gettimeofday (&end);
76 end.tv_usec += timeout->tv_usec;
77 if (end.tv_usec >= 1000000)
80 end.tv_usec -= 1000000;
82 end.tv_sec += timeout->tv_sec;
88 delay.tv_sec = delay.tv_usec = 0;
93 int r = select (n, readfds, writefds, exceptfds,
94 timeout ? &delay : NULL);
95 if (r != -1 || errno != EINTR)
98 // Here we know we got EINTR.
99 if (java::lang::Thread::interrupted ())
100 throw new java::io::InterruptedIOException (JvNewStringLatin1 ("select interrupted"));
102 struct timeval after;
105 _Jv_platform_gettimeofday (&after);
106 // Now compute new timeout argument.
107 delay.tv_usec = end.tv_usec - after.tv_usec;
108 delay.tv_sec = end.tv_sec - after.tv_sec;
109 if (delay.tv_usec < 0)
112 delay.tv_usec += 1000000;
114 if (delay.tv_sec < 0)
116 // We assume that the user wants a valid select() call
117 // more than precise timing. So if we get a series of
118 // EINTR we just keep trying with delay 0 until we get a
124 #else /* HAVE_SELECT */