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 (struct timeval
*tv
)
30 #if defined (HAVE_GETTIMEOFDAY)
31 gettimeofday (tv
, NULL
);
32 #elif defined (HAVE_TIME)
33 tv
->tv_sec
= time (NULL
);
35 #elif defined (HAVE_FTIME)
39 tv
->tv_usec
= t
.millitm
* 1000;
42 tv
->tv_sec
= _clock () / 1000;
45 // In the absence of any function, time remains forever fixed.
51 // Platform-specific VM initialization.
53 _Jv_platform_initialize (void)
55 #if defined (HAVE_SIGACTION)
56 // We only want this on POSIX systems.
58 act
.sa_handler
= SIG_IGN
;
59 sigemptyset (&act
.sa_mask
);
61 sigaction (SIGPIPE
, &act
, NULL
);
63 signal (SIGPIPE
, SIG_IGN
);
67 // A wrapper for select() which ignores EINTR.
69 _Jv_select (int n
, fd_set
*readfds
, fd_set
*writefds
,
70 fd_set
*exceptfds
, struct timeval
*timeout
)
73 // If we have a timeout, compute the absolute ending time.
74 struct timeval end
, delay
;
77 _Jv_platform_gettimeofday (&end
);
78 end
.tv_usec
+= timeout
->tv_usec
;
79 if (end
.tv_usec
>= 1000000)
82 end
.tv_usec
-= 1000000;
84 end
.tv_sec
+= timeout
->tv_sec
;
90 delay
.tv_sec
= delay
.tv_usec
= 0;
95 int r
= select (n
, readfds
, writefds
, exceptfds
,
96 timeout
? &delay
: NULL
);
97 if (r
!= -1 || errno
!= EINTR
)
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
;
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)
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
126 #else /* HAVE_SELECT */