OSDN Git Service

* config/rs6000/rs6000.c (rs6000_rtx_costs): Add EQ, GTU, and LTU.
[pf3gnuchains/gcc-fork.git] / libjava / posix.cc
1 // posix.cc -- Helper functions for POSIX-flavored OSs.
2
3 /* Copyright (C) 2000, 2001, 2002  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 <stdlib.h>
16 #include <errno.h>
17 #include <signal.h>
18 #include <stdio.h>
19
20 #include <jvm.h>
21 #include <java/lang/Thread.h>
22 #include <java/io/InterruptedIOException.h>
23 #include <java/util/Properties.h>
24
25 #if defined (ECOS)
26 extern "C" unsigned long long _clock (void);
27 #endif
28
29 #if defined(HAVE_PROC_SELF_EXE)
30 static char exec_name[20];
31   // initialized in _Jv_platform_initialize()
32 #endif
33
34 const char *_Jv_ThisExecutable (void)
35 {
36 #if defined(DISABLE_MAIN_ARGS)
37   return "[Embedded App]";
38 #elif defined(HAVE_PROC_SELF_EXE)
39   return exec_name;
40     // initialized in _Jv_platform_initialize()
41 #else
42   return _Jv_GetSafeArg (0);
43 #endif
44 }
45
46 // gettimeofday implementation.
47 jlong
48 _Jv_platform_gettimeofday ()
49 {
50 #if defined (HAVE_GETTIMEOFDAY)
51   timeval tv;
52   gettimeofday (&tv, NULL);
53   return (tv.tv_sec * 1000LL) + (tv.tv_usec / 1000LL);
54 #elif defined (HAVE_TIME)
55   return time (NULL) * 1000LL;
56 #elif defined (HAVE_FTIME)
57   struct timeb t;
58   ftime (&t);
59   return (t.time * 1000LL) + t.millitm;
60 #elif defined (ECOS)
61   // FIXME.
62   return _clock();
63 #else
64   // In the absence of any function, time remains forever fixed.
65   return 23000;
66 #endif
67 }
68
69 // Platform-specific VM initialization.
70 void
71 _Jv_platform_initialize (void)
72 {
73 #if defined (HAVE_SIGACTION)
74   // We only want this on POSIX systems.
75   struct sigaction act;
76   act.sa_handler = SIG_IGN;
77   sigemptyset (&act.sa_mask);
78   act.sa_flags = 0;
79   sigaction (SIGPIPE, &act, NULL);
80 #else
81   signal (SIGPIPE, SIG_IGN);
82 #endif
83
84 #if defined (HAVE_PROC_SELF_EXE)
85   // Compute our executable name
86   sprintf (exec_name, "/proc/%d/exe", getpid ());
87 #endif
88 }
89
90 // Set platform-specific System properties.
91 void
92 _Jv_platform_initProperties (java::util::Properties* newprops)
93 {
94   // A convenience define.
95 #define SET(Prop,Val) \
96   newprops->put(JvNewStringLatin1 (Prop), JvNewStringLatin1 (Val))
97
98   SET ("file.separator", "/");
99   SET ("path.separator", ":");
100   SET ("line.separator", "\n");
101   char *tmpdir = ::getenv("TMPDIR");
102   if (! tmpdir)
103     tmpdir = "/tmp";
104   SET ("java.io.tmpdir", tmpdir);
105 }
106
107 static inline void
108 internal_gettimeofday (struct timeval *result)
109 {
110 #if defined (HAVE_GETTIMEOFDAY)
111   gettimeofday (result, NULL);
112 #else
113   jlong val = _Jv_platform_gettimeofday ();
114   result->tv_sec = val / 1000;
115   result->tv_usec = (val % 1000) * 1000;
116 #endif /* HAVE_GETTIMEOFDAY */
117 }
118
119 // A wrapper for select() which ignores EINTR.
120 int
121 _Jv_select (int n, fd_set *readfds, fd_set  *writefds,
122             fd_set *exceptfds, struct timeval *timeout)
123 {
124 #ifdef HAVE_SELECT
125   // If we have a timeout, compute the absolute ending time.
126   struct timeval end, delay;
127   if (timeout)
128     {
129       internal_gettimeofday (&end);
130       end.tv_usec += timeout->tv_usec;
131       if (end.tv_usec >= 1000000)
132         {
133           ++end.tv_sec;
134           end.tv_usec -= 1000000;
135         }
136       end.tv_sec += timeout->tv_sec;
137       delay = *timeout;
138     }
139   else
140     {
141       // Placate compiler.
142       delay.tv_sec = delay.tv_usec = 0;
143     }
144
145   while (1)
146     {
147       int r = select (n, readfds, writefds, exceptfds,
148                       timeout ? &delay : NULL);
149       if (r != -1 || errno != EINTR)
150         return r;
151
152       // Here we know we got EINTR.
153       if (java::lang::Thread::interrupted ())
154         throw new java::io::InterruptedIOException (JvNewStringLatin1 ("select interrupted"));
155
156       struct timeval after;
157       if (timeout)
158         {
159           internal_gettimeofday (&after);
160           // Now compute new timeout argument.
161           delay.tv_usec = end.tv_usec - after.tv_usec;
162           delay.tv_sec = end.tv_sec - after.tv_sec;
163           if (delay.tv_usec < 0)
164             {
165               --delay.tv_sec;
166               delay.tv_usec += 1000000;
167             }
168           if (delay.tv_sec < 0)
169             {
170               // We assume that the user wants a valid select() call
171               // more than precise timing.  So if we get a series of
172               // EINTR we just keep trying with delay 0 until we get a
173               // valid result.
174               delay.tv_sec = 0;
175             }
176         }
177     }
178 #else /* HAVE_SELECT */
179   return 0;
180 #endif
181 }