[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: green-threads in lua
- From: "Ivan Gagis" <igagis@...>
- Date: Thu, 31 Jul 2008 14:07:44 +0400
Hi All,
I saw some discussion about different implementations of threads in lua, but no one talked about green threads.
In my opinion introducing the green treads would be a very good alternative to all implementations of threads (with native OS threads) existing at the moment and coroutines.
I think that introducing some function which allows to execute a specified number of opcodes from lua script would allow implementing the green treads to any developer who uses lua.
For example, there is a C API function lua_pcall(L, ....) which calls a lua function and does not return until the lua function returns.
I propose to untroduce a new function lua_pexec(L, numOpcodes, ....) which will execute a specified number of opcodes and return, the current instruction pointer will be saved in lua state L, so when calling the function for subsecquent times it will continue executing the lua program.
Of course, there are some difficulties to introducing such a function (or a set of functions):
- For example, the lua "exceptions" mechanism will not work ( longjump()/throw ), so, it will be necessary to introduce some kind of language built-in exception throwing/catching ability, mayme add new lua opcodes for that.
- calling a lua function from a C implementation of another lua function is not good, as it may cause the lua_pexec() to block for a long time. Thus, some of the lua std lib functions will need to be rewritten in the mix of lua and native calls instead of complete native implementation.
- Maybe introduce a "multistep compilation" of lua scripts. For example if lua program calls a require() function it may need to read a .lua file and build a bytecode from from it which is a time consuming operation. So it would be good to read the .lua file by small portions and compile them while giving other green threads to execute between two consequent .lua file reads.
- ... much other things I mussed.
I think that it would be good if Lua will be moving towards the introduction of such abilities.
Thanks,
Ivan