lua-users home
lua-l archive

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


On Tue, 28 Mar 2023 at 07:29, Schmalzer, Lukas
<Lukas.Schmalzer@trumpf.com> 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.


Hi.

handling coroutines across different languages is a complex situation
and usually has only very ad-hoc implementations.

a more general solution is to use coroutines to do an inversion of
control.  where each side of the system (the Lua side and the C side)
have the illusion of being in control.

basically, on the C side, you have an event loop (which can be based
on threads, coroutines, or event driven, as you find more
appropriate), where it picks an already suspended Lua coroutine and
resumes it.

on the Lua side, each "thread" does a yield() when it wants to call a
C blocking function.  with the right wrapping functions, it does look
like a function call that suspends the current coroutine and might run
others while waiting for some result.

that always reminds of this picture:
https://i.pinimg.com/474x/e6/00/83/e60083c35d2094566499c551e3002085--math-cartoons-funny-cartoons.jpg

look at the implementation of OpenResty, where it matches the nginx
single-threaded events with multiple Lua threads.

-- 
Javier