Java crash when fpc library CallVoidMethod in a thread.

classic Classic list List threaded Threaded
28 messages Options
12
Reply | Threaded
Open this post in threaded view
|

Java crash when fpc library CallVoidMethod in a thread.

fredvs
Hello.

When a java class use a fpc library that call jni.CallVoidMethod inside a thread it crash the application.

If the fpc library call jni.CallVoidMethod  directly (not via a thread) => perfect, it works.

Is that a known problem ?

Maybe does it exist something else than jni.CallVoidMethod for inside a thread (like synchronize or queue for procedure of object) ?

Here the java error message :

# A fatal error has been detected by the Java Runtime Environment:
#
#  SIGSEGV (0xb) at pc=0x00007f6d0e405be1, pid=5145, tid=140105962755840
#
# JRE version: OpenJDK Runtime Environment (7.0_51) (build 1.7.0_51-b00)
# Java VM: OpenJDK 64-Bit Server VM (24.45-b08 mixed mode linux-amd64 compressed oops)
# Problematic frame:
# V  [libjvm.so+0x4debe1]  Exceptions::throw_stack_overflow_exception(Thread*, char const*, int, methodHandle)+0x211
#
# Failed to write core dump. Core dumps have been disabled. To enable core dumping, try "ulimit -c unlimited" before starting Java again

Many thanks.
Reply | Threaded
Open this post in threaded view
|

Re: Java crash when fpc library CallVoidMethod in a thread.

Michael Van Canneyt


On Thu, 1 May 2014, fredvs wrote:

> Hello.
>
> When a java class use a fpc library that call jni.CallVoidMethod inside a
> thread it crash the application.
>
> If the fpc library call jni.CallVoidMethod  directly (not via a thread) =>
> perfect, it works.
>
> Is that a known problem ?

Not known, but definitely expected.

FPC has it's specific threading mechanism in the RTL.
Java has such a mechanism in place as well.

I would be very surprised if the 2 could cooperate without a lot of additional glue code.

In general, libraries and threading is not an easy story.

Michael.
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Java crash when fpc library CallVoidMethod in a thread.

fredvs
>> In general, libraries and threading is not an easy story.

Ok,..., but what is your advice to do a  CallVoidMethod inside a thread ?

PS: Everything, inside the thread is working perfectly, only CallVoidMethod does crash...

Do you think i could have better result with a kind of post-messages ?

Many thanks.

Fred
Reply | Threaded
Open this post in threaded view
|

Re: Java crash when fpc library CallVoidMethod in a thread.

patspiper
In reply to this post by fredvs
On 02/05/14 02:22, fredvs wrote:

> Hello.
>
> When a java class use a fpc library that call jni.CallVoidMethod inside a
> thread it crash the application.
>
> If the fpc library call jni.CallVoidMethod  directly (not via a thread) =>
> perfect, it works.
>
> Is that a known problem ?
>
> Maybe does it exist something else than jni.CallVoidMethod for inside a
> thread (like synchronize or queue for procedure of object) ?
>
> Here the java error message :
>
> # A fatal error has been detected by the Java Runtime Environment:
> #
> #  SIGSEGV (0xb) at pc=0x00007f6d0e405be1, pid=5145, tid=140105962755840
> #
> # JRE version: OpenJDK Runtime Environment (7.0_51) (build 1.7.0_51-b00)
> # Java VM: OpenJDK 64-Bit Server VM (24.45-b08 mixed mode linux-amd64
> compressed oops)
> # Problematic frame:
> # V  [libjvm.so+0x4debe1]
> Exceptions::throw_stack_overflow_exception(Thread*, char const*, int,
> methodHandle)+0x211
> #
> # Failed to write core dump. Core dumps have been disabled. To enable core
> dumping, try "ulimit -c unlimited" before starting Java again

Did you use AttachCurrentThread and DetachCurrentThread?

Pay attention to the validity of local references as well as their number.

Posting some code might help.

