1 // posix.cc -- Helper functions for POSIX-flavored OSs.
3 /* Copyright (C) 2000, 2001, 2002, 2006 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
21 #include <java/lang/Thread.h>
22 #include <java/io/InterruptedIOException.h>
23 #include <java/util/Properties.h>
26 extern "C" unsigned long long _clock (void);
29 #if defined(HAVE_PROC_SELF_EXE)
30 static char exec_name
[20];
31 // initialized in _Jv_platform_initialize()
34 const char *_Jv_ThisExecutable (void)
36 #if defined(DISABLE_MAIN_ARGS)
37 return "[Embedded App]";
38 #elif defined(HAVE_PROC_SELF_EXE)
40 // initialized in _Jv_platform_initialize()
42 return _Jv_GetSafeArg (0);
46 // gettimeofday implementation.
48 _Jv_platform_gettimeofday ()
50 #if defined (HAVE_GETTIMEOFDAY)
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)
59 return (t
.time
* 1000LL) + t
.millitm
;
64 // In the absence of any function, time remains forever fixed.
70 _Jv_platform_nanotime ()
72 #ifdef HAVE_CLOCK_GETTIME
74 if (clock_gettime (CLOCK_REALTIME
, &now
) == 0)
76 jlong result
= (jlong
) now
.tv_sec
;
77 result
= result
* 1000 * 1000 + now
.tv_nsec
;
80 // clock_gettime failed, but we can fall through.
81 #endif // HAVE_CLOCK_GETTIME
82 return _Jv_platform_gettimeofday () * 1000LL;
85 // Platform-specific VM initialization.
87 _Jv_platform_initialize (void)
89 #if defined (HAVE_SIGACTION)
90 // We only want this on POSIX systems.
92 act
.sa_handler
= SIG_IGN
;
93 sigemptyset (&act
.sa_mask
);
95 sigaction (SIGPIPE
, &act
, NULL
);
97 signal (SIGPIPE
, SIG_IGN
);
100 #if defined (HAVE_PROC_SELF_EXE)
101 // Compute our executable name
102 sprintf (exec_name
, "/proc/%d/exe", getpid ());
106 // Set platform-specific System properties.
108 _Jv_platform_initProperties (java::util::Properties
* newprops
)
110 // A convenience define.
111 #define SET(Prop,Val) \
112 newprops->put(JvNewStringLatin1 (Prop), JvNewStringLatin1 (Val))
114 SET ("file.separator", "/");
115 SET ("path.separator", ":");
116 SET ("line.separator", "\n");
117 const char *tmpdir
= ::getenv("TMPDIR");
120 SET ("java.io.tmpdir", tmpdir
);
124 internal_gettimeofday (struct timeval
*result
)
126 #if defined (HAVE_GETTIMEOFDAY)
127 gettimeofday (result
, NULL
);
129 jlong val
= _Jv_platform_gettimeofday ();
130 result
->tv_sec
= val
/ 1000;
131 result
->tv_usec
= (val
% 1000) * 1000;
132 #endif /* HAVE_GETTIMEOFDAY */
135 // A wrapper for select() which ignores EINTR.
137 _Jv_select (int n
, fd_set
*readfds
, fd_set
*writefds
,
138 fd_set
*exceptfds
, struct timeval
*timeout
)
141 // If we have a timeout, compute the absolute ending time.
142 struct timeval end
, delay
;
145 internal_gettimeofday (&end
);
146 end
.tv_usec
+= timeout
->tv_usec
;
147 if (end
.tv_usec
>= 1000000)
150 end
.tv_usec
-= 1000000;
152 end
.tv_sec
+= timeout
->tv_sec
;
158 delay
.tv_sec
= delay
.tv_usec
= 0;
163 int r
= select (n
, readfds
, writefds
, exceptfds
,
164 timeout
? &delay
: NULL
);
165 if (r
!= -1 || errno
!= EINTR
)
168 // Here we know we got EINTR.
169 if (java::lang::Thread::interrupted ())
170 throw new java::io::InterruptedIOException (JvNewStringLatin1 ("select interrupted"));
172 struct timeval after
;
175 internal_gettimeofday (&after
);
176 // Now compute new timeout argument.
177 delay
.tv_usec
= end
.tv_usec
- after
.tv_usec
;
178 delay
.tv_sec
= end
.tv_sec
- after
.tv_sec
;
179 if (delay
.tv_usec
< 0)
182 delay
.tv_usec
+= 1000000;
184 if (delay
.tv_sec
< 0)
186 // We assume that the user wants a valid select() call
187 // more than precise timing. So if we get a series of
188 // EINTR we just keep trying with delay 0 until we get a
194 #else /* HAVE_SELECT */