[Date Prev][Date Next][Thread Prev][Thread Next]
- Subject: Re: C functions in Lua coroutines
- From: Johann ''Myrkraverk'' Oskarsson <johann+lua@...>
- Date: Tue, 28 Mar 2023 15:39:03 +0800
On 3/28/2023 2:28 PM, 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
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.
I only skimmed that chapter, but isn't exactly this covered in chapter
29.2 in /Programming in Lua/ 4th edition?