Stephano
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Java crash when fpc library CallVoidMethod in a thread.

Michael Van Canneyt


On Fri, 2 May 2014, patspiper wrote:

>> dumping, try "ulimit -c unlimited" before starting Java again
>
> Did you use AttachCurrentThread and DetachCurrentThread?

That is probably the "additional glue code" I was talking about in my reply.

Michael.
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Java crash when fpc library CallVoidMethod in a thread.

fredvs
In reply to this post by patspiper
>> Did you use AttachCurrentThread and DetachCurrentThread?

Oops, what is that ? How must i use it ?

>> Pay attention to the validity of local references as well as their number.

Idem, what is "local references as well as their number." ?

PS: Im a beginner with Java but, apart CallVoidMethod in threads, everything is working as i want.

Many thanks.

Reply | Threaded
Open this post in threaded view
|

Re: Java crash when fpc library CallVoidMethod in a thread.

fredvs
In reply to this post by patspiper
>> Posting some code might help.

I may post code but not sure it will help.
The code is the same as http://wiki.freepascal.org/Using_Pascal_Libraries_with_Java, section dealing with callback procedure ( but inside a thread).

Hum, i would prefer some demo-working-code from somebody who knows how to do it...

Many thanks.
Reply | Threaded
Open this post in threaded view
|

Re: Java crash when fpc library CallVoidMethod in a thread.

fredvs
In reply to this post by Michael Van Canneyt
Hello.
What is the best way to  unthread a procedure ?
I have try with a fptimer outside the thread but the thing that disturb me is that you need a tcomponent as owner to create it.
And in the library i do not use any tcomponent...
How to create a fptimer without owner ?
I have try with fptimer.create(Nil) but without luck...
Thanks

PS: if you have a better idea how to "unthread", im verry interessed....
Reply | Threaded
Open this post in threaded view
|

Re: Java crash when fpc library CallVoidMethod in a thread.

Michael Van Canneyt


On Mon, 26 May 2014, fredvs wrote:

> Hello.
> What is the best way to  unthread a procedure ?
> I have try with a fptimer outside the thread but the thing that disturb me
> is that you need a tcomponent as owner to create it.
> And in the library i do not use any tcomponent...
> How to create a fptimer without owner ?
> I have try with fptimer.create(Nil) but without luck...

A fptimer.create(nil) should always work.
fptimer does not need a non-nil owner.

But if you don't have components, you probably also don't have a main application loop.
So I think it likely that then you probably also do not call checksynchronize() at
regular intervals, meaning that fptimer will not work, as this is required to get the ticks.

So, the Nil owner is (with 99.99% certainty) not the problem.

Michael.
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Java crash when fpc library CallVoidMethod in a thread.

fredvs
Yep, Michael, thanks...
Ok then for Nil owner...
Hum, checksynchronize(), i do not realy understand how to use in the thread/loop...
In the loop i do not use synchronize, because the procedure that is called is a java/method  and synchronize is for procedure of object outside the thread... (Or i miss something ).
Ps : this is the very last obstacle for total intégration into java (thread are working perfect, only calling java/method iniside the thread is problematic...)
Ps2 : maybe i have to forget to use thread inside a library (but if it is possible then: whow...)
Reply | Threaded
Open this post in threaded view
|

Re: Java crash when fpc library CallVoidMethod in a thread.

Sven Barth-2
On 26.05.2014 21:47, fredvs wrote:
> Yep, Michael, thanks...
> Ok then for Nil owner...
> Hum, checksynchronize(), i do not realy understand how to use in the
> thread/loop...
> In the loop i do not use synchronize, because the procedure that is called
> is a java/method  and synchronize is for procedure of object outside the
> thread... (Or i miss something ).

CheckSynchronize is used to process calls to TThread.Synchronize and
TThread.Queue. It *must* be called in the context of the main thread
(the thread that loaded the library). TFPTimer uses Synchronize
internally, because it uses a thread to keep track of the timing and
notifies the main thread using Synchronize. Maybe one could add a
non-synchronize mode, then oneself would need to make sure that eventual
accesses inside the event handler are synchronized (CritialSections,
etc.) correctly.

