lua-users home
lua-l archive

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


lemkef@execpc.com wrote:
> 
> Hello,
> I recently started using Lua, and am very impressed.
> I looked through the archives a little regrading cooperative
> multitasking, and I'm happy to see that it is doable.  Is there a
> distribution of the library out there that already does this?  Or
> will I have to dig in and make the modifications myself?

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). 

If you want the interpreter to deal with the multitasking you'll need to
rewrite some things..

I was thinking you might be able to "misuse" the lua states for this,
but I was wondering:

* 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).

* 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
registered that does nothing else then setting this state member
variable to false. whenever the lvm return after this suspend function
is called, it sees that it doesn't have to run anymore and returns
(leaving the stack in such a state that execution can be resumed
later).  - this can probably also be done when there's a lua function
that executes a single lua opcode from a specific state, then you
wouldn't need to rewrite lua's "internals".

You would have a list with lua states and iterate over them (from "C")
and resume execution one after the other.

I'm thinking about implementing something like this, but I need to get
familiar with the lua vm internals first (this is also why I was
interested in the lua opcodes explanation).

---
Jeroen