Re: FFI callbacks and coroutines

  • From: Matt Hellige <matt@xxxxxxxxxx>
  • To: luajit@xxxxxxxxxxxxx
  • Date: Tue, 15 Nov 2016 10:06:15 -0600

Just a quick bump on this one. Any thoughts? It would be nice to use this
approach, as avoiding FFI callbacks will involve a more clumsy registration
API on the C side. But maybe I have no choice.

Thanks!
Matt

On Wed, Nov 9, 2016 at 10:14 AM, Matt Hellige <matt@xxxxxxxxxx> wrote:

I'm using the LuaJIT FFI to register some callbacks with a C library. I'm
also using coroutines in my Lua code (but note that I'm *not* trying to
yield/resume across a C boundary). It seems to me that after calling into C
via the FFI from a Lua coroutine, and then yielding back to the main Lua
thread, subsequent calls from C into the FFI callbacks occur on the
coroutine rather than the main thread.

This doesn't seem to happen if I force an FFI call into C from the main
thread after the coroutine yields, but that's awkward and might not always
be possible. Is this expected? I'm guessing that there's a notion of
"current Lua thread" that's cached someplace in the FFI upon calls into C,
and that's used for subsequent callbacks. Is that correct? Is there any way
to force a callback to occur on the main Lua thread, or should I just avoid
FFI callbacks in this case and rely on manual bindings instead?

I can provide example code if this isn't clear, but I figured I'd just ask
before trying to craft a minimal example.

Thanks!
Matt

Other related posts: