lua-users home
lua-l archive

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

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.