This is the mail archive of the
java-discuss@sourceware.cygnus.com
mailing list for the Java project.
Re: CNI namespace
- To: Paul Fisher <pnfisher at redhat dot com>
- Subject: Re: CNI namespace
- From: Cedric Berger <cedric at wireless-networks dot com>
- Date: Tue, 04 Apr 2000 09:43:16 -0700
- CC: "java-discuss at sourceware dot cygnus dot com" <java-discuss at sourceware dot cygnus dot com>
- Organization: Wireless Networks Inc
- References: <y683dpaetn2.fsf_-_@ignucius.devel.redhat.com> <m2r9ct8pho.fsf@kelso.bothner.com> <y68k8ilcrfj.fsf@idoru.labs.redhat.com> <m2hfdp8hjy.fsf@kelso.bothner.com> <y6866u5cn4b.fsf@idoru.labs.redhat.com> <m27lel8eb3.fsf@kelso.bothner.com> <200004031916.MAA02911@ferrule.cygnus.com> <m2d7o7m05d.fsf@kelso.bothner.com> <m3vh1ysb3u.fsf@localhost.localdomain>
Paul Fisher wrote:
> Per Bothner <per@bothner.com> writes:
>
> > Paul sent me some private email, which didn't really explain the
> > idea. For one thing, I don't see how the JNIEnv can get passed.
>
> Sorry for the delay in replying -- been sick of late.
>
> The JNIEnv and jobject (this) is passed into a function from the
> foreign VM, so it's always around for calling member functions and
> accessing fields. When it's not possible to pass around the JNIEnv
> (ie. when calling a non-member function), it can be simply retrieved
> as thread local data. No changes to g++ should be necessary.
>
...
>
> class Object
> {
> static jmethodID *method_ids;
>
> jobject obj;
> JNIEnv *env;
You cannot store JNIEnv in an object, because this pointer is different
for each thread. So if two threads access the same object, CRASH.
In another end, retrieving JNIEnv is very easy if the VM use JNI/1.2:
simply call
- jint GetEnv(JavaVM *vm, void **env, jint version);
and that's it.
you just have to have a global variable to store the vm pointer.
When I deal with native functions and JNI, I define a function that looks
like:
JNIEnv *jni_get_env() {
extern JavaVM *java_vm;
JNIEnv *env;
int err = GetEnv(java_vm, &env, JNI_VERSION_1_2);
if(err == JNI_EDETACHED) {
struct JavaVMAttachArgs args = { JNI_VERSION_1_2 };
err = AttachCurrentThread(java_vm, &env, &args);
}
assert(err == JNI_OK);
return env;
}
Cedric
begin:vcard
n:Berger;Cedric
tel;fax:++1 (650) 574-4476
tel;work:++1 (650) 574-4472
x-mozilla-html:TRUE
org:Wireless Networks Inc
version:2.1
email;internet:cedric@wireless-networks.com
title:Embedded Software Engineer
adr;quoted-printable:;;2322 El Camino Real=0D=0A;San Mateo;CA;94403;USA
fn:Cedric Berger
end:vcard