OSDN Git Service

2000-11-23 Bryce McKinlay <bryce@albatross.co.nz>
[pf3gnuchains/gcc-fork.git] / libjava / posix.cc
1 // posix.cc -- Helper functions for POSIX-flavored OSs.
2
3 /* Copyright (C) 2000  Free Software Foundation
4
5    This file is part of libgcj.
6
7 This software is copyrighted work licensed under the terms of the
8 Libgcj License.  Please consult the file "LIBGCJ_LICENSE" for
9 details.  */
10
11 #include <config.h>
12
13 #include "posix.h"
14
15 #include <errno.h>
16
17 #if defined (ECOS)
18 extern "C" unsigned long long _clock (void);
19 #endif
20
21 // gettimeofday implementation.
22 void
23 _Jv_gettimeofday (struct timeval *tv)
24 {
25 #if defined (HAVE_GETTIMEOFDAY)
26   gettimeofday (tv, NULL);
27 #elif defined (HAVE_TIME)
28   tv->tv_sec = time (NULL);
29   tv->tv_usec = 0;
30 #elif defined (HAVE_FTIME)
31   struct timeb t;
32   ftime (&t);
33   tv->tv_sec = t.time;
34   tv->tv_usec = t.millitm * 1000;
35 #elif defined (ECOS)
36   // FIXME.
37   tv->tv_sec = _clock () / 1000;
38   tv->tv_usec = 0;
39 #else
40   // In the absence of any function, time remains forever fixed.
41   tv->tv_sec = 23;
42   tv->tv_usec = 0;
43 #endif
44 }
45
46 // A wrapper for select() which ignores EINTR.
47 int
48 _Jv_select (int n, fd_set *readfds, fd_set  *writefds,
49             fd_set *exceptfds, struct timeval *timeout)
50 {
51 #ifdef HAVE_SELECT
52   // If we have a timeout, compute the absolute ending time.
53   struct timeval end, delay;
54   if (timeout)
55     {
56       _Jv_gettimeofday (&end);
57       end.tv_usec += timeout->tv_usec;
58       if (end.tv_usec >= 1000000)
59         {
60           ++end.tv_sec;
61           end.tv_usec -= 1000000;
62         }
63       end.tv_sec += timeout->tv_sec;
64       delay = *timeout;
65     }
66   else
67     {
68       // Placate compiler.
69       delay.tv_sec = delay.tv_usec = 0;
70     }
71
72   while (1)
73     {
74       int r = select (n, readfds, writefds, exceptfds,
75                       timeout ? &delay : NULL);
76       if (r != -1 || errno != EINTR)
77         return r;
78
79       struct timeval after;
80       if (timeout)
81         {
82           _Jv_gettimeofday (&after);
83           // Now compute new timeout argument.
84           delay.tv_usec = end.tv_usec - after.tv_usec;
85           delay.tv_sec = end.tv_sec - after.tv_sec;
86           if (delay.tv_usec < 0)
87             {
88               --delay.tv_sec;
89               delay.tv_usec += 1000000;
90             }
91           if (delay.tv_sec < 0)
92             {
93               // We assume that the user wants a valid select() call
94               // more than precise timing.  So if we get a series of
95               // EINTR we just keep trying with delay 0 until we get a
96               // valid result.
97               delay.tv_sec = 0;
98             }
99         }
100     }
101 #else /* HAVE_SELECT */
102   return 0;
103 #endif
104 }