lua-users home
lua-l archive

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


Hi...

Now, i'm trying to get lua to do some form of "preemptive" tasking. 
My code right now handles multiple threads and all that... Now the 
problem is that i have to figure out a way to bail out of 
luaV_execute during execution and being able to resume execution 
later on. 

Ok, i know this question has been asked many times before but the 
solution proposed (building C stacks for each execution) seems 
somewhat hacky and not well suited for my case. So i'm trying to 
explore other solutions...

Now it seems that the main issue is mostly the fact that the 
luaV_execute function can call itself recursivley and you could end 
up with cases like C->Lua->C->Lua->Lua->C... Now, i am willing to 
make some compromises for the execution. Some of the restrictions 
would be :

* It would not be allowed to make a tasking call (like sleep or 
suspend from a dofile or dostring (which implicitly calls the VM 
recursivley).
* You can't call lua functions from a C function called by your 
script (unless you are starting a new thread of execution, or calling 
a non threaded function).

Now with thoes restrictions, i remove most of the cases where i'd 
have to keep track of the C stack in case of a task switch. With this 
in mind, it might be possible to get lua to work iterativley instead 
of recursivley. I'll expose the scheme i was thinking about and i 
hope anybody out there can comment on it.

Now, we'd change the luaV_execute function. Instead of receiving 
information about the function to run, it would retreive information 
like the "pc" and other relevant information from the actual lua 
stack. Then proceed with it's execution. Now, whenever execution has 
to stop (due to preemption or some other thread event), we'd simply 
copy push the current execution state back to the lua stack.

Now as for functions which call luaV_execute (like luaD_call), 
instead of passing the function information to the execute function, 
it would gather the function information and push it onto the lua 
stack then call luaV_execute. And my thread scheduler would also 
simply need to call luaV_execute to resume execution since the 
apropriate state information would already be on the lua stack.

Now questions that arises from this scheme...

* What do i need to push onto the lua stack to apropriateley 
represent the execution state? (i'm not too sure about the answer to 
that one but i could probably figure it out by looking more in 
details on how luaV_execute works)

* Since most threading functions will have to be implemented as 
registered C functions, how do i let luaV_execute know it's time to 
bail out of a thread? (Well for this i was thinking addind extra info 
to a lua_State and check the info on a function return, the info 
would indicate what action to take on the thread like : for how long 
to sleep it, which events the thread has to wait for, ...)

* I have added restrictions to the execution of lua scripts to avoir 
VM recusrion. How do i detect if someone does something nasty? or do 
i just get the users to understand what is not to be done.

* Have i missed something which would make this undoable??? (I HOPE 
NOT :) )

Anyways, any comments/suggestion on my idea would be greatly 
apreciated. Although i could probably figure it all on my own, i 
thought it might make sense to ask the people who have a better 
understanding of lua's internals than me (since i'v been playing with 
it only for a few days).

Thanks in advance
--
Sebastien St-Laurent
Software Engineer, Z-Axis ltd.
sebby@z-axis.com