[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Cooperative Multitasking
- From: "Sebby " <beast@...>
- Date: Fri, 20 Oct 2000 00:21:40 -0000
Hi.
I'm in a similar situation and need to implement some cooperative
multitasking with Lua. I'm fairly new to Lua myself and don't know
all the internals but i am starting to understand how it could be
doable.
> Well, it depends how you want to implement the "cooperative"
> multitasking.. you could use the C++ equivalent of a "task" class
in lua
> that has a Run function and the basic idea would be to iterate all
tasks
> and execute the Run funcion (you could do this with lua code &
tables
> with "Run" functions).
[...]
I myself would prefer adding a sleep function from within the VM,
which flags the thread for sleep and the VM would take care of
sleeping the task once the current OPCODE is done processing.
> * is it possible to transfer all globals from one state to another
state
> (ie you "once" initialise a state with all globals and let other
states
> use these globals - this would also might allow "visibility" of new
> globals from one state in another - or you would need to have a
> "registerglobal" function that registers a global among *all*
states).
Seems like yes. Since i don't really have any multitasking in there,
i can't say for sure. But i have created a Thread/Process system
around lua and it constructs lua_State on the fly by binding the
process (global segment) and the thread together. Didn't have any
trouble so far with that, i can run different threads on that and
they share the global state. In fact the seperation between global
and thread info is specified in the lstate.h file.
>
> * is it possible to adapt the "main" lua vm "loop" in such a way
that it
> can stop at any point during execution of lua opcodes and the
current
> state contains all information needed for this main loop to continue
> executing again? (the state would have an extra member
variable "run"
> that's initially set to true. a "C" function calles (suspend()) can
be
[...]
Well, that's the main problem. Lua runs it's VM in a recusrive way.
Basicly, if you call a Lua, from within a Lua function, the VM will
simply call itself recursivly. Now, one solution is to basicly back
up the section of the system stack that was used in the task (along
with the IP and some other info) and to restore it once the thread is
brought back. This is an icky solution since you hav eto go backing
up some registers and the system stack of the computer.
Another solution would be to modify the Lua VM so it works in an
iterative manner instead of recursive. I tried implementing that
approach last weekend and had some (but limited) success. I basicly
modified the VM function so it would push it's state (mostly the
variables it received in parameter) into a memory buffer when i
entered the VM and poping them when i was supposed to go out. Just
looping inside the VM. Now, this has it's own set of problems. First
of all, the is some code to modify in lots of places if you want to
account all the special cases. Another case that is annoying to
handle is the case of CClosures which call Lua functions (foreach is
a good example). I got my thing to work for all test in the lua
archive except 2 (trace-globals and factorial) but the problem
somehow seems related with the use of the % operand.
Hope thins help and hope someone can come up with a good solution
---
Sebastien St-Laurent
Software Engineer
sebby@z-axis.com