Thread.sleep() and problems with jlong within native code on glibc
Bryce McKinlay
bryce@albatross.co.nz
Sun Jun 13 20:34:00 GMT 1999
I have been investigating the problem of Thread.sleep() not working on
linux/glibc.
There is some kind of problem with the jlong value returned by System::currentTimeMillis()
when it is called from within native code. The System::currentTimeMillis()
result seen is wrong - currentTimeMillis() / 1000 is much smaller than
the tv_sec value returned from gettimeofday. However, when System.currentTimeMillis()
is called from within Java code, it works fine.
The patch below works around the problem by avoiding the call to currentTimeMillis(),
and fixed Thread.sleep() on Linux. I have not committed it to cvs, yet,
because this patch is really just fixing the symptom and not the problem.
Clues?
regards
ÃÂ [ bryce ]
ÃÂ
Index: posix-threads.cc
===================================================================
RCS file: /cvs/java/libgcj/libjava/posix-threads.cc,v
retrieving revision 1.1.1.1
diff -u -r1.1.1.1 posix-threads.cc
--- posix-threads.ccÃÂ ÃÂ ÃÂ 1999/04/07 14:52:32ÃÂ ÃÂ ÃÂ ÃÂ
1.1.1.1
+++ posix-threads.ccÃÂ ÃÂ ÃÂ 1999/06/14 03:04:08
@@ -27,6 +27,10 @@
ÃÂ #include <time.h>
ÃÂ #include <signal.h>
ÃÂ
+#ifdef HAVE_SYS_TIME_H
+#include <sys/time.h>
+#endif
+
ÃÂ #include <cni.h>
ÃÂ #include <jvm.h>
ÃÂ #include <java/lang/Thread.h>
@@ -85,12 +89,13 @@
ÃÂ ÃÂ ÃÂ ÃÂ r = pthread_cond_wait (cv, pmu);
ÃÂ ÃÂ else
ÃÂ ÃÂ ÃÂ ÃÂ {
+ÃÂ ÃÂ ÃÂ ÃÂ ÃÂ struct timeval tv;
ÃÂ ÃÂ ÃÂ ÃÂ ÃÂ ÃÂ struct timespec ts;
-ÃÂ ÃÂ ÃÂ ÃÂ ÃÂ unsigned long m = millis + java::lang::System::currentTimeMillis
();
-
-ÃÂ ÃÂ ÃÂ ÃÂ ÃÂ ts.tv_sec = m / 1000;
-ÃÂ ÃÂ ÃÂ ÃÂ ÃÂ ts.tv_nsec = (m % 1000) * 1000
* 1000 + nanos;
-
+
+ÃÂ ÃÂ ÃÂ ÃÂ ÃÂ gettimeofday (&tv, NULL);
+ÃÂ ÃÂ ÃÂ ÃÂ ÃÂ ts.tv_sec = tv.tv_sec + (millis
/ 1000);
+ÃÂ ÃÂ ÃÂ ÃÂ ÃÂ ts.tv_nsec = (tv.tv_usec * 1000)
+ ((millis % 1000) * 1000000) + nanos;
+
ÃÂ ÃÂ ÃÂ ÃÂ ÃÂ ÃÂ r = pthread_cond_timedwait
(cv, pmu, &ts);
ÃÂ ÃÂ ÃÂ ÃÂ }
ÃÂ ÃÂ return r;
ÃÂ
More information about the Java
mailing list