lua-users home
lua-l archive

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


Hi Jörg ,

> But this will only work with coroutine.create/wrap?  I have the problem
> when lua_newthread is called independent of the coroutine module.

I only tested at the Lua level, but I don't see the hook applied to
the coroutines it was not set for:

debug.sethook(function(ev, line) print(ev, line) end, "l")
print(coroutine.running())
print(debug.gethook(coroutine.running()))
coroutine.resume(coroutine.create(function()
      print(coroutine.running())
      print(debug.gethook(coroutine.running()))
    end))
debug.sethook()
print("done")

The hook is not triggered for lines 5 and 6 that are executed in a
different coroutine; tested with Lua 5.2 and Lua 5.3. Note that it
*will* be called for those lines if running under LuaJIT, where one
hook is shared among all coroutines (including the main chunk).

> Lacking an interface to enumerate all threads, it seems impossible to disable a hook completely. Is this correct?

No; when the hook is triggered, you can call coroutine.running() to
get the current thread, from which you can call debug.gethook() to
check if that particular thread has the hook enabled (although if the
hook is called the result may be true anyway). If needed, you can call
debug.sethook(coroutine.running()) to disable the hook for that
thread.

> It seems that using only one hook setting for all threads is more desirable.
> Does anybody use the feature to install different hooks on two threads?

Yes; I use it for the debugger. The user may have the debugging
enabled for some threads, but not others; the debugger itself runs in
a separate coroutine that doesn't need to be debugged. In reality,
there is likely not much difference between the two mechanisms as they
can probably emulate each other. The debugger I mentioned works with
both Lua and LuaJIT approaches; for LuaJIT it checks if the debugging
has been enabled for a particular coroutine and exits the hook if it's
not.

Paul.