jnr/jnr-ffi

Use one thread for all callbacks

Opened this issue · 4 comments

I have simple library with API:

    interface OnImage {
        @Delegate
        void callback(int width, int height, Pointer buff);
    }

    void open(Pointer cam, OnImage callback);

Each callback call create a new java thread to handle the callback. How I can handle callback in fixed/single thread?
In JNA can use CallbackThreadInitializer:

Native.setCallbackThreadInitializer(callback, new CallbackThreadInitializer());

What analogy in JNR?
Thank!

Wow, coincidence on the timing.

I've been using this library for 6 months and just realised I'm seeing the same thing and was about to raise an issue.

As far as I can see, this issue should be moved to https://github.com/jnr/jffi

It looks like ClosureMagazine.c method closure_invoke(...) needs to support configuring the behaviour of the thread attach/detach logic. Currently if possible to attach the thread before invocation it is always detached after invocation.

Yes this should be a jffi issue most likely, and I agree it should be fixed. We should do a round of patches.

The unfortunate part of having to change the C code is rebuilding the native library for all platforms, so this fix would require a pretty major release.

Any chance of getting a fix for this?