Regards,
Sven

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Java crash when fpc library CallVoidMethod in a thread.

Michael Schnell
In reply to this post by fredvs
On 05/26/2014 09:47 PM, fredvs wrote:
> Hum, checksynchronize(), i do not realy understand how to use in the
> thread/loop...

I am working on an "ActiveNoUIi" widget type to be usable with Lazarus.
Here you can use "normal" Lazarus compatible TTimers.

This already works nicely for me (Linux X86 32 Bit).

The code is some 500 lines.

If you want to test it I can send it to you.

-Michael
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Java crash when fpc library CallVoidMethod in a thread.

fredvs
Hello Sven, hello Michael => many tanks.

I have follow your advices... but...

I have then create a object and a procedure inside that object.
I use then queue() or synchronize()  inside the thread to execute that procedure of object.
That procedure call CallvoidMethod.
OK, everything compiles but when queue() or synchronize() inside the thread is called, now, ok, no crash but CallvoidMethod is not executed...
I have try also to use a fptimer(nil) (outside the thread) that uses, for ontimer, a procedure that call CallvoidMethod but, same as queue() or synchronize() inside the thread, no crash but the timer does not execute CallvoidMethod at fptimer.ontimer...

PS : Im sure that CallvoidMethod is working and use the right parameters (if a call CallvoidMethod outside the thread, directly (not via fptimer.ontimer), the java method is executed..

@ Michael => of course, im hyper interested by your code....

Many thanks.

Fred
Reply | Threaded
Open this post in threaded view
|

Re: Java crash when fpc library CallVoidMethod in a thread.

Ewald-2
On 05/29/2014 11:18 AM, fredvs wrote:
 but the timer does not execute
CallvoidMethod at fptimer.ontimer...
Just a guess here...

To quote Sven a few mails ago: `CheckSynchronize is used to process calls to TThread.Synchronize and TThread.Queue. It *must* be called in the context of the main thread (the thread that loaded the library). TFPTimer uses Synchronize internally, [...]`

But you didn't use the word `CheckSynchronize` anywhere in your mail. You need to call this method regularly in you main thread, if you don't call it, nothing will happen; you only fill up the queue that CheckSynchronize is supposed to empty. See http://www.freepascal.org/docs-html/rtl/classes/checksynchronize.html for more details on this call.
-- 
Ewald

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Java crash when fpc library CallVoidMethod in a thread.

fredvs
@ Ewald => thanks.
Hum, i did use checksynchronize but it did not help...
Must it be used with fptimer.ontimer too ?
And why fptimer.ontimer is not executed ?
Will i have more luck with other timer like epiktimer from Graeme (or the new one from Michael) ?
Thanks.

PS : I will try with some more checksynchronize in main thread...
Reply | Threaded
Open this post in threaded view
|

Re: Java crash when fpc library CallVoidMethod in a thread.

Michael Van Canneyt


On Fri, 30 May 2014, fredvs wrote:

> @ Ewald => thanks.
> Hum, i did use checksynchronize but it did not help...

When do you call it.

> Must it be used with fptimer.ontimer too ?

No.

> And why fptimer.ontimer is not executed ?

If CheckSynchronize is not executed at regular intervals,
fptimer.ontimer is not used.

> Will i have more luck with other timer like epiktimer from Graeme (or the
> new one from Michael) ?

AFAIK:
Those are not the same kind of timers. They just tell you how much time passed,
they do not deliver events at specified intervals.

Michael.
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Java crash when fpc library CallVoidMethod in a thread.

fredvs
In reply to this post by Ewald-2
re-@ Ewald :

>> It *must* be called in the context of the main thread (the thread that loaded the library)

OOps, so do you mean that CheckSynchronize must be called by Java ?
So i have to add a "custom" procedure in the fpc library (who will be only a fpc CheckSynchronize()) ?

Is it that that you mean, calling CheckSynchronize from the main Java class (not from the main thread of the library) ?

Thanks.

Reply | Threaded
Open this post in threaded view
|

Re: Java crash when fpc library CallVoidMethod in a thread.

Michael Van Canneyt


On Fri, 30 May 2014, fredvs wrote:

> re-@ Ewald :
>
>>> It *must* be called in the context of the main thread (the thread that
>>> loaded the library)
>
> OOps, so do you mean that CheckSynchronize must be called by Java ?
> So i have to add a "custom" procedure in the fpc library (who will be only a
> fpc CheckSynchronize()) ?
>
> Is it that that you mean, calling CheckSynchronize from the main Java class
> (not from the main thread of the library) ?

It must be called at regular intervals from the main thread of the library.
Whether this is triggered from Java, or you have some loop in your main
thread that triggers this, is something that is in fact irrelevant for the functioning.

I don't know enough about your architecture to comment on how to do it.
I suspect though, that you will need to make it callable from Java and
call it at regular intervals from Java code.

Michael.
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Java crash when fpc library CallVoidMethod in a thread.

Ewald-2
In reply to this post by fredvs

On 30 May 2014, at 10:46, fredvs wrote:

> re-@ Ewald :
>
>>> It *must* be called in the context of the main thread (the thread that
>>> loaded the library)
>
> OOps, so do you mean that CheckSynchronize must be called by Java ?
> So i have to add a "custom" procedure in the fpc library (who will be only a
> fpc CheckSynchronize()) ?
>
> Is it that that you mean, calling CheckSynchronize from the main Java class
> (not from the main thread of the library) ?
>


Have you got an event/main loop?

If you have something like
        While true do
        Begin
                ... code ...
        End;

Simply append CheckSynchronize at the end of this block:
        While true do
        Begin
                ... code ...

                CheckSynchronize;
        End;

If you have an eventloop, but you haven't got access to the actual code of the loop, you should look for mechanisms that hook up to this loop (something like http://wiki.freepascal.org/Main_Loop_Hooks ?); how this can be done in java, I do not know. You could even try using a timer to accomplish this (it is far from ideal, but you can experiment further then ;-) ), as long as it is a timer that doesn't execute it's events in it's own different thread (if applicable).

As you can see, I am rather vague on the subject, because (1) I am not a java programmer and (2) I don't know enough of your architecture to make any specific comment.

--
Ewald

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Java crash when fpc library CallVoidMethod in a thread.

fredvs
Yep, yep, yep some very good news...

1) Added a new procedure in fpc library:
 =>
