lua-users home
lua-l archive

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

Wow, I do not know HOW I missed the lua_newthread() and related functions.  Yes, that certainly looks easier and I feel a bit dumb for not noticing.

Thanks for the info on the per-thread hooks, exactly the answer I was looking for.


-----Original Message-----
From: [] On Behalf Of Rici Lake
Sent: Friday, December 15, 2006 7:34 PM
To: Lua list
Subject: Re: lua instruction count hook problem when using coroutines

On 15-Dec-06, at 9:52 PM, Andrew Yount wrote:

>     // function main() = <loaded script>                // STACK  
> '...'=string    [...]=table   ...()=function   <...>=number   
> {...}=thread   ?...=boolean
>     lua_pushstring( p_state, func_name );               // -> 
> 'func_name'
>     lua_load( p_state, Chunkreader, this, m_package );  // -> 
> 'func_name'  script()
>     lua_settable( p_state, LUA_GLOBALSINDEX );          // 
> ->                                                                 ( 
> sets func_name = the script function )
>     lua_pushstring( p_state, "coroutine" );             // -> 
> 'coroutine'

You might find it easier (or at least fewer lines of code)
to create the coroutine with lua_newthread(). You can then resume it
with lua_resume(). These functions do the same thing as the code
you're using (roughly) but with quite a bit less work.

> I'm setting the hook with:
>     lua_sethook( m_p_state, InstructionCountHook, LUA_MASKCOUNT, 1000 
> );
> ... just prior to loading the script. I have verified that my hook is 
> set properly just before resuming the coroutine.  This code works 
> fine, everything runs as expected... BUT
> The hook function is never called.

Although it's not clear from the manual, every thread has its own
indepedent hook settings. You have to call sethook on the coroutine
thread (which would be easy to do if you'd created it with
lua_newthread() :) ).