1 // posix.cc -- Helper functions for POSIX-flavored OSs.
3 /* Copyright (C) 2000, 2001 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
18 #include <java/lang/Thread.h>
19 #include <java/io/InterruptedIOException.h>
22 extern "C" unsigned long long _clock (void);
25 // gettimeofday implementation.
27 _Jv_gettimeofday (struct timeval
*tv
)
29 #if defined (HAVE_GETTIMEOFDAY)
30 gettimeofday (tv
, NULL
);
31 #elif defined (HAVE_TIME)
32 tv
->tv_sec
= time (NULL
);
34 #elif defined (HAVE_FTIME)
38 tv
->tv_usec
= t
.millitm
* 1000;
41 tv
->tv_sec
= _clock () / 1000;
44 // In the absence of any function, time remains forever fixed.
50 // A wrapper for select() which ignores EINTR.
52 _Jv_select (int n
, fd_set
*readfds
, fd_set
*writefds
,
53 fd_set
*exceptfds
, struct timeval
*timeout
)
56 // If we have a timeout, compute the absolute ending time.
57 struct timeval end
, delay
;
60 _Jv_gettimeofday (&end
);
61 end
.tv_usec
+= timeout
->tv_usec
;
62 if (end
.tv_usec
>= 1000000)
65 end
.tv_usec
-= 1000000;
67 end
.tv_sec
+= timeout
->tv_sec
;
73 delay
.tv_sec
= delay
.tv_usec
= 0;
78 int r
= select (n
, readfds
, writefds
, exceptfds
,
79 timeout
? &delay
: NULL
);
80 if (r
!= -1 || errno
!= EINTR
)
83 // Here we know we got EINTR.
84 if (java::lang::Thread::interrupted ())
85 throw new java::io::InterruptedIOException (JvNewStringLatin1 ("select interrupted"));
90 _Jv_gettimeofday (&after
);
91 // Now compute new timeout argument.
92 delay
.tv_usec
= end
.tv_usec
- after
.tv_usec
;
93 delay
.tv_sec
= end
.tv_sec
- after
.tv_sec
;
94 if (delay
.tv_usec
< 0)
97 delay
.tv_usec
+= 1000000;
101 // We assume that the user wants a valid select() call
102 // more than precise timing. So if we get a series of
103 // EINTR we just keep trying with delay 0 until we get a
109 #else /* HAVE_SELECT */