This is the mail archive of the cygwin mailing list for the Cygwin 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: JNI call crashes the JVM


Gerrit, thanks for your advice.  I've eliminated the
reliance on the Cygwin runtime, and it works fine now.

Lapo, I downloaded your software and compiled it, and
it runs fine.  Thanks.  I believe there's a bug in the
logic you use to compute the frequency, though:

  while(!valid)
    try {
      tsc1 = RDTSC.getClock();
      Thread.sleep(100);
      tsc2 = RDTSC.getClock();
      frequency = (tsc2 - tsc1 - delta) / 100;
      valid = true;
    } catch(InterruptedException e) {
  }

You wait 100 ms in your loop, but when you convert the
time to a frequency you divide by 100 -- I believe
that gives you the number of ticks per millisecond,
not per second as the comments imply.

In any case, after some investigation (see this
article:
http://www.javaworld.com/javaworld/javaqa/2003-01/01-qa-0110-timing.html)
it turns out you don't have to go through the trouble
of measuring the clock speed, you can just get it from
the OS; see the QueryPerformanceFrequency and
QueryPerformanceCounter functions mentioned in the
article.  I've implemented a simple class with a
static native method to return	the time since the last
reboot as a double.  Here's the C++ part of it:

  namespace {
    double period = -1.0;
  };

  JNIEXPORT jdouble JNICALL
  Java_blah_currentTime(JNIEnv *, jclass)
  {
    LARGE_INTEGER cBuffer;
    LARGE_INTEGER fBuffer;

    if(period < 0.0) {
      if(QueryPerformanceFrequency(&fBuffer)) {
        const double frequency =
          (double) fBuffer.QuadPart;
        period = 1.0 / frequency;
      } else {
        // Call failed, deal with error
      }
    }

    if(QueryPerformanceCounter(&cBuffer)) {
      return (double) cBuffer.QuadPart * period;
    } else {
      // Call failed, deal with error
    }
  }

Thanks to both of you for answering, you have both
been a big help.

Cheers,
Sol Ezekiel



		
__________________________________ 
Do you Yahoo!? 
Check out the new Yahoo! Front Page. 
www.yahoo.com 
 


--
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple
Problem reports:       http://cygwin.com/problems.html
Documentation:         http://cygwin.com/docs.html
FAQ:                   http://cygwin.com/faq/


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