[Date Prev][Date Next][Thread Prev][Thread Next]
- Subject: RE: running the Lua VM "in time slices"
- From: "Dolan, Ryanne Thomas (UMR-Student)" <rtdmr6@...>
- Date: Fri, 30 Dec 2005 09:55:12 -0600
No problem there. I think you can just remove the initial lua_pcall in the C code and it should always work as expected.
It's funny, but I came up with this same solution last night before I fell asleep. The idea is just to call lua_yield in a hook that runs after a while, and replace lua_pcall with lua_resume.
What's really cool is that you can write a Lua module to do this from Lua scripts. You could even extend the coroutine library as follows:
f = function ()
while counter < 100000 do
counter = counter + 1
t = coroutine.create (f);
while coroutine.resume_for_given_instructions (f, 100) do
print ("coroutine t preempted!");
This approach still uses coroutines but doesn't require explicit yields within each thread. Instead, the currently running thread gets preempted by the hook.
The new resume_for_given_instructions function is easily written using only the Lua C API. For this reason, I would suggest including it (with a better name) in the next release of the standard library. It would simply register a hook which calls lua_yield and then call lua_resume. It allows scripts that weren't written as coroutines (ie with yields) to be run as coroutines. This would at least evaporate my disdain for the coroutine library.
From: firstname.lastname@example.org on behalf of Gerhard Sittig
Sent: Fri 12/30/2005 7:39 AM
To: Lua list
Subject: Re: running the Lua VM "in time slices"
On Thu, Dec 29, 2005 at 23:05 -0800, David Andersen wrote:
> You want to do this I think:
Thank you for the pointer. Haven't thought of such an approach yet. I
could start some Lua glue of my own from the C application and have this
Lua glue run another user provided Lua chunk with periodic callbacks.
I took the code snippet and it works as advertised. But then I tried to
make sure that even the initial pcall (where the loop() routine gets
declared) runs with debug hooks set. Because nobody could stop users
providing a script like this:
-- the loop() declaration has been there before
counter = 0
while counter < 10000 do
counter = counter + 1
-- this immediate calling loop() is new
Doing luaL_loadfile(), i.e. compiling the source, is no problem. But
running this code the first time to declare the counter variable and the
loop() routine (as the message you refer to does) may already block in a
possibly infinite loop. Remember, I won't have any influence on the
scripts the VM is fed with. Neither can I (hard) require users to
provide an entry point like a main() routine or something. I want the
thing to look as much Lua as possible.
Will have to look a little more into how to even "supervise" the first
invocation. Maybe wrapping the Lua user code in my own Lua glue will be
If you don't understand or are scared by any of the above
ask your parents or an adult to help you.