This is the mail archive of the java-discuss@sourceware.cygnus.com mailing list for the Java project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Thread.sleep() and problems with jlong within native code on glibc


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;
 


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]