lua-users home
lua-l archive

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


> Let's suppose I would want to yield every N instructions or a line of 
code.
> Yielding from hooks, as I had pointed out, is not a viable option.
> However, let's suppose that the hook function instead of direct
> yielding sets the > flag in lua_State, say, kfWantToYield.
> Is there any place in Lua main VM that would be safe/suitable
> for calling lua_yield?

You can only yield when you're in the base Lua frame (i.e. nCcalls is 0). 
If that is not true when the wantToYield flag is set, it can only become 
true when a C function returns (although it may still not be true). The 
only place you need to check that is where nCcalls is decremented, at line 
315 of ldo.c (I think that is the only place.)

The strategy would be to set a timer alarm where you either set a 
counthook of 1 if nCcalls is 0, or else set wantToYield. In luaD_call, you 
can check the wantToYield flag, and set the counthook to 1 at that point, 
assuming that nCcalls has decremented to 0.

But it won't guarantee anything, because the program might be something 
like this (possibly less exaggerated):

function endless_loop() while true do end end
string.gsub(" ", " ", endless_loop)

It will work if you're reasonably confident that callbacks will terminate 
in a reasonable amount of time.

R.