lua-users home
lua-l archive

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


Hi,

Speaking of primary goal:

I want give Lua code access to functions implemented by my C program
that can block while they wait for something to be ready, but the
program might also want to do other things while it's waiting, and I'd
rather not make that happen using OS threads. 

If you want to yield from your blocking c function and be able to continue it's execution later, you have to restore it's stack frame.

Have a look at this: 
https://stackoverflow.com/questions/17478264/implementing-yield-in-c

Have you considered different approach here?

You can run the blocking c code in a separate process and communicate with it via sockets/pipes/shared mem.

Or you can split your blocking code into several non-blocking pieces and teach your Lua code to control their exec order, handling necessary signals/events.

On Tue, 31 Oct 2023 at 07:55, Thomas Jericke <tje@indel.ch> wrote:
Am 31.10.2023 um 10:43 schrieb bil til:
Am Di., 31. Okt. 2023 um 09:24 Uhr schrieb Thijs Schreijer
<thijs@thijsschreijer.nl>:

      

      
imho it should be dealt with on a Lua level. I actually discussed this with Roberto during the 2022 Lua workshop, and he had some ideas like a "goto" for coroutines. So hopefully we'll see this fixed in the next Lua version.

I would be surprised if this would work in Lua at all, as coroutines
in Lua are by principle cooperatively multitasking, not in "any
preemtive way"... .

(...)

... why Lua does not support pre-emptive multithreading is explained
very clearly in Roberto's book Programming in Lua... . I think he is
very correct and adding pre-emptive multithreading to Lua sound a bit
like a "Halloween proposal" to me, sorry.... . I am not a big friend
of pre-emptive multitasking, also not in Windows... . If you like
pre-emptive things so much, you can without problems start Lua on
different pre-emptive machines of your operating system (like e. g.
Windows...).

I don't think that a "yield_to" or something equivalent is the same as pre-emptive multitasking.
The yield will still be from a very specific point in code, the call to the function.
So actually if you are writing a script it doesn't change anything, as you already need to consider that in Lua any function can yield.
--

Thomas