FFI callbacks and coroutines

  • From: Matt Hellige <matt@xxxxxxxxxx>
  • To: luajit@xxxxxxxxxxxxx
  • Date: Wed, 9 Nov 2016 10:14:53 -0600

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: