lua-users home
lua-l archive

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


Greg wrote:
> > You can yield from a C hook (lua_sethook), but not from a Lua
> > hook (debug.sethook) which sits on top of a C frame. That's the
> > same behavior as plain Lua.
> 
> Can lua_sethook be called on a coroutine?  Will it interrupt
> only the coroutine or also the main "thread/coroutine"? 

For Lua you need to set it on each coroutine separately. This can
get difficult, because you are not notified when a new coroutine
is created. For LuaJIT you only need to set it once and it applies
to all coroutines.

> Here is an example:
> 
> Should lua_sethook call "hook" on l thread or on both l and j threads?

With LuaJIT it applies to both (output is abababa...). With Lua it
only applies to 'l' (output is bbbb... because 'j' never yields).

>     lua_sethook(l, hook, LUA_MASKLINE, 0);

The line hook is very, very inefficient. Never use it, except for
debugging! Use the count hook with a high number. Or better yet,
set the count hook with a count of 1 from a timer (signal or other
thread). And keep it off most of the time, i.e. disable it as soon
as the hook is called. Otherwise everything will slow down.

[Note that lua_sethook() is the only cross-thread-safe and
signal-safe function from the Lua API.]

Also, please note that JIT-compiled code does not call hooks by
default. Either disable the JIT compiler or edit src/Makefile:
  XCFLAGS= -DLUAJIT_ENABLE_CHECKHOOK
This comes with a speed penalty and you can only use this with the
timer method -- the hook must be off most of the time.

--Mike