[EMAIL PROTECTED] wrote:
> 
> Hi,
> 
> I got a question on JNI.  It would be appreciated if someone could help.
> 
> Q:
> In order to hide java and jni related issues (e.g. jni function name
> convention, etc.) from .so programmers, a wrapper .so file so1.so is used in
> between java app and another .so file so2.so (the one with native functions
> we need).  This way, the java app interacts with the wrapper so1.so
> directly.  Every time when java app needs a native function service in
> so2.so, it first calls a native function in so1.so through jni.  so1.so, in
> turn, calls the required function in so2.so.  Everything works fine in this
> direction (i.e. java app->so1.so->so2.so.  java app loads so1.so, and so1.so
> loads so2.so, then java app invokes a function in so2.so through a function
> in so1.so).  The question is how a function in so2.so calls back to java app
> through a function in so1.so?  In fact, we only need to know how functions
> in so2.so can call functions in so1.so because jni will let us call back to
> java app from so1.so.

You have just pushed the problem out of the Java arena, haven't you?  As
it stands now, your question is essentially: How can a function in
so2.so call back to a function in so1.so?  There are two ways I can
think of: 1. so2.so can call an external function by name (I can't think
of a Linux example off the top of my head, but in Windows, one of the
Windows system DLLs calls your WinMain() function); 2. so2.so can
provide a registration machanism to give so1.so a chance to pass one of
its function pointers into so2.so (callback functions, they are all over
the place in any widget library).

> More information:
> - the java app is multithreaded application using Thread.start().
> - each java thread may call the same function in so1.so, then
>   this function, in turn, calls a function in so2.so
> - native code doesn't create any thread.

Your C functions still need to be MT safe, since they are potentially
called from different threads (if you are using native threads in Java).

> Use IPC? which one is easier on Linux?  Is there any other mechanism
> for this on Linux?

You have only one process (the one started by 'java YourClass'),
everything else are threads within that one process.  So there's no need
to use any IPCs.  If, however, you intend to issue 'java MyClass' twice
and have the two resultant processes communicate, then you need to use
IPCs.  All the IPC mechanisms described in Stevens' UNIX Network
Programming are available on Linux: sockets, pipes, FIFOs, SYSV IPCs
(shared memories, message queues, semaphores).

> A simple sample code will help a lot.

A simple registration example:
/* so2.c */ typedef void (*F)(); F so2_f; void so2_g(F _f){so2_f = _f;}
void so2_h(){so2_f();}
/* so1.c */ int i=0; void so1_x(){} void so1_y(){so2_g(so1_x); i=1;}
void so1_z(){i && so2_h();}

-- 
Weiqi Gao
[EMAIL PROTECTED]


----------------------------------------------------------------------
To UNSUBSCRIBE, email to [EMAIL PROTECTED]
with a subject of "unsubscribe". Trouble? Contact [EMAIL PROTECTED]

Reply via email to