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
* 1000LL) + (tv
.tv_usec
/ 1000LL);
34 #elif defined (HAVE_TIME)
35 return time (NULL
) * 1000LL;
36 #elif defined (HAVE_FTIME)
39 return (t
.time
* 1000LL) + 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
);
66 internal_gettimeofday (struct timeval
*result
)
68 #if defined (HAVE_GETTIMEOFDAY)
69 gettimeofday (result
, NULL
);
71 jlong val
= _Jv_platform_gettimeofday ();
72 result
->tv_sec
= val
/ 1000;
73 result
->tv_usec
= (val
% 1000) * 1000;
74 #endif /* HAVE_GETTIMEOFDAY */
77 // A wrapper for select() which ignores EINTR.
79 _Jv_select (int n
, fd_set
*readfds
, fd_set
*writefds
,
80 fd_set
*exceptfds
, struct timeval
*timeout
)
83 // If we have a timeout, compute the absolute ending time.
84 struct timeval end
, delay
;
87 internal_gettimeofday (&end
);
88 end
.tv_usec
+= timeout
->tv_usec
;
89 if (end
.tv_usec
>= 1000000)
92 end
.tv_usec
-= 1000000;
94 end
.tv_sec
+= timeout
->tv_sec
;
100 delay
.tv_sec
= delay
.tv_usec
= 0;
105 int r
= select (n
, readfds
, writefds
, exceptfds
,
106 timeout
? &delay
: NULL
);
107 if (r
!= -1 || errno
!= EINTR
)
110 // Here we know we got EINTR.
111 if (java::lang::Thread::interrupted ())
112 throw new java::io::InterruptedIOException (JvNewStringLatin1 ("select interrupted"));
114 struct timeval after
;
117 internal_gettimeofday (&after
);
118 // Now compute new timeout argument.
119 delay
.tv_usec
= end
.tv_usec
- after
.tv_usec
;
120 delay
.tv_sec
= end
.tv_sec
- after
.tv_sec
;
121 if (delay
.tv_usec
< 0)
124 delay
.tv_usec
+= 1000000;
126 if (delay
.tv_sec
< 0)
128 // We assume that the user wants a valid select() call
129 // more than precise timing. So if we get a series of
130 // EINTR we just keep trying with delay 0 until we get a
136 #else /* HAVE_SELECT */