jnr / jnr-ffi

Java Abstracted Foreign Function Layer
Other
1.25k stars 155 forks source link

Use one thread for all callbacks #125

Open deripas opened 7 years ago

deripas commented 7 years ago

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!

ghost commented 7 years ago

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.

ghost commented 7 years ago

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.

headius commented 7 years ago

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.

re-thc commented 6 years ago

Any chance of getting a fix for this?