lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]

Currently there is no way to yield from C.

There are libraries (cross-platform to varying extents) that allow creating stackful coroutines (or "fibers" as they are sometimes called) in C or C++, which then allow yielding and resuming C/C++ functions.  For example, in C++ there is the boost::coroutine library.  I'm sure there are others for C.  A quick Google search yielded this library which might be of interest:
    "Minicoro is single-file library for using asymmetric coroutines in C. The API is inspired by Lua coroutines but with C use in mind."

(I have not looked at it in detail, so can't vouch for it, but looks relevant).


On Tue, Mar 28, 2023 at 2:29 AM Schmalzer, Lukas <> wrote:



I don’t know if it was ever discussed, that when you call C functions in Lua coroutines, the whole Lua system is blocked, i.e., no coroutine can advance.

Currently there is no way to yield from C. This behavior is problematic for time critical applications, e.g., as in our application, the execution environment for machine sequences.


We implemented a way to yield from C, i.e., only the coroutine executing the long running C code is blocked.

As a solution, we create a coroutine in the interpreter (C code) for each coroutine in the Lua code. We applied the solution to the blocking C functions in “io” and “os” (liolib.c and loslib.c).

Of course, we are aware, that this cannot be a general solution, as existing code would not expect yields from these libraries. Maybe a separate library with functions to yield from C would be a good approach.

The yield from C feature makes most sense for custom, application specific C libraries.

I think that the possibility to yield from C would be a useful feature for the interpreter, because not the whole Lua system needs to block when a long running C function is executed.

Please let me know what you think about this feature.


Best regards,