procedure uos_checksynchro(PEnv: PJNIEnv; Obj: JObject) ; cdecl;
begin
checksynchronize();
end;

2) And, of course, in fpc library exported as:
 =>
exports
...
uos_checksynchro name 'Java_uos_checksynchro',
...

3) Added in Java wrapper class:
public static native void checksynchro();

4) In main Java application:
After that the fpc thread (who does a CallVoidMethod) was called by Java (with  uos.play(0);):

=> uos.checksynchro();

5) And guess what... The CallVoidMethod is executed...

So, first conclusion : it is possible to synchronize a CallVoidMethod inside a thread but Java main thread must call fpc-checksynchronize().

Hum, so maybe, (im new in Java) use a Java-timer who calls uos.checksynchro(); at regular time...

1) @ Ewald => Oops i read just your answer now, i will study it...
2) @ Ewald => i prefer your solution (if i can do it work) because i prefer not oblige Java-users to use a Java-timer.

PS : All other solutions are welcome...

PS2 : All those problems of synchronize CallVoidMethod inside a thread are problems only for uos (audio library).
For fpGUI library, no any problems (because fpGUI library does not use threads...), those Java developers have now the best widgetset for their Java applications.

PS3 : Hum, if one day, i need a fptimer in a fpc library, is it possible to use fptimer.ontimer who works with Java ?

Many thanks.

Fred.


12