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]

Patch: FYI: System.nanoTime


I'm checking this in on the trunk.

This implements the new System.nanoTime API.

I haven't tested the Windows implementation, but it looks correct.
It isn't very good, however.

Tom

2006-03-09  Tom Tromey  <tromey@redhat.com>

	* win32.cc (_Jv_platform_nanotime): New function.
	* include/win32.h (_Jv_platform_nanotime): Declare.
	* posix.cc (_Jv_platform_nanotime): New function.
	* include/posix.h (_Jv_platform_nanotime): Declare.
	* java/lang/natSystem.cc (nanoTime): New method.
	* java/lang/System.java (nanoTime): Declare.
	* include/config.h.in, configure: Rebuilt.
	* configure.ac: Check for clock_gettime.

Index: configure.ac
===================================================================
--- configure.ac	(revision 111843)
+++ configure.ac	(working copy)
@@ -1015,6 +1015,14 @@
 	    AC_DEFINE(HAVE_SCHED_YIELD)
 	    THREADLIBS="$THREADLIBS -lposix4"
 	    THREADSPEC="$THREADSPEC -lposix4"])])])
+
+      AC_CHECK_LIB(rt, clock_gettime, [
+         AC_DEFINE(HAVE_CLOCK_GETTIME, 1, [Define if you have clock_gettime()])
+	 case "$THREADSPEC" in
+	   *-lrt*) ;;
+	   *) THREADSPEC="$THREADSPEC -lrt" ;;
+	 esac])
+
       LIBS="$save_LIBS"
 
       # We can save a little space at runtime if the mutex has m_count
Index: java/lang/System.java
===================================================================
--- java/lang/System.java	(revision 111843)
+++ java/lang/System.java	(working copy)
@@ -1,5 +1,5 @@
 /* System.java -- useful methods to interface with the system
-   Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005
+   Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006
    Free Software Foundation, Inc.
 
 This file is part of GNU Classpath.
@@ -208,6 +208,15 @@
   public static native long currentTimeMillis();
 
   /**
+   * Get the current time, measured in nanoseconds.  The result is as
+   * precise as possible, and is measured against a fixed epoch.
+   * However, unlike currentTimeMillis(), the epoch chosen is
+   * arbitrary and may vary by platform, etc.
+   * @since 1.5
+   */
+  public static native long nanoTime();
+
+  /**
    * Copy one array onto another from <code>src[srcStart]</code> ...
    * <code>src[srcStart+len-1]</code> to <code>dest[destStart]</code> ...
    * <code>dest[destStart+len-1]</code>. First, the arguments are validated:
Index: java/lang/natSystem.cc
===================================================================
--- java/lang/natSystem.cc	(revision 111843)
+++ java/lang/natSystem.cc	(working copy)
@@ -1,6 +1,6 @@
 // natSystem.cc - Native code implementing System class.
 
-/* Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003 Free Software Foundation
+/* Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2006 Free Software Foundation
 
    This file is part of libgcj.
 
@@ -124,6 +124,12 @@
   return _Jv_platform_gettimeofday ();
 }
 
+jlong
+java::lang::System::nanoTime ()
+{
+  return _Jv_platform_nanotime ();
+}
+
 jint
 java::lang::System::identityHashCode (jobject obj)
 {
Index: include/posix.h
===================================================================
--- include/posix.h	(revision 111843)
+++ include/posix.h	(working copy)
@@ -1,6 +1,6 @@
 // posix.h -- Helper functions for POSIX-flavored OSs.
 
-/* Copyright (C) 2000, 2002, 2003  Free Software Foundation
+/* Copyright (C) 2000, 2002, 2003, 2006  Free Software Foundation
 
    This file is part of libgcj.
 
@@ -79,6 +79,7 @@
 
 extern int _Jv_select (int n, fd_set *, fd_set *, fd_set *, struct timeval *);
 extern jlong _Jv_platform_gettimeofday ();
+extern jlong _Jv_platform_nanotime ();
 extern void _Jv_platform_initialize (void);
 extern void _Jv_platform_initProperties (java::util::Properties*);
 
Index: include/win32.h
===================================================================
--- include/win32.h	(revision 111843)
+++ include/win32.h	(working copy)
@@ -1,6 +1,6 @@
 // win32.h -- Helper functions for Microsoft-flavored OSs.
 
-/* Copyright (C) 2002, 2003  Free Software Foundation
+/* Copyright (C) 2002, 2003, 2006  Free Software Foundation
 
    This file is part of libgcj.
 
@@ -154,6 +154,7 @@
 extern void _Jv_platform_initialize (void);
 extern void _Jv_platform_initProperties (java::util::Properties*);
 extern jlong _Jv_platform_gettimeofday ();
+extern jlong _Jv_platform_nanotime ();
 extern int _Jv_pipe (int filedes[2]);
 
 extern void
Index: posix.cc
===================================================================
--- posix.cc	(revision 111843)
+++ posix.cc	(working copy)
@@ -1,6 +1,6 @@
 // posix.cc -- Helper functions for POSIX-flavored OSs.
 
-/* Copyright (C) 2000, 2001, 2002  Free Software Foundation
+/* Copyright (C) 2000, 2001, 2002, 2006  Free Software Foundation
 
    This file is part of libgcj.
 
@@ -66,6 +66,22 @@
 #endif
 }
 
+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;
+}
+
 // Platform-specific VM initialization.
 void
 _Jv_platform_initialize (void)
Index: win32.cc
===================================================================
--- win32.cc	(revision 111843)
+++ win32.cc	(working copy)
@@ -1,6 +1,6 @@
 // win32.cc - Helper functions for Microsoft-flavored OSs.
 
-/* Copyright (C) 2002, 2003  Free Software Foundation
+/* Copyright (C) 2002, 2003, 2006  Free Software Foundation
 
    This file is part of libgcj.
 
@@ -279,6 +279,12 @@
   return t.time * 1000LL + t.millitm;
 }
 
+jlong
+_Jv_platform_nanotime ()
+{
+  return _Jv_platform_gettimeofday () * 1000LL;
+}
+
 // The following definitions "fake out" mingw to think that -mthreads
 // was enabled and that mingwthr.dll was linked. GCJ-compiled
 // applications don't need this helper library because we can safely


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