This is the mail archive of the java-patches@gcc.gnu.org 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]
Other format: [Raw text]

Re: Patch: FYI: System.nanoTime


Hi Tom,

+jlong
+_Jv_platform_nanotime ()
+{
+#ifdef HAVE_CLOCK_GETTIME
+  struct timespec now;
+  if (clock_gettime (CLOCK_REALTIME, &now) == 0)
+    {
+      jlong result = (jlong) now.tv_sec;
+      result = result * 1000 * 1000 + now.tv_nsec;
+      return result;
+    }
+  // clock_gettime failed, but we can fall through.
+#endif // HAVE_CLOCK_GETTIME
+  return _Jv_platform_gettimeofday () * 1000LL;
+}

Why do you use CLOCK_REALTIME? it represents the time
from the epoch, but nanoTime() javadoc atate that
nanoTime() value should be used for measuring elapsed
time and is unrelated to wall-clock time.

The problem is when you use CLOCK_REALTIME to measure
elapsed time and the user changes the time...
I had to write the same code for another project, and
I used the following snipped for Linux/BSD/Solaris:

  int		id;
  #ifdef CLOCK_MONOTONIC
	id = CLOCK_MONOTONIC;
  #else
  #ifdef CLOCK_HIGHRES
	id = CLOCK_HIGHRES;
  #else
  #error bad platform
  #endif
  #endif
	if (clock_gettime(id, &tv) - 0)

Cedric


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