JNI call crashes the JVM
Solly Ezekiel
sol_ezekiel@yahoo.com
Mon Nov 8 04:12:00 GMT 2004
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/
More information about the Cygwin
mailing list