lua-users home
lua-l archive

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


Hi Patrick,

lua docs for lua_yield() says that:
This function should only be called as the return _expression_ of a C function, as follows:
     return lua_yield (L, nresults);
so, I'm not sure it is possible to use lua_yield() from within the debug hook.
I have not tried this but if it works the it will be a possible way to implement the green threads in lua, thank you for the idea.

But anyway, I do not think that using the debug abilities for normal work is not a very good solution, and not the fastest one.
So, I think that introducing the capability of executing specified number of instructions in the next version of lua will be a good language improvement.

Thanks,
Ivan



2008/7/31 Patrick Donnelly <batrick.donnelly@gmail.com>
On Thu, Jul 31, 2008 at 4:21 AM, Ivan Gagis <igagis@gmail.com> wrote:
> Hi Patrick,
>
> i'm not sure how can I achieve that with debug hooks. I can set a callback
> to be called when every ,say, 10 instructions are executed, but then I need
> to see how much time they were executing and then execute other lua thread
> (lua_state). How can I do this from within the called callback function? if
> I return from this function the same lua_State will continue execution...

What you are asking for should be done in coroutines; that is, you
want a function to be resumable and the thread to yield after a
certain number of instructions. You set a debug hook function in C
that will quite simply yield the thread, preferably with a unique
return value you can differentiate from actual return values in your
dispatcher (e.g. a light userdata sentinel). Your dispatcher will then
resume threads constantly until all are finished. For this to work,
you need to somehow handle a thread running other threads using
coroutine.resume (perhaps do nothing if this is acceptable).

Cheers,

--
-Patrick Donnelly

"One of the lessons of history is that nothing is often a good thing
to do and always a clever thing to say."

-Will Durant