Might not be the only issue, but it's the most obvious operation that sits between the native callback invocation and execution of Java code.
T.
I saw your message on the GStreamer-Java list too. I use that library
quite a bit myself. As a follow on to Timothy's reply, I wondered if
you'd tried using VisualVM and looking to see if anything spikes when
you see this problem? I found this useful when I reported the thread
detach issue because there were obvious garbage collection spikes
happening. Whether GC could be the cause of your pause I don't know
though, as it seems rather long.
It might be easier to use Native.detach(false) to check if thread
detachment is an issue. Using a callback thread initializer is a bit
awkward with GStreamer-Java as the JNA callback can be buried quite
deep.
You'll need a newer JNA (3.4.0) than you're using too.
Best wishes,
Neil
--
Neil C Smith
Artist : Technologist : Adviser
http://neilcsmith.net
Thanks for the lead I will follow up today. Will provide update when I
make changes
Declan
Thanks for feedback, I have some comments inline.
Declan
> I saw your message on the GStreamer-Java list too. I use that library
> quite a bit myself. As a follow on to Timothy's reply, I wondered if
> you'd tried using VisualVM and looking to see if anything spikes when
> you see this problem? I found this useful when I reported the thread
> detach issue because there were obvious garbage collection spikes
> happening. Whether GC could be the cause of your pause I don't know
> though, as it seems rather long.
>
Neil, can you provide your perspective on the previous issue you
mentioned with the thread detach issue.
> It might be easier to use Native.detach(false) to check if thread
> detachment is an issue. Using a callback thread initializer is a bit
> awkward with GStreamer-Java as the JNA callback can be buried quite
> deep.
>
Will try this first as suggested. Thanks
> You'll need a newer JNA (3.4.0) than you're using too.
Yea I have downloaded and sync the master version from github
Quick response as I'm going to be away and offline for a couple of days.
If you want all the long and gory details, check out this thread -
https://groups.google.com/d/topic/jna-users/bxd-yf2Al9g/discussion
I've noticed a lot more GC in GStreamer-Java when not using thread
detach management, but nothing that causes problems anywhere near as
badly as you're seeing. It's not like GStreamer is doing 500
callbacks a second, which was my problem in JNAJack.
The other thing I'd look for is if your code or the GStreamer-Java
code you're using is allocating lots of Java memory per callback
rather than caching and reusing.
Good luck with it!
Best wishes,
Neil
--
Checking the JavaDoc for method, Native.detach(boolean), says the
following "Warning: avoid calling {@link #detach detach(true)} on a VM
thread;
the resulting behavior is not defined.<p/>"
How and where should this be invoked if not from my Java Application
layer, or does the GStreamer-Java library need to do this. I had
assumed that I would call this from my Java application each time I
create a gstreamer pipeline but I don't see this working. I suspect
it must be done when GStreamer-Java library when it registers all its
callback.
Apologies if these questions are very basic, I'm not familiar with
the detail of JNA development but I'm keen to learn
Thanks
Declan
* You have threads spawned from native code (dependent on whatever native libraries you're using). These native threads are temporarily mapped to a Java Thread object if a JNA callback is invoked.
Native.detach(false) should only be called when you're on one of the latter types of threads.
Threads are normally detached when they are unattached on entry, so if you want to force a detach (presumably on a thread on which you previously called Native.detach(false)), you must call Native.detach(true);
You want to watch out for a scenario where the native thread dies without first calling a callback in which you can determine that it's time to detach.
>
> As I said the change recommended have worked really well for me, just
> need some clarity on the effect of this change has as I have
> subsequently seen a number of threads in JAVA in a "Runnable" state
> but no back stacktrace, when I do a jstack.
This would be attached native threads from which your Java callback has returned. I doubt the VM is set up to automatically detect thread death if the VM didn't spawn it (which would be required in order to automatically GC finished threads).
>
> Thanks
> Declan
Might be worth revisiting the conversation about thread-local
destructors, such as mentioned here -
https://groups.google.com/d/msg/jna-users/bxd-yf2Al9g/h34MsCnW7CcJ
The infrastructure now in JNA should make it possible to create a
utility for this without any further changes to JNA itself. I may be
able to help out with this if needed as I'm in the same situation with
JNAJack (at the moment I'm not seeing any adverse affects, but I'd
rather not have the dead Thread objects hanging around).
Best wishes,
Neil
--
>
>>
>> Might be worth revisiting the conversation about thread-local
>> destructors, such as mentioned here -https://groups.google.com/d/msg/jna-users/bxd-yf2Al9g/h34MsCnW7CcJ
>>
>
> Hi Neil
>
> Re-read this email thread again. Yea I see the feature you guys
> discussed which is this actual use case I have. Its called out in the
> email thread as " add thread termination handler to non-detached
> threads" and suggested "ways to do this: o additional interface
> "CallbackNoDetach", "CallbackAsDaemon".
Well, internally it'd be something that JNA does when you call Native.detach(false). pthreads has a mechanism for registering a callback when a thread is about to be destroyed, so JNA could use JNA to register such a callback (with an equivalent on w32) and handle all bookkeeping, without requiring any changes on the native side.
>
> However I think in the end it was decided not to implement this but to
> leave it until a requirement was needed in a real world use case. So
> are you thinking of having a callback in JNA to handle the case when
> thread is destroyed or something outside of JNA? It seems to me that
> having the ability to do both is ideal if JNA provided some hooks it
> would be good I think.
>
> Let me know your thoughts
You can actually implement this outside of JNA to isolate the proper behavior, and then we can figure out where best to have JNA handle it automagically. Prior to calling Native.detach(false) (i.e. identifying a new thread that you want to "keep around"), you set up the pthread callbacks. For the moment, the only thing the destroy callback needs to do is call Native.detach(true) before returning, but you may come up with other things (like perhaps a hook to client code for native thread destroy notification, which might be nice for cleaning up any memory which might have been allocated for that thread's use).
DWORD result = WaitForSingleObject(hThread, 0);
if (result == WAIT_OBJECT_0) {
// thread has terminated
}
else {
// thread still alive
}
So you'd need a dedicated thread to watch the native thread...this could probably be adapted to watch for multiples, though, so you'd only need one monitoring thread on win32.
On Feb 7, 2012, at 6:06 AM, declan wrote:
That's not exactly my recollection / perspective on it, as I've always
had a real-world use case in mind. However, the only change that had
to be made *in* JNA to facilitate this was the detach stuff. The rest
can hopefully be built *with* JNA (not that eventually having
something in the core library eventually wouldn't be a big plus).
On 7 February 2012 20:00, Timothy Wall <twal...@java.net> wrote:
> BTW, this seems to be the preferred method of detecting a win32 thread exit:
>
> DWORD result = WaitForSingleObject(hThread, 0);
> if (result == WAIT_OBJECT_0) {
> // thread has terminated
> }
> else {
> // thread still alive
> }
>
> So you'd need a dedicated thread to watch the native thread...this could probably be adapted to watch for multiples, though, so you'd only need one monitoring thread on win32.
>
Is that the mechanism Boost uses? Not in a position to look through
that code for a few days as I'm travelling. I know that the Boost
library provides thread destructors across Windows and pthreads
though.
N
>
> Hi Guys
>
> I was planning to do something on the native side based on this
> article http://w01fe.com/blog/2009/05/c-callbacks-into-java-via-jni-made-easyier/,
> which was referenced in the original email thread.
>
That link is roughly equivalent to what JNA already takes care of (via Native.detach()); it already handles automatically attaching native threads for Java callbacks. I was under the impression that boost has some sort of implementation for thread termination listeners; hopefully this is not just an attachment to a boost class object destructor.
> Is this the best approach to take here? I don't have much JNI/JNA
> experience, so any tips welcome. Also my assumption is that all of
> this processing must be done in native side as there are no available
> hooks at present in Java to invoke when this native thread terminates.
What needs to be done is hook up listeners for thread termination (using JNA mappings onto libpthread or kernel32). Boost apparently has these hooks in place so it should be straightforward to mimic the operations.
I do know that pthreads provides at least pthread_join and pthread_cleanup_push (the latter is probably more useful). The general idea is something like this:
on Native.detach(false) (eventually performed by JNA automatically, but ok for client to do it before calling Native.detach for now)
if thread not tracked, track it and add a termination callback handler
in termination callback handler:
optionally call a client-supplied cleanup handler
call Native.detach(true);
The thread termination handler ensures JNA gets one final callback before the thread exits.
On 8 February 2012 00:52, Timothy Wall <twal...@java.net> wrote:
>
> On Feb 7, 2012, at 5:57 PM, declan wrote:
>
>>
>> Hi Guys
>>
>> I was planning to do something on the native side based on this
>> article http://w01fe.com/blog/2009/05/c-callbacks-into-java-via-jni-made-easyier/,
>> which was referenced in the original email thread.
>>
>
> That link is roughly equivalent to what JNA already takes care of (via Native.detach()); it already handles automatically attaching native threads for Java callbacks. I was under the impression that boost has some sort of implementation for thread termination listeners; hopefully this is not just an attachment to a boost class object destructor.
>
>
That link is meant to do automatic detachment at thread death using
boost::thread_specific_ptr. Not sure how easy that method would be to
use through JNA though, or what mechanism is going on under the hood.
>> Is this the best approach to take here? I don't have much JNI/JNA
>> experience, so any tips welcome. Also my assumption is that all of
>> this processing must be done in native side as there are no available
>> hooks at present in Java to invoke when this native thread terminates.
>
> What needs to be done is hook up listeners for thread termination (using JNA mappings onto libpthread or kernel32). Boost apparently has these hooks in place so it should be straightforward to mimic the operations.
>
> I do know that pthreads provides at least pthread_join and pthread_cleanup_push (the latter is probably more useful). The general idea is something like this:
>
> on Native.detach(false) (eventually performed by JNA automatically, but ok for client to do it before calling Native.detach for now)
> if thread not tracked, track it and add a termination callback handler
>
> in termination callback handler:
> optionally call a client-supplied cleanup handler
> call Native.detach(true);
>
> The thread termination handler ensures JNA gets one final callback before the thread exits.
From recollection (our previous conversation on this was a while ago
now!) the way to achieve this with pthreads is pthread_key_create and
pthread_setspecific. That's the method in the other (Android) link
from the earlier thread, but I've also seen it elsewhere. I'm not
sure off-hand, but think that's how Boost handles thread termination
callbacks for pthreads. I think there's something similar for Windows
too.
I recall reading of problems with using pthread_cleanup_push for this.
Just need to see if I can find the links again!
Best wishes,
Neil
Why not use JNA to wrap pthreads? As far as I remember all the necessary changes are in callback.c that would allow this to be done without making any further changes to JNA's native code.
N
--
Neil C Smith
Artist : Technologist : Adviser
http://neilcsmith.net
(sent while mobile through GoogleMail)
take a step back and think about it.
* you want to call a native function where one of the arguments is a function pointer
You callback (function pointer) is in Java. You only need to call Native.detach(true) before the thread termination callback returns; the existing JNA callback handling code (native) already does what's required to clean up the thread.
It might get messy if the callback is not called from the terminating thread, but I don't think that's going to be the case.
>
> Sorry about this, Seems I have misread the changes required for JNA to
> enable this to work, doh.
>
> So I should use JNA as it is to register listeners for the pthread
> commands in relation thread specific data and the termination of the
> thread. Once I have these listeners in place then once the thread
> termination callback is invoked we call Native.detach(true) so the
> thread always detaches.
Right. and once you get it working, we splice it into the Java side of JNA's Native.detach(true) so that it happens automagically.
>
> Ok, I get this now, doh. All new to me this, do I need to support all
> the calls for pthreads, or just some. Any reference code worth
> looking at here?
You only need to map the stuff you actually use.