Re: Manual timeslicing the VM.

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Re: Manual timeslicing the VM.

David Jones-2
Warning: I'm replying to an 8 month old thread.

On Aug 05, 2005, at 11:19, Mike Pall wrote:

> Hi,
>
> David Morris-Oliveros wrote:
>> It would be similar to the hooks in lua, but rather lua calling the
>> hook
>> every 5 instructions, i let it execute those 5 instructions, then it
>> comes back to me.
>
> That's inversion of control. Lua already supports that with the
> help of coroutines. And lucky you, you can yield from a count hook
> (or line hook).
>
> So from C do a lua_newthread() and then lua_sethook() to install
> your count hook. When the hook decides that the timeout is reached
> it forces a yield for the coroutine with 'return lua_yield(L, 0)'.

Doesn't this go wrong if the hook tries to yield when we were running a
metamethod, or an iterator?

If so, do you have a recommended workaround?  There doesn't seem to be
any official way to tell if it is safe to lua_yield.

David Jones

Reply | Threaded
Open this post in threaded view
|

Re: Manual timeslicing the VM.

Mike Pall-4-2
Hi,

David Jones wrote:
> On Aug 05, 2005, at 11:19, Mike Pall wrote:
> >So from C do a lua_newthread() and then lua_sethook() to install
> >your count hook. When the hook decides that the timeout is reached
> >it forces a yield for the coroutine with 'return lua_yield(L, 0)'.
>
> Doesn't this go wrong if the hook tries to yield when we were running a
> metamethod, or an iterator?

Yes, it would raise an error.

> If so, do you have a recommended workaround?

Use Coco. It allows you to yield from pretty much anywhere.

Note that hooks are turned off for __gc, so AFAIK the above
approach is safe.

Bye,
     Mike