[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: RE: Running a script in each Lua thread.
 
- From: "Kevin Baca" <lualist@...>
 
- Date: Sun, 19 Oct 2003 16:40:40 -0700
 
A C function can only call lua_yield() in its return statement:
return lua_yield( L, nresults );
So you shouldn't really be trying to resume/yield C functions, rather
you should be resuming/yielding lua functions.  It's prefectly
reasonable, however, for a resumed lua function to call a C function
that yields.
If you want to resume a lua function different than the one that yielded
you have to create a new thread (that's how coroutines work).
You could, perhaps, instead of switching to a different function, pass a
parameter to resume that would tell the resumed function what to do
next.  The parameter could be a function to call.
You could use tail calls to avoid deep stacks:
function funcA()
    print( "funcA" )
    local nextfunc = coroutine.yield( funcB )
    return nextfunc()
end
function funcB()
    print( "funcB" )
    local nextfunc = coroutine.yield( funcA )
    return nextfunc()
end
> co = coroutine.wrap( funcA )
> next = co()
funcA
> next = co( next )
funcB
> next = co( next )
funcA
-Kevin
> Kevin,
> 
> Thank you for the follow up. Further to the execution of a 
> specific function, if I have a thread that has called a C 
> function which invokes lua_yield(), is there any way I can 
> get the thread to begin execution of the function from the 
> top of the function body? Eg. The thread goes into a
> Wait() function, waiting for some event, but while waiting 
> for the event, the C code decides the thread should start 
> either the same function over again, or switch to another 
> one. What is the correct syntax for doing this? Currently, 
> calling the doit() function as you've shown below resumes 
> execution of the function body; it doesn't restart it. Do I 
> need to flush the stack somehow, or roll it back somehow? Or 
> do I have to close the thread somehow (since 
> lua_closethread() doesn't exist!) or can I do something to 
> get it executing another function?
> 
> - Steven