lua-users home
lua-l archive

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


On Wed Nov 1, 2023 at 5:24 AM EDT, bil til wrote:
> ?? Lua also is programmed in C??
>
> Of course coroutines exist in C - Lua as given with complete C source
> code - there are NO "miracuolous unknown hideaways".

I mean that there is no native coroutine concept in C. There aren't
yield and resume functions in C. The reasono Lua can have coroutines is
becaues it's an interpreter and has transparent control over the stack.
You're still able to handle the "yield" behavior in C, but it's more
awkward [1]. And changing the behavior of Lua coroutines might mean a
lot of C code that interacts with Lua would also need to be rewritten.

> If a user opens such a "task" with my library function task.open, the
> user must specify a task function, which then starts running, and the
> task will finish automatically if this task function is finished. If
> there is any "sleep time" / "delay time" inside this task function,
> the user must use my function "sys.sleep"... and this sys.sleep will
> then pass control / "resume" control/ "yield" to other "concurrntly
> running tasks" (also to the main task).

That's basically what I'm going for, if I understand you correctly. You
have a "task", which in my case is started in response to an event, and
from its perspective it just runs to completion. From the tasks's
perspective, when it hits the sys.sleep function, it's just waiting for
the timer to finish. It doesn't care about the fact that it's actually a
coroutine that's yielding control to another part of the program.

The place where that could become a problem is if the task is itself
was allowed to directly use Lua's coroutines. Which, there are plenty of
valid cases where it might want to, for example it might use them for an
iteratior. But if inside one of those coroutines our "sys.sleep" or
whatever else is called, control will not be passed back to the main
task, because we're nested inside a different coroutine. That's the
problem I'm stuck on and the reason I started this thread.

[1] https://www.lua.org/manual/5.4/manual.html#4.5