This is the mail archive of the
cygwin
mailing list for the Cygwin project.
Re: JNI call crashes the JVM
- From: Solly Ezekiel <sol_ezekiel at yahoo dot com>
- To: cygwin at cygwin dot com
- Date: Sun, 7 Nov 2004 20:12:09 -0800 (PST)
- Subject: Re: JNI call crashes the JVM
- Comment: DomainKeys? See http://antispam.yahoo.com/domainkeys
- Domainkey-signature: a=rsa-sha1; q=dns; c=nofws; s=s1024; d=yahoo.com; b=2ZgwrGXDR5fi13LFmLZMm5kLJILzfASRYreuIiB0XGchLau+OPcNycmu6Pxnpa5ygmWeOpea71b30AVrM3VfzYYK1j3ksRX2mQVTgQJekTifi9NrWlgzL7LcEjjLdtwbCl8ijgnJar0gLbXI5ryIvqZYB9Ry+v/OsCFoCX/0paM= ;
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/