D called from Java

Frank Benoit keinfarbton at googlemail.com
Sun Aug 19 07:40:22 PDT 2007


Sean Kelly schrieb:
> Tango will have this once I have time for it.  Likely not long after the
> conference :-)

The more i think about it, how will it work?

The d runtime (drt) needs to know all running threads, for the following
reason:
 * be able to stop all threads while the GC is running
 * provide a thread local storage container.
 * synchronization?

But does the drt also need to know about the termination of a thread?

If so, the registration must be paired with a deregistration on each
library entry point. This also means, register and deregister need to be
really fast because they are called every time the lib is entered or left.

export extern(System)
jint Java_test_Test_test( JNIEnv* env, jobject obj ) {
    Thread.register();
    scope( exit ) Thread.deregister();
    // ...
}

In the scenario, were the D GC is actually running and has all known
threads suspended, the Thread.register() call needs to block until the
GC completes. Probably the methods are better called like enterD/leaveD.

export extern(System)
jint Java_test_Test_test( JNIEnv* env, jobject obj ) {
    Thread.enterD();
    scope( exit ) Thread.leaveD();
    // ...
}

Hopefully this will not raise some new deadlock conditions.
1. D ctor remove held Java global references
2. this removal triggers the Java finalizer
3. another java object.finalize calls therefore the D lib, to signal the
end of the javas object lifecycle.
4. the call blocks and the dtor call from (1) does not return.






More information about the Digitalmars-d mailing list