diff options
author | Andrew John Hughes <gnu_andrew@member.fsf.org> | 2010-11-05 00:09:46 +0000 |
---|---|---|
committer | Andrew John Hughes <gnu_andrew@member.fsf.org> | 2010-11-05 00:09:46 +0000 |
commit | ce35f0abe5467d056c79abfe9bfb8acbc8f0c580 (patch) | |
tree | 40f0ae598fabd66e04c24f43017b90f8c30da5cd | |
parent | 6605e28839909048684f485fa931cce6fa11535b (diff) | |
download | classpath-ce35f0abe5467d056c79abfe9bfb8acbc8f0c580.tar.gz |
PR44411: Make VMSystem.nanoTime independent of wall time where possible.
2010-11-04 Andrew John Hughes <ahughes@redhat.com>
Provide a fallback for systems without POSIX timers.
* native/jni/java-lang/java_lang_VMSystem.c:
(currentTimeMillis): New function which provides
the behaviour for both Java_java_lang_VMSystem_currentTimeMillis
and the fallback by obtaining the result of gettimeofday.
(Java_java_lang_VMSystem_nanoTime): Return currentTimeMillis
multiplied by a 1000 if a monotonic clock is unavailable.
(Java_java_lang_VMSystem_currentTimeMillis): Split main behaviour
out into currentTimeMillis and then return its result divided by a
1000.
2010-07-08 Roland Brand <roland.brand@ergon.ch>
Pekka Enberg <penberg@kernel.org>
PR classpath/44411
* native/jni/java-lang/java_lang_VMSystem.c:
(Java_java_lang_VMSystem_nanoTime): Implement
using POSIX monotonic clock support and clock_gettime.
(Java_java_lang_VMSystem_currentTimeMillis):
Use old nanoTime method (which uses gettimeofday) to
provide the current time in milliseconds.
* vm/reference/java/lang/VMSystem.java:
(currentTimeMillis()): Make native with its own implementation
rather than using nanoTime, which should be
independent of wall-clock time.
-rw-r--r-- | ChangeLog | 28 | ||||
-rw-r--r-- | native/jni/java-lang/java_lang_VMSystem.c | 54 | ||||
-rw-r--r-- | vm/reference/java/lang/VMSystem.java | 5 |
3 files changed, 75 insertions, 12 deletions
@@ -1,3 +1,31 @@ +2010-11-04 Andrew John Hughes <ahughes@redhat.com> + + Provide a fallback for systems without POSIX timers. + * native/jni/java-lang/java_lang_VMSystem.c: + (currentTimeMillis): New function which provides + the behaviour for both Java_java_lang_VMSystem_currentTimeMillis + and the fallback by obtaining the result of gettimeofday. + (Java_java_lang_VMSystem_nanoTime): Return currentTimeMillis + multiplied by a 1000 if a monotonic clock is unavailable. + (Java_java_lang_VMSystem_currentTimeMillis): Split main behaviour + out into currentTimeMillis and then return its result divided by a + 1000. + +2010-07-08 Roland Brand <roland.brand@ergon.ch> + Pekka Enberg <penberg@kernel.org> + + PR classpath/44411 + * native/jni/java-lang/java_lang_VMSystem.c: + (Java_java_lang_VMSystem_nanoTime): Implement + using POSIX monotonic clock support and clock_gettime. + (Java_java_lang_VMSystem_currentTimeMillis): + Use old nanoTime method (which uses gettimeofday) to + provide the current time in milliseconds. + * vm/reference/java/lang/VMSystem.java: + (currentTimeMillis()): Make native with its own implementation + rather than using nanoTime, which should be + independent of wall-clock time. + 2010-05-09 Ivan Maidanski <ivmai@mail.ru> * gnu/java/security/Properties.java: Only diff --git a/native/jni/java-lang/java_lang_VMSystem.c b/native/jni/java-lang/java_lang_VMSystem.c index d20322791..a71247d12 100644 --- a/native/jni/java-lang/java_lang_VMSystem.c +++ b/native/jni/java-lang/java_lang_VMSystem.c @@ -39,8 +39,12 @@ exception statement from your version. */ #include <jcl.h> +#include <time.h> #include <sys/time.h> #include <stdlib.h> +#include <errno.h> +#include <unistd.h> +#include <string.h> /* * Class: java_lang_VMSystem @@ -111,6 +115,22 @@ Java_java_lang_VMSystem_setErr (JNIEnv * env, (*env)->SetStaticObjectField (env, cls, field, obj); } +static jlong currentTimeMillis(JNIEnv * env) +{ + /* Note: this implementation copied directly from Japhar's, by Chris Toshok. */ + jlong result; + struct timeval tp; + + if (gettimeofday (&tp, NULL) == -1) + (*env)->FatalError (env, "gettimeofday call failed."); + + result = (jlong) tp.tv_sec; + result *= (jlong)1000000L; + result += (jlong)tp.tv_usec; + + return result; +} + /* * Class: java_lang_VMSystem * Method: nanoTime @@ -118,22 +138,40 @@ Java_java_lang_VMSystem_setErr (JNIEnv * env, */ JNIEXPORT jlong JNICALL Java_java_lang_VMSystem_nanoTime - (JNIEnv * env __attribute__ ((__unused__)), + (JNIEnv * env, jclass thisClass __attribute__ ((__unused__))) { - /* Note: this implementation copied directly from Japhar's, by Chris Toshok. */ +#ifdef _POSIX_MONOTONIC_CLOCK jlong result; - struct timeval tp; + struct timespec tp; - if (gettimeofday (&tp, NULL) == -1) - (*env)->FatalError (env, "gettimeofday call failed."); + if (clock_gettime (CLOCK_MONOTONIC, &tp) == -1) { + char error[64]; + snprintf(error, 64, "clock_gettime call failed: %s.", strerror(errno)); + (*env)->FatalError (env, error); + } result = (jlong) tp.tv_sec; - result *= (jlong)1000000L; - result += (jlong)tp.tv_usec; - result *= (jlong)1000; + result *= (jlong)1000000000L; + result += (jlong)tp.tv_nsec; return result; +#else + return currentTimeMillis(env) * (jlong)1000; +#endif +} + +/* + * Class: java_lang_VMSystem + * Method: currentTimeMillis + * Signature: ()J + */ +JNIEXPORT jlong JNICALL +Java_java_lang_VMSystem_currentTimeMillis + (JNIEnv * env, + jclass thisClass __attribute__ ((__unused__))) +{ + return currentTimeMillis(env) / (jlong)1000L; } JNIEXPORT jstring JNICALL diff --git a/vm/reference/java/lang/VMSystem.java b/vm/reference/java/lang/VMSystem.java index a19466328..0b3d69200 100644 --- a/vm/reference/java/lang/VMSystem.java +++ b/vm/reference/java/lang/VMSystem.java @@ -129,10 +129,7 @@ final class VMSystem * @return the current time * @see java.util.Date */ - static long currentTimeMillis() - { - return nanoTime() / 1000000L; - } + static native long currentTimeMillis(); /** * <p> |