A question about threads and callbacks

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

A question about threads and callbacks

Chris Uppal-3
Hi,

I'm working on wrapping the JavaNativeInterface (JNI) stuff, so as to be
able to drive Java dynamically from Dolphin.  It's going OK so far but is a
lot of work to get right.  If anyone wants to know more then email me.

I see one problem looming; in order to be fully useful, I'd like it to be
possible to call back from Java into Dolphin.  The problem is that the JVM
uses OS threads, whereas Dolphin is (as far as the OS is concerned)
single-threaded (ignoring overlapped calls).

So, the question is: what happens if I set an ExternalCallback which is then
used from a different OS thread by the external code ?  I haven't tried it
(haven't got that far yet) but I fear that everything just breaks with two
threads accessing the GC-heap (etc.) at the same time.  If so, is there any
way to prevent such callbacks ?

Also, what happens if an overlapped external call uses a callback ?  Is that
a problem ?  If not then I can probably use some helper C code to trap Java
callbacks on the wrong thread and transfer them over to a background
Smalltalk process which is using an overlapped call to wait for such events.

I don't actually need to solve this problem (if it even is a problem) for my
own purposes, but it'd be more generally useful if I could find a way of
doing callbacks.

TIA for any info.

    -- chris


Reply | Threaded
Open this post in threaded view
|

Re: A question about threads and callbacks

Ted Bracht-2
Hi Chris,

"Chris Uppal" <[hidden email]> wrote in message
news:[hidden email]...
> Hi,
>
> I'm working on wrapping the JavaNativeInterface (JNI) stuff, so as to be
> able to drive Java dynamically from Dolphin.  It's going OK so far but is
a
> lot of work to get right.  If anyone wants to know more then email me.
>

I can't help you with your question, but a while back I tried to get Dolphin
talk to Java as well. Never got round to getting it to work, but it's still
nagging. So I am very interested in your work in that area, could you mail
me please ([hidden email])

Thanks in advance,

Ted


Reply | Threaded
Open this post in threaded view
|

Re: A question about threads and callbacks

Blair McGlashan
In reply to this post by Chris Uppal-3
Chris

You wrote in message
news:[hidden email]...
> ...
> I'm working on wrapping the JavaNativeInterface (JNI) stuff, so as to be
> able to drive Java dynamically from Dolphin.  It's going OK so far but is
a
> lot of work to get right.  If anyone wants to know more then email me.
>
> I see one problem looming; in order to be fully useful, I'd like it to be
> possible to call back from Java into Dolphin.  The problem is that the JVM
> uses OS threads, whereas Dolphin is (as far as the OS is concerned)
> single-threaded (ignoring overlapped calls).
>
> So, the question is: what happens if I set an ExternalCallback which is
then
> used from a different OS thread by the external code ?  I haven't tried it
> (haven't got that far yet) but I fear that everything just breaks with two
> threads accessing the GC-heap (etc.) at the same time.  If so, is there
any
> way to prevent such callbacks ?

The VM (as of D4 I think) will route the call from the "foreign" thread to
the main VM thread. This does prevent the problems you are worried about,
but it is not entirely satisfactory as deadlocks become a distinct
possibility.

>
> Also, what happens if an overlapped external call uses a callback ?  Is
that
> a problem ?  If not then I can probably use some helper C code to trap
Java
> callbacks on the wrong thread and transfer them over to a background
> Smalltalk process which is using an overlapped call to wait for such
events.

Essentially the VM is already doing that for you, as above. You must make
sure, however, that the overlapped call is not issued from the main/GUI
Dolphin Process since the foreign thread call is synchronised through the
message loop in much the same way that COM does such things. There is an
outstanding defect request to address this (103: "External callback
deadlocks if results from overlapped call initiated from main process"), but
at the moment it has not been actioned.

> I don't actually need to solve this problem (if it even is a problem) for
my
> own purposes, but it'd be more generally useful if I could find a way of
> doing callbacks.
>
> TIA for any info.

Please try it and let us know how you get on.

Regards

